Jump to content

Recommended Posts

Posted

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.

Posted

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

 

Posted

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.

Posted

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.

Posted

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 :(

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

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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