Skip to content

Commit d61d8df

Browse files
authored
Cleanup crafter slot diff (#13662)
1 parent 73aded4 commit d61d8df

4 files changed

Lines changed: 70 additions & 81 deletions

File tree

paper-api/src/main/java/org/bukkit/inventory/EntityEquipment.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public interface EntityEquipment {
162162
*
163163
* <p>
164164
* This returns a copy if this equipment instance is from a non-player.
165-
* For stacks from players, this returns a live mirror (or null). You can check if this
165+
* For items from players, this returns a live mirror. You can check if this
166166
* will return a mirror with
167167
* <pre>{@code
168168
* EntityEquipment equipment = entity.getEquipment();
@@ -175,8 +175,7 @@ public interface EntityEquipment {
175175
*
176176
* @return The helmet being worn
177177
*/
178-
@org.bukkit.UndefinedNullability("not null for entities, nullable for players") // Paper
179-
ItemStack getHelmet();
178+
@NotNull ItemStack getHelmet();
180179

181180
/**
182181
* Sets the helmet worn by the entity
@@ -198,7 +197,7 @@ public interface EntityEquipment {
198197
*
199198
* <p>
200199
* This returns a copy if this equipment instance is from a non-player.
201-
* For stacks from players, this returns a live mirror (or null). You can check if this
200+
* For items from players, this returns a live mirror. You can check if this
202201
* will return a mirror with
203202
* <pre>{@code
204203
* EntityEquipment equipment = entity.getEquipment();
@@ -211,8 +210,7 @@ public interface EntityEquipment {
211210
*
212211
* @return The chest plate being worn
213212
*/
214-
@org.bukkit.UndefinedNullability("not null for entities, nullable for players") // Paper
215-
ItemStack getChestplate();
213+
@NotNull ItemStack getChestplate();
216214

217215
/**
218216
* Sets the chest plate worn by the entity
@@ -234,7 +232,7 @@ public interface EntityEquipment {
234232
*
235233
* <p>
236234
* This returns a copy if this equipment instance is from a non-player.
237-
* For stacks from players, this returns a live mirror (or null). You can check if this
235+
* For items from players, this returns a live mirror. You can check if this
238236
* will return a mirror with
239237
* <pre>{@code
240238
* EntityEquipment equipment = entity.getEquipment();
@@ -247,8 +245,7 @@ public interface EntityEquipment {
247245
*
248246
* @return The leggings being worn
249247
*/
250-
@org.bukkit.UndefinedNullability("not null for entities, nullable for players") // Paper
251-
ItemStack getLeggings();
248+
@NotNull ItemStack getLeggings();
252249

253250
/**
254251
* Sets the leggings worn by the entity
@@ -270,7 +267,7 @@ public interface EntityEquipment {
270267
*
271268
* <p>
272269
* This returns a copy if this equipment instance is from a non-player.
273-
* For stacks from players, this returns a live mirror (or null). You can check if this
270+
* For items from players, this returns a live mirror. You can check if this
274271
* will return a mirror with
275272
* <pre>{@code
276273
* EntityEquipment equipment = entity.getEquipment();
@@ -283,8 +280,7 @@ public interface EntityEquipment {
283280
*
284281
* @return The boots being worn
285282
*/
286-
@org.bukkit.UndefinedNullability("not null for entities, nullable for players") // Paper
287-
ItemStack getBoots();
283+
@NotNull ItemStack getBoots();
288284

289285
/**
290286
* Sets the boots worn by the entity

paper-api/src/main/java/org/bukkit/inventory/PlayerInventory.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,28 @@ public interface PlayerInventory extends Inventory {
3434
*
3535
* @return The ItemStack in the helmet slot
3636
*/
37-
@Nullable
38-
public ItemStack getHelmet();
37+
public @NotNull ItemStack getHelmet();
3938

4039
/**
4140
* Return the ItemStack from the chestplate slot
4241
*
4342
* @return The ItemStack in the chestplate slot
4443
*/
45-
@Nullable
46-
public ItemStack getChestplate();
44+
public @NotNull ItemStack getChestplate();
4745

4846
/**
4947
* Return the ItemStack from the leg slot
5048
*
5149
* @return The ItemStack in the leg slot
5250
*/
53-
@Nullable
54-
public ItemStack getLeggings();
51+
public @NotNull ItemStack getLeggings();
5552

5653
/**
5754
* Return the ItemStack from the boots slot
5855
*
5956
* @return The ItemStack in the boots slot
6057
*/
61-
@Nullable
62-
public ItemStack getBoots();
58+
public @NotNull ItemStack getBoots();
6359

6460
/**
6561
* Stores the ItemStack at the given index of the inventory.

paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftAbstractInventoryView.java

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,10 @@ public Inventory getInventory(final int rawSlot) {
5252
}
5353
Preconditions.checkArgument(rawSlot >= 0, "Negative, non outside slot %s", rawSlot);
5454
Preconditions.checkArgument(rawSlot < this.countSlots(), "Slot %s greater than inventory slot count", rawSlot);
55+
return this.mapValidSlotToInventory(rawSlot);
56+
}
5557

56-
// Paper start - Fix crafter slot ID conversions
57-
if (this.getType() == InventoryType.CRAFTER) {
58-
// Raw slot ID for crafter inventory views is 45 for result slot and 0-8 for crafting grid slots.
59-
// Crafter inventory size is 10. Only check 0-8 and 45.
60-
if (rawSlot < (this.getTopInventory().getSize() - 1) || rawSlot == 45) {
61-
return this.getTopInventory();
62-
} else {
63-
return this.getBottomInventory();
64-
}
65-
}
66-
// Paper end - Fix crafter slot ID conversions
67-
58+
public Inventory mapValidSlotToInventory(final int rawSlot) {
6859
if (rawSlot < this.getTopInventory().getSize()) {
6960
return this.getTopInventory();
7061
} else {
@@ -74,43 +65,6 @@ public Inventory getInventory(final int rawSlot) {
7465

7566
@Override
7667
public int convertSlot(final int rawSlot) {
77-
// Paper start - Fix crafter slot ID conversions
78-
// Crafter inventory size is 10, but the view uses non-contiguous raw slot IDs 0-8 (grid) and 45 (result).
79-
// The numInTop check and slot number shift lower in this method assume contiguous slot IDs.
80-
if (this.getType() == InventoryType.CRAFTER) {
81-
/*
82-
* Raw Slots:
83-
*
84-
* 0 1 2
85-
* 3 4 5 45
86-
* 6 7 8
87-
* 9 10 11 12 13 14 15 16 17
88-
* 18 19 20 21 22 23 24 25 26
89-
* 27 28 29 30 31 32 33 34 35
90-
* 36 37 38 39 40 41 42 43 44
91-
*/
92-
93-
/*
94-
* Converted Slots:
95-
*
96-
* 0 1 2
97-
* 3 4 5 9
98-
* 6 7 8
99-
* 9 10 11 12 13 14 15 16 17
100-
* 18 19 20 21 22 23 24 25 26
101-
* 27 28 29 30 31 32 33 34 35
102-
* 0 1 2 3 4 5 6 7 8
103-
*/
104-
if (rawSlot == 45) {
105-
return 9; // Result
106-
} else if (rawSlot >= 36) {
107-
return rawSlot - 36; // Quickbar
108-
} else {
109-
return rawSlot; // Crafting grid or player inventory
110-
}
111-
}
112-
// Paper end - Fix crafter slot ID conversions
113-
11468
int numInTop = this.getTopInventory().getSize();
11569
// Index from the top inventory as having slots from [0,size]
11670
if (rawSlot < numInTop) {
@@ -178,18 +132,7 @@ public int convertSlot(final int rawSlot) {
178132
@Override
179133
public InventoryType.SlotType getSlotType(final int slot) {
180134
InventoryType.SlotType type = InventoryType.SlotType.CONTAINER;
181-
// Paper start - Fix crafter slot ID conversions
182-
if (this.getType() == InventoryType.CRAFTER) {
183-
// Crafter inventory size is 10, but the view uses non-contiguous raw slot IDs 0-8 (grid) and 45 (result).
184-
if (slot < 0) {
185-
type = InventoryType.SlotType.OUTSIDE;
186-
} else if (slot == 45) {
187-
type = InventoryType.SlotType.RESULT;
188-
} else if (slot > 35) {
189-
type = InventoryType.SlotType.QUICKBAR;
190-
}
191-
} else if (slot >= 0 && slot < this.getTopInventory().getSize()) {
192-
// Paper end - Fix crafter slot ID conversions
135+
if (slot >= 0 && slot < this.getTopInventory().getSize()) {
193136
switch (this.getType()) {
194137
case BLAST_FURNACE:
195138
case FURNACE:
@@ -261,6 +204,12 @@ public InventoryType.SlotType getSlotType(final int slot) {
261204
} else if (slot > 35) {
262205
type = InventoryType.SlotType.QUICKBAR;
263206
}
207+
} else if (this.getType() == InventoryType.CRAFTER) {
208+
if (slot == 45) {
209+
type = InventoryType.SlotType.RESULT;
210+
} else if (slot > 35) {
211+
type = InventoryType.SlotType.QUICKBAR;
212+
}
264213
} else if (slot >= (this.countSlots() - (9 + 4 + 1))) { // Quickbar, Armor, Offhand
265214
type = InventoryType.SlotType.QUICKBAR;
266215
}

paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftCrafterView.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
66
import org.bukkit.entity.HumanEntity;
77
import org.bukkit.inventory.CrafterInventory;
8+
import org.bukkit.inventory.Inventory;
89
import org.bukkit.inventory.view.CrafterView;
910

1011
public class CraftCrafterView extends CraftInventoryView<CrafterMenu, CrafterInventory> implements CrafterView {
@@ -23,6 +24,53 @@ public boolean isPowered() {
2324
return this.container.isPowered();
2425
}
2526

27+
@Override
28+
public int convertSlot(final int rawSlot) {
29+
// Crafter inventory size is 10, but the view uses non-contiguous raw slot IDs 0-8 (grid) and 45 (result).
30+
// The numInTop check and slot number shift lower in this method assume contiguous slot IDs.
31+
/*
32+
* Raw Slots:
33+
*
34+
* 0 1 2
35+
* 3 4 5 45
36+
* 6 7 8
37+
* 9 10 11 12 13 14 15 16 17
38+
* 18 19 20 21 22 23 24 25 26
39+
* 27 28 29 30 31 32 33 34 35
40+
* 36 37 38 39 40 41 42 43 44
41+
*/
42+
43+
/*
44+
* Converted Slots:
45+
*
46+
* 0 1 2
47+
* 3 4 5 9
48+
* 6 7 8
49+
* 9 10 11 12 13 14 15 16 17
50+
* 18 19 20 21 22 23 24 25 26
51+
* 27 28 29 30 31 32 33 34 35
52+
* 0 1 2 3 4 5 6 7 8
53+
*/
54+
if (rawSlot == 45) {
55+
return 9; // Result
56+
} else if (rawSlot >= 36) {
57+
return rawSlot - 36; // Quickbar
58+
} else {
59+
return rawSlot; // Crafting grid or player inventory
60+
}
61+
}
62+
63+
@Override
64+
public Inventory mapValidSlotToInventory(final int rawSlot) {
65+
// Raw slot ID for crafter inventory views is 45 for result slot and 0-8 for crafting grid slots.
66+
// Crafter inventory size is 10. Only check 0-8 and 45.
67+
if (rawSlot < (this.getTopInventory().getSize() - 1) || rawSlot == 45) {
68+
return this.getTopInventory();
69+
} else {
70+
return this.getBottomInventory();
71+
}
72+
}
73+
2674
@Override
2775
public void setSlotDisabled(final int slot, final boolean disabled) {
2876
Preconditions.checkArgument(slot >= 0 && slot < 9, "Invalid slot index %s for Crafter", slot);

0 commit comments

Comments
 (0)