From d3099f311a27e33f84e74611af973e90f63f136d Mon Sep 17 00:00:00 2001 From: miozune Date: Sat, 22 Oct 2022 18:41:23 +0900 Subject: [PATCH] Add API for server to tell client to show message above hotbar (#11) * Add API for server to tell client to show message above hotbar * Address reviews --- .../com/gtnewhorizon/gtnhlib/CommonProxy.java | 23 +++++++- .../gtnhlib/network/NetworkHandler.java | 19 +++++++ .../network/PacketMessageAboveHotbar.java | 56 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java b/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java index a87e975c..8fa8d22f 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java @@ -1,5 +1,7 @@ package com.gtnewhorizon.gtnhlib; +import com.gtnewhorizon.gtnhlib.network.NetworkHandler; +import com.gtnewhorizon.gtnhlib.network.PacketMessageAboveHotbar; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; @@ -8,7 +10,9 @@ import cpw.mods.fml.common.event.FMLServerStartedEvent; import cpw.mods.fml.common.event.FMLServerStartingEvent; import cpw.mods.fml.common.event.FMLServerStoppedEvent; import cpw.mods.fml.common.event.FMLServerStoppingEvent; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.util.IChatComponent; +import net.minecraftforge.common.util.FakePlayer; public class CommonProxy { @@ -16,7 +20,9 @@ public class CommonProxy { GTNHLib.info("GTNHLib version " + com.gtnewhorizon.gtnhlib.Tags.VERSION + " loaded."); } - public void init(FMLInitializationEvent event) {} + public void init(FMLInitializationEvent event) { + NetworkHandler.init(); + } public void postInit(FMLPostInitializationEvent event) {} @@ -49,4 +55,19 @@ public class CommonProxy { public void addMessageToChat(IChatComponent componentText) {} public void printMessageAboveHotbar(String message, int displayDuration, boolean drawShadow, boolean shouldFade) {} + + /** + * Sends packet from server to client that will display message above hotbar. + * @see ClientProxy#printMessageAboveHotbar + */ + public void sendMessageAboveHotbar( + EntityPlayerMP player, + IChatComponent chatComponent, + int displayDuration, + boolean drawShadow, + boolean shouldFade) { + if (player instanceof FakePlayer) return; + NetworkHandler.instance.sendTo( + new PacketMessageAboveHotbar(chatComponent, displayDuration, drawShadow, shouldFade), player); + } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java new file mode 100644 index 00000000..3f507881 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java @@ -0,0 +1,19 @@ +package com.gtnewhorizon.gtnhlib.network; + +import com.gtnewhorizon.gtnhlib.Tags; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import cpw.mods.fml.relauncher.Side; + +public class NetworkHandler { + + public static final SimpleNetworkWrapper instance = NetworkRegistry.INSTANCE.newSimpleChannel(Tags.MODID); + + public static void init() { + instance.registerMessage( + PacketMessageAboveHotbar.HandlerMessageAboveHotbar.class, + PacketMessageAboveHotbar.class, + 0, + Side.CLIENT); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java new file mode 100644 index 00000000..c9283e57 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java @@ -0,0 +1,56 @@ +package com.gtnewhorizon.gtnhlib.network; + +import com.gtnewhorizon.gtnhlib.GTNHLib; +import cpw.mods.fml.common.network.ByteBufUtils; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; +import net.minecraft.util.IChatComponent; + +public class PacketMessageAboveHotbar implements IMessage { + + private IChatComponent chatComponent; + private int displayDuration; + private boolean drawShadow; + private boolean shouldFade; + + @SuppressWarnings("unused") + public PacketMessageAboveHotbar() {} + + public PacketMessageAboveHotbar( + IChatComponent chatComponent, int displayDuration, boolean drawShadow, boolean shouldFade) { + this.chatComponent = chatComponent; + this.displayDuration = displayDuration; + this.drawShadow = drawShadow; + this.shouldFade = shouldFade; + } + + @Override + public void fromBytes(ByteBuf buf) { + this.chatComponent = IChatComponent.Serializer.func_150699_a(ByteBufUtils.readUTF8String(buf)); + this.displayDuration = buf.readInt(); + this.drawShadow = buf.readBoolean(); + this.shouldFade = buf.readBoolean(); + } + + @Override + public void toBytes(ByteBuf buf) { + ByteBufUtils.writeUTF8String(buf, IChatComponent.Serializer.func_150696_a(this.chatComponent)); + buf.writeInt(this.displayDuration); + buf.writeBoolean(this.drawShadow); + buf.writeBoolean(this.shouldFade); + } + + public static class HandlerMessageAboveHotbar implements IMessageHandler { + @Override + public IMessage onMessage(PacketMessageAboveHotbar message, MessageContext ctx) { + GTNHLib.proxy.printMessageAboveHotbar( + message.chatComponent.getFormattedText(), + message.displayDuration, + message.drawShadow, + message.shouldFade); + return null; + } + } +}