Increase protocol version to 6, implement PlayerInteractEvent in SetBlockPacketListener

Dieser Commit ist enthalten in:
Moulberry 2023-09-21 15:22:34 +08:00
Ursprung aff54bcd34
Commit 95ac82cf73
2 geänderte Dateien mit 117 neuen und 77 gelöschten Zeilen

Datei anzeigen

@ -12,7 +12,7 @@ public class AxiomConstants {
} }
} }
public static final int API_VERSION = 5; public static final int API_VERSION = 6;
public static final NamespacedKey ACTIVE_HOTBAR_INDEX = new NamespacedKey("axiom", "active_hotbar_index"); public static final NamespacedKey ACTIVE_HOTBAR_INDEX = new NamespacedKey("axiom", "active_hotbar_index");
public static final NamespacedKey HOTBAR_DATA = new NamespacedKey("axiom", "hotbar_data"); public static final NamespacedKey HOTBAR_DATA = new NamespacedKey("axiom", "hotbar_data");

Datei anzeigen

@ -8,6 +8,8 @@ import net.minecraft.core.SectionPos;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock; import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
@ -16,9 +18,14 @@ import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection; 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 org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_20_R1.block.CraftBlock;
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.messaging.PluginMessageListener; import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.jpenilla.reflectionremapper.ReflectionRemapper; import xyz.jpenilla.reflectionremapper.ReflectionRemapper;
@ -61,17 +68,49 @@ public class SetBlockPacketListener implements PluginMessageListener {
// Read packet // Read packet
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
BlockPos blockPos = friendlyByteBuf.readBlockPos(); Map<BlockPos, BlockState> blocks = friendlyByteBuf.readMap(FriendlyByteBuf::readBlockPos, buf -> buf.readById(Block.BLOCK_STATE_REGISTRY));
BlockState blockState = friendlyByteBuf.readById(Block.BLOCK_STATE_REGISTRY);
boolean updateNeighbors = friendlyByteBuf.readBoolean(); boolean updateNeighbors = friendlyByteBuf.readBoolean();
int sequenceId = friendlyByteBuf.readInt();
int reason = friendlyByteBuf.readVarInt();
boolean breaking = friendlyByteBuf.readBoolean();
BlockHitResult blockHit = friendlyByteBuf.readBlockHitResult();
InteractionHand hand = friendlyByteBuf.readEnum(InteractionHand.class);
int sequenceId = friendlyByteBuf.readVarInt();
ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle(); ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle();
org.bukkit.inventory.ItemStack heldItem;
if (hand == InteractionHand.MAIN_HAND) {
heldItem = bukkitPlayer.getInventory().getItemInMainHand();
} else {
heldItem = bukkitPlayer.getInventory().getItemInOffHand();
}
org.bukkit.block.Block blockClicked = bukkitPlayer.getWorld().getBlockAt(blockHit.getBlockPos().getX(),
blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ());
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()) {
if (sequenceId >= 0) {
player.connection.ackBlockChangesUpTo(sequenceId);
}
return;
}
// Update blocks // Update blocks
if (updateNeighbors) { if (updateNeighbors) {
player.level().setBlock(blockPos, blockState, 3); for (Map.Entry<BlockPos, BlockState> entry : blocks.entrySet()) {
player.level().setBlock(entry.getKey(), entry.getValue(), 3);
}
} else { } else {
for (Map.Entry<BlockPos, BlockState> entry : blocks.entrySet()) {
BlockPos blockPos = entry.getKey();
BlockState blockState = entry.getValue();
int bx = blockPos.getX(); int bx = blockPos.getX();
int by = blockPos.getY(); int by = blockPos.getY();
int bz = blockPos.getZ(); int bz = blockPos.getZ();
@ -154,6 +193,7 @@ public class SetBlockPacketListener implements PluginMessageListener {
level.getChunkSource().getLightEngine().updateSectionStatus(SectionPos.of(cx, cy, cz), nowHasOnlyAir); level.getChunkSource().getLightEngine().updateSectionStatus(SectionPos.of(cx, cy, cz), nowHasOnlyAir);
} }
} }
}
if (sequenceId >= 0) { if (sequenceId >= 0) {
player.connection.ackBlockChangesUpTo(sequenceId); player.connection.ackBlockChangesUpTo(sequenceId);