Jump to content

[1.8][SOLVED] Custom Tool Sound On Break


statphantom

Recommended Posts

Hey everyone, I have a tool that extends ItemTool since it is a custom tool that does custom things.

 

however since it is custom I have made a custom damage function and custom break function and custom sound on break, this is due to my tool use doesn't destroy any blocks but now I have the problem that when I do use the tool to destroy a block it revers back to the normal onitemuse functions etc.

 

basically since onitemuse does things like damage items and breaks items already the only thing I would like to override is the sound animation of a particular tool (or if possible a ToolMaterial since I am making a custom sound for each material)

 

any help? thanks.

Link to comment
Share on other sites

This is all very abstract. Could you please show your code?

 

this is my event handlers (onPlayerClickEvent is where I call my 'attemptToDamage' method and in there is where i do my checking and playing sounds etc):

 

package statslevelmod;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class LevelEventHandler {

@SubscribeEvent
public void onEntityConstructing(EntityConstructing event) {

	if (event.entity instanceof EntityPlayer && PlayerVariables.get((EntityPlayer) event.entity) == null) {
		PlayerVariables.register((EntityPlayer) event.entity);
	}
}

@SubscribeEvent
public void onPlayerClonning(PlayerEvent.Clone event) {
	PlayerVariables pnew = PlayerVariables.get(event.entityPlayer);
	PlayerVariables pold = PlayerVariables.get(event.original);
	NBTTagCompound comp = new NBTTagCompound();
	pold.saveNBTData(comp);
	pnew.loadNBTData(comp);

}

@SubscribeEvent
public void onBlockDestroyedEvent(HarvestDropsEvent event) {

}

@SubscribeEvent
public void onPlayerMineEvent(PlayerEvent.BreakSpeed event) {

	ItemStack helditem = event.entityPlayer.getCurrentEquippedItem();
	event.newSpeed = -1;

	if (helditem == null) {

	} else if (event.entityPlayer.getCurrentEquippedItem().getItem() == Items.wooden_axe) {

		if (event.state.getBlock() == Blocks.log || event.state.getBlock() ==  Blocks.log2) {
			event.newSpeed = .33f;
		} else {

		}

	} else if (helditem.getItem() == Level.woodenrake) {

		if (event.state.getBlock() == Blocks.dirt) {
			event.newSpeed = .5f;
		}

	} else {

	}
}

@SubscribeEvent
public void onPlayerClickEvent(PlayerInteractEvent event) {

	if (event.action == Action.LEFT_CLICK_BLOCK) {

		Block block = event.world.getBlockState(event.pos).getBlock();

		ItemStack helditem = event.entityPlayer.getCurrentEquippedItem();

		if (helditem == null) {

			if (block == Blocks.grass && event.face == event.face.UP) {

				if (!event.world.isRemote) {

					if (checkIfUnderLeaves(event)) {

						float chance = Level.rnumber.nextFloat();
						float increase = 1 + (PlayerVariables.get(event.entityPlayer).getGatheringlvl() * 0.04f);

						if (chance < 0.10f) {
							event.world.setBlockState(event.pos, Blocks.dirt.getDefaultState());
						}

						chance = Level.rnumber.nextFloat();

						if (chance < 0.02f * increase) {
							spawnSharpStickFromGrass(event);
						} else if (chance < 0.15f * increase) {
							spawnStickFromGrass(event);
						}
					}
				}
				attemptToDamage(event);
			}

			if (block == Blocks.log || block == Blocks.log2) {

				if (!event.world.isRemote) {

					attemptToDamage(event);

					float chance = Level.rnumber.nextFloat();

					if (chance < 0.02) {
						spawnBarkFromLog(event);
					}
				}
			}
		} else if (helditem.getItem() == Items.wooden_axe) {

			if (block == Blocks.log || block == Blocks.log2) {

				if (!event.world.isRemote) {

					attemptToDamage(event);

					float chance = Level.rnumber.nextFloat();

					if (chance < 0.18) {
						spawnBarkFromLog(event);
					}
				}
			}
		} else if (helditem.getItem() == Level.woodenrake) {

			if (block == Blocks.grass && event.face == event.face.UP) {

				if (!event.world.isRemote) {

					attemptToDamage(event);

					if (checkIfUnderLeaves(event)) {

						float chance = Level.rnumber.nextFloat();

						if (chance < 0.20f) {
							event.world.setBlockState(event.pos, Blocks.dirt.getDefaultState());
						}

						if (chance < 0.35f) {
							spawnStickFromGrass(event);
						} else if (chance > 0.92f) {
							spawnSharpStickFromGrass(event);

						}
					}
				}
			}
		}
	}
}

public boolean checkIfUnderLeaves(PlayerInteractEvent event) {

	for (int i = 1; i <= 20; i++) {

		BlockPos pos = event.pos.add(0, i, 0);
		Block block = event.world.getBlockState(pos).getBlock();

		if (block == Blocks.air || block == Blocks.leaves || block == Blocks.leaves2 || block == Blocks.log || block == Blocks.log2) {

			if (block == Blocks.leaves || block == Blocks.leaves2) {
				return true;
			}
		} else {
			return false;
		}
	}

	return false;
}

public void liftEntityUp(EntityItem item) {
	item.posY += 1;
}

public void liftEntityForward(EntityItem item) {
	item.posX -= .7;
}

public void spawnStickFromGrass(PlayerInteractEvent event) {
	EntityItem drop = new EntityItem(event.world, event.pos.getX(), event.pos.getY(), event.pos.getZ(), new ItemStack(Items.stick));
	drop.setRotationYawHead(event.entityPlayer.rotationYaw);
	liftEntityUp(drop);
	event.world.spawnEntityInWorld(drop);

	PlayerVariables playerentity = PlayerVariables.get(event.entityPlayer);
	playerentity.addGatheringExp(3);
}

public void spawnSharpStickFromGrass(PlayerInteractEvent event) {
	EntityItem drop = new EntityItem(event.world, event.pos.getX(), event.pos.getY(), event.pos.getZ(), new ItemStack(Level.sharpstick));
	drop.setRotationYawHead(event.entityPlayer.rotationYaw);
	liftEntityUp(drop);
	event.world.spawnEntityInWorld(drop);

	PlayerVariables playerentity = PlayerVariables.get(event.entityPlayer);
	playerentity.addGatheringExp(10);
}

public void spawnBarkFromLog(PlayerInteractEvent event) {
	EntityItem drop = new EntityItem(event.world, event.pos.getX(), event.pos.getY(), event.pos.getZ(), new ItemStack(Level.bark));
	drop.setRotationYawHead(event.entityPlayer.rotationYaw);
	liftEntityForward(drop);
	event.world.spawnEntityInWorld(drop);

	PlayerVariables playerentity = PlayerVariables.get(event.entityPlayer);
	playerentity.addGatheringExp(20);

}

public void attemptToDamage(PlayerInteractEvent event) {
	if (event.entityPlayer.getHeldItem() != null) {
		ItemStack helditem = event.entityPlayer.getHeldItem();
		if (helditem.isItemStackDamageable()) {
			helditem.attemptDamageItem(1, Level.rnumber);
			if (helditem.getItemDamage() >= helditem.getMaxDamage()) {
				event.entityPlayer.inventory.mainInventory[event.entityPlayer.inventory.currentItem] = null;
				if (helditem.getItem() instanceof ItemTool) {
					playBreakSound(event, (ItemTool) helditem.getItem());

				}
			}
		}
	}
}

@SideOnly(Side.CLIENT)
public void playBreakSound(PlayerInteractEvent event, ItemTool item) {
	if (item.getToolMaterial() == ToolMaterial.WOOD) {
		event.world.playSoundAtEntity(event.entityPlayer, "statslevelmod:woodbreak", 1.0f, 1.0f);
	}
}
}

 

 

