Jump to content

[1.7.10] Odd glitch encountered when changing item damage value


CactusCoffee

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Sometimes, tweaking load order or specific configs can also help ease those conflicts.
    • I have recently tired downloading forge so i can run mods for hypixel skyblock but whenever i download forge and do it the same way as videos and tutorials forge keeps downloading as log file and doesnt download the .jar file any help will be aprecciated.
    • So I have created a Create Mod Server with my friends and i to play everyone can join play except one of my friends because when he tries to launch the game from the CurseForge Launcher it will load but never open or give him a crash report, we have tried everything from deleting CF and reinstalling it to Updating his drivers, IDK what to do if anyone can help I would be very Grateful
    • I get this error when trying to start the server for forged mods. In the code below it mentions distant horizons but no matter what mod I put in (deleting distant horizons) it still gives an error and doesn't launch Crash Report UUID: 3e91d5c7-18a7-43c2-a935-a8d28de560d1 FML: 47.3 Forge: net.minecraftforge:47.3.10[23:33:47] [main/ERROR] [minecraft/Main]: Failed to start the minecraft server net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [    Distant Horizons (distanthorizons) encountered an error during the sided_setup event phase§7java.lang.ExceptionInInitializerError: null]     at net.minecraftforge.fml.ModLoader.waitForTransition(ModLoader.java:246) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at net.minecraftforge.fml.ModLoader.lambda$dispatchAndHandleError$20(ModLoader.java:210) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at java.util.Optional.ifPresent(Optional.java:178) ~[?:?] {}     at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:210) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at net.minecraftforge.fml.ModLoader.lambda$loadMods$15(ModLoader.java:190) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?] {}     at net.minecraftforge.fml.ModLoader.loadMods(ModLoader.java:190) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at net.minecraft.server.loading.ServerModLoader.load(ServerModLoader.java:31) ~[forge-1.20.1-47.3.10-universal.jar%23108!/:?] {re:classloading}     at net.minecraft.server.Main.main(Main.java:125) ~[server-1.20.1-20230612.114412-srg.jar%23103!/:?] {re:classloading}     at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:580) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:111) ~[fmlloader-1.20.1-47.3.10.jar%2369!/:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.lambda$run$1(CommonLaunchHandler.java:103) ~[fmlloader-1.20.1-47.3.10.jar%2369!/:?] {}     at net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$makeService$0(CommonServerLaunchHandler.java:27) ~[fmlloader-1.20.1-47.3.10.jar%2369!/:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:108) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:78) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {}  
    • This error keeps coming up after player tries to join server, doesn't happen to anyone else.  Error: Internal Exception: io.netty.handler.codec.DecoderException: java.lang.ArrayIndexOutOfBoundsException: Index 17196645 out of bounds for length 13 Heres the latest.log https://pastebin.com/uaw3KC0K  Heres the debug.log https://drive.google.com/file/d/1QzqtCMUf7ps1Iz85AOsMM7W8QhbHjBeU/view?usp=sharing
  • Topics

×
×
  • Create New...

Important Information

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