Skip to content

Commit

Permalink
added Directional Layout, Button widget now works with both client an…
Browse files Browse the repository at this point in the history
…d server side condition/action
  • Loading branch information
megadoxs committed Jun 28, 2024
1 parent 8e24e85 commit b7b53b6
Show file tree
Hide file tree
Showing 36 changed files with 792 additions and 85 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/megadoxs/megalib/Megalib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.megadoxs.megalib;

import com.megadoxs.megalib.networking.task.ModPacketsC2S;
import com.megadoxs.megalib.registry.MegalibRegistries;
import com.megadoxs.megalib.registry.factory.MegalibPowers;
import com.megadoxs.megalib.screen_element.ScreenElements;
Expand All @@ -18,6 +19,8 @@ public class Megalib implements ModInitializer {

@Override
public void onInitialize() {
ModPacketsC2S.register();

ScreenElements.register();

MegalibPowers.register();
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/megadoxs/megalib/MegalibClient.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.megadoxs.megalib;

import com.megadoxs.megalib.networking.packet.c2s.PerformButtonActionsC2SPacket;
import com.megadoxs.megalib.networking.packet.c2s.IsButtonConditionFulfilledC2SPacket;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.util.Identifier;

import java.util.LinkedList;
import java.util.List;

@Environment(EnvType.CLIENT)
public class MegalibClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
}

public static void performButtonActions(Identifier identifier) {
ClientPlayNetworking.send(new PerformButtonActionsC2SPacket(identifier));
// does will only work for buttons,
public static void performButtonActions(int index) {
ClientPlayNetworking.send(new PerformButtonActionsC2SPacket(index));
}

public static void isButtonConditionFulfilled(int index) {
ClientPlayNetworking.send(new IsButtonConditionFulfilledC2SPacket(index));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.megadoxs.megalib.access;

import io.github.apace100.apoli.power.factory.action.ActionFactory;
import net.minecraft.entity.Entity;

public interface UserInterfaceAction {
ActionFactory<Entity>.Instance megalib$getWidgetAction(int index);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.megadoxs.megalib.access;

import io.github.apace100.apoli.power.factory.condition.ConditionFactory;
import net.minecraft.entity.Entity;

public interface UserInterfaceCondition {
ConditionFactory<Entity>.Instance megalib$getWidgetCondition(int index);

void megalib$setWidgetConditionResult(int index, boolean result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import io.github.apace100.apoli.power.PowerType;

public interface UserInterfaceViewer {
void megalib$showInterface(UserInterfaceData interfaceData, PowerType<?> powerType);
void megalib$showInterface(UserInterfaceData interfaceData);
}
15 changes: 10 additions & 5 deletions src/main/java/com/megadoxs/megalib/data/MegalibDataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.megadoxs.megalib.registry.MegalibRegistries;
import com.megadoxs.megalib.screen_element.ScreenElementFactory;
import com.megadoxs.megalib.screen_element.ScreenElements;
import com.megadoxs.megalib.util.Screen.Size;
import com.megadoxs.megalib.util.DataType.Axis;
import com.megadoxs.megalib.util.DataType.Size;
import io.github.apace100.apoli.data.ApoliDataTypes;
import io.github.apace100.calio.util.IdentifierAlias;
import io.github.apace100.calio.ClassUtil;
import io.github.apace100.calio.data.SerializableData;
import io.github.apace100.calio.data.SerializableDataType;
import io.github.apace100.calio.data.SerializableDataTypes;
import io.github.apace100.calio.util.DynamicIdentifier;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
Expand All @@ -20,12 +21,16 @@
import java.util.function.BiFunction;

public class MegalibDataTypes {

public static final SerializableDataType<Size.Unit> UNIT = SerializableDataType.enumValue(Size.Unit.class);
public static final SerializableDataType<Axis> AXIS = SerializableDataType.enumValue(Axis.class);

public static final SerializableDataType<Size> SIZE = SerializableDataType.compound(
Size.class,
new SerializableData()
.add("unit", SerializableDataTypes.STRING, "percents")
.add("width", SerializableDataTypes.INT, 0)
.add("height", SerializableDataTypes.INT, 0),
.add("unit", MegalibDataTypes.UNIT, Size.Unit.PERCENTS)
.add("width", ApoliDataTypes.NON_NEGATIVE_INT, 0)
.add("height", ApoliDataTypes.NON_NEGATIVE_INT, 0),
(data) -> new Size(
data.get("unit"),
data.get("width"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.megadoxs.megalib.data;

import com.megadoxs.megalib.screen_element.ScreenElementFactory;
import com.megadoxs.megalib.util.Screen.Size;
import com.megadoxs.megalib.util.DataType.Size;
import io.github.apace100.calio.data.SerializableData;
import io.github.apace100.calio.data.SerializableDataType;
import io.github.apace100.calio.data.SerializableDataTypes;
Expand All @@ -10,7 +10,7 @@ public record UserInterfaceData(String title, Size size, String location, Screen

public static final SerializableData DATA = new SerializableData()
.add("title", SerializableDataTypes.STRING, "UserInterface.Title")
.add("size", MegalibDataTypes.SIZE, new Size("percents", 100, 100))
.add("size", MegalibDataTypes.SIZE, new Size(Size.Unit.PERCENTS, 100, 100))
.add("location", SerializableDataTypes.STRING, "centered")
.add("element", MegalibDataTypes.SCREEN_ELEMENT, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public ClientPlayerEntityMixin(ClientWorld world, GameProfile profile) {
}

@Override
public void megalib$showInterface(UserInterfaceData InterfaceData, PowerType<?> powerType) {
public void megalib$showInterface(UserInterfaceData InterfaceData) {
MinecraftClient client = MinecraftClient.getInstance();
client.execute(() -> {
UserInterface userInterface = new UserInterface(InterfaceData, powerType);
UserInterface userInterface = new UserInterface(InterfaceData);
client.setScreen(userInterface);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.megadoxs.megalib.mixin;

import com.megadoxs.megalib.access.UserInterfaceAction;
import com.megadoxs.megalib.access.UserInterfaceCondition;
import com.megadoxs.megalib.access.UserInterfaceViewer;
import com.megadoxs.megalib.data.UserInterfaceData;
import com.megadoxs.megalib.networking.packet.s2c.ShowInterfaceS2CPacket;
import com.megadoxs.megalib.screen.UserInterface.UserInterface;
import com.megadoxs.megalib.util.Screen.Button;
import com.mojang.authlib.GameProfile;
import io.github.apace100.apoli.Apoli;
import io.github.apace100.apoli.power.PowerType;
import io.github.apace100.apoli.power.factory.action.ActionFactory;
import io.github.apace100.apoli.power.factory.condition.ConditionFactory;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin extends PlayerEntity implements UserInterfaceViewer, UserInterfaceAction, UserInterfaceCondition {
public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(world, pos, yaw, gameProfile);
}

@Override
public void megalib$showInterface(UserInterfaceData interfaceData) {
ServerPlayNetworking.send((ServerPlayerEntity) (Object) this, new ShowInterfaceS2CPacket(interfaceData));
}

@Override
public ActionFactory<Entity>.Instance megalib$getWidgetAction(int index){
MinecraftClient client = MinecraftClient.getInstance();
if (client.currentScreen instanceof UserInterface screen)
return ((Button) screen.getWidget(index)).getAction();
return null;
}

@Override
public ConditionFactory<Entity>.Instance megalib$getWidgetCondition(int index){
MinecraftClient client = MinecraftClient.getInstance();
if (client.currentScreen instanceof UserInterface screen)
return ((Button) screen.getWidget(index)).getCondition();
return null;
}

@Override
public void megalib$setWidgetConditionResult(int index, boolean result){
MinecraftClient client = MinecraftClient.getInstance();
if (client.currentScreen instanceof UserInterface screen)
((Button) screen.getWidget(index)).active = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.megadoxs.megalib.networking.packet.c2s;

import com.megadoxs.megalib.Megalib;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.minecraft.network.PacketByteBuf;

public record IsButtonConditionFulfilledC2SPacket(int index) implements FabricPacket {
public static final PacketType<IsButtonConditionFulfilledC2SPacket> TYPE = PacketType.create(
Megalib.identifier("c2s/is_button_condition_fulfilled"), IsButtonConditionFulfilledC2SPacket::read
);

private static IsButtonConditionFulfilledC2SPacket read(PacketByteBuf buffer) {
return new IsButtonConditionFulfilledC2SPacket(buffer.readInt());
}
@Override
public void write(PacketByteBuf buffer) {
buffer.writeInt(index);
}

@Override
public PacketType<?> getType() {
return TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public record PerformButtonActionsC2SPacket(Identifier identifier) implements FabricPacket {
public record PerformButtonActionsC2SPacket(int index) implements FabricPacket {

public static final PacketType<PerformButtonActionsC2SPacket> TYPE = PacketType.create(
Megalib.identifier("c2s/perform_button_action"), PerformButtonActionsC2SPacket::read
);

private static PerformButtonActionsC2SPacket read(PacketByteBuf buffer) {
return new PerformButtonActionsC2SPacket(buffer.readIdentifier());
return new PerformButtonActionsC2SPacket(buffer.readInt());
}
@Override
public void write(PacketByteBuf buffer) {
buffer.writeIdentifier(identifier);
buffer.writeInt(index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.megadoxs.megalib.networking.packet.s2c;

import com.megadoxs.megalib.data.UserInterfaceData;
import io.github.apace100.apoli.Apoli;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.minecraft.network.PacketByteBuf;

public record ShowInterfaceS2CPacket(UserInterfaceData interfaceData) implements FabricPacket {

public static final PacketType<ShowInterfaceS2CPacket> TYPE = PacketType.create(
Apoli.identifier("s2c/show_interface"), ShowInterfaceS2CPacket::read
);

public static ShowInterfaceS2CPacket read(PacketByteBuf buf) {
return new ShowInterfaceS2CPacket(UserInterfaceData.DATA_TYPE.receive(buf));
}
@Override
public void write(PacketByteBuf buf) {
UserInterfaceData.DATA_TYPE.send(buf, interfaceData);
}

@Override
public PacketType<?> getType() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
package com.megadoxs.megalib.networking.task;

import com.megadoxs.megalib.access.UserInterfaceAction;
import com.megadoxs.megalib.access.UserInterfaceCondition;
import com.megadoxs.megalib.networking.packet.c2s.PerformButtonActionsC2SPacket;
import io.github.apace100.apoli.Apoli;
import io.github.apace100.apoli.component.PowerHolderComponent;
import io.github.apace100.apoli.power.Power;
import io.github.apace100.apoli.power.PowerType;
import io.github.apace100.apoli.power.PowerTypeRegistry;
import com.megadoxs.megalib.networking.packet.c2s.IsButtonConditionFulfilledC2SPacket;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

@SuppressWarnings("UnstableApiUsage")
public class ModPacketsC2S {

public static void register() {
ServerPlayNetworking.registerGlobalReceiver(PerformButtonActionsC2SPacket.TYPE, ModPacketsC2S::performButtonActions);
ServerPlayNetworking.registerGlobalReceiver(IsButtonConditionFulfilledC2SPacket.TYPE, ModPacketsC2S::IsButtonConditionFulfilled);
}

// I'm thinking I could maybe return a ScreenElementFactory and only have one function in the mixin instead that returns the whole widget
private static void performButtonActions(PerformButtonActionsC2SPacket packet, ServerPlayerEntity player, PacketSender responseSender) {
int index = packet.index();

PowerHolderComponent component = PowerHolderComponent.KEY.get(player);
Identifier identifier = packet.identifier();
if(!player.getWorld().isClient && player instanceof UserInterfaceAction action)
action.megalib$getWidgetAction(index).accept(player);
}

PowerType<?> powerType = PowerTypeRegistry.getNullable(identifier);
if (powerType == null) {
Apoli.LOGGER.warn("Found unknown power \"{}\" while receiving packet for triggering active powers of player {}!", identifier, player.getName().getString());
}
private static void IsButtonConditionFulfilled(IsButtonConditionFulfilledC2SPacket packet, ServerPlayerEntity player, PacketSender responseSender) {
int index = packet.index();

Power power = component.getPower(powerType);
//something goes here to activate the action on the server
if(!player.getWorld().isClient && player instanceof UserInterfaceCondition condition)
condition.megalib$setWidgetConditionResult(index, condition.megalib$getWidgetCondition(index).isFulfilled(player));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public void onUse() {
// !entity.getWorld().isClient && entity instanceof UserInterfaceViewer viewer
// the condition I changed it to and works now...
if (entity.getWorld().isClient && entity instanceof UserInterfaceViewer viewer) {
viewer.megalib$showInterface(UserInterfaceData.fromData(data), getType());
viewer.megalib$showInterface(UserInterfaceData.fromData(data));
}
}

//maybe make a method that takes an int for index, makes the interface, and trigger an action?

@Override
public Key getKey() {
return key;
Expand Down
Loading

0 comments on commit b7b53b6

Please sign in to comment.