and this is my Tool Class (though probably not needed):

 

package statslevelmod;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class WoodenRake extends ItemTool {

private final String name = "woodenrake";

public WoodenRake() {
	super(2f, Level.material, Level.emptyset);
	setHarvestLevel("WoodenRake", toolMaterial.getHarvestLevel());

	GameRegistry.registerItem(this, name);
	setCreativeTab(CreativeTabs.tabMisc);
	setUnlocalizedName(name);
	setMaxDamage(61);
}
}

 

Link to comment
Share on other sites

i 'rake' through grass, turning grass to dirt, doing this i also damage my item and play a sound when it breaks, however if i damage the item till it destroyes by 'breaking' blocks it wont play my sound since it uses its own methods to damage on block break. i would like my sound to be played when the tool breaks from block breaking.

Link to comment
Share on other sites

Listen for

PlaySoundAtEntityEvent

. Check if

event.name

is

random.break

. If so and the entity is living (

instanceof EntityLivingBase

) and holding your Item (

entity.getHeldItem()

), change

event.name

to whatever you wish the sound to be instead.

 

Thanks, no idea there was an event for that.

Link to comment
Share on other sites

Listen for

PlaySoundAtEntityEvent

. Check if

event.name

is

random.break

. If so and the entity is living (

instanceof EntityLivingBase

) and holding your Item (

entity.getHeldItem()

), change

event.name

to whatever you wish the sound to be instead.

 

what is the exact name of random.break to check against? can't seem to find it at all :(

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



×
×
  • Create New...

Important Information

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