Ferrettomato Posted July 23, 2015 Posted July 23, 2015 This code is going to be the end of me. I've tried everything I can think of - sides, packets, breaks, you name it - and I can't get this code to work. I'm trying to damage an armor item when a key binding is pressed, utilizing the armor tick, and everything points to this code being run on both the client and the server. Despite that, relogging consistently reverts the item's durability to either full or nearly full after it's been lowered to any given durability by the key press. Please, for my sanity's sake, what's wrong? *braces for obvious realization* public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) { if(KeyBindings.ability.isKeyDown()) { if(itemStack.getItemDamage() + 350 <= itemStack.getMaxDamage()) { world.setWorldTime(world.getWorldTime() + 50); itemStack.setItemDamage(itemStack.getItemDamage() + 350); System.out.println(world.isRemote); } else if(itemStack.getItemDamage() > itemStack.getMaxDamage()) itemStack.setItemDamage(itemStack.getMaxDamage()); } } Quote
coolAlias Posted July 23, 2015 Posted July 23, 2015 KeyBinding = CLIENT side only, so of course it's not working. You have to send a packet to the server. Quote http://i.imgur.com/NdrFdld.png[/img]
Ferrettomato Posted July 23, 2015 Author Posted July 23, 2015 I've tried that, nothing. Here's the relevant code, I've probably done something wrong there. onArmorTick method: public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) { if(KeyBindings.ability.isKeyDown()) { if(itemStack.getItemDamage() + 350 <= itemStack.getMaxDamage()) { world.setWorldTime(world.getWorldTime() + 50); PacketHandler.INSTANCE.sendToServer(new ItemDamageMessage(350)); System.out.println(world.isRemote); } else if(itemStack.getItemDamage() > itemStack.getMaxDamage()) itemStack.setItemDamage(itemStack.getMaxDamage()); } } ItemDamageMessage: package com.ferret.myfirstmod.handlers; import io.netty.buffer.ByteBuf; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; public class ItemDamageMessage implements IMessage { public ItemDamageMessage(){} public int toSend; public ItemDamageMessage(int toSend) { this.toSend = toSend; } @Override public void toBytes(ByteBuf buf) { buf.writeInt(toSend); } @Override public void fromBytes(ByteBuf buf) { toSend = buf.readInt(); } } ItemDamageMessageHandler: package com.ferret.myfirstmod.handlers; import java.util.Random; import java.util.concurrent.Callable; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; 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 ItemDamageMessageHandler implements IMessageHandler<ItemDamageMessage, IMessage> { @Override public IMessage onMessage(ItemDamageMessage message, MessageContext ctx) { EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity; // The value that was sent int amount = message.toSend; serverPlayer.getServerForPlayer().addScheduledTask(new ItemDamageRunnable(serverPlayer, amount)); System.out.println("Packet received!"); // No response packet return null; } } ItemDamageRunnable: package com.ferret.myfirstmod.handlers; import net.minecraft.entity.player.EntityPlayerMP; public class ItemDamageRunnable implements Runnable { EntityPlayerMP player; int amount; public ItemDamageRunnable(EntityPlayerMP player, int amount) { this.player = player; this.amount = amount; } @Override public void run() { if(player.getCurrentArmor(3) != null) player.getCurrentArmor(3).setItemDamage(player.getCurrentArmor(3).getItemDamage() + amount); } } I know that there's nothing wrong with the packet registration because I have a working packet that was copied from this. This performs identically to the client-only code, for some reason. Quote
Ferrettomato Posted July 23, 2015 Author Posted July 23, 2015 Hang on... Relogging is making the durability go up by exactly 131072 points each time, and relogging with the game-increased durability doesn't revert anything. The mask has a total of 167992 durability. Could the high maximum durability be causing problems? To clarify, its durability is that high because it allows for roughly 1 day of fast-forwarding followed by roughly 1 week of recharging. Quote
Ernio Posted July 23, 2015 Posted July 23, 2015 If by durability you mean itemDamage (or "meta") then yes. While itemDamage field is an integer, it is being saved to memory as short (max 32k). EDIT Also - you should NOT allow client to dictate amount, and if you really need that, then you SHOULD make additional check on server if player can send such amount. Quote 1.7.10 is no longer supported by forge, you are on your own.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.