Jump to content

Recommended Posts

Posted

I'm trying to make a Item with battery, so I use stack.damageItem() to damage the itemstack, but don't works.
Here's the register:
 

public static final RegistryObject<Item> REMOTE = ITEMS.register("remote", () -> new RemoteItem(new Item.Properties().group(ModItemGroup.LM_TAB)));

Here's the class of the item:
 

package me.foxware.lm.items;

import java.awt.Color;
import java.util.List;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

public class RemoteItem extends Item {
	public RemoteItem(Properties properties) {
		super(properties.maxDamage(100));
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) { 
		ItemStack item = player.getHeldItem(hand);
		
		if(!world.isRemote()) {
			if(item.getDamage() < item.getMaxDamage()) {
				Vector3d pos = player.getPositionVec();
				int X = Math.round((float) pos.x);
				int Y = Math.round((float) pos.y);
				int Z = Math.round((float) pos.z);
				
				item.damageItem(1, player, null);
				
				player.getCooldownTracker().setCooldown(this, 20);
				
				return ActionResult.resultSuccess(item);
			}
		}
		
		return ActionResult.resultFail(item);
	}
	
	@Override
	public boolean showDurabilityBar(ItemStack item) {
		if(item.getDamage() != 0)
			return true;
		return false;
	}
	@Override
	public int getRGBDurabilityForDisplay(ItemStack item) {
		return Color.CYAN.getRGB();
	}
	
	@Override
	public void addInformation(ItemStack item, World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
		tooltip.add(new TranslationTextComponent("item.battery", Integer.toString(item.getMaxDamage() - item.getDamage())));
		
		super.addInformation(item, world, tooltip, flag);
	}
	
	@Override
	public int getItemStackLimit(ItemStack item) {
		return 1;
	}
}

 

Posted
5 hours ago, FoxWare said:

if(item.getDamage() < item.getMaxDamage()) {

This is pointless. If damage exceeds max damage, the item is broken and removed from your inventory.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
7 hours ago, diesieben07 said:

That code should crash with a NullPointerException, you cannot pass null as onBroken.

 

Also:

Do not do this, translation keys ("item.battery" here) must include your ModID.

I "adjusted" my code but still don't work :(
 

package me.foxware.lm.items;

import java.awt.Color;
import java.util.List;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

public class RemoteItem extends Item {
	public RemoteItem(Properties properties) {
		super(properties.maxDamage(100));
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) { 
		ItemStack item = player.getHeldItem(hand);
		
		if(!world.isRemote()) {
			Vector3d pos = player.getPositionVec();
			int X = Math.round((float) pos.x);
			int Y = Math.round((float) pos.y);
			int Z = Math.round((float) pos.z);
			
			if(!player.abilities.isCreativeMode)
				item.damageItem(1, player, (damagePlayer) -> {damagePlayer.sendBreakAnimation(hand);});
			
			player.getCooldownTracker().setCooldown(this, 20);
			
			return ActionResult.resultSuccess(item);
		}
		
		return ActionResult.resultFail(item);
	}
	
	@Override
	public boolean showDurabilityBar(ItemStack item) {
		if(item.getDamage() != 0)
			return true;
		return false;
	}
	@Override
	public int getRGBDurabilityForDisplay(ItemStack item) {
		return Color.CYAN.getRGB();
	}
	
	@Override
	public void addInformation(ItemStack item, World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
		if(item.getDamage() != 0)
			tooltip.add(new TranslationTextComponent("info.lm.battery", Integer.toString(item.getMaxDamage() - item.getDamage())));
		
		super.addInformation(item, world, tooltip, flag);
	}
	
	@Override
	public int getItemStackLimit(ItemStack item) {
		return 1;
	}
}

 

Posted

Make sure you are in survival mode when testing the item durability...also i believe there are some lines in your code that are redundant and not necessary, for example:

this check

if(!player.abilities.isCreativeMode)

and this override

	@Override
	public boolean showDurabilityBar(ItemStack item) {
		if(item.getDamage() != 0)
			return true;
		return false;
	}
	

 

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

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.