From 302f9edb026e2ea33adaa5a3a7df5055318a08e8 Mon Sep 17 00:00:00 2001 From: Lunatrius Date: Tue, 18 Nov 2014 18:59:25 +0100 Subject: [PATCH] The server now sends the world seed to players on login. --- .../lunatrius/ingameinfo/InGameInfoXML.java | 4 +- .../ingameinfo/handler/PlayerHandler.java | 21 ++++++++++ .../ingameinfo/network/PacketHandler.java | 15 +++++++ .../network/message/MessageSeed.java | 39 +++++++++++++++++++ .../ingameinfo/proxy/ServerProxy.java | 11 ++++++ .../ingameinfo/reference/Reference.java | 2 +- 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/github/lunatrius/ingameinfo/handler/PlayerHandler.java create mode 100644 src/main/java/com/github/lunatrius/ingameinfo/network/PacketHandler.java create mode 100644 src/main/java/com/github/lunatrius/ingameinfo/network/message/MessageSeed.java create mode 100644 src/main/java/com/github/lunatrius/ingameinfo/proxy/ServerProxy.java diff --git a/src/main/java/com/github/lunatrius/ingameinfo/InGameInfoXML.java b/src/main/java/com/github/lunatrius/ingameinfo/InGameInfoXML.java index 8138dc4..bcb09c2 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/InGameInfoXML.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/InGameInfoXML.java @@ -2,6 +2,7 @@ import com.github.lunatrius.core.version.VersionChecker; import com.github.lunatrius.ingameinfo.handler.ConfigurationHandler; +import com.github.lunatrius.ingameinfo.network.PacketHandler; import com.github.lunatrius.ingameinfo.proxy.CommonProxy; import com.github.lunatrius.ingameinfo.reference.Reference; import cpw.mods.fml.common.Mod; @@ -19,7 +20,7 @@ public class InGameInfoXML { @Instance(Reference.MODID) public static InGameInfoXML instance; - @SidedProxy(serverSide = Reference.PROXY_COMMON, clientSide = Reference.PROXY_CLIENT) + @SidedProxy(serverSide = Reference.PROXY_SERVER, clientSide = Reference.PROXY_CLIENT) public static CommonProxy proxy; @EventHandler @@ -34,6 +35,7 @@ public void preInit(FMLPreInitializationEvent event) { @EventHandler public void init(FMLInitializationEvent event) { + PacketHandler.init(); proxy.registerEvents(); proxy.registerCommands(); } diff --git a/src/main/java/com/github/lunatrius/ingameinfo/handler/PlayerHandler.java b/src/main/java/com/github/lunatrius/ingameinfo/handler/PlayerHandler.java new file mode 100644 index 0000000..87028fb --- /dev/null +++ b/src/main/java/com/github/lunatrius/ingameinfo/handler/PlayerHandler.java @@ -0,0 +1,21 @@ +package com.github.lunatrius.ingameinfo.handler; + +import com.github.lunatrius.ingameinfo.network.PacketHandler; +import com.github.lunatrius.ingameinfo.network.message.MessageSeed; +import com.github.lunatrius.ingameinfo.reference.Reference; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import net.minecraft.entity.player.EntityPlayerMP; + +public class PlayerHandler { + @SubscribeEvent + public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) { + if (event.player instanceof EntityPlayerMP) { + try { + PacketHandler.INSTANCE.sendTo(new MessageSeed(event.player.worldObj.getSeed()), (EntityPlayerMP) event.player); + } catch (Exception ex) { + Reference.logger.error("Failed to send the seed!", ex); + } + } + } +} diff --git a/src/main/java/com/github/lunatrius/ingameinfo/network/PacketHandler.java b/src/main/java/com/github/lunatrius/ingameinfo/network/PacketHandler.java new file mode 100644 index 0000000..0962972 --- /dev/null +++ b/src/main/java/com/github/lunatrius/ingameinfo/network/PacketHandler.java @@ -0,0 +1,15 @@ +package com.github.lunatrius.ingameinfo.network; + +import com.github.lunatrius.ingameinfo.network.message.MessageSeed; +import com.github.lunatrius.ingameinfo.reference.Reference; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import cpw.mods.fml.relauncher.Side; + +public class PacketHandler { + public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MODID.toLowerCase()); + + public static void init() { + INSTANCE.registerMessage(MessageSeed.class, MessageSeed.class, 0, Side.CLIENT); + } +} diff --git a/src/main/java/com/github/lunatrius/ingameinfo/network/message/MessageSeed.java b/src/main/java/com/github/lunatrius/ingameinfo/network/message/MessageSeed.java new file mode 100644 index 0000000..886d08d --- /dev/null +++ b/src/main/java/com/github/lunatrius/ingameinfo/network/message/MessageSeed.java @@ -0,0 +1,39 @@ +package com.github.lunatrius.ingameinfo.network.message; + +import com.github.lunatrius.ingameinfo.tag.Tag; +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 cpw.mods.fml.relauncher.Side; +import io.netty.buffer.ByteBuf; + +public class MessageSeed implements IMessage, IMessageHandler { + public long seed; + + public MessageSeed() { + this.seed = 0; + } + + public MessageSeed(long seed) { + this.seed = seed; + } + + @Override + public void fromBytes(ByteBuf buf) { + this.seed = buf.readLong(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeLong(this.seed); + } + + @Override + public IMessage onMessage(MessageSeed message, MessageContext ctx) { + if (ctx.side == Side.CLIENT) { + Tag.setSeed(message.seed); + } + + return null; + } +} diff --git a/src/main/java/com/github/lunatrius/ingameinfo/proxy/ServerProxy.java b/src/main/java/com/github/lunatrius/ingameinfo/proxy/ServerProxy.java new file mode 100644 index 0000000..1251300 --- /dev/null +++ b/src/main/java/com/github/lunatrius/ingameinfo/proxy/ServerProxy.java @@ -0,0 +1,11 @@ +package com.github.lunatrius.ingameinfo.proxy; + +import com.github.lunatrius.ingameinfo.handler.PlayerHandler; +import cpw.mods.fml.common.FMLCommonHandler; + +public class ServerProxy extends CommonProxy { + @Override + public void registerEvents() { + FMLCommonHandler.instance().bus().register(new PlayerHandler()); + } +} diff --git a/src/main/java/com/github/lunatrius/ingameinfo/reference/Reference.java b/src/main/java/com/github/lunatrius/ingameinfo/reference/Reference.java index 758c7e5..5524b4d 100644 --- a/src/main/java/com/github/lunatrius/ingameinfo/reference/Reference.java +++ b/src/main/java/com/github/lunatrius/ingameinfo/reference/Reference.java @@ -9,7 +9,7 @@ public class Reference { public static final String VERSION = "${version}"; public static final String FORGE = "${forgeversion}"; public static final String MINECRAFT = "${mcversion}"; - public static final String PROXY_COMMON = "com.github.lunatrius.ingameinfo.proxy.CommonProxy"; + public static final String PROXY_SERVER = "com.github.lunatrius.ingameinfo.proxy.ServerProxy"; public static final String PROXY_CLIENT = "com.github.lunatrius.ingameinfo.proxy.ClientProxy"; public static final String GUI_FACTORY = "com.github.lunatrius.ingameinfo.client.gui.GuiFactory";