Jump to content

Recommended Posts

Posted

I'm a novice modder, so what I'm missing/doing wrong is probably rather obvious. I've done some looking around for tutorials / other support requests for relevant tips but couldn't find that either.

 

Anyway, part of the mod I'm making is an item which uses damage values as a charge: pretty basic, starts at 0 damage, using the item adds damage, item doesn't break at max damage, etc. That all works fine. But I also want to have another item used as fuel to recharge this item. I've got it so it consumes fuel on a button press when holding the item, depletes fuel, that also works as expected. However, when using the item after recharging, it acts as if it was not recharged.

 

Example: I use the item until it reaches a damage of, say, 34. On charging the item, it removes up to 10 damage points. So the item is now at 24 damage. When I use the item again, instead of going to 25 damage as expected, it goes to 35, as if the recharge didn't actually do anything.

 

It's not that it doesn't recharge; the damage meter appears properly, refilling as the item is recharged and disappearing at full charge. The item also uses addInformation() to display the charge as a tooltip, and that refills properly as well. So I'm proper confused. I've tried using both the setDamage(itemstack, value) and itemstack.damageItem(value) methods, both have the same result. I've also tried replacing the item with a new item with a lower damage value, but that prevented it from charging at all. I've seen mods like EE do this properly, what am I doing wrong?

 

Relevant code bits:

package [...]
import [...]
public class ItemStaff extends Item
{
protected ItemStaff(int l) 
{
	[...]
}
[...]
//Method for recharging
public void charge(ItemStack itemStack, EntityPlayer player)
{
	if (itemStack.stackTagCompound != null && getDamage(itemStack) > 0)
	{
		boolean flag = player.capabilities.isCreativeMode;
		if (!flag && player.inventory.hasItem(SimplyMagicMod.manaCrystal))
		{
			player.inventory.consumeInventoryItem(SimplyMagicMod.manaCrystal);
			itemStack.damageItem(-20, player);
		}
	}
}
[...]
//Item use if not targeting block
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{
	if (itemStack.stackTagCompound != null && canDoSpell(itemStack))
	{
		[...]
		itemStack.damageItem(itemStack.stackTagCompound.getInteger("Cost"), player);
	}
	[...]
}
//Item use if targeting block
@Override
public boolean onItemUse(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int p_77648_7_, float p_77648_8_, float p_77648_9_, float p_77648_10_)
{
	if (itemStack.stackTagCompound != null && canDoSpell(itemStack))
	{
		itemStack.damageItem(itemStack.stackTagCompound.getInteger("Cost"), player);
		[...]
	}
	else return false;
}
//Add tooltip
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack itemStack, EntityPlayer player, List dataList, boolean b) 
{
	[...]
	dataList.add("Charge: " + (getMaxDamage() - getDamage(itemStack)) + "/" + (getMaxDamage()));
	[...]
}
[...]
//Can execute special function (current charge - action cost >= 0 charge)
public boolean canDoSpell(ItemStack itemStack)
{
	return getDamage(itemStack) + itemStack.stackTagCompound.getInteger("Cost") <= getMaxDamage();
}
}

 

 

Posted

I'm pretty sure you're only updating the display damage (client side) when you need to update the actual damage (server side)

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

Just tried adding a world parameter and an if (!world.isRemote) to the charge method. That prevents the display from changing, so you seem to be right. I suspect that the reason is that the method is only activated on the client side, as it's called by a keyboard press. I've just started experimenting with keyboard bindings, so I probably did something wrong in my key handler. This is the relevant method in the keyboard handler:

@SubscribeEvent
public void onKeyInput(KeyInputEvent event) 
{
if (!FMLClientHandler.instance().isGUIOpen(GuiChat.class)) 
{
	if (keys[CUSTOM_INV].isPressed()) 
	{
		Minecraft mcInstance = Minecraft.getMinecraft();
		EntityPlayer player = (EntityPlayer)mcInstance.thePlayer;
		ItemStack inHand = player.inventory.getCurrentItem();
		if (inHand != null)
		{
			Item itemInUse = inHand.getItem();
			if (itemInUse instanceof ItemStaff)
			{
				((ItemStaff)itemInUse).charge(inHand, mcInstance.theWorld, player);
			}
		}
	}
}
}

Not sure what I need to do in order to fix this.

Posted

KeyHandlers are ALWAYS client side. I think you'd need to send an update packet to the server to update the damage then, update the client. The reason it goes to 35 like it never edited it is because the client is the only object that is changed rather than the variables on the server. Erino would be able to tell you more about how to fix it because he helped me with close to the same problem. :P

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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