Skip to content

Commit

Permalink
fix(spigot): player inventory detection not working with spigot mapping
Browse files Browse the repository at this point in the history
Names of containers/menu differs between Paper and Spigot.
  • Loading branch information
diogotcorreia committed Jun 19, 2024
1 parent fdcbbc4 commit a9fab60
Showing 1 changed file with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,27 @@ public ProtocolLibListener(SpigotMLP main, HandlerFunction.HandlerType... allowe
SIGN_NBT_ID = "Sign";
}

val containerClass = MinecraftReflection.getMinecraftClass("world.inventory.Container", "world.inventory.AbstractContainerMenu", "Container");
CONTAINER_PLAYER_CLASS = MinecraftReflection.getMinecraftClass("world.inventory.ContainerPlayer", "world.inventory.InventoryMenu", "ContainerPlayer");
if (MinecraftVersion.v1_20_5.atOrAbove()) { // 1.20.5+
val fuzzyHuman = FuzzyReflection.fromClass(MinecraftReflection.getEntityHumanClass());
val abstractContainerMenuClass = MinecraftReflection.getMinecraftClass("world.inventory.AbstractContainerMenu");
// We have to use this field matcher because the function in accessors matches superclasses
PLAYER_ACTIVE_CONTAINER_FIELD = Accessors.getFieldAccessor(
fuzzyHuman.getField((field, clazz) -> field.getType() == abstractContainerMenuClass)
fuzzyHuman.getField((field, clazz) -> field.getType() == containerClass)
);
val inventoryMenuClass = MinecraftReflection.getMinecraftClass("world.inventory.InventoryMenu");
PLAYER_INVENTORY_CONTAINER_FIELD = Accessors.getFieldAccessor(
fuzzyHuman.getField((field, clazz) -> field.getType() == inventoryMenuClass)
fuzzyHuman.getField((field, clazz) -> field.getType() == CONTAINER_PLAYER_CLASS)
);
CONTAINER_PLAYER_CLASS = null;

// Sanity check
assert PLAYER_ACTIVE_CONTAINER_FIELD.getField() != PLAYER_INVENTORY_CONTAINER_FIELD.getField();
} else {
val containerClass = MinecraftReflection.getMinecraftClass("world.inventory.Container", "Container");
val activeContainerField = Arrays.stream(MinecraftReflection.getEntityHumanClass().getDeclaredFields())
.filter(field -> field.getType() == containerClass && !field.getName().equals("defaultContainer"))
.findAny()
.orElseThrow(() -> new RuntimeException("Failed to find field for player's active container"));
PLAYER_ACTIVE_CONTAINER_FIELD = Accessors.getFieldAccessor(activeContainerField);
PLAYER_INVENTORY_CONTAINER_FIELD = null;
CONTAINER_PLAYER_CLASS = MinecraftReflection.getMinecraftClass("world.inventory.ContainerPlayer", "ContainerPlayer");
}

setupPacketHandlers();
Expand Down

0 comments on commit a9fab60

Please sign in to comment.