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

Hello! first of all I'm new to programming and this is my first mod, sop please go "easy" on me :3

I am making a mod that adds more kinds of shears, so far I've added the first shear, and it actually works, but i want to make it so when i break a block that a shear isn't able to "shear", I don't want the durability to go down.

any way i can make this happen? here's the code of my Wooden Shears class, thanks!

 

package me.lilykat24.moarshears.items;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class ItemWoodenShears extends Item
{

	public ItemWoodenShears(String name)
	{
		setUnlocalizedName(name);
		setRegistryName(name);
	}
	
	 /**
     * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
     */
	public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving)
	{
		if (!worldIn.isRemote)
		{
			stack.damageItem(1, entityLiving);
		}
		
		Block block = state.getBlock();
		if (block instanceof net.minecraftforge.common.IShearable) return true;
        return state.getMaterial() != Material.LEAVES && block != Blocks.WEB && block != Blocks.TALLGRASS && block != Blocks.VINE && block != Blocks.TRIPWIRE && block != Blocks.WOOL ? super.onBlockDestroyed(stack, worldIn, state, pos, entityLiving) : true;
	}
	
	/**
     * Check whether this Item can harvest the given Block
     */
	public boolean canHarvestBlock(IBlockState blockIn)
	{
		Block block = blockIn.getBlock();
		return block == Blocks.WEB || block == Blocks.REDSTONE_WIRE || block == Blocks.TRIPWIRE;
	}
	
	/**
     * Returns true if the item can be used on the given entity, e.g. shears on sheep.
     */
	@Override
	public boolean itemInteractionForEntity(ItemStack itemstack, net.minecraft.entity.player.EntityPlayer player, EntityLivingBase entity, net.minecraft.util.EnumHand hand)
	{
		if (entity.world.isRemote)
		{
			return false;
		}
		if (entity instanceof net.minecraftforge.common.IShearable)
		{
			net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable)entity;
			BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
			if (target.isShearable(itemstack, entity.world, pos))
			{
				java.util.List<ItemStack> drops = target.onSheared(itemstack, entity.world, pos,
						net.minecraft.enchantment.EnchantmentHelper.getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
				
				java.util.Random rand = new java.util.Random();
				for (ItemStack stack : drops)
				{
					net.minecraft.entity.item.EntityItem ent = entity.entityDropItem(stack, 1.0F);
					ent.motionY += rand.nextFloat() * 0.5F;
					ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
					ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
				}
				itemstack.damageItem(1, entity);
			}
			return true;
		}
		return false;
	}
	
	@Override
	public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, net.minecraft.entity.player.EntityPlayer player)
	{
		if (player.world.isRemote || player.capabilities.isCreativeMode)
		{
			return false;
		}
		Block block = player.world.getBlockState(pos).getBlock();
		if (block instanceof net.minecraftforge.common.IShearable)
		{
			net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable)block;
			if (target.isShearable(itemstack, player.world, pos))
			{
				java.util.List<ItemStack> drops = target.onSheared(itemstack, player.world, pos,
						net.minecraft.enchantment.EnchantmentHelper.getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
				java.util.Random rand = new java.util.Random();
				
				for (ItemStack stack : drops)
				{
					float f = 0.7F;
					double d = (double)(rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
					double d1 = (double)(rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
					double d2 = (double)(rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
					net.minecraft.entity.item.EntityItem entityitem = new EntityItem(player.world, (double)pos.getX() + d, (double)pos.getY() + d1, (double)pos.getZ() + d2, stack);
					entityitem.setDefaultPickupDelay();
					player.world.spawnEntity(entityitem);
				}
				
				itemstack.damageItem(1, player);
				player.addStat(net.minecraft.stats.StatList.getBlockStats(block));
				player.world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
				return true;
			}
		}
		return false;
	}
	
	public float getDestroySpeed(ItemStack stack, IBlockState state)
	{
		Block block = state.getBlock();
		
		if (block != Blocks.WEB && state.getMaterial() != Material.LEAVES)
		{
			return block == Blocks.WOOL ? 5.0F : super.getDestroySpeed(stack, state);
		}
		else
		{
			return 15.0F;
		}
	}
}

 

Edited by LilyKat24

  • Author
2 hours ago, diesieben07 said:
  • Code style, issue 2.
  • You call damageItem in multiple places. Simply don't call it when you don't want the item to take damage.

Oh! So simple lol, thanks so much!

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.