Jump to content

FlashHUN

Members
  • Posts

    84
  • Joined

  • Last visited

Everything posted by FlashHUN

  1. I need to give the player Exp from my mod when they kill specific entities, however when the event fires, they don't get any exp. What am I doing wrong? The event is fired, everything works, but the EXP still doesn't get set to what it should be set to. @SubscribeEvent public void onDeath(LivingDeathEvent event) { Entity entity = event.getEntityLiving(); if (event.getSource().getTrueSource() instanceof EntityPlayer) { Random rand = new Random(); EntityPlayer player = (EntityPlayer)event.getSource().getTrueSource(); if (entity instanceof EntityShinobi) { EntityShinobi shinobi = (EntityShinobi)entity; int i = rand.nextInt(17); int r = rand.nextInt(1); if (r == 0) { player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setExp(player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getExp() + 150+i); PacketHandler.INSTANCE.sendTo(new PacketExp(player), (EntityPlayerMP)player); } else { player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setExp(player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getExp() + 150-i); PacketHandler.INSTANCE.sendTo(new PacketExp(player), (EntityPlayerMP)player); } } } }
  2. Sorry, been coding for 6 hours now, my brain has started to melt. Realized that I'm dumb, and the only data that I didn't sync was what I tried to get from the GUI. Sorry for wasting your time. Actually, nevermind. Like I said, I'm tired. It's synced. PacketLand: public class PacketLand implements IMessage { private int mana; public PacketLand() {} public PacketLand(EntityPlayer player) { mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getLand(); } @Override public void fromBytes(ByteBuf buf) { mana = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(mana); } public static class Handler implements IMessageHandler<PacketLand, IMessage> { @Override public IMessage onMessage(PacketLand message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } private void handle(PacketLand message, MessageContext ctx) { Main.proxy.handleMana(message.mana, 27); } } } Events for syncing: @SubscribeEvent public static void playerClone(final PlayerEvent.Clone event) { final IMana oldMana = getMana(event.getOriginal()); final IMana newMana = getMana(event.getEntityPlayer()); if (newMana != null && oldMana != null) { newMana.setLand(oldMana.getLand()); System.out.println("Player Clone Event Successful"); } } @SubscribeEvent public static void loginEvent(final PlayerLoggedInEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketLand(player), (EntityPlayerMP)player); System.out.println("Synced"); } @SubscribeEvent public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketLand(player), (EntityPlayerMP)player); System.out.println("Synced"); } @SubscribeEvent public static void respawnEvent(final PlayerRespawnEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketLand(player), (EntityPlayerMP)player); System.out.println("Synced"); } ClientProxy: public void handleMana(int mana, int packetType) { EntityPlayer player = Minecraft.getMinecraft().player; switch (packetType) { case 24: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setLand(mana); break; } }
  3. Sorry, been coding for 6 hours now, my brain has started to melt. Realized that I'm dumb, and the only data that I didn't sync was what I tried to get from the GUI. Sorry for wasting your time.
  4. In my mod I need to set up a GuiScreen that just simply shows the player some of their stats. However, even though the client is synced with the server through packets, I can't get the data in the GUI. The problem is that the mc.player.getCapability(CapabilityMana.MANA_CAPABILITY, null).get...() methods return 0. What could be causing this?
  5. Then how else should I do it? This is a packet that would be sent when the player first starts out and picks their base stats (they can pick 2 things through a GUI and those 2 things determine what stats you get at the start), so there aren't really any restrictions. For example, depending on the clan they choose at the start they might get more mana than you'd normally start out with.
  6. And now suddenly the data syncs without changing anything... What the... Anyways, marking as solved.
  7. I just figured out the way to shorten all of these into just 3 packets, but anyway, the packets should be arriving on the client, these work in the respawn and dimension change events too.
  8. The event fires, but the capabilities aren't synced to the client from the server. @SubscribeEvent public static void loginEvent(final PlayerLoggedInEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketMaxMana(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketClan(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketLand(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketAffiliation(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketGenjutsu(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketTaijutsu(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketKenjutsu(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketSharingan(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketSharinganSize(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketSharinganPattern(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketSharinganActive(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketByakugan(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketByakuganSize(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketByakuganActive(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketKetsuryuugan(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketKetsuryuuganActive(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketCurseMark(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketCurseMarkSize(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketCurseMarkType(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketCurseMarkActive(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketFireRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketWindRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketWaterRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketLightningRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketEarthRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketWoodRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketYinRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketYangRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketIceRelease(player), (EntityPlayerMP)player); PacketHandler.INSTANCE.sendTo(new PacketShikotsumyaku(player), (EntityPlayerMP)player); System.out.println("Synced"); }
  9. So, the packet that the client sends to the server should look something like this, right? public class ClientMana implements IMessage { private int data; public ClientMana(int data) { this.data = data; } @Override public void fromBytes(ByteBuf buf) { data = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(data); } public static class Handler implements IMessageHandler<ClientMana, IMessage> { @Override public IMessage onMessage(ClientMana message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } private void handle(ClientMana message, MessageContext ctx) { Main.proxy.handleServerMana(message.data, 1, ctx); } } } And the data would then get handled in the CommonProxy where this happens: public class CommonProxy { public void registerItemRenderer(Item item, int meta, String id) {} public EntityPlayer getPlayerEntityFromContext(MessageContext ctx) { return ctx.getServerHandler().player; } public void handleMana(int mana, int packetType) {} public void handleManaB(boolean mana, int packetType) {} public void handleManaD(double mana, int packetType) {} public void handleServerMana(int mana, int packetType, MessageContext ctx) { EntityPlayer player = ctx.getServerHandler().player; switch (packetType) { case 1: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); break; case 2: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMaxMana(mana); break; } } } And the ClientProxy looks like this: public class ClientProxy extends CommonProxy { public void registerItemRenderer(Item item, int meta, String id) { ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), id)); } public EntityPlayer getPlayerEntityFromContext(MessageContext ctx) { return ctx.side.isClient() ? Minecraft.getMinecraft().player : ctx.getServerHandler().player; } public void handleMana(int mana, int packetType) { EntityPlayer player = Minecraft.getMinecraft().player; switch (packetType) { case 1: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); break; case 2: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMaxMana(mana); break; case 3: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setByakugan(mana); break; case 5: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setByakuganSize(mana); break; case 6: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setKetsuryuugan(mana); break; case 8: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharingan(mana); break; case 10: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharinganPattern(mana); break; case 11: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharinganSize(mana); break; case 12: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMark(mana); break; case 13: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMarkSize(mana); break; case 14: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMarkType(mana); break; case 15: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setShikotsumyaku(mana); break; case 16: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setEarthRelease(mana); break; case 17: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setFireRelease(mana); break; case 18: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setIceRelease(mana); break; case 19: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setLightningRelease(mana); break; case 20: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setWaterRelease(mana); break; case 21: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setWindRelease(mana); break; case 22: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setWoodRelease(mana); break; case 23: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setYangRelease(mana); break; case 24: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setYinRelease(mana); break; case 25: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setAffiliation(mana); break; case 26: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setClan(mana); break; case 27: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setLand(mana); break; case 28: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setGenjutsu(mana); break; } } public void handleManaB(boolean mana, int packetType) { EntityPlayer player = Minecraft.getMinecraft().player; switch (packetType) { case 4: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setByakuganActive(mana); break; case 7: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setKetsuryuuganActive(mana); break; case 9: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharinganActive(mana); break; case 13: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMarkActive(mana); break; } } public void handleManaD(double mana, int packetType) { EntityPlayer player = Minecraft.getMinecraft().player; switch (packetType) { case 29: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setKenjutsu(mana); break; case 30: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setTaijutsu(mana); break; } } }
  10. I'm trying to understand, but all of this was a bit too much information to handle at once after a tiring day for my brain.
  11. We literally sent the message at the same time. So it would just be in the onMessage part? (or should I do it through a scheduled task this way too and schedule a handle method?)
  12. Wait, nevermind, the constructor would take the int itself and then the data would equal to the int in the constructor. I realized that now, but then again, how do I detect the packet arriving on a server?
  13. Then the data in the packet would equal something like GUISkillMenu.pointstospend in the constructor? How can I detect if a packet arrives on a server? Sorry for all of these stupid questions, but these anti cheating things are new to me.
  14. So, for example, I have a GUI where a player can spend their acquired points in skills. If the player wants to do that, then when they click the button in the GUI to do so, a packet is sent where the data is that the button was pressed. Then how can I get that data from the packet, and in what type of method should I enforce the restrictions on the server?
  15. Why is it terrible? I can see through it quite clearly that way. What would be a better way of doing it? I would have A LOT of different values. How could I make it so the packet differentiates which one I want changed? How could I do that? I would not like if players cheated, but how can I make the server decide what to do with said information?
  16. Thank you so much for all the help! I really appreciate it. Now I finally don't have memory leaks. I have two hopefully last questions though: Can I make the ClientProxy method handle multiple capabilities? For example instead of public void handleMana(int mana) { EntityPlayer player = Minecraft.getMinecraft().player; player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); } could I make it take a String too, and depending on what that is make it handle other capabilities? Something like this: public void handleMana(int data, String type) { EntityPlayer player = Minecraft.getMinecraft().player; if (type.equals("Mana") { player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(data); } else if (type.equals("MaxMana") { player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMaxMana(data); } } and then in PacketMana do: Main.proxy.handleMana(message.mana, "Mana"); and in PacketMaxMana do: Main.proxy.handleMana(message.mana, "MaxMana"); If I have to send one of these packets to the server, do I have to change anything? Like I said before, most of these packets need to be bi-directional since most of them could be edited through a GUI. Do I have to just copy the handleMana() in the ClientProxy to the CommonProxy so the data changes on both sides, or do I have to change something in the PacketHandler aswell, or even create a new packet that handles specifically receiving packets on the server?
  17. No. This packet is sent to the client. So, should I change the Side.SERVER to Side.CLIENT? This will always be null. How can I make that not be null? Use @SidedProxy? This is not correct on the client. You have to check if you are on the integrated server or not. That is done with world.isRemote, right?
  18. Alright, this is how it looks now: PacketHandler: public class PacketHandler { private static int packetId = 0; public static SimpleNetworkWrapper INSTANCE = null; public PacketHandler() { } public static int nextID() { return packetId++; } public static void registerMessages(String channelName) { INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(channelName); registerMessages(); } public static void registerMessages() { INSTANCE.registerMessage(PacketMana.Handler.class, PacketMana.class, nextID(), Side.SERVER); } } PacketMana: public class PacketMana implements IMessage { static CommonProxy proxy; private int mana; public PacketMana() {} public PacketMana(EntityPlayer player) { mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana(); } @Override public void fromBytes(ByteBuf buf) { mana = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(mana); } public static class Handler implements IMessageHandler<PacketMana, IMessage> { @Override public IMessage onMessage(PacketMana message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } private void handle(PacketMana message, MessageContext ctx) { proxy.handleMana(message.mana); } } } ClientProxy: public class ClientProxy extends CommonProxy { public void registerItemRenderer(Item item, int meta, String id) { ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), id)); } public EntityPlayer getPlayerEntityFromContext(MessageContext ctx) { return Minecraft.getMinecraft().player; } public void handleMana(int mana) { EntityPlayer player = Minecraft.getMinecraft().player; player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); } } CommonProxy: public class CommonProxy { public void registerItemRenderer(Item item, int meta, String id) {} public EntityPlayer getPlayerEntityFromContext(MessageContext ctx) { return ctx.getServerHandler().player; } public void handleMana(int mana) {} } CapabilityMana: public class CapabilityMana { @CapabilityInject(IMana.class) public static final Capability<IMana> MANA_CAPABILITY = null; public static final EnumFacing DEFAULT_FACING = null; public static final ResourceLocation ID = new ResourceLocation(References.mod_id, "Mana"); public static void register(){ CapabilityManager.INSTANCE.register(IMana.class, new Capability.IStorage<IMana>() { @Override public NBTBase writeNBT(Capability<IMana> capability, IMana instance, EnumFacing side) { NBTTagCompound nbt = new NBTTagCompound(); nbt.setInteger("mana", instance.getMana()); System.out.println("Writing"); return nbt; } @Override public void readNBT(Capability<IMana> capability, IMana instance, EnumFacing side, NBTBase nbt) { instance.setMana(((NBTTagCompound) nbt).getInteger("mana")); System.out.println("Reading"); } }, () -> new Mana()); } @Nullable public static IMana getMana(final EntityLivingBase entity){ return CapabilityUtils.getCapability(entity, MANA_CAPABILITY, DEFAULT_FACING); } public static ICapabilityProvider createProvider(final IMana mana){ return new CapabilityProviderSerializable<>(MANA_CAPABILITY, DEFAULT_FACING, mana); } @Mod.EventBusSubscriber(modid = References.mod_id) private static class EventHandler {@SubscribeEvent public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof EntityPlayer) { final Mana mana = new Mana(); event.addCapability(ID, createProvider(mana)); } } @SubscribeEvent public static void playerClone(final PlayerEvent.Clone event) { final IMana oldMana = getMana(event.getOriginal()); final IMana newMana = getMana(event.getEntityPlayer()); if (newMana != null && oldMana != null) { newMana.setMana(oldMana.getMana()); System.out.println("Player Clone Event Successful"); } } @SubscribeEvent public static void onUpdate(final PlayerTickEvent event) { final IMana mana = getMana(event.player); if(event.player instanceof EntityPlayerMP){ mana.fillMana(1); PacketHandler.INSTANCE.sendTo(new PacketMana(event.player), (EntityPlayerMP)event.player); } } @SubscribeEvent public static void loginEvent(final PlayerLoggedInEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); System.out.println("Synced"); } @SubscribeEvent public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); System.out.println("Synced"); } @SubscribeEvent public static void respawnEvent(final PlayerRespawnEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); System.out.println("Synced"); } } } Is there anything else I should be doing? Since the memory leak still happens.
  19. So, then the packet should look like this? public class PacketMana implements IMessage { static CommonProxy proxy; private int mana; public PacketMana() {} public PacketMana(EntityPlayer player) { mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana(); } @Override public void fromBytes(ByteBuf buf) { mana = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(mana); } public static class Handler implements IMessageHandler<PacketMana, IMessage> { @Override public IMessage onMessage(PacketMana message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } private void handle(PacketMana message, MessageContext ctx) { proxy.handleMana(mana); } } } The problem then is that the proxy.handleMana(mana); method asks for the mana int to be static. Is that fine? handleMana() in CommonProxy: public void handleMana(int mana) {} handleMana() in ClientProxy: public void handleMana(int mana) { EntityPlayer player = Minecraft.getMinecraft().player; player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); } This is not true.  Odd. When I did it that way the data didn't sync unless I did it through the scheduled task way.
  20. So, should it look something like this? (At least am I getting closer to how it should look like?) public class PacketMana implements IMessage { private int data; public PacketMana() {} public PacketMana(EntityPlayer player) { data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana(); } @Override public void fromBytes(ByteBuf buf) { data = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(data); } public static class Handler implements IMessageHandler<PacketMana, IMessage> { @Override public IMessage onMessage(PacketMana message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } private void handle(PacketMana message, MessageContext ctx) { EntityPlayer playerEntity = ctx.getServerHandler().player; playerEntity.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(message.data); } } } Should the EntityPlayer playerEntity = ctx.getServerHandler().player; be something else? If I do @SubscribeEvent public static void loginEvent(final PlayerLoggedInEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); System.out.println("Synced"); } @SubscribeEvent public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); System.out.println("Synced"); } @SubscribeEvent public static void respawnEvent(final PlayerRespawnEvent event) { EntityPlayer player = event.player; PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); System.out.println("Synced"); } the EntityPlayer hasn't constructed yet in the PlayerLoggedInEvent since it fires a tick before that happens. Should I just do public void handleData(MessageContext ctx) { EntityPlayer player = Minecraft.getMinecraft().player; player.getCapability(CapabilityMana.MANA_CAPABILITY, null); } in the ClientProxy?
  21. Should the packet look something like this then? (I don't really know how else to do it without the @SidedProxy) public class PacketMana implements IMessage { @SidedProxy(clientSide = References.client_proxy_class, serverSide = References.common_proxy_class) static CommonProxy proxy; MessageContext ctx; private int data; @Override public void fromBytes(ByteBuf buf) { EntityPlayer player = proxy.getPlayerEntityFromContext(ctx); data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(buf.readInt()); } @Override public void toBytes(ByteBuf buf) { EntityPlayer player = proxy.getPlayerEntityFromContext(ctx); buf.writeInt(player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana()); } public PacketMana() { EntityPlayer player = proxy.getPlayerEntityFromContext(ctx); data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana(); } public PacketMana(EntityPlayer player) { data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana(); } public static class Handler implements IMessageHandler<PacketMana, IMessage> { @Override public IMessage onMessage(PacketMana message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } private void handle(PacketMana message, MessageContext ctx) { EntityPlayerMP playerEntity = ctx.getServerHandler().player; playerEntity.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(message.data); } } } I need all packets in my mod to be bi-directional. Events for syncing: private static void sync(EntityPlayer player) { player.getCapability(MANA_CAPABILITY, null); PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); } @SubscribeEvent public static void loginEvent(final PlayerLoggedInEvent event) { EntityPlayer player = event.player; Minecraft.getMinecraft().addScheduledTask(() -> sync(player)); System.out.println("Synced"); } @SubscribeEvent public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) { EntityPlayer player = event.player; Minecraft.getMinecraft().addScheduledTask(() -> sync(player)); System.out.println("Synced"); } @SubscribeEvent public static void respawnEvent(final PlayerRespawnEvent event) { EntityPlayer player = event.player; Minecraft.getMinecraft().addScheduledTask(() -> sync(player)); System.out.println("Synced"); } } Should the handle method in my ClientProxy look like this? Or does casting EntityPlayerMP to EntityPlayer in the ClientProxy cause any problems? public void handleData(MessageContext ctx) { EntityPlayer player = Minecraft.getMinecraft().player; player.getCapability(CapabilityMana.MANA_CAPABILITY, null); PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player); } How do I pass the data from the received data from PacketHandler to the proxy?
  22. I understand what I should be doing now, I just don't know how and can't really find anything on it. Sorry for making you waste your time on me like this. How can I get the data only from the server side player in the events? How can I apply the data to the client player in the client proxy? Just simply send packets to them? If I don't access the client side player in the IMessage, should I do this in the packets, or am I just a completely lost cause: (Sorry for not putting it in code blocks but it wouldn't load) CommonProxy: ClientProxy:
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.