-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Directional Layout, Button widget now works with both client an…
…d server side condition/action
- Loading branch information
Showing
36 changed files
with
792 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/megadoxs/megalib/access/UserInterfaceAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/megadoxs/megalib/access/UserInterfaceCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/megadoxs/megalib/mixin/ServerPlayerEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../java/com/megadoxs/megalib/networking/packet/c2s/IsButtonConditionFulfilledC2SPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/megadoxs/megalib/networking/packet/s2c/ShowInterfaceS2CPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 13 additions & 14 deletions
27
src/main/java/com/megadoxs/megalib/networking/task/ModPacketsC2S.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.