Skip to content

Commit bddbfb3

Browse files
committed
perf(bukkit): avoid full held-item component serialization on interact path
1 parent 256fc2a commit bddbfb3

5 files changed

Lines changed: 44 additions & 13 deletions

File tree

worldedit-bukkit/adapters/adapter-1.21.11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_11/PaperweightAdapter.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,20 @@ public org.bukkit.inventory.ItemStack adapt(BaseItemStack baseItemStack) {
665665

666666
@Override
667667
public BaseItemStack adapt(org.bukkit.inventory.ItemStack itemStack) {
668-
var registryAccess = DedicatedServer.getServer().registryAccess();
669-
final ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
670-
CompoundTag tag = (CompoundTag) COMPONENTS_CODEC.encodeStart(
671-
registryAccess.createSerializationContext(NbtOps.INSTANCE),
672-
nmsStack.getComponentsPatch()
673-
).getOrThrow();
674-
return new BaseItemStack(BukkitAdapter.asItemType(itemStack.getType()), LazyReference.from(() -> (LinCompoundTag) toNative(tag)), itemStack.getAmount());
668+
final org.bukkit.inventory.ItemStack snapshot = itemStack.clone();
669+
return new BaseItemStack(
670+
BukkitAdapter.asItemType(snapshot.getType()),
671+
LazyReference.from(() -> {
672+
var registryAccess = DedicatedServer.getServer().registryAccess();
673+
final ItemStack nmsStack = CraftItemStack.asNMSCopy(snapshot);
674+
CompoundTag tag = (CompoundTag) COMPONENTS_CODEC.encodeStart(
675+
registryAccess.createSerializationContext(NbtOps.INSTANCE),
676+
nmsStack.getComponentsPatch()
677+
).getOrThrow();
678+
return (LinCompoundTag) toNative(tag);
679+
}),
680+
snapshot.getAmount()
681+
);
675682
}
676683

677684
private final LoadingCache<ServerLevel, PaperweightFakePlayer> fakePlayers

worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.sk89q.worldedit.world.block.BlockTypes;
4646
import com.sk89q.worldedit.world.gamemode.GameMode;
4747
import com.sk89q.worldedit.world.gamemode.GameModes;
48+
import com.sk89q.worldedit.world.item.ItemType;
4849
import org.bukkit.Bukkit;
4950
import org.bukkit.Location;
5051
import org.bukkit.entity.Player;
@@ -83,6 +84,14 @@ public BaseItemStack getItemInHand(HandSide handSide) {
8384
return BukkitAdapter.adapt(itemStack);
8485
}
8586

87+
@Override
88+
public ItemType getItemTypeInHand(HandSide handSide) {
89+
ItemStack itemStack = handSide == HandSide.MAIN_HAND
90+
? player.getInventory().getItemInMainHand()
91+
: player.getInventory().getItemInOffHand();
92+
return BukkitAdapter.asItemType(itemStack.getType());
93+
}
94+
8695
@Override
8796
public BaseBlock getBlockInHand(HandSide handSide) throws WorldEditException {
8897
ItemStack itemStack = handSide == HandSide.MAIN_HAND

worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.sk89q.worldedit.world.block.BaseBlock;
3636
import com.sk89q.worldedit.world.block.BlockStateHolder;
3737
import com.sk89q.worldedit.world.gamemode.GameMode;
38+
import com.sk89q.worldedit.world.item.ItemType;
3839

3940
import javax.annotation.Nullable;
4041

@@ -72,6 +73,20 @@ public interface Player extends Entity, Actor {
7273
*/
7374
BaseItemStack getItemInHand(HandSide handSide);
7475

76+
/**
77+
* Get the type of item that the player is holding.
78+
*
79+
* <p>Platforms may override this to provide a cheaper implementation
80+
* than adapting the full held stack, which is useful for hot paths
81+
* that only need the item type.</p>
82+
*
83+
* @param handSide the hand side
84+
* @return the held item type
85+
*/
86+
default ItemType getItemTypeInHand(HandSide handSide) {
87+
return getItemInHand(handSide).getType();
88+
}
89+
7590
/**
7691
* Get the Block that the player is holding.
7792
*

worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static Direction getDirection(double rot) {
9595

9696
@Override
9797
public boolean isHoldingPickAxe() {
98-
ItemType item = getItemInHand(HandSide.MAIN_HAND).getType();
98+
ItemType item = getItemTypeInHand(HandSide.MAIN_HAND);
9999
return item == ItemTypes.IRON_PICKAXE
100100
|| item == ItemTypes.WOODEN_PICKAXE
101101
|| item == ItemTypes.STONE_PICKAXE
@@ -420,7 +420,7 @@ public Direction getCardinalDirection(int yawOffset) {
420420

421421
@Override
422422
public BaseBlock getBlockInHand(HandSide handSide) throws WorldEditException {
423-
final ItemType typeId = getItemInHand(handSide).getType();
423+
final ItemType typeId = getItemTypeInHand(handSide);
424424
if (typeId.hasBlockType()) {
425425
return typeId.getBlockType().getDefaultState().toBaseBlock();
426426
} else {

worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public void handleBlockInteract(BlockInteractEvent event) {
418418
}
419419
}
420420

421-
Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType());
421+
Tool tool = session.getTool(player.getItemTypeInHand(HandSide.MAIN_HAND));
422422
if (tool instanceof DoubleActionBlockTool doubleActionBlockTool && tool.canUse(player)) {
423423
if (doubleActionBlockTool.actSecondary(queryCapability(Capability.WORLD_EDITING),
424424
getConfiguration(), player, session, location, event.getFace())) {
@@ -427,7 +427,7 @@ public void handleBlockInteract(BlockInteractEvent event) {
427427
}
428428

429429
} else if (event.getType() == Interaction.OPEN) {
430-
Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType());
430+
Tool tool = session.getTool(player.getItemTypeInHand(HandSide.MAIN_HAND));
431431
if (tool instanceof BlockTool blockTool && tool.canUse(player)) {
432432
if (blockTool.actPrimary(queryCapability(Capability.WORLD_EDITING),
433433
getConfiguration(), player, session, location, event.getFace())) {
@@ -453,7 +453,7 @@ public void handlePlayerInput(PlayerInputEvent event) {
453453
try {
454454
exhaustive(switch (event.getInputType()) {
455455
case PRIMARY -> {
456-
Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType());
456+
Tool tool = session.getTool(player.getItemTypeInHand(HandSide.MAIN_HAND));
457457
if (tool instanceof DoubleActionTraceTool doubleActionTraceTool && tool.canUse(player)) {
458458
if (doubleActionTraceTool.actSecondary(queryCapability(Capability.WORLD_EDITING),
459459
getConfiguration(), player, session)) {
@@ -464,7 +464,7 @@ public void handlePlayerInput(PlayerInputEvent event) {
464464
yield dummyValue();
465465
}
466466
case SECONDARY -> {
467-
Tool tool = session.getTool(player.getItemInHand(HandSide.MAIN_HAND).getType());
467+
Tool tool = session.getTool(player.getItemTypeInHand(HandSide.MAIN_HAND));
468468
if (tool instanceof TraceTool traceTool && tool.canUse(player)) {
469469
if (traceTool.actPrimary(queryCapability(Capability.WORLD_EDITING),
470470
getConfiguration(), player, session)) {

0 commit comments

Comments
 (0)