3
0
Mirror von https://github.com/Moulberry/AxiomPaperPlugin.git synchronisiert 2024-11-17 05:40:06 +01:00

Improve SetBlockPacket logic

Dieser Commit ist enthalten in:
Moulberry 2024-04-12 21:36:32 +08:00
Ursprung ea791cde07
Commit 63c3d91ece

Datei anzeigen

@ -16,8 +16,7 @@ import net.minecraft.world.entity.ai.village.poi.PoiTypes;
import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.chunk.LevelChunk;
@ -25,6 +24,8 @@ import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.lighting.LightEngine; import net.minecraft.world.level.lighting.LightEngine;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
@ -89,26 +90,29 @@ public class SetBlockPacketListener implements PluginMessageListener {
ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle(); ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle();
org.bukkit.inventory.ItemStack heldItem; if (sequenceId >= 0) {
if (hand == InteractionHand.MAIN_HAND) { player.connection.ackBlockChangesUpTo(sequenceId);
heldItem = bukkitPlayer.getInventory().getItemInMainHand();
} else {
heldItem = bukkitPlayer.getInventory().getItemInOffHand();
} }
org.bukkit.block.Block blockClicked = bukkitPlayer.getWorld().getBlockAt(blockHit.getBlockPos().getX(), if (!blockHit.getLocation().equals(Vec3.ZERO)) {
blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); org.bukkit.inventory.ItemStack heldItem;
if (hand == InteractionHand.MAIN_HAND) {
BlockFace blockFace = CraftBlock.notchToBlockFace(blockHit.getDirection()); heldItem = bukkitPlayer.getInventory().getItemInMainHand();
} else {
// Call interact event heldItem = bukkitPlayer.getInventory().getItemInOffHand();
PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(bukkitPlayer, }
breaking ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK, heldItem, blockClicked, blockFace);
if (!playerInteractEvent.callEvent()) { org.bukkit.block.Block blockClicked = bukkitPlayer.getWorld().getBlockAt(blockHit.getBlockPos().getX(),
if (sequenceId >= 0) { blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ());
player.connection.ackBlockChangesUpTo(sequenceId);
BlockFace blockFace = CraftBlock.notchToBlockFace(blockHit.getDirection());
// Call interact event
PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(bukkitPlayer,
breaking ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK, heldItem, blockClicked, blockFace);
if (!playerInteractEvent.callEvent()) {
return;
} }
return;
} }
CraftWorld world = player.level().getWorld(); CraftWorld world = player.level().getWorld();
@ -124,6 +128,11 @@ public class SetBlockPacketListener implements PluginMessageListener {
BlockPos blockPos = entry.getKey(); BlockPos blockPos = entry.getKey();
BlockState blockState = entry.getValue(); BlockState blockState = entry.getValue();
// Disallow in unloaded chunks
if (!player.level().isLoaded(blockPos)) {
continue;
}
// Check PlotSquared // Check PlotSquared
if (blockState.isAir()) { if (blockState.isAir()) {
if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) { if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) {
@ -144,6 +153,11 @@ public class SetBlockPacketListener implements PluginMessageListener {
BlockPos blockPos = entry.getKey(); BlockPos blockPos = entry.getKey();
BlockState blockState = entry.getValue(); BlockState blockState = entry.getValue();
// Disallow in unloaded chunks
if (!player.level().isLoaded(blockPos)) {
continue;
}
// Check PlotSquared // Check PlotSquared
if (blockState.isAir()) { if (blockState.isAir()) {
if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) { if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) {
@ -254,15 +268,32 @@ public class SetBlockPacketListener implements PluginMessageListener {
if (!breaking) { if (!breaking) {
BlockPos clickedPos = blockPlaceContext.getClickedPos(); BlockPos clickedPos = blockPlaceContext.getClickedPos();
// Disallow in unloaded chunks
if (!player.level().isLoaded(clickedPos)) {
return;
}
BlockState desiredBlockState = blocks.get(clickedPos);
BlockState actualBlockState = player.level().getBlockState(clickedPos);
Block actualBlock = actualBlockState.getBlock();
// Ensure block is correct
if (desiredBlockState == null || desiredBlockState.isAir() || actualBlockState.isAir()) return;
if (desiredBlockState.getBlock() != actualBlock) return;
// Check plot squared
if (!PlotSquaredIntegration.canPlaceBlock(bukkitPlayer, new Location(world, clickedPos.getX(), clickedPos.getY(), clickedPos.getZ()))) {
return;
}
ItemStack inHand = player.getItemInHand(hand); ItemStack inHand = player.getItemInHand(hand);
BlockState blockState = player.level().getBlockState(clickedPos);
BlockItem.updateCustomBlockEntityTag(player.level(), player, clickedPos, inHand); BlockItem.updateCustomBlockEntityTag(player.level(), player, clickedPos, inHand);
blockState.getBlock().setPlacedBy(player.level(), clickedPos, blockState, player, inHand);
}
if (sequenceId >= 0) { if (!(actualBlock instanceof BedBlock) && !(actualBlock instanceof DoublePlantBlock) && !(actualBlock instanceof DoorBlock)) {
player.connection.ackBlockChangesUpTo(sequenceId); actualBlock.setPlacedBy(player.level(), clickedPos, actualBlockState, player, inHand);
}
} }
} }