Skip to content

Commit 1e27cd9

Browse files
committed
PlayerInventory
1 parent 33f3a11 commit 1e27cd9

File tree

3 files changed

+32
-62
lines changed

3 files changed

+32
-62
lines changed

build-data/paper.at

+1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ public net.minecraft.world.entity.npc.WanderingTrader getWanderTarget()Lnet/mine
419419
public net.minecraft.world.entity.player.Abilities flyingSpeed
420420
public net.minecraft.world.entity.player.Abilities walkingSpeed
421421
public net.minecraft.world.entity.player.Inventory compartments
422+
public net.minecraft.world.entity.player.Inventory equipment
422423
public net.minecraft.world.entity.player.Player DATA_PLAYER_MODE_CUSTOMISATION
423424
public net.minecraft.world.entity.player.Player closeContainer()V
424425
public net.minecraft.world.entity.player.Player enchantmentSeed

paper-server/patches/sources/net/minecraft/world/entity/player/Inventory.java.patch

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
+++ b/net/minecraft/world/entity/player/Inventory.java
33
@@ -49,6 +_,66 @@
44
public final Player player;
5-
private final EntityEquipment equipment;
5+
public final EntityEquipment equipment;
66
private int timesChanged;
77
+ // CraftBukkit start - add fields and methods
88
+ public java.util.List<org.bukkit.entity.HumanEntity> transaction = new java.util.ArrayList<>();

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

+30-61
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.bukkit.craftbukkit.inventory;
22

33
import com.google.common.base.Preconditions;
4+
import java.util.List;
45
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
56
import net.minecraft.network.protocol.game.ClientboundSetHeldSlotPacket;
67
import net.minecraft.server.level.ServerPlayer;
78
import net.minecraft.world.entity.player.Inventory;
9+
import org.bukkit.craftbukkit.CraftEquipmentSlot;
810
import org.bukkit.craftbukkit.entity.CraftPlayer;
911
import org.bukkit.entity.HumanEntity;
1012
import org.bukkit.inventory.EntityEquipment;
@@ -43,14 +45,12 @@ public void setItemInMainHand(ItemStack item, boolean silent) {
4345

4446
@Override
4547
public ItemStack getItemInOffHand() {
46-
return CraftItemStack.asCraftMirror(this.getInventory().offhand.get(0));
48+
return CraftItemStack.asCraftMirror(this.getInventory().equipment.get(net.minecraft.world.entity.EquipmentSlot.OFFHAND));
4749
}
4850

4951
@Override
5052
public void setItemInOffHand(ItemStack item) {
51-
ItemStack[] extra = this.getExtraContents();
52-
extra[0] = item;
53-
this.setExtraContents(extra);
53+
this.getInventory().equipment.set(net.minecraft.world.entity.EquipmentSlot.OFFHAND, CraftItemStack.asNMSCopy(item));
5454
}
5555

5656
@Override
@@ -122,29 +122,11 @@ public void setItem(EquipmentSlot slot, ItemStack item) {
122122
Preconditions.checkArgument(slot != null, "slot must not be null");
123123

124124
switch (slot) {
125-
case HAND:
126-
this.setItemInMainHand(item);
127-
break;
128-
case OFF_HAND:
129-
this.setItemInOffHand(item);
130-
break;
131-
case FEET:
132-
this.setBoots(item);
133-
break;
134-
case LEGS:
135-
this.setLeggings(item);
136-
break;
137-
case CHEST:
138-
this.setChestplate(item);
139-
break;
140-
case HEAD:
141-
this.setHelmet(item);
142-
break;
143-
// Paper start
144-
case BODY:
145-
throw new IllegalArgumentException("BODY is not valid for players!");
146-
// Paper end
147-
default:
125+
case HAND -> this.setItemInMainHand(item);
126+
case OFF_HAND, FEET, LEGS, CHEST, HEAD ->
127+
this.getInventory().equipment.set(CraftEquipmentSlot.getNMS(slot), CraftItemStack.asNMSCopy(item));
128+
case BODY -> throw new IllegalArgumentException("BODY is not valid for players!"); // Paper end
129+
default ->
148130
throw new IllegalArgumentException("Could not set slot " + slot + " - not a valid slot for PlayerInventory");
149131
}
150132
}
@@ -158,63 +140,50 @@ public void setItem(EquipmentSlot slot, ItemStack item, boolean silent) {
158140
public ItemStack getItem(EquipmentSlot slot) {
159141
Preconditions.checkArgument(slot != null, "slot must not be null");
160142

161-
switch (slot) {
162-
case HAND:
163-
return this.getItemInMainHand();
164-
case OFF_HAND:
165-
return this.getItemInOffHand();
166-
case FEET:
167-
return java.util.Objects.requireNonNullElseGet(this.getBoots(), () -> new ItemStack(org.bukkit.Material.AIR)); // Paper - make nonnull
168-
case LEGS:
169-
return java.util.Objects.requireNonNullElseGet(this.getLeggings(), () -> new ItemStack(org.bukkit.Material.AIR)); // Paper - make nonnull
170-
case CHEST:
171-
return java.util.Objects.requireNonNullElseGet(this.getChestplate(), () -> new ItemStack(org.bukkit.Material.AIR)); // Paper - make nonnull
172-
case HEAD:
173-
return java.util.Objects.requireNonNullElseGet(this.getHelmet(), () -> new ItemStack(org.bukkit.Material.AIR)); // Paper - make nonnull
174-
// Paper start
175-
case BODY:
176-
throw new IllegalArgumentException("BODY is not valid for players!");
177-
// Paper end
178-
default:
143+
return switch (slot) {
144+
case HAND -> this.getItemInMainHand();
145+
case OFF_HAND, FEET, LEGS, CHEST, HEAD -> CraftItemStack.asCraftMirror(this.getInventory().equipment.get(CraftEquipmentSlot.getNMS(slot)));
146+
case BODY -> throw new IllegalArgumentException("BODY is not valid for players!");
147+
default ->
179148
throw new IllegalArgumentException("Could not get slot " + slot + " - not a valid slot for PlayerInventory");
180-
}
149+
};
181150
}
182151

183152
@Override
184153
public int getHeldItemSlot() {
185-
return this.getInventory().selected;
154+
return this.getInventory().getSelectedSlot();
186155
}
187156

188157
@Override
189158
public void setHeldItemSlot(int slot) {
190159
Preconditions.checkArgument(slot >= 0 && slot < Inventory.getSelectionSize(), "Slot (%s) is not between 0 and %s inclusive", slot, Inventory.getSelectionSize() - 1);
191-
this.getInventory().selected = slot;
160+
this.getInventory().setSelectedSlot(slot);
192161
((CraftPlayer) this.getHolder()).getHandle().connection.send(new ClientboundSetHeldSlotPacket(slot));
193162
}
194163

195164
@Override
196165
public ItemStack getHelmet() {
197-
return this.getItem(this.getSize() - 2);
166+
return this.getItem(EquipmentSlot.HEAD);
198167
}
199168

200169
@Override
201170
public ItemStack getChestplate() {
202-
return this.getItem(this.getSize() - 3);
171+
return this.getItem(EquipmentSlot.CHEST);
203172
}
204173

205174
@Override
206175
public ItemStack getLeggings() {
207-
return this.getItem(this.getSize() - 4);
176+
return this.getItem(EquipmentSlot.LEGS);
208177
}
209178

210179
@Override
211180
public ItemStack getBoots() {
212-
return this.getItem(this.getSize() - 5);
181+
return this.getItem(EquipmentSlot.FEET);
213182
}
214183

215184
@Override
216185
public void setHelmet(ItemStack helmet) {
217-
this.setItem(this.getSize() - 2, helmet);
186+
this.setItem(EquipmentSlot.HEAD, helmet);
218187
}
219188

220189
@Override
@@ -224,7 +193,7 @@ public void setHelmet(ItemStack helmet, boolean silent) {
224193

225194
@Override
226195
public void setChestplate(ItemStack chestplate) {
227-
this.setItem(this.getSize() - 3, chestplate);
196+
this.setItem(EquipmentSlot.CHEST, chestplate);
228197
}
229198

230199
@Override
@@ -234,7 +203,7 @@ public void setChestplate(ItemStack chestplate, boolean silent) {
234203

235204
@Override
236205
public void setLeggings(ItemStack leggings) {
237-
this.setItem(this.getSize() - 4, leggings);
206+
this.setItem(EquipmentSlot.LEGS, leggings);
238207
}
239208

240209
@Override
@@ -244,7 +213,7 @@ public void setLeggings(ItemStack leggings, boolean silent) {
244213

245214
@Override
246215
public void setBoots(ItemStack boots) {
247-
this.setItem(this.getSize() - 5, boots);
216+
this.setItem(EquipmentSlot.FEET, boots);
248217
}
249218

250219
@Override
@@ -254,7 +223,7 @@ public void setBoots(ItemStack boots, boolean silent) {
254223

255224
@Override
256225
public ItemStack[] getArmorContents() {
257-
return this.asCraftMirror(this.getInventory().armor);
226+
return this.asCraftMirror(this.getInventory().getArmorContents());
258227
}
259228

260229
private void setSlots(ItemStack[] items, int baseSlot, int length) {
@@ -274,22 +243,22 @@ private void setSlots(ItemStack[] items, int baseSlot, int length) {
274243

275244
@Override
276245
public void setStorageContents(ItemStack[] items) throws IllegalArgumentException {
277-
this.setSlots(items, 0, this.getInventory().items.size());
246+
this.setSlots(items, 0, this.getInventory().getNonEquipmentItems().size());
278247
}
279248

280249
@Override
281250
public void setArmorContents(ItemStack[] items) {
282-
this.setSlots(items, this.getInventory().items.size(), this.getInventory().armor.size());
251+
this.setSlots(items, this.getInventory().getNonEquipmentItems().size(), this.getInventory().getArmorContents().size());
283252
}
284253

285254
@Override
286255
public ItemStack[] getExtraContents() {
287-
return this.asCraftMirror(this.getInventory().offhand);
256+
return this.asCraftMirror(List.of(this.getInventory().equipment.get(net.minecraft.world.entity.EquipmentSlot.OFFHAND)));
288257
}
289258

290259
@Override
291260
public void setExtraContents(ItemStack[] items) {
292-
this.setSlots(items, this.getInventory().items.size() + this.getInventory().armor.size(), this.getInventory().offhand.size());
261+
this.setSlots(items, this.getInventory().getNonEquipmentItems().size() + this.getInventory().getArmorContents().size(), 1);
293262
}
294263

295264
@Override

0 commit comments

Comments
 (0)