1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Mon, 5 Nov 2018 04:23:51 +0000
Subject: [PATCH] Restore custom InventoryHolder support
Upstream removed the ability to consistently use a custom InventoryHolder,
However, the implementation does not use an InventoryHolder in any form
outside of custom inventories.
== AT ==
public-f net.minecraft.world.inventory.AbstractContainerMenu dataSlots
public-f net.minecraft.world.inventory.AbstractContainerMenu remoteDataSlots
Co-authored-by: Shane Freeder <theboyetronic@gmail.com>
diff --git a/src/main/java/io/papermc/paper/inventory/PaperInventoryCustomHolderContainer.java b/src/main/java/io/papermc/paper/inventory/PaperInventoryCustomHolderContainer.java
new file mode 100644
index 0000000000000000000000000000000000000000..224d4b2cc45b0d02230a76caee9c88573a448b4c
--- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/PaperInventoryCustomHolderContainer.java
@@ -0,0 +1,141 @@
+package io.papermc.paper.inventory;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
+import net.minecraft.world.Container;
+import net.minecraft.world.entity.player.Player;
+import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
+import org.bukkit.Location;
+import org.bukkit.craftbukkit.entity.CraftHumanEntity;
+import org.bukkit.entity.HumanEntity;
+import org.bukkit.event.inventory.InventoryType;
+import org.bukkit.inventory.InventoryHolder;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.framework.qual.DefaultQualifier;
+
+import java.util.List;
+
+@DefaultQualifier(NonNull.class)
+public final class PaperInventoryCustomHolderContainer implements Container {
+
+ private final InventoryHolder owner;
+ private final Container delegate;
+ private final InventoryType type;
+ private final String title;
+ private final Component adventure$title;
+
+ public PaperInventoryCustomHolderContainer(InventoryHolder owner, Container delegate, InventoryType type) {
+ this.owner = owner;
+ this.delegate = delegate;
+ this.type = type;
+ @Nullable Component adventure$title = null;
+ if (delegate instanceof BaseContainerBlockEntity blockEntity) {
+ adventure$title = blockEntity.getCustomName() != null ? PaperAdventure.asAdventure(blockEntity.getCustomName()) : null;
+ }
+ if (adventure$title == null) {
+ adventure$title = type.defaultTitle();
+ }
+ this.adventure$title = adventure$title;
+ this.title = LegacyComponentSerializer.legacySection().serialize(this.adventure$title);
+ }
+
+ public Component title() {
+ return this.adventure$title;
+ }
+
+ public String getTitle() {
+ return this.title;
+ }
+
+ public InventoryType getType() {
+ return this.type;
+ }
+
+ @Override
+ public int getContainerSize() {
+ return this.delegate.getContainerSize();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return this.delegate.isEmpty();
+ }
+
+ @Override
+ public ItemStack getItem(int slot) {
+ return this.delegate.getItem(slot);
+ }
+
+ @Override
+ public ItemStack removeItem(int slot, int amount) {
+ return this.delegate.removeItem(slot, amount);
+ }
+
+ @Override
+ public ItemStack removeItemNoUpdate(int slot) {
+ return this.delegate.removeItemNoUpdate(slot);
+ }
+
+ @Override
+ public void setItem(int slot, ItemStack stack) {
+ this.delegate.setItem(slot, stack);
+ }
+
+ @Override
+ public int getMaxStackSize() {
+ return this.delegate.getMaxStackSize();
+ }
+
+ @Override
+ public void setChanged() {
+ this.delegate.setChanged();
+ }
+
+ @Override
+ public boolean stillValid(Player player) {
+ return this.delegate.stillValid(player);
+ }
+
+ @Override
+ public List<ItemStack> getContents() {
+ return this.delegate.getContents();
+ }
+
+ @Override
+ public void onOpen(CraftHumanEntity who) {
+ this.delegate.onOpen(who);
+ }
+
+ @Override
+ public void onClose(CraftHumanEntity who) {
+ this.delegate.onClose(who);
+ }
+
+ @Override
+ public List<HumanEntity> getViewers() {
+ return this.delegate.getViewers();
+ }
+
+ @Override
+ public InventoryHolder getOwner() {
+ return this.owner;
+ }
+
+ @Override
+ public void setMaxStackSize(int size) {
+ this.delegate.setMaxStackSize(size);
+ }
+
+ @Override
+ public Location getLocation() {
+ return this.delegate.getLocation();
+ }
+
+ @Override
+ public void clearContent() {
+ this.delegate.clearContent();
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java
index 1b4b397c08d7c05f5a066274a00a6ec80601624a..b4bd318d61834d70d666577073f18e4c49ded113 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftContainer.java
@@ -49,7 +49,7 @@ public class CraftContainer extends AbstractContainerMenu {
public CraftContainer(final Inventory inventory, final Player player, int id) {
this(new InventoryView() {
- private final String originalTitle = (inventory instanceof CraftInventoryCustom) ? ((CraftInventoryCustom.MinecraftInventory) ((CraftInventory) inventory).getInventory()).getTitle() : inventory.getType().getDefaultTitle();
+ private final String originalTitle = inventory instanceof CraftInventoryCustom ? ((CraftInventoryCustom) inventory).getTitle() : inventory.getType().getDefaultTitle(); // Paper
private String title = this.originalTitle;
@Override
@@ -75,7 +75,7 @@ public class CraftContainer extends AbstractContainerMenu {
// Paper start
@Override
public net.kyori.adventure.text.Component title() {
- return inventory instanceof CraftInventoryCustom ? ((CraftInventoryCustom.MinecraftInventory) ((CraftInventory) inventory).getInventory()).title() : net.kyori.adventure.text.Component.text(inventory.getType().getDefaultTitle());
+ return inventory instanceof CraftInventoryCustom custom ? custom.title() : inventory.getType().defaultTitle(); // Paper
}
// Paper end
@@ -247,6 +247,10 @@ public class CraftContainer extends AbstractContainerMenu {
this.lastSlots = this.delegate.lastSlots;
this.slots = this.delegate.slots;
this.remoteSlots = this.delegate.remoteSlots;
+ // Paper start - copy data slots for InventoryView#set/getProperty
+ this.dataSlots = this.delegate.dataSlots;
+ this.remoteDataSlots = this.delegate.remoteDataSlots;
+ // Paper end
}
// SPIGOT-4598 - we should still delegate the shift click handler
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
index e97c7809fa6572695377e5ca63bf86e5515ef531..c064022d248ff3e0b52c0e815ab90527f9132fb7 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
@@ -492,6 +492,10 @@ public class CraftInventory implements Inventory {
return InventoryType.BREWING;
} else if (this.inventory instanceof CraftInventoryCustom.MinecraftInventory) {
return ((CraftInventoryCustom.MinecraftInventory) this.inventory).getType();
+ // Paper start
+ } else if (this.inventory instanceof io.papermc.paper.inventory.PaperInventoryCustomHolderContainer holderContainer) {
+ return holderContainer.getType();
+ // Paper end
} else if (this.inventory instanceof PlayerEnderChestContainer) {
return InventoryType.ENDER_CHEST;
} else if (this.inventory instanceof MerchantContainer) {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCustom.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCustom.java
index fc0e1212022d1aa3506699b60ef338196eb54eba..da1c1fe0faf6819b15a81d6ad53370948e5f984f 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCustom.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCustom.java
@@ -15,6 +15,11 @@ import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryHolder;
public class CraftInventoryCustom extends CraftInventory {
+ // Paper start
+ public CraftInventoryCustom(InventoryHolder owner, InventoryType type, Container delegate) {
+ super(new io.papermc.paper.inventory.PaperInventoryCustomHolderContainer(owner, delegate, type));
+ }
+ // Paper end
public CraftInventoryCustom(InventoryHolder owner, InventoryType type) {
super(new MinecraftInventory(owner, type));
}
@@ -42,6 +47,27 @@ public class CraftInventoryCustom extends CraftInventory {
public CraftInventoryCustom(InventoryHolder owner, int size, String title) {
super(new MinecraftInventory(owner, size, title));
}
+ // Paper start
+ public String getTitle() {
+ if (this.inventory instanceof MinecraftInventory minecraftInventory) {
+ return minecraftInventory.getTitle();
+ } else if (this.inventory instanceof io.papermc.paper.inventory.PaperInventoryCustomHolderContainer customHolderContainer) {
+ return customHolderContainer.getTitle();
+ } else {
+ throw new UnsupportedOperationException(this.inventory.getClass() + " isn't a recognized Container type here");
+ }
+ }
+
+ public net.kyori.adventure.text.Component title() {
+ if (this.inventory instanceof MinecraftInventory minecraftInventory) {
+ return minecraftInventory.title();
+ } else if (this.inventory instanceof io.papermc.paper.inventory.PaperInventoryCustomHolderContainer customHolderContainer) {
+ return customHolderContainer.title();
+ } else {
+ throw new UnsupportedOperationException(this.inventory.getClass() + " isn't a recognized Container type here");
+ }
+ }
+ // Paper end
static class MinecraftInventory implements Container {
private final NonNullList<ItemStack> items;
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/util/CraftTileInventoryConverter.java b/src/main/java/org/bukkit/craftbukkit/inventory/util/CraftTileInventoryConverter.java
index 7a7f3f53aef601f124d474d9890e23d87dd96900..54e61b9b058bee2167461aaaf828ed7a00949c29 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/util/CraftTileInventoryConverter.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/util/CraftTileInventoryConverter.java
@@ -28,7 +28,7 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
@Override
public Inventory createInventory(InventoryHolder holder, InventoryType type) {
- return this.getInventory(this.getTileEntity());
+ return this.getInventory(holder, type, this.getTileEntity()); // Paper
}
// Paper start
@@ -39,7 +39,7 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
((RandomizableContainerBlockEntity) te).setCustomName(io.papermc.paper.adventure.PaperAdventure.asVanilla(title));
}
- return getInventory(te);
+ return this.getInventory(owner, type, te); // Paper
}
// Paper end
@@ -50,10 +50,18 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
((RandomizableContainerBlockEntity) te).setCustomName(CraftChatMessage.fromStringOrNull(title));
}
- return this.getInventory(te);
+ return this.getInventory(holder, type, te); // Paper
}
+ @Deprecated // Paper - use getInventory with owner and type
public Inventory getInventory(Container tileEntity) {
+ // Paper start
+ return this.getInventory(null, null, tileEntity);
+ }
+
+ public Inventory getInventory(InventoryHolder owner, InventoryType type, Container tileEntity) { // Paper
+ if (owner != null) return new org.bukkit.craftbukkit.inventory.CraftInventoryCustom(owner, type, tileEntity); // Paper
+ // Paper end
return new CraftInventory(tileEntity);
}
@@ -70,7 +78,7 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
public Inventory createInventory(InventoryHolder owner, InventoryType type, net.kyori.adventure.text.Component title) {
Container tileEntity = getTileEntity();
((AbstractFurnaceBlockEntity) tileEntity).setCustomName(io.papermc.paper.adventure.PaperAdventure.asVanilla(title));
- return getInventory(tileEntity);
+ return this.getInventory(owner, type, tileEntity); // Paper
}
// Paper end
@@ -78,11 +86,19 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
public Inventory createInventory(InventoryHolder owner, InventoryType type, String title) {
Container tileEntity = this.getTileEntity();
((AbstractFurnaceBlockEntity) tileEntity).setCustomName(CraftChatMessage.fromStringOrNull(title));
- return this.getInventory(tileEntity);
+ return this.getInventory(owner, type, tileEntity); // Paper
}
@Override
public Inventory getInventory(Container tileEntity) {
+ // Paper start
+ return getInventory(null, null, tileEntity);
+ }
+
+ @Override
+ public Inventory getInventory(InventoryHolder owner, InventoryType type, net.minecraft.world.Container tileEntity) { // Paper
+ if (owner != null) return new org.bukkit.craftbukkit.inventory.CraftInventoryCustom(owner, type, tileEntity); // Paper
+ // Paper end
return new CraftInventoryFurnace((AbstractFurnaceBlockEntity) tileEntity);
}
}
@@ -102,7 +118,7 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
if (tileEntity instanceof BrewingStandBlockEntity) {
((BrewingStandBlockEntity) tileEntity).setCustomName(io.papermc.paper.adventure.PaperAdventure.asVanilla(title));
}
- return getInventory(tileEntity);
+ return this.getInventory(owner, type, tileEntity); // Paper
}
// Paper end
@@ -113,11 +129,19 @@ public abstract class CraftTileInventoryConverter implements CraftInventoryCreat
if (tileEntity instanceof BrewingStandBlockEntity) {
((BrewingStandBlockEntity) tileEntity).setCustomName(CraftChatMessage.fromStringOrNull(title));
}
- return this.getInventory(tileEntity);
+ return this.getInventory(holder, type, tileEntity); // Paper
}
@Override
public Inventory getInventory(Container tileEntity) {
+ // Paper start
+ return getInventory(null, null, tileEntity);
+ }
+
+ @Override
+ public Inventory getInventory(InventoryHolder owner, InventoryType type, net.minecraft.world.Container tileEntity) { // Paper
+ if (owner != null) return new org.bukkit.craftbukkit.inventory.CraftInventoryCustom(owner, type, tileEntity); // Paper
+ // Paper end
return new CraftInventoryBrewer(tileEntity);
}
}
|