mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-23 17:09:19 +00:00
Merge pull request #308 from iqtesterr/dev
revert place when event cancel
This commit is contained in:
@@ -47,6 +47,8 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -125,15 +127,19 @@ public class BlockItemBehavior extends BlockBoundItemBehavior {
|
||||
|
||||
// it's just world + pos
|
||||
BlockState previousState = bukkitBlock.getState();
|
||||
List<BlockState> revertStates = new ArrayList<>(2);
|
||||
revertStates.add(previousState);
|
||||
// place custom block
|
||||
placeBlock(placeLocation, blockStateToPlace);
|
||||
placeBlock(placeLocation, blockStateToPlace, revertStates);
|
||||
|
||||
if (player != null) {
|
||||
// call bukkit event
|
||||
BlockPlaceEvent bukkitPlaceEvent = new BlockPlaceEvent(bukkitBlock, previousState, againstBlock, (ItemStack) context.getItem().getItem(), bukkitPlayer, true, context.getHand() == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND);
|
||||
if (EventUtils.fireAndCheckCancel(bukkitPlaceEvent)) {
|
||||
// revert changes
|
||||
previousState.update(true, false);
|
||||
for (BlockState state : revertStates) {
|
||||
state.update(true, false);
|
||||
}
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
|
||||
@@ -141,7 +147,9 @@ public class BlockItemBehavior extends BlockBoundItemBehavior {
|
||||
CustomBlockPlaceEvent customPlaceEvent = new CustomBlockPlaceEvent(bukkitPlayer, placeLocation.clone(), blockStateToPlace, world.getBlockAt(placeLocation), context.getHand());
|
||||
if (EventUtils.fireAndCheckCancel(customPlaceEvent)) {
|
||||
// revert changes
|
||||
previousState.update(true, false);
|
||||
for (BlockState state : revertStates) {
|
||||
state.update(true, false);
|
||||
}
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
}
|
||||
@@ -215,7 +223,7 @@ public class BlockItemBehavior extends BlockBoundItemBehavior {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean placeBlock(Location location, ImmutableBlockState blockState) {
|
||||
protected boolean placeBlock(Location location, ImmutableBlockState blockState, List<BlockState> revertStates) {
|
||||
return CraftEngineBlocks.place(location, blockState, UpdateOption.UPDATE_ALL_IMMEDIATE, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,10 @@ import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigExce
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.BlockState;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DoubleHighBlockItemBehavior extends BlockItemBehavior {
|
||||
@@ -25,14 +27,15 @@ public class DoubleHighBlockItemBehavior extends BlockItemBehavior {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean placeBlock(Location location, ImmutableBlockState blockState) {
|
||||
protected boolean placeBlock(Location location, ImmutableBlockState blockState, List<BlockState> revertState) {
|
||||
Object level = FastNMS.INSTANCE.field$CraftWorld$ServerLevel(location.getWorld());
|
||||
Object blockPos = FastNMS.INSTANCE.constructor$BlockPos(location.getBlockX(), location.getBlockY() + 1, location.getBlockZ());
|
||||
UpdateOption option = UpdateOption.builder().updateNeighbors().updateClients().updateImmediate().updateKnownShape().build();
|
||||
Object fluidData = FastNMS.INSTANCE.method$BlockGetter$getFluidState(level, blockPos);
|
||||
Object stateToPlace = fluidData == MFluids.WATER$defaultState ? MBlocks.WATER$defaultState : MBlocks.AIR$defaultState;
|
||||
revertState.add(location.getWorld().getBlockAt(location.getBlockX(), location.getBlockY() + 1, location.getBlockZ()).getState());
|
||||
FastNMS.INSTANCE.method$LevelWriter$setBlock(level, blockPos, stateToPlace, option.flags());
|
||||
return super.placeBlock(location, blockState);
|
||||
return super.placeBlock(location, blockState, revertState);
|
||||
}
|
||||
|
||||
public static class Factory implements ItemBehaviorFactory {
|
||||
|
||||
Reference in New Issue
Block a user