Fix cancelling BlockPlaceEvent calling onRemove

This commit is contained in:
Tamion 2024-05-23 11:02:20 +02:00
parent acdfeeea3d
commit 276e09c3b0
3 changed files with 55 additions and 56 deletions

View file

@ -152,7 +152,7 @@
});
}
}
@@ -370,32 +418,198 @@
@@ -370,32 +418,200 @@
}
public InteractionResult useOn(UseOnContext context) {
@ -169,7 +169,7 @@
+ DataComponentPatch oldData = this.components.asPatch();
+ int oldCount = this.getCount();
+ ServerLevel world = (ServerLevel) context.getLevel();
+
+ if (!(item instanceof BucketItem/* || item instanceof SolidBucketItem*/)) { // if not bucket // Paper - Fix cancelled powdered snow bucket placement
+ world.captureBlockStates = true;
+ // special case bonemeal
@ -200,7 +200,7 @@
+ structureEvent = new StructureGrowEvent(location, treeType, isBonemeal, (Player) entityhuman.getBukkitEntity(), (List< BlockState>) (List<? extends BlockState>) blocks);
+ org.bukkit.Bukkit.getPluginManager().callEvent(structureEvent);
+ }
+
+ BlockFertilizeEvent fertilizeEvent = new BlockFertilizeEvent(CraftBlock.at(world, blockposition), (Player) entityhuman.getBukkitEntity(), (List< BlockState>) (List<? extends BlockState>) blocks);
+ fertilizeEvent.setCancelled(structureEvent != null && structureEvent.isCancelled());
+ org.bukkit.Bukkit.getPluginManager().callEvent(fertilizeEvent);
@ -246,9 +246,11 @@
+ world.capturedTileEntities.clear(); // Paper - Allow chests to be placed with NBT data; clear out block entities as chests and such will pop loot
+ // revert back all captured blocks
+ world.preventPoiUpdated = true; // CraftBukkit - SPIGOT-5710
+ world.isBlockPlaceCancelled = true; // Paper - prevent calling cleanup logic when undoing a block place upon a cancelled BlockPlaceEvent
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true, false);
+ }
+ world.isBlockPlaceCancelled = false; // Paper - prevent calling cleanup logic when undoing a block place upon a cancelled BlockPlaceEvent
+ world.preventPoiUpdated = false;
+
+ // Brute force all possible updates
@ -356,7 +358,7 @@
ItemStack itemstack = this.copy();
boolean flag = this.getUseDuration(user) <= 0;
InteractionResult enuminteractionresult = this.getItem().use(world, user, hand);
@@ -490,27 +704,66 @@
@@ -490,27 +706,66 @@
return this.isDamageableItem() && this.getDamageValue() >= this.getMaxDamage() - 1;
}
@ -430,7 +432,7 @@
this.shrink(1);
breakCallback.accept(item);
@@ -518,7 +771,7 @@
@@ -518,7 +773,7 @@
}
@ -439,7 +441,7 @@
if (player instanceof ServerPlayer entityplayer) {
int j = this.processDurabilityChange(amount, entityplayer.serverLevel(), entityplayer);
@@ -535,6 +788,11 @@
@@ -535,6 +790,11 @@
}
public void hurtAndBreak(int amount, LivingEntity entity, EquipmentSlot slot) {
@ -451,7 +453,7 @@
Level world = entity.level();
if (world instanceof ServerLevel worldserver) {
@@ -546,9 +804,9 @@
@@ -546,9 +806,9 @@
entityplayer = null;
}
@ -464,7 +466,7 @@
}
}
@@ -580,11 +838,11 @@
@@ -580,11 +840,11 @@
return this.getItem().getBarColor(this);
}
@ -478,7 +480,7 @@
return this.getItem().overrideOtherStackedOnMe(this, stack, slot, clickType, player, cursorStackReference);
}
@@ -592,8 +850,8 @@
@@ -592,8 +852,8 @@
Item item = this.getItem();
if (item.hurtEnemy(this, target, user)) {
@ -489,7 +491,7 @@
entityhuman.awardStat(Stats.ITEM_USED.get(item));
}
@@ -608,7 +866,7 @@
@@ -608,7 +868,7 @@
this.getItem().postHurtEnemy(this, target, user);
}
@ -498,7 +500,7 @@
Item item = this.getItem();
if (item.mineBlock(this, world, state, pos, miner)) {
@@ -617,11 +875,11 @@
@@ -617,11 +877,11 @@
}
@ -512,7 +514,7 @@
return this.getItem().interactLivingEntity(this, user, entity, hand);
}
@@ -736,7 +994,7 @@
@@ -736,7 +996,7 @@
}
@ -521,7 +523,7 @@
player.awardStat(Stats.ITEM_CRAFTED.get(this.getItem()), amount);
this.getItem().onCraftedBy(this, world, player);
}
@@ -768,7 +1026,13 @@
@@ -768,7 +1028,13 @@
public boolean useOnRelease() {
return this.getItem().useOnRelease(this);
@ -535,7 +537,7 @@
@Nullable
public <T> T set(DataComponentType<? super T> type, @Nullable T value) {
@@ -805,6 +1069,25 @@
@@ -805,6 +1071,25 @@
this.getItem().verifyComponentsAfterLoad(this);
}
}
@ -561,7 +563,7 @@
public void applyComponents(DataComponentPatch changes) {
this.components.applyPatch(changes);
@@ -858,7 +1141,7 @@
@@ -858,7 +1143,7 @@
}
private <T extends TooltipProvider> void addToTooltip(DataComponentType<T> componentType, Item.TooltipContext context, Consumer<Component> textConsumer, TooltipFlag type) {
@ -570,7 +572,7 @@
if (t0 != null) {
t0.addToTooltip(context, textConsumer, type);
@@ -866,7 +1149,7 @@
@@ -866,7 +1151,7 @@
}
@ -579,7 +581,7 @@
boolean flag = this.getItem().shouldPrintOpWarning(this, player);
if (!type.isCreative() && this.has(DataComponents.HIDE_TOOLTIP)) {
@@ -941,7 +1224,7 @@
@@ -941,7 +1226,7 @@
}
}
@ -588,7 +590,7 @@
ItemAttributeModifiers itemattributemodifiers = (ItemAttributeModifiers) this.getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
if (itemattributemodifiers.showInTooltip()) {
@@ -966,7 +1249,7 @@
@@ -966,7 +1251,7 @@
}
}
@ -597,7 +599,7 @@
double d0 = modifier.amount();
boolean flag = false;
@@ -1091,6 +1374,19 @@
@@ -1091,6 +1376,19 @@
EnchantmentHelper.forEachModifier(this, slot, attributeModifierConsumer);
}
@ -617,7 +619,7 @@
public Component getDisplayName() {
MutableComponent ichatmutablecomponent = Component.empty().append(this.getHoverName());
@@ -1153,7 +1449,7 @@
@@ -1153,7 +1451,7 @@
}
public void consume(int amount, @Nullable LivingEntity entity) {

View file

@ -69,7 +69,7 @@
protected final NeighborUpdater neighborUpdater;
private final List<TickingBlockEntity> pendingBlockEntityTickers = Lists.newArrayList();
private boolean tickingBlockEntities;
@@ -121,23 +145,90 @@
@@ -121,23 +145,91 @@
private final DamageSources damageSources;
private long subTickCount;
@ -87,6 +87,7 @@
+ public boolean preventPoiUpdated = false; // CraftBukkit - SPIGOT-5710
+ public boolean captureBlockStates = false;
+ public boolean captureTreeGeneration = false;
+ public boolean isBlockPlaceCancelled = false; // Paper - prevent calling cleanup logic when undoing a block place upon a cancelled BlockPlaceEvent
+ public Map<BlockPos, org.bukkit.craftbukkit.block.CraftBlockState> capturedBlockStates = new java.util.LinkedHashMap<>(); // Paper
+ public Map<BlockPos, BlockEntity> capturedTileEntities = new java.util.LinkedHashMap<>(); // Paper - Retain block place order when capturing blockstates
+ public List<ItemEntity> captureDrops;
@ -169,7 +170,7 @@
}
};
} else {
@@ -145,13 +236,90 @@
@@ -145,13 +237,90 @@
}
this.thread = Thread.currentThread();
@ -265,7 +266,7 @@
@Override
public boolean isClientSide() {
return this.isClientSide;
@@ -163,6 +331,13 @@
@@ -163,6 +332,13 @@
return null;
}
@ -279,7 +280,7 @@
public boolean isInWorldBounds(BlockPos pos) {
return !this.isOutsideBuildHeight(pos) && Level.isInWorldBoundsHorizontal(pos);
}
@@ -172,25 +347,87 @@
@@ -172,25 +348,87 @@
}
private static boolean isInWorldBoundsHorizontal(BlockPos pos) {
@ -312,7 +313,7 @@
+ // Paper start - if loaded
@Nullable
+ @Override
@Override
+ public final ChunkAccess getChunkIfLoadedImmediately(int x, int z) {
+ return ((ServerLevel)this).chunkSource.getChunkAtIfLoadedImmediately(x, z);
+ }
@ -365,13 +366,13 @@
+ return getWorldBorder().isWithinBounds(blockposition) ? getBlockStateIfLoaded(blockposition) : null;
+ }
+
@Override
+ @Override
public ChunkAccess getChunk(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create) {
+ // Paper end
ChunkAccess ichunkaccess = this.getChunkSource().getChunk(chunkX, chunkZ, leastStatus, create);
if (ichunkaccess == null && create) {
@@ -207,6 +444,22 @@
@@ -207,6 +445,22 @@
@Override
public boolean setBlock(BlockPos pos, BlockState state, int flags, int maxUpdateDepth) {
@ -394,7 +395,7 @@
if (this.isOutsideBuildHeight(pos)) {
return false;
} else if (!this.isClientSide && this.isDebug()) {
@@ -214,45 +467,126 @@
@@ -214,44 +468,125 @@
} else {
LevelChunk chunk = this.getChunkAt(pos);
Block block = state.getBlock();
@ -479,10 +480,10 @@
+ // CraftBukkit end
+
return true;
}
}
}
+ }
+ }
+ }
+
+ // CraftBukkit start - Split off from above in order to directly send client and physic updates
+ public void notifyAndUpdatePhysics(BlockPos blockposition, LevelChunk chunk, BlockState oldBlock, BlockState newBlock, BlockState actualBlock, int i, int j) {
+ BlockState iblockdata = newBlock;
@ -522,21 +523,20 @@
+ iblockdata.updateNeighbourShapes(this, blockposition, k, j - 1);
+ iblockdata.updateIndirectNeighbourShapes(this, blockposition, k, j - 1);
+ } // Paper - Fix block place logic
+ }
}
+
+ // CraftBukkit start - SPIGOT-5710
+ if (!this.preventPoiUpdated) {
+ this.onBlockStateChange(blockposition, iblockdata1, iblockdata2);
+ }
+ // CraftBukkit end
+ }
+ }
}
}
+ // CraftBukkit end
+
public void onBlockStateChange(BlockPos pos, BlockState oldBlock, BlockState newBlock) {}
@Override
@@ -270,15 +604,33 @@
@@ -270,15 +605,33 @@
return false;
} else {
FluidState fluid = this.getFluidState(pos);
@ -573,7 +573,7 @@
}
boolean flag1 = this.setBlock(pos, fluid.createLegacyBlock(), 3, maxUpdateDepth);
@@ -340,10 +692,18 @@
@@ -340,10 +693,18 @@
@Override
public BlockState getBlockState(BlockPos pos) {
@ -593,7 +593,7 @@
return chunk.getBlockState(pos);
}
@@ -446,34 +806,53 @@
@@ -446,34 +807,53 @@
this.pendingBlockEntityTickers.clear();
}
@ -656,7 +656,7 @@
public boolean shouldTickDeath(Entity entity) {
return true;
@@ -510,13 +889,32 @@
@@ -510,13 +890,32 @@
@Nullable
@Override
public BlockEntity getBlockEntity(BlockPos pos) {
@ -690,7 +690,7 @@
this.getChunkAt(blockposition).addAndRegisterBlockEntity(blockEntity);
}
}
@@ -643,7 +1041,7 @@
@@ -643,7 +1042,7 @@
for (int k = 0; k < j; ++k) {
EnderDragonPart entitycomplexpart = aentitycomplexpart[k];
@ -699,7 +699,7 @@
if (t0 != null && predicate.test(t0)) {
result.add(t0);
@@ -912,7 +1310,7 @@
@@ -912,7 +1311,7 @@
public static enum ExplosionInteraction implements StringRepresentable {

View file

@ -202,8 +202,9 @@
boolean flag3 = iblockdata1.hasBlockEntity();
if (!this.level.isClientSide) {
- if (!this.level.isClientSide) {
- iblockdata1.onRemove(this.level, pos, state, moved);
+ if (!this.level.isClientSide && !this.level.isBlockPlaceCancelled) { // Paper - prevent calling cleanup logic when undoing a block place upon a cancelled BlockPlaceEvent
+ iblockdata1.onRemove(this.level, blockposition, iblockdata, flag);
} else if (!iblockdata1.is(block) && flag3) {
- this.removeBlockEntity(pos);
@ -288,14 +289,10 @@
if (tileentity != null) {
Level world = this.level;
@@ -549,10 +626,69 @@
if (this.postLoad != null) {
this.postLoad.run(this);
this.postLoad = null;
+ }
+
+ }
+
@@ -553,6 +630,65 @@
}
+ // CraftBukkit start
+ public void loadCallback() {
+ // Paper start
@ -333,9 +330,9 @@
+ }
+ server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkPopulateEvent(bukkitChunk));
+ }
}
+ }
+ }
+
+ public void unloadCallback() {
+ org.bukkit.Server server = this.level.getCraftServer();
+ org.bukkit.Chunk bukkitChunk = new org.bukkit.craftbukkit.CraftChunk(this);
@ -347,8 +344,8 @@
+ // Paper start
+ this.loadedTicketLevel = false;
+ // Paper end
}
+ }
+
+ @Override
+ public boolean isUnsaved() {
+ return super.isUnsaved() && !this.mustNotSave;