PaperMC/patches/unapplied/server/0964-Add-CompostItemEvent-and-EntityCompostItemEvent.patch
2023-03-14 18:11:24 +01:00

94 lines
5.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Noah van der Aa <ndvdaa@gmail.com>
Date: Sun, 8 Aug 2021 19:56:02 +0200
Subject: [PATCH] Add CompostItemEvent and EntityCompostItemEvent
diff --git a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
index 564822911c25238900b361d564c5db3103900fb3..f6268231e39f50bb6adedd85e3c18d746ae3792d 100644
--- a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
@@ -227,7 +227,10 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
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(state, world, pos, itemstack, rand);
+ BlockState iblockdata1 = ComposterBlock.addItem(state, world, pos, itemstack, player);
+ if (iblockdata1 == null) {
+ return InteractionResult.PASS;
+ }
// Paper end
world.levelEvent(1500, pos, state != iblockdata1 ? 1 : 0);
@@ -252,11 +255,16 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
if (i < 7 && ComposterBlock.COMPOSTABLES.containsKey(itemstack.getItem())) {
// CraftBukkit start
double rand = worldserver.getRandom().nextDouble();
- BlockState iblockdata1 = ComposterBlock.addItem(iblockdata, DummyGeneratorAccess.INSTANCE, blockposition, itemstack, rand);
+ BlockState iblockdata1 = ComposterBlock.addItem(iblockdata, DummyGeneratorAccess.INSTANCE, blockposition, itemstack, rand, entity); // Paper
if (iblockdata == iblockdata1 || org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(entity, blockposition, iblockdata1).isCancelled()) {
return iblockdata;
}
- iblockdata1 = ComposterBlock.addItem(iblockdata, (LevelAccessor) worldserver, blockposition, itemstack, rand);
+ // Paper start
+ iblockdata1 = ComposterBlock.addItem(iblockdata, (LevelAccessor) worldserver, blockposition, itemstack, rand, entity);
+ if (iblockdata1 == null) {
+ return iblockdata;
+ }
+ // Paper end
// CraftBukkit end
itemstack.shrink(1);
@@ -303,13 +311,39 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
// CraftBukkit start
return ComposterBlock.addItem(state, world, pos, item, world.getRandom().nextDouble());
}
+ // Paper start
+ static BlockState addItem(BlockState state, LevelAccessor world, BlockPos pos, ItemStack item, @Nullable Entity entity) {
+ return ComposterBlock.addItem(state, world, pos, item, world.getRandom().nextDouble(), entity);
+ }
+ // Paper end
static BlockState addItem(BlockState iblockdata, LevelAccessor generatoraccess, BlockPos blockposition, ItemStack itemstack, double rand) {
+ // Paper start
+ return ComposterBlock.addItem(iblockdata, generatoraccess, blockposition, itemstack, rand, null);
+ }
+
+ @Nullable
+ static BlockState addItem(BlockState iblockdata, LevelAccessor generatoraccess, BlockPos blockposition, ItemStack itemstack, double rand, @Nullable Entity entity) {
+ // Paper end
// CraftBukkit end
int i = (Integer) iblockdata.getValue(ComposterBlock.LEVEL);
float f = ComposterBlock.COMPOSTABLES.getFloat(itemstack.getItem());
- if ((i != 0 || f <= 0.0F) && rand >= (double) f) {
+ // Paper start
+ io.papermc.paper.event.block.CompostItemEvent event;
+ boolean willRaiseLevel = !((i != 0 || f <= 0.0F) && rand >= (double) f);
+ 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;
+ }
+
+ if (!event.willRaiseLevel()) {
+ // Paper end
return iblockdata;
} else {
int j = i + 1;
@@ -453,6 +487,11 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
this.changed = true;
BlockState iblockdata = ComposterBlock.addItem(this.state, this.level, this.pos, itemstack);
+ // Paper start
+ if (iblockdata == null) {
+ return;
+ }
+ // Paper end
this.level.levelEvent(1500, this.pos, iblockdata != this.state ? 1 : 0);
this.removeItemNoUpdate(0);
}