From 36d17c312fbaaf5b37854a385aaf9f0f7ef80a63 Mon Sep 17 00:00:00 2001 From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com> Date: Wed, 31 May 2023 03:07:04 +0200 Subject: [PATCH] Re-implement the compost events (#9192) --- ...ItemEvent-and-EntityCompostItemEvent.patch | 43 ++++---------- ...ntityChangeBlockEvent-in-more-places.patch | 57 ++++++++++++++++--- 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch b/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch index 06a56fa82b..e3e57658f2 100644 --- a/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch +++ b/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch @@ -8,28 +8,6 @@ diff --git a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java b index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java +++ b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java -@@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { - // Paper start - EntityChangeBlockEvent - double rand = world.getRandom().nextDouble(); - BlockState dummyBlockState = ComposterBlock.addItem(player, state, org.bukkit.craftbukkit.util.DummyGeneratorAccess.INSTANCE, pos, itemstack, rand); -+ if (dummyBlockState == null) { -+ return InteractionResult.PASS; -+ } - if (state != dummyBlockState && org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(player, pos, dummyBlockState).isCancelled()) { // if block state will change and event cancelled - return InteractionResult.sidedSuccess(world.isClientSide); - } -@@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { - // CraftBukkit start - double rand = world.getRandom().nextDouble(); - BlockState iblockdata1 = ComposterBlock.addItem(user, state, DummyGeneratorAccess.INSTANCE, pos, stack, rand); -+ // Paper start -+ if (iblockdata1 == null) { -+ return state; -+ } -+ // Paper end - if (state == iblockdata1 || org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(user, pos, iblockdata1).isCancelled()) { - return state; - } @@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { int i = (Integer) iblockdata.getValue(ComposterBlock.LEVEL); float f = ComposterBlock.COMPOSTABLES.getFloat(itemstack.getItem()); @@ -37,18 +15,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - if ((i != 0 || f <= 0.0F) && rand >= (double) f) { + // Paper start + boolean willRaiseLevel = !((i != 0 || f <= 0.0F) && rand >= (double) f); -+ if (generatoraccess == DummyGeneratorAccess.INSTANCE || entity == null) { // call event on test run or when entity is null (via hopper) -+ final io.papermc.paper.event.block.CompostItemEvent event; -+ if (entity == null) { -+ event = new io.papermc.paper.event.block.CompostItemEvent(org.bukkit.craftbukkit.block.CraftBlock.at(generatoraccess, blockposition), itemstack.getBukkitStack(), willRaiseLevel); -+ } else { -+ event = new io.papermc.paper.event.entity.EntityCompostItemEvent(entity.getBukkitEntity(), org.bukkit.craftbukkit.block.CraftBlock.at(generatoraccess, blockposition), itemstack.getBukkitStack(), willRaiseLevel); -+ } -+ if (!event.callEvent()) { -+ return null; -+ } -+ willRaiseLevel = event.willRaiseLevel(); ++ final io.papermc.paper.event.block.CompostItemEvent event; ++ if (entity == null) { ++ event = new io.papermc.paper.event.block.CompostItemEvent(org.bukkit.craftbukkit.block.CraftBlock.at(generatoraccess, blockposition), itemstack.getBukkitStack(), willRaiseLevel); ++ } else { ++ event = new io.papermc.paper.event.entity.EntityCompostItemEvent(entity.getBukkitEntity(), org.bukkit.craftbukkit.block.CraftBlock.at(generatoraccess, blockposition), itemstack.getBukkitStack(), willRaiseLevel); + } ++ if (!event.callEvent()) { // check for cancellation of entity event (non entity event can't be cancelled cause of hoppers) ++ return null; ++ } ++ willRaiseLevel = event.willRaiseLevel(); ++ + if (!willRaiseLevel) { + // Paper end return iblockdata; diff --git a/patches/server/Fire-EntityChangeBlockEvent-in-more-places.patch b/patches/server/Fire-EntityChangeBlockEvent-in-more-places.patch index 079e367313..1154c0db7d 100644 --- a/patches/server/Fire-EntityChangeBlockEvent-in-more-places.patch +++ b/patches/server/Fire-EntityChangeBlockEvent-in-more-places.patch @@ -203,21 +203,62 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java +++ b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java @@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { - if (i < 8 && ComposterBlock.COMPOSTABLES.containsKey(itemstack.getItem())) { if (i < 7 && !world.isClientSide) { -- BlockState iblockdata1 = ComposterBlock.addItem(player, state, world, pos, itemstack); -+ // Paper start - EntityChangeBlockEvent -+ double rand = world.getRandom().nextDouble(); -+ BlockState dummyBlockState = ComposterBlock.addItem(player, state, org.bukkit.craftbukkit.util.DummyGeneratorAccess.INSTANCE, pos, itemstack, rand); -+ if (state != dummyBlockState && org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(player, pos, dummyBlockState).isCancelled()) { // if block state will change and event cancelled -+ return InteractionResult.sidedSuccess(world.isClientSide); + BlockState iblockdata1 = ComposterBlock.addItem(player, state, world, pos, itemstack); ++ // Paper start - handle cancelled events ++ if (iblockdata1 == null) { ++ return InteractionResult.PASS; + } -+ BlockState iblockdata1 = ComposterBlock.addItem(player, state, world, pos, itemstack, rand); + // Paper end world.levelEvent(1500, pos, state != iblockdata1 ? 1 : 0); player.awardStat(Stats.ITEM_USED.get(itemstack.getItem())); +@@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { + if (i < 7 && ComposterBlock.COMPOSTABLES.containsKey(stack.getItem())) { + // CraftBukkit start + double rand = world.getRandom().nextDouble(); +- BlockState iblockdata1 = ComposterBlock.addItem(user, state, DummyGeneratorAccess.INSTANCE, pos, stack, rand); +- if (state == iblockdata1 || org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(user, pos, iblockdata1).isCancelled()) { ++ BlockState iblockdata1 = null; // Paper ++ if (false && (state == iblockdata1 || org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(user, pos, iblockdata1).isCancelled())) { // Paper - call it later + return state; + } + iblockdata1 = ComposterBlock.addItem(user, state, world, pos, stack, rand); ++ // Paper start - handle cancelled events ++ if (iblockdata1 == null) { ++ return state; ++ } ++ // Paper end + // CraftBukkit end + + stack.shrink(1); +@@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { + return iblockdata1; + } + ++ @Nullable // Paper + static BlockState addItem(@Nullable Entity user, BlockState state, LevelAccessor world, BlockPos pos, ItemStack stack) { + // CraftBukkit start + return ComposterBlock.addItem(user, state, world, pos, stack, world.getRandom().nextDouble()); + } + ++ @Nullable // Paper - make it nullable + static BlockState addItem(@Nullable Entity entity, BlockState iblockdata, LevelAccessor generatoraccess, BlockPos blockposition, ItemStack itemstack, double rand) { + // CraftBukkit end + int i = (Integer) iblockdata.getValue(ComposterBlock.LEVEL); +@@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder { + } else { + int j = i + 1; + BlockState iblockdata1 = (BlockState) iblockdata.setValue(ComposterBlock.LEVEL, j); ++ // Paper start - move the EntityChangeBlockEvent here to avoid conflict later for the compost events ++ if (entity != null && org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(entity, blockposition, iblockdata1).isCancelled()) { ++ return null; ++ } ++ // Paper end + + generatoraccess.setBlock(blockposition, iblockdata1, 3); + generatoraccess.gameEvent(GameEvent.BLOCK_CHANGE, blockposition, GameEvent.Context.of(entity, iblockdata1)); diff --git a/src/main/java/org/bukkit/craftbukkit/util/DummyGeneratorAccess.java b/src/main/java/org/bukkit/craftbukkit/util/DummyGeneratorAccess.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/DummyGeneratorAccess.java