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
This commit is contained in:
miozune authored and GitHub committed 2022-10-22 11:41:23 +02:00
1 parent 8017b39000
commit d3099f311a
3 files changed
+97 -1

No files matched your search

@@ -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);
}
}
@@ -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);
}
}
@@ -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<PacketMessageAboveHotbar, IMessage> {
@Override
public IMessage onMessage(PacketMessageAboveHotbar message, MessageContext ctx) {
GTNHLib.proxy.printMessageAboveHotbar(
message.chatComponent.getFormattedText(),
message.displayDuration,
message.drawShadow,
message.shouldFade);
return null;
}
}
}