Jump to content

JimiIT92

Members
  • Posts

    867
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by JimiIT92

  1. I've done the class to sendToClient in the tick method (since this is called only from the tickHandler when world.isRemote is false). public void tick() { this.ticks++; if (this.ticks == this.getManaRegenTimer() && this.mana < this.maxMana) { this.ticks = 0; this.addMana(5); this.sendToClient(new ManaValue(this.getMana())); } } In game the bar updates correctly but still when logs out and then re-login the bar goes to default value (95/100) EDIT: in the PlayerJoinEvent i've printed the mana value and it is 100, while the bar displays 95 @SubscribeEvent public void onPlayerJoin(PlayerLoggedInEvent event) { PlayerProperties props = PlayerProperties.get(event.player); if(props != null) { System.out.println(props.getMana()); if(props.isFirstJoin()) { props.setJoined(); } } } This event is registered as well
  2. Ok, so i've changed the proxy methods to this public IThreadListener getListener(MessageContext ctx) { return ctx.side == Side.CLIENT ? Minecraft.getMinecraft() : super.getListener(ctx); } public EntityPlayer getPlayer(MessageContext ctx) { return ctx.side == Side.CLIENT ? Minecraft.getMinecraft().thePlayer : super.getPlayer(ctx); } but i did not udnerstand where i should call the sendToClient method. I think that this is incorrect and it will loop, so where should i call that method? package com.rpg.handler; import com.rpg.RPG; import com.rpg.messages.ManaValue; import com.rpg.player.PlayerProperties; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.IThreadListener; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class ManaHandler implements IMessageHandler<ManaValue, IMessage>{ public ManaHandler() { } @Override public IMessage onMessage(final ManaValue message, MessageContext ctx) { IThreadListener thread = RPG.proxy.getListener(ctx); EntityPlayer player = RPG.proxy.getPlayer(ctx); if(player != null) { final PlayerProperties props = PlayerProperties.get(player); if(props != null) { thread.addScheduledTask(new Runnable() { @Override public void run() { props.setMana(message.getValue()); props.sendToClient(new ManaValue(props.getMana())); } }); } } return null; } } EDIT: i've change the addMana function to this public void addMana(int amount) { this.setMana(this.mana + amount); this.sendToClient(new ManaValue(this.getMana())); } now the bar actually updates but it still reset once the player log out
  3. I've changed the check in the tick handler, but now this happens: the server sets the value to 100 and the bar in game goes to 100, but this error is fired as soon as it does [11:56:22] [Client thread/FATAL]: Error executing task java.util.concurrent.ExecutionException: java.lang.ClassCastException: net.minecraft.client.entity.EntityPlayerSP cannot be cast to net.minecraft.entity.player.EntityPlayerMP at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_92] at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_92] at net.minecraft.util.Util.runTask(Util.java:23) [util.class:?] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1070) [Minecraft.class:?] at net.minecraft.client.Minecraft.run(Minecraft.java:380) [Minecraft.class:?] at net.minecraft.client.main.Main.main(Main.java:116) [Main.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92] at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?] at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92] at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?] at GradleStart.main(GradleStart.java:26) [start/:?] Caused by: java.lang.ClassCastException: net.minecraft.client.entity.EntityPlayerSP cannot be cast to net.minecraft.entity.player.EntityPlayerMP at com.rpg.player.PlayerProperties.sendToClient(PlayerProperties.java:208) ~[PlayerProperties.class:?] at com.rpg.player.PlayerProperties.setMana(PlayerProperties.java:118) ~[PlayerProperties.class:?] at com.rpg.handler.ManaHandler$1.run(ManaHandler.java:30) ~[ManaHandler$1.class:?] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_92] at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_92] at net.minecraft.util.Util.runTask(Util.java:22) ~[util.class:?] ... 15 more and also when relogging the bar is resetted
  4. So i'm trying to add some extended player properties and makes so no unnecessary packets are sends through client/server. But since i've never worked with packets i did not really understand when i should send a packet to the client and when i should send it to the server. By now i send a packet only when something change (for example: the mana value has changed), but if i do this in a tick handler the mod crashes, giving a ClassCastException java.lang.ClassCastException: net.minecraft.client.entity.EntityPlayerSP cannot be cast to net.minecraft.entity.player.EntityPlayerMP at com.rpg.player.PlayerProperties.sendToClient(PlayerProperties.java:194) at com.rpg.player.PlayerProperties.setMana(PlayerProperties.java:113) at com.rpg.player.PlayerProperties.removeMana(PlayerProperties.java:121) at com.rpg.player.PlayerProperties.tick(PlayerProperties.java:217) at com.rpg.events.PlayerTick.onTick(PlayerTick.java:14) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_12_PlayerTick_onTick_PlayerTickEvent.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:49) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140) at net.minecraftforge.fml.common.FMLCommonHandler.onPlayerPostTick(FMLCommonHandler.java:357) at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:402) at net.minecraft.client.entity.EntityPlayerSP.onUpdate(EntityPlayerSP.java:163) at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2011) at net.minecraft.world.World.updateEntity(World.java:1976) at net.minecraft.world.World.updateEntities(World.java:1805) at net.minecraft.client.Minecraft.runTick(Minecraft.java:2176) at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1080) at net.minecraft.client.Minecraft.run(Minecraft.java:380) at net.minecraft.client.main.Main.main(Main.java:116) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) at GradleStart.main(GradleStart.java:26) And this are all the classes involved: PlayerProperties package com.rpg.player; import com.rpg.RPG; import com.rpg.messages.EnergyRegenTimer; import com.rpg.messages.EnergyValue; import com.rpg.messages.GoldValue; import com.rpg.messages.ManaRegenTimer; import com.rpg.messages.ManaValue; import com.rpg.messages.MaxEnergyValue; import com.rpg.messages.MaxManaValue; import com.rpg.messages.PlayerClass; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; public class PlayerProperties implements IExtendedEntityProperties { public final static String PLAYER_PROPERTIES = "PlayerProperties"; private EntityPlayer player; private int mana; private int maxMana; private int energy; private int maxEnergy; private int gold; private String playerClass; private boolean firstJoin; private int manaRegenTimer; private int energyRegenTimer; private int ticks = 0; public PlayerProperties(EntityPlayer player) { this.player = player; this.mana = this.maxMana = 100; this.energy = this.maxEnergy = 100; this.firstJoin = true; this.playerClass = ""; this.manaRegenTimer = 10; this.energyRegenTimer = 10; this.gold = 0; } public static final void register(EntityPlayer player) { player.registerExtendedProperties(PLAYER_PROPERTIES, new PlayerProperties(player)); } public static final PlayerProperties get(EntityPlayer player) { return (PlayerProperties) player.getExtendedProperties(PLAYER_PROPERTIES); } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound nbt = new NBTTagCompound(); nbt.setInteger("mana", this.mana); nbt.setInteger("maxMana", this.maxMana); nbt.setInteger("energy", this.energy); nbt.setInteger("maxEnergy", this.maxEnergy); nbt.setInteger("gold", this.gold); nbt.setString("playerClass", this.playerClass); nbt.setBoolean("firstJoin", this.firstJoin); nbt.setInteger("manaRegenTimer", this.manaRegenTimer); nbt.setInteger("energyRegenTimer", this.energyRegenTimer); compound.setTag(PLAYER_PROPERTIES, nbt); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound nbt = (NBTTagCompound) compound.getTag(PLAYER_PROPERTIES); this.mana = nbt.getInteger("mana"); this.maxMana = nbt.getInteger("maxMana"); this.energy = nbt.getInteger("energy"); this.maxEnergy = nbt.getInteger("maxEnergy"); this.gold = nbt.getInteger("gold"); this.playerClass = nbt.getString("playerClass"); this.firstJoin = nbt.getBoolean("firstJoin"); this.manaRegenTimer = nbt.getInteger("manaRegenTimer"); this.energyRegenTimer = nbt.getInteger("energyRegenTimer"); } @Override public void init(Entity entity, World world) { } public boolean isFirstJoin() { return this.firstJoin; } public void setJoined() { this.firstJoin = false; } public int getMana() { return this.mana; } public void setMana(int amount) { this.mana = amount; if (this.mana > this.maxMana) this.mana = this.maxMana; if (this.mana < 0) this.mana = 0; this.sendToClient(new ManaValue(this.mana)); } public void addMana(int amount) { this.setMana(this.mana + amount); } public void removeMana(int amount) { this.setMana(this.mana - amount); } public int getMaxMana() { return this.maxMana; } public void setMaxMana(int amount) { this.maxMana = amount; this.sendToClient(new MaxManaValue(this.maxMana)); } public int getEnergy() { return this.energy; } public void setEnergy(int amount) { this.energy = amount; if (this.energy > this.maxEnergy) this.energy = this.maxEnergy; if (this.energy < 0) this.energy = 0; this.sendToClient(new EnergyValue(this.energy)); } public int getMaxEnergy() { return this.maxEnergy; } public void setMaxEnergy(int amount) { this.maxEnergy = amount; this.sendToClient(new MaxEnergyValue(this.maxEnergy)); } public int getGold() { return this.gold; } public void setGold(int amount) { this.gold = amount; if (this.gold < 0) this.gold = 0; this.sendToClient(new GoldValue(this.gold)); } public String getPlayerClass() { return this.playerClass; } public void setPlayerClass(String playerClass) { this.playerClass = playerClass; this.sendToClient(new PlayerClass(this.playerClass.charAt(0))); } public int getManaRegenTimer() { return 20 * this.manaRegenTimer; } public void setManaRegenTimer(int amount) { this.manaRegenTimer = amount; this.sendToClient(new ManaRegenTimer(this.manaRegenTimer)); } public int getEnergyRegenTimer() { return 20 * this.energyRegenTimer; } public void setEnergyRegenTimer(int amount) { this.energyRegenTimer = amount; this.sendToClient(new EnergyRegenTimer(this.energyRegenTimer)); } private void sendToClient(IMessage message) { RPG.network.sendTo(message, (EntityPlayerMP) this.player); } private void sendToServer(IMessage message) { RPG.network.sendToServer(message); } public void copy(PlayerProperties props) { this.mana = props.getMana(); this.maxMana = props.getMaxMana(); this.energy = props.getEnergy(); this.maxEnergy = props.getMaxEnergy(); this.gold = props.getGold(); this.playerClass = props.getPlayerClass(); this.firstJoin = props.isFirstJoin(); this.manaRegenTimer = props.getManaRegenTimer(); this.energyRegenTimer = props.getEnergyRegenTimer(); } public void tick() { this.ticks++; if (this.ticks == this.getManaRegenTimer() && this.mana < this.maxMana) { this.ticks = 0; this.addMana(5); } } } ManaValue package com.rpg.messages; import io.netty.buffer.ByteBuf; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; public class ManaValue implements IMessage{ private int value; public ManaValue() { this.value = 0; } public ManaValue(int amount) { this.value = amount; } @Override public void fromBytes(ByteBuf buf) { this.value = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(this.value); } public void setValue(int amount) { this.value = amount; } public int getValue() { return this.value; } } ManaHandler package com.rpg.handler; import com.rpg.RPG; import com.rpg.messages.ManaValue; import com.rpg.player.PlayerProperties; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.IThreadListener; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class ManaHandler implements IMessageHandler<ManaValue, IMessage>{ public ManaHandler() { } @Override public IMessage onMessage(final ManaValue message, MessageContext ctx) { IThreadListener thread = RPG.proxy.getListener(ctx); EntityPlayer player = RPG.proxy.getPlayer(ctx); if(player != null) { final PlayerProperties props = PlayerProperties.get(player); if(props != null) { thread.addScheduledTask(new Runnable() { @Override public void run() { props.setMana(message.getValue()); } }); } } return null; } } TickHandler package com.rpg.events; import com.rpg.player.PlayerProperties; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent; public class PlayerTick { @SubscribeEvent public void onTick(PlayerTickEvent event) { PlayerProperties props = PlayerProperties.get(event.player); if(props != null && event.player.worldObj.isRemote) props.tick(); } } ClientProxy package com.rpg.proxy; import com.rpg.RPG; import com.rpg.events.DimensionChange; import com.rpg.events.EntityCreation; import com.rpg.events.PlayerClone; import com.rpg.events.PlayerJoin; import com.rpg.events.PlayerRespawn; import com.rpg.events.PlayerTick; import com.rpg.gui.GuiOverlay; import com.rpg.handler.EnergyHandler; import com.rpg.handler.EnergyRegenHandler; import com.rpg.handler.GoldHandler; import com.rpg.handler.ManaHandler; import com.rpg.handler.ManaRegenHandler; import com.rpg.handler.MaxEnergyHandler; import com.rpg.handler.MaxManaHandler; import com.rpg.handler.PlayerClassHandler; import com.rpg.messages.EnergyRegenTimer; import com.rpg.messages.EnergyValue; import com.rpg.messages.GoldValue; import com.rpg.messages.ManaRegenTimer; import com.rpg.messages.ManaValue; import com.rpg.messages.MaxEnergyValue; import com.rpg.messages.MaxManaValue; import com.rpg.messages.PlayerClass; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.IThreadListener; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; import net.minecraftforge.fml.relauncher.Side; public class ClientProxy extends CommonProxy{ public void preInit() { RPG.network = NetworkRegistry.INSTANCE.newSimpleChannel(RPG.MODID); RPG.network.registerMessage(ManaHandler.class, ManaValue.class, 0, Side.CLIENT); } public void init() { MinecraftForge.EVENT_BUS.register(new GuiOverlay(Minecraft.getMinecraft())); } public IThreadListener getListener(MessageContext ctx) { return Minecraft.getMinecraft(); } public EntityPlayer getPlayer(MessageContext ctx) { return Minecraft.getMinecraft().thePlayer; } public void registerEvents() { MinecraftForge.EVENT_BUS.register(new PlayerTick()); } } CommonProxy package com.rpg.proxy; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.IThreadListener; import net.minecraft.world.WorldServer; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class CommonProxy { public void preInit() {} public void init() {} public IThreadListener getListener(MessageContext ctx) { return (WorldServer) ctx.getServerHandler().playerEntity.worldObj; } public EntityPlayer getPlayer(MessageContext ctx) { return ctx.getServerHandler().playerEntity; } public void registerEvents() {} } Main Mod File package com.rpg; import com.rpg.proxy.CommonProxy; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.Mod.Instance; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; @Mod(modid = RPG.MODID, version = RPG.VERSION, name = RPG.NAME) public class RPG { @SidedProxy(clientSide = "com.rpg.proxy.ClientProxy", serverSide = "com.rpg.proxy.CommonProxy") public static CommonProxy proxy; @Instance("rpg") public static RPG instance; public static final String NAME = "RPG Mod"; public static final String MODID = "rpg"; public static final String VERSION = "1.0"; public static SimpleNetworkWrapper network; @EventHandler public void preInit(FMLPreInitializationEvent event) { proxy.preInit(); } @EventHandler public void init(FMLInitializationEvent event) { proxy.init(); proxy.registerEvents(); } } Let me know if other classes are needed So i want to understand how can this work? When i should send a packet to the server and when to the client?
  5. Just as i was suspecting, centralized the functions to change mana (basically add or subtract) and then send packets inside them Allright, thanks for the tip on optimization
  6. Mmmm, so i could just use one single method in the IEEP class that send the packet with the amount of mana and then call that or send the packet when i change the mana value directly into IEEP functions? For example the setCurrentMana
  7. I've never worked with packets so i know that are not handled as well So basically the calls i do in the handlers i shoul do in some methods ins the IEEP?
  8. I think i'm going somewhere. Right now i got this tick handler @SubscribeEvent public void onTick(PlayerTickEvent event) { ExtendedPlayer props = ExtendedPlayer.get(event.player); props.elapseTick(); if(props.shouldSend()) { RPG.INSTANCE.sendTo(new ManaValue(props.getCurrentMana()), (EntityPlayerMP) event.player); } } That every 5 seconds should update the mana value by one. However the game crashes when should send the packet, giving a cast Exception [03:16:42] [Client thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.fml.common.gameevent.TickEvent$PlayerTickEvent@1b8354aa: java.lang.ClassCastException: net.minecraft.client.entity.EntityPlayerSP cannot be cast to net.minecraft.entity.player.EntityPlayerMP at com.rpg.events.EventHandler.onTick(EventHandler.java:60) ~[EventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_8_EventHandler_onTick_PlayerTickEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:49) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140) [EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.onPlayerPostTick(FMLCommonHandler.java:357) [FMLCommonHandler.class:?] at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:402) [EntityPlayer.class:?] at net.minecraft.client.entity.EntityPlayerSP.onUpdate(EntityPlayerSP.java:163) [EntityPlayerSP.class:?] at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2011) [World.class:?] at net.minecraft.world.World.updateEntity(World.java:1976) [World.class:?] at net.minecraft.world.World.updateEntities(World.java:1805) [World.class:?] at net.minecraft.client.Minecraft.runTick(Minecraft.java:2176) [Minecraft.class:?] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1080) [Minecraft.class:?] at net.minecraft.client.Minecraft.run(Minecraft.java:380) [Minecraft.class:?] at net.minecraft.client.main.Main.main(Main.java:116) [Main.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92] at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?] at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92] at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?] at GradleStart.main(GradleStart.java:26) [start/:?]
  9. Didn't saw that edit Thanks, this works now PS: if i want to do some sort of regen (like every 5 seconds add 1 mana point) how could i do? I've tried the playerTickEvent but it lags of course)
  10. Got this now @SubscribeEvent public void onClonePlayer(PlayerEvent.Clone event) { ExtendedPlayer props = ExtendedPlayer.get(event.original); ExtendedPlayer.get(event.entityPlayer).copy(props); } but still full bar until a new spell is casted
  11. So sorry to reopen this thread, but i've noticed that when the player comes back from the End the bar glitches out (it's shown fully charge when instead the player has a different value). I've already checked if the value is passed correctly, and it is, so i don't know why it is shown full. I've added this function in the event handler to catch the "player return from End event" @SubscribeEvent public void onClonePlayer(PlayerEvent.Clone event) { ExtendedPlayer props = ExtendedPlayer.get(event.original); ExtendedPlayer.get(event.entityPlayer).copy(props); RPG.INSTANCE.sendTo(new ManaValue(props.getCurrentMana()), (EntityPlayerMP) event.entityPlayer); } I've tried sending a specific amount of mana in the message, but the bar is still full
  12. Don't know why but reloaded the game and it doesn't crash O_o That fully works now Thanks for the precious help to everybody
  13. I use this to notify the client RPG.INSTANCE.sendTo(new ManaValue(props.getCurrentMana()), player); but player is an EntityPlayerMP, wich it isn't in the event If i do RPG.INSTANCE.sendTo(new ManaValue(props.getCurrentMana()), (EntityPlayerMP) event.player); It will crash
  14. I use that event to check if the player first join a world. In that event i get the IEEP of the the player, so what should i add to also set the GUI?
  15. Ok, so i've added message and handler for the mana value (server send to client the current amount of mana after the skill), but of course i can't use getServerHandler() in the onMessage method, so what should i use? (Tried getClientHandler but don't know how to get the equivalent result) EDIT: fixed that (see git), but still if the player log out a world and then login the bar is full (but when it cast a skill it goes to the right value, so if before logout the mana was 75, when he relog and use a skill, he bar will go to 50 (75 he had before - 15 just used))
  16. Oh ;D In fact now i get no error But the GUI does not update, so i assume i should send a packet to the client in response with the current amount of mana or what?
  17. Sooooo what should i change?
  18. Ok. So now i've updated the key handler and the message handler code to send/check specific values, but nothing changed, still got the out of bounds exception
  19. So i should not send the keycode but instead a specific number (for example: 1 for first skill, 2 for second...) ?
  20. I've uploaded all the code to a git repo https://github.com/JimiIT92/RPGMod
  21. Done but still get the same error
  22. Ok, so i've added a packet/message handler, but now when i press Z i get this crash [18:59:41] [Client thread/ERROR] [FML]: FMLIndexedMessageCodec exception caught io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256) at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107) ~[MessageToMessageEncoder.class:4.0.23.Final] at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:658) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:716) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:706) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:741) ~[AbstractChannelHandlerContext.class:4.0.23.Final] at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:895) ~[DefaultChannelPipeline.class:4.0.23.Final] at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:240) ~[AbstractChannel.class:4.0.23.Final] at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:269) [simpleNetworkWrapper.class:?] at com.rpg.events.KeyHandler.playerTick(KeyHandler.java:43) [KeyHandler.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_8_KeyHandler_playerTick_PlayerTickEvent.invoke(.dynamic) [?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) [ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140) [EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.onPlayerPreTick(FMLCommonHandler.java:352) [FMLCommonHandler.class:?] at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:248) [EntityPlayer.class:?] at net.minecraft.client.entity.EntityPlayerSP.onUpdate(EntityPlayerSP.java:163) [EntityPlayerSP.class:?] at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2009) [World.class:?] at net.minecraft.world.World.updateEntity(World.java:1974) [World.class:?] at net.minecraft.world.World.updateEntities(World.java:1803) [World.class:?] at net.minecraft.client.Minecraft.runTick(Minecraft.java:2176) [Minecraft.class:?] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1080) [Minecraft.class:?] at net.minecraft.client.Minecraft.run(Minecraft.java:380) [Minecraft.class:?] at net.minecraft.client.main.Main.main(Main.java:116) [Main.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92] at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?] at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92] at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?] at GradleStart.main(GradleStart.java:26) [start/:?] Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256) at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1175) ~[AbstractByteBuf.class:4.0.23.Final] at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:619) ~[AbstractByteBuf.class:4.0.23.Final] at net.minecraft.network.PacketBuffer.readInt(PacketBuffer.java:697) ~[PacketBuffer.class:?] at com.rpg.messages.KeyPressed.toBytes(KeyPressed.java:26) ~[KeyPressed.class:?] at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:11) ~[simpleIndexedCodec.class:?] at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:7) ~[simpleIndexedCodec.class:?] at net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:55) ~[FMLIndexedMessageToMessageCodec.class:?] at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67) ~[MessageToMessageCodec$1.class:4.0.23.Final] at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:89) ~[MessageToMessageEncoder.class:4.0.23.Final] ... 34 more This is how the KeyHandler looks now package com.rpg.events; import org.lwjgl.input.Keyboard; import com.rpg.RPG; import com.rpg.messages.KeyPressed; import com.rpg.player.ExtendedPlayer; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class KeyHandler { public static KeyBinding skill_1 = new KeyBinding("Skill 1", Keyboard.KEY_Z, "key.categories.skills"); public static KeyBinding skill_2 = new KeyBinding("Skill 2", Keyboard.KEY_X, "key.categories.skills"); public static KeyBinding skill_3 = new KeyBinding("Skill 3", Keyboard.KEY_C, "key.categories.skills"); public static KeyBinding skill_4 = new KeyBinding("Skill 4", Keyboard.KEY_V, "key.categories.skills"); public KeyHandler() { ClientRegistry.registerKeyBinding(skill_1); ClientRegistry.registerKeyBinding(skill_2); ClientRegistry.registerKeyBinding(skill_3); ClientRegistry.registerKeyBinding(skill_4); } @SideOnly(Side.CLIENT) @SubscribeEvent public void playerTick(PlayerTickEvent event) { if (event.side == Side.SERVER) return; if (event.phase == Phase.START) { Minecraft mc = Minecraft.getMinecraft(); if (skill_1.isPressed()) { RPG.INSTANCE.sendToServer(new KeyPressed(skill_1.getKeyCode())); } if (skill_2.isPressed()) { } if (skill_3.isPressed()) { } if (skill_4.isPressed()) { } } } } Basically i send a message to the server with the code of the key pressed This is the KeyPressed message that i send package com.rpg.messages; import io.netty.buffer.ByteBuf; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; public class KeyPressed implements IMessage{ public int toSend; public KeyPressed() { this.toSend = 0; } public KeyPressed(int key) { this.toSend = key; } @Override public void fromBytes(ByteBuf buf) { System.out.println("FROM " + this.toSend); buf.writeInt(this.toSend); } @Override public void toBytes(ByteBuf buf) { System.out.println("TO " + buf.readInt()); this.toSend = buf.readInt(); } } And this is the MessageHandler package com.rpg.messages; import com.rpg.events.KeyHandler; import com.rpg.player.ExtendedPlayer; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; import scala.xml.TopScope; public class MessageHandler implements IMessageHandler<KeyPressed, IMessage>{ public MessageHandler() { } @Override public IMessage onMessage(KeyPressed message, MessageContext ctx) { ExtendedPlayer props = ExtendedPlayer.get(ctx.getServerHandler().playerEntity); int key = message.toSend; if(key == KeyHandler.skill_1.getKeyCode()) if(props.consumeMana(15)) { System.out.println("[sKILL 1] Player had enough mana to use"); } else { System.out.println("[sKILL 1] Player ran out of mana"); props.replenishMana(); } return null; } } Registered in the init method with MinecraftForge.EVENT_BUS.register(new MessageHandler()); Also the SimpleNetworkMapper instance is created and registered in the init method public static SimpleNetworkWrapper INSTANCE; .... INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(RPG.MODID); INSTANCE.registerMessage(MessageHandler.class, KeyPressed.class, 0, Side.SERVER);
  23. So the problem is that i must send a packet to the server with the current amount of mana, am i right? If so how can i create and send this packet? (never worked with packets )
  24. @coolAlias The mana is used whenever a player press the skill button (added by a key handler, default Z). This is the code of the key handler package com.rpg.events; import org.lwjgl.input.Keyboard; import com.rpg.player.ExtendedPlayer; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class KeyHandler { public static KeyBinding skill_1 = new KeyBinding("Skill 1", Keyboard.KEY_Z, "key.categories.skills"); public static KeyBinding skill_2 = new KeyBinding("Skill 2", Keyboard.KEY_X, "key.categories.skills"); public static KeyBinding skill_3 = new KeyBinding("Skill 3", Keyboard.KEY_C, "key.categories.skills"); public static KeyBinding skill_4 = new KeyBinding("Skill 4", Keyboard.KEY_V, "key.categories.skills"); public KeyHandler() { ClientRegistry.registerKeyBinding(skill_1); ClientRegistry.registerKeyBinding(skill_2); ClientRegistry.registerKeyBinding(skill_3); ClientRegistry.registerKeyBinding(skill_4); } @SideOnly(Side.CLIENT) @SubscribeEvent public void playerTick(PlayerTickEvent event) { if (event.side == Side.SERVER) return; if (event.phase == Phase.START) { Minecraft mc = Minecraft.getMinecraft(); if (skill_1.isPressed()) { ExtendedPlayer props = ExtendedPlayer.get(event.player); if(props.consumeMana(15)) { System.out.println("[sKILL 1] Player had enough mana to use"); } else { System.out.println("[sKILL 1] Player ran out of mana"); props.replenishMana(); } } if (skill_2.isPressed()) { } if (skill_3.isPressed()) { } if (skill_4.isPressed()) { } } } } @Choonster Thank you for the link, i'll check it right now
  25. I'm trying to update to use Capability but i really understand anything However i've added a first join check (to give an item to the player if is the very first time he join a world) and this it saves if the player logout (so first login the player gets the item, the if he logs out and then relog he did not get the item as he's already joined one time). So i don't understand why that works but the mana don't This is the updated class (still IEPP) package com.rpg.player; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; public class ExtendedPlayer implements IExtendedEntityProperties{ public final static String EXT_PROP_NAME = "ExtendedPlayer"; private final EntityPlayer player; private int currentMana; private int maxMana; private boolean firstJoin; public ExtendedPlayer(EntityPlayer player) { this.player = player; this.maxMana = 50; this.firstJoin = true; } public void setCurrentMana(int amount) { this.currentMana = amount; } public static final void register(EntityPlayer player) { player.registerExtendedProperties(EXT_PROP_NAME, new ExtendedPlayer(player)); } public static final ExtendedPlayer get(EntityPlayer player) { return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME); } @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); properties.setInteger("CurrentMana", this.currentMana); properties.setInteger("MaxMana", this.maxMana); properties.setBoolean("FirstJoin", this.firstJoin); compound.setTag(EXT_PROP_NAME, properties); } @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME); this.firstJoin = properties.getBoolean("FirstJoin"); this.currentMana = properties.getInteger("CurrentMana"); this.maxMana = properties.getInteger("MaxMana"); System.out.println("First Join? " + this.firstJoin); System.out.println("[Mana from NBT] " + this.currentMana + "/" + this.maxMana); } public boolean isFirstJoin() { return this.firstJoin; } public void setJoined() { this.firstJoin = false; } @Override public void init(Entity entity, World world) {} public boolean consumeMana(int amount) { boolean sufficient = amount <= this.currentMana; this.currentMana -= (amount < this.currentMana ? amount : this.currentMana); return sufficient; } public int getCurrentMana() { return this.currentMana; } public int getMaxMana() { return this.maxMana; } public void replenishMana() { this.currentMana = this.maxMana; } }
×
×
  • Create New...

Important Information

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