From aa74a821ba3c53dc87ccdd1ea446aa6775bb8e1d Mon Sep 17 00:00:00 2001 From: Caltinor <62700786+Caltinor@users.noreply.github.com> Date: Tue, 1 Jun 2021 09:11:00 -0400 Subject: [PATCH 1/2] Break Event Posting --- .../events/harvest/CropHarvest.java | 81 ++++++++++--------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java b/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java index 0bf32c0..0136726 100644 --- a/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java +++ b/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java @@ -15,6 +15,7 @@ import net.minecraft.world.World; import net.minecraft.world.server.ServerWorld; import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock; +import net.minecraftforge.event.world.BlockEvent.BreakEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.ObfuscationReflectionHelper; import pam.pamhc2crops.Pamhc2crops; @@ -53,32 +54,35 @@ public void onCropHarvest(RightClickBlock event) { CropsBlock crop = (CropsBlock) event.getWorld().getBlockState(event.getPos()).getBlock(); if (crop.isMaxAge(event.getWorld().getBlockState(event.getPos()))) { if (!event.getWorld().isRemote) { - drops = Block.getDrops(event.getWorld().getBlockState(event.getPos()), - (ServerWorld) event.getWorld(), event.getPos(), - event.getWorld().getTileEntity(event.getPos())); - for (int i = 0; i < drops.size(); i++) { - if (drops.get(i).getItem() != getCropSeed(crop)) - event.getWorld() - .addEntity(new ItemEntity((World) event.getWorld(), event.getPos().getX(), - event.getPos().getY(), event.getPos().getZ(), - (ItemStack) drops.get(i))); - } - for (int i = 0; i < drops.size(); i++) { - if (drops.stream().distinct().limit(2).count() <= 1 || crop == Blocks.POTATOES - || crop == Blocks.CARROTS) { - drops.remove(0); - event.getWorld() - .addEntity(new ItemEntity((World) event.getWorld(), event.getPos().getX(), - event.getPos().getY(), event.getPos().getZ(), - (ItemStack) drops.get(i))); + BreakEvent breakEvent = new BreakEvent(event.getWorld(), event.getPos(), event.getWorld().getBlockState(event.getPos()), event.getPlayer()); + if (!breakEvent.isCanceled()) { + drops = Block.getDrops(event.getWorld().getBlockState(event.getPos()), + (ServerWorld) event.getWorld(), event.getPos(), + event.getWorld().getTileEntity(event.getPos())); + for (int i = 0; i < drops.size(); i++) { + if (drops.get(i).getItem() != getCropSeed(crop)) + event.getWorld() + .addEntity(new ItemEntity((World) event.getWorld(), event.getPos().getX(), + event.getPos().getY(), event.getPos().getZ(), + (ItemStack) drops.get(i))); } - + for (int i = 0; i < drops.size(); i++) { + if (drops.stream().distinct().limit(2).count() <= 1 || crop == Blocks.POTATOES + || crop == Blocks.CARROTS) { + drops.remove(0); + event.getWorld() + .addEntity(new ItemEntity((World) event.getWorld(), event.getPos().getX(), + event.getPos().getY(), event.getPos().getZ(), + (ItemStack) drops.get(i))); + } + + } + event.getPlayer().addExhaustion(.05F); + event.getWorld().playSound((PlayerEntity) null, event.getPos(), SoundEvents.BLOCK_CROP_BREAK, + SoundCategory.BLOCKS, 1.0F, 0.8F + event.getWorld().rand.nextFloat() * 0.4F); + event.getWorld().setBlockState(event.getPos(), crop.getDefaultState(), 2); + } - event.getPlayer().addExhaustion(.05F); - event.getWorld().playSound((PlayerEntity) null, event.getPos(), SoundEvents.BLOCK_CROP_BREAK, - SoundCategory.BLOCKS, 1.0F, 0.8F + event.getWorld().rand.nextFloat() * 0.4F); - event.getWorld().setBlockState(event.getPos(), crop.getDefaultState(), 2); - } event.getPlayer().swingArm(Hand.MAIN_HAND); } @@ -93,20 +97,23 @@ public void onCropHarvest(RightClickBlock event) { if (event.getWorld().getBlockState(event.getPos()).get(NetherWartBlock.AGE) == 3) { if (!event.getWorld().isRemote) { - drops = Block.getDrops(event.getWorld().getBlockState(event.getPos()), - (ServerWorld) event.getWorld(), event.getPos(), - event.getWorld().getTileEntity(event.getPos())); - for (int i = 0; i < drops.size(); i++) { - event.getWorld() - .addEntity(new ItemEntity((World) event.getWorld(), event.getPos().getX(), - event.getPos().getY(), event.getPos().getZ(), - (ItemStack) drops.get(i))); + BreakEvent breakEvent = new BreakEvent(event.getWorld(), event.getPos(), event.getWorld().getBlockState(event.getPos()), event.getPlayer()); + if (!breakEvent.isCanceled()) { + drops = Block.getDrops(event.getWorld().getBlockState(event.getPos()), + (ServerWorld) event.getWorld(), event.getPos(), + event.getWorld().getTileEntity(event.getPos())); + for (int i = 0; i < drops.size(); i++) { + event.getWorld() + .addEntity(new ItemEntity((World) event.getWorld(), event.getPos().getX(), + event.getPos().getY(), event.getPos().getZ(), + (ItemStack) drops.get(i))); + } + event.getPlayer().addExhaustion(.05F); + event.getWorld().playSound((PlayerEntity) null, event.getPos(), + SoundEvents.BLOCK_NETHER_WART_BREAK, SoundCategory.BLOCKS, 1.0F, + 0.8F + event.getWorld().rand.nextFloat() * 0.4F); + event.getWorld().setBlockState(event.getPos(), nether.getDefaultState(), 2); } - event.getPlayer().addExhaustion(.05F); - event.getWorld().playSound((PlayerEntity) null, event.getPos(), - SoundEvents.BLOCK_NETHER_WART_BREAK, SoundCategory.BLOCKS, 1.0F, - 0.8F + event.getWorld().rand.nextFloat() * 0.4F); - event.getWorld().setBlockState(event.getPos(), nether.getDefaultState(), 2); } event.getPlayer().swingArm(Hand.MAIN_HAND); } From 419a90b4bf90caa9a08ddbeab6d54cd240b4a3a5 Mon Sep 17 00:00:00 2001 From: Caltinor <62700786+Caltinor@users.noreply.github.com> Date: Tue, 1 Jun 2021 09:14:18 -0400 Subject: [PATCH 2/2] Break Event Posting (small fix) --- src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java b/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java index 0136726..b2c6f0e 100644 --- a/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java +++ b/src/main/java/pam/pamhc2crops/events/harvest/CropHarvest.java @@ -14,6 +14,7 @@ import net.minecraft.util.SoundEvents; import net.minecraft.world.World; import net.minecraft.world.server.ServerWorld; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock; import net.minecraftforge.event.world.BlockEvent.BreakEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -55,6 +56,7 @@ public void onCropHarvest(RightClickBlock event) { if (crop.isMaxAge(event.getWorld().getBlockState(event.getPos()))) { if (!event.getWorld().isRemote) { BreakEvent breakEvent = new BreakEvent(event.getWorld(), event.getPos(), event.getWorld().getBlockState(event.getPos()), event.getPlayer()); + MinecraftForge.EVENT_BUS.post(breakEvent); if (!breakEvent.isCanceled()) { drops = Block.getDrops(event.getWorld().getBlockState(event.getPos()), (ServerWorld) event.getWorld(), event.getPos(), @@ -98,6 +100,7 @@ public void onCropHarvest(RightClickBlock event) { if (event.getWorld().getBlockState(event.getPos()).get(NetherWartBlock.AGE) == 3) { if (!event.getWorld().isRemote) { BreakEvent breakEvent = new BreakEvent(event.getWorld(), event.getPos(), event.getWorld().getBlockState(event.getPos()), event.getPlayer()); + MinecraftForge.EVENT_BUS.post(breakEvent); if (!breakEvent.isCanceled()) { drops = Block.getDrops(event.getWorld().getBlockState(event.getPos()), (ServerWorld) event.getWorld(), event.getPos(),