Skip to content

Commit da22aef

Browse files
committed
Fix inventory type enum resolver on paper 1.21.11
1 parent 31cc95f commit da22aef

3 files changed

Lines changed: 22 additions & 29 deletions

File tree

.idea/compiler.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/pcgf_pluginlib-platform-nms/pcgf_pluginlib-platform-nms-reflection/resources/mappings/1_21_R7_Paper/FieldMappings.txt

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,4 @@ net.minecraft.world.entity.player.EntityHuman#activeContainer bV
77
net.minecraft.world.inventory.Container#windowId q
88

99
#Containers enum mappings
10-
net.minecraft.world.inventory.MenuType#GENERIC_9X1 a
11-
net.minecraft.world.inventory.MenuType#GENERIC_9X2 b
12-
net.minecraft.world.inventory.MenuType#GENERIC_9X3 c
13-
net.minecraft.world.inventory.MenuType#GENERIC_9X4 d
14-
net.minecraft.world.inventory.MenuType#GENERIC_9X5 e
15-
net.minecraft.world.inventory.MenuType#GENERIC_9X6 f
16-
net.minecraft.world.inventory.MenuType#GENERIC_3X3 g
17-
net.minecraft.world.inventory.MenuType#CRAFTER h
18-
net.minecraft.world.inventory.MenuType#CRAFTER_3x3 h
19-
net.minecraft.world.inventory.MenuType#ANVIL i
20-
net.minecraft.world.inventory.MenuType#BEACON j
21-
net.minecraft.world.inventory.MenuType#BLAST_FURNACE k
22-
net.minecraft.world.inventory.MenuType#BREWING_STAND l
23-
net.minecraft.world.inventory.MenuType#CRAFTING m
24-
net.minecraft.world.inventory.MenuType#ENCHANTMENT n
25-
net.minecraft.world.inventory.MenuType#FURNACE o
26-
net.minecraft.world.inventory.MenuType#GRINDSTONE p
27-
net.minecraft.world.inventory.MenuType#HOPPER q
28-
net.minecraft.world.inventory.MenuType#LECTERN r
29-
net.minecraft.world.inventory.MenuType#LOOM s
30-
net.minecraft.world.inventory.MenuType#MERCHANT t
31-
net.minecraft.world.inventory.MenuType#SHULKER_BOX u
32-
net.minecraft.world.inventory.MenuType#SMITHING v
33-
net.minecraft.world.inventory.MenuType#SMOKER w
34-
net.minecraft.world.inventory.MenuType#CARTOGRAPHY_TABLE x
35-
net.minecraft.world.inventory.MenuType#STONECUTTER y
10+
net.minecraft.world.inventory.MenuType#CRAFTER CRAFTER_3x3

platform/pcgf_pluginlib-platform-nms/pcgf_pluginlib-platform-nms-reflection/src/at/pcgamingfreaks/Bukkit/Util/InventoryTypeMapper_Reflection.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
import at.pcgamingfreaks.Bukkit.MCVersion;
2121
import at.pcgamingfreaks.Bukkit.NmsReflector;
22+
import at.pcgamingfreaks.Reflection;
2223

2324
import org.bukkit.event.inventory.InventoryType;
2425
import org.bukkit.inventory.Inventory;
2526
import org.jetbrains.annotations.NotNull;
2627

2728
import java.lang.reflect.Field;
28-
import java.util.EnumMap;
29+
import java.util.*;
30+
import java.util.function.Function;
2931

3032
final class InventoryTypeMapper_Reflection
3133
{
@@ -37,6 +39,20 @@ final class InventoryTypeMapper_Reflection
3739
{
3840
if(CLASS_CONTAINERS != null)
3941
{
42+
Collection<Field> fields = Reflection.getFieldsIncludeParents(CLASS_CONTAINERS);
43+
Map<String, Field> fieldMap = new HashMap<>();
44+
for(Field f : fields)
45+
{
46+
//System.out.println(f.getName());
47+
fieldMap.put(f.getName(), f);
48+
fieldMap.put(f.getName().toUpperCase(Locale.ENGLISH), f);
49+
}
50+
Function<String, Field> resolveField = type -> {
51+
Field field = fieldMap.get(type); // Fast pass when no remapping is needed
52+
if(field != null) return field;
53+
field = NmsReflector.INSTANCE.getNmsField(CLASS_CONTAINERS, type); // Fallback when remapping is needed
54+
return field;
55+
};
4056
for(InventoryType inventoryType : InventoryType.values())
4157
{
4258
String type = inventoryType.name();
@@ -51,7 +67,7 @@ final class InventoryTypeMapper_Reflection
5167
else if(type.equals("COMPOSTER") || type.equals("CHISELED_BOOKSHELF") || type.equals("SMITHING_NEW") || type.equals("JUKEBOX") || type.equals("DECORATED_POT") || type.equals("SHELF")) continue; // They don't have inventory screens
5268
try
5369
{
54-
Field field = NmsReflector.INSTANCE.getNmsField(CLASS_CONTAINERS, type);
70+
Field field = resolveField.apply(type);
5571
if(field == null) continue;
5672
INVENTORY_TYPE_MAP.put(inventoryType, field.get(null));
5773
}
@@ -62,7 +78,7 @@ final class InventoryTypeMapper_Reflection
6278
{
6379
try
6480
{
65-
INVENTORY_TYPE_CHEST[i] = NmsReflector.INSTANCE.getNmsField(CLASS_CONTAINERS, "GENERIC_9X" + (i + 1)).get(null);
81+
INVENTORY_TYPE_CHEST[i] = resolveField.apply("GENERIC_9X" + (i + 1)).get(null);
6682
}
6783
catch(IllegalAccessException | NullPointerException ignored) {}
6884
}

0 commit comments

Comments
 (0)