Skip to content

Commit

Permalink
Make Spade work more like a vanilla Hoe
Browse files Browse the repository at this point in the history
Now fires the UseHoeEvent and makes Hoe sounds.
  • Loading branch information
wlhlm committed Nov 10, 2024
1 parent 94745e3 commit 2b19fb3
Showing 1 changed file with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.minecraft.item.ItemTool;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.UseHoeEvent;

import com.github.bartimaeusnek.croploadcore.MyRandom;
import com.github.bartimaeusnek.croploadcore.Operators;
Expand Down Expand Up @@ -107,18 +109,32 @@ public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world,
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side,
float hitX, float hitY, float hitZ) {
if (side != 0 && world.getBlock(x, y + 1, z).getMaterial() == Material.air
&& (world.getBlock(x, y, z) == Blocks.grass || world.getBlock(x, y, z) == Blocks.dirt
|| world.getBlock(x, y, z) == Blocks.mycelium)) {
world.setBlock(x, y, z, Blocks.farmland);
return true;
} else if (side != 0 && world.getBlock(x, y + 1, z).getMaterial() == Material.air
&& (world.getBlock(x, y, z) == Blocks.farmland)) {
world.setBlock(x, y, z, Blocks.dirt);
return true;
} else {
return false;
if (!player.canPlayerEdit(x, y, z, side, stack)) {
return false;
}

var event = new UseHoeEvent(player, stack, world, x, y, z);
if (MinecraftForge.EVENT_BUS.post(event)) {
return false;
}

if (side != 0 && world.getBlock(x, y + 1, z).getMaterial() == Material.air) {
if (world.getBlock(x, y, z) == Blocks.grass || world.getBlock(x, y, z) == Blocks.dirt
|| world.getBlock(x, y, z) == Blocks.mycelium
|| world.getBlock(x, y, z) == Blocks.farmland) {
playSpadeSound(world, x, y, z);
if (!world.isRemote) {
world.setBlock(
x,
y,
z,
(world.getBlock(x, y, z) == Blocks.farmland) ? Blocks.dirt : Blocks.farmland);
}
return true;
}
}

return false;
}

@Override
Expand All @@ -131,4 +147,15 @@ public boolean onBlockDestroyed(ItemStack stack, World p_150894_2_, Block p_1508
public boolean hitEntity(ItemStack stack, EntityLivingBase p_77644_2_, EntityLivingBase player) {
return true;
}

private void playSpadeSound(World world, int x, int y, int z) {
var spadeBlock = Blocks.farmland;
world.playSoundEffect(
x + 0.5f,
y + 0.5f,
z + 0.5f,
spadeBlock.stepSound.getStepResourcePath(),
(spadeBlock.stepSound.getVolume() + 1f) + 0.5f,
spadeBlock.stepSound.getPitch() * 0.8f);
}
}

0 comments on commit 2b19fb3

Please sign in to comment.