Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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());
	}
}

  • Author

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.

  • Author

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.

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.

1.7.10 is no longer supported by forge, you are on your own.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.