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

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

 

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.

  • Author
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;
	}
}

 

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

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.