Inventory code cleanup

This commit is contained in:
themode 2021-04-03 03:43:13 +02:00
parent 785e002a50
commit 0478b696f4
2 changed files with 15 additions and 51 deletions

View File

@ -161,9 +161,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
@Override
public void clear() {
// Clear the item array
for (int i = 0; i < getSize(); i++) {
setItemStackInternal(i, ItemStack.AIR);
}
Arrays.fill(itemStacks, ItemStack.AIR);
// Send the cleared inventory to viewers
update();
}
@ -295,7 +293,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
* @param itemStack the item to insert
*/
private synchronized void safeItemInsert(int slot, @NotNull ItemStack itemStack) {
setItemStackInternal(slot, itemStack);
this.itemStacks[slot] = itemStack;
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = getWindowId();
setSlotPacket.slot = (short) slot;
@ -303,19 +301,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
sendPacketToViewers(setSlotPacket);
}
/**
* Inserts an item into the inventory without notifying viewers.
* <p>
* This will also warn the inventory that the cached window items packet is
* not up-to-date.
*
* @param slot the internal slot
* @param itemStack the item to insert
*/
protected void setItemStackInternal(int slot, @NotNull ItemStack itemStack) {
itemStacks[slot] = itemStack;
}
/**
* Creates a complete new {@link WindowItemsPacket}.
*
@ -604,22 +589,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
return !clickResult.isCancel();
}
/**
* Refresh a slot for all viewers
* <p>
* WARNING: this does not update the items in the inventory, this is only visual
*
* @param slot the packet slot
* @param itemStack the item stack to set at the slot
*/
private void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = getWindowId();
setSlotPacket.slot = slot;
setSlotPacket.itemStack = itemStack;
sendPacketToViewers(setSlotPacket);
}
/**
* Used to update the inventory for a specific player in order to fix his cancelled actions
*

View File

@ -34,7 +34,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
public static final int INVENTORY_SIZE = 46;
protected final Player player;
protected final ItemStack[] items = new ItemStack[INVENTORY_SIZE];
protected final ItemStack[] itemStacks = new ItemStack[INVENTORY_SIZE];
private ItemStack cursorItem = ItemStack.AIR;
private final List<InventoryCondition> inventoryConditions = new CopyOnWriteArrayList<>();
@ -44,17 +44,17 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
public PlayerInventory(@NotNull Player player) {
this.player = player;
Arrays.fill(items, ItemStack.AIR);
Arrays.fill(itemStacks, ItemStack.AIR);
}
@Override
public @NotNull ItemStack getItemStack(int slot) {
return this.items[slot];
return this.itemStacks[slot];
}
@Override
public @NotNull ItemStack[] getItemStacks() {
return items.clone();
return itemStacks.clone();
}
@Override
@ -109,9 +109,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
@Override
public void clear() {
// Clear the item array
for (int i = 0; i < getSize(); i++) {
setItemStackInternal(i, ItemStack.AIR);
}
Arrays.fill(itemStacks, ItemStack.AIR);
// Send the cleared inventory to the inventory's owner
update();
@ -265,7 +264,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
}
}
this.items[slot] = itemStack;
this.itemStacks[slot] = itemStack;
// Sync equipment
if (equipmentSlot != null) {
@ -278,10 +277,6 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
//refreshSlot((short) slot);
}
protected void setItemStackInternal(int slot, @NotNull ItemStack itemStack) {
items[slot] = itemStack;
}
/**
* Sets an item from a packet slot.
*
@ -303,7 +298,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
*/
protected ItemStack getItemStack(int slot, int offset) {
final int convertedSlot = convertPlayerInventorySlot(slot, offset);
return this.items[convertedSlot];
return this.itemStacks[convertedSlot];
}
/**
@ -329,9 +324,9 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
private WindowItemsPacket createWindowItemsPacket() {
ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE];
for (int i = 0; i < items.length; i++) {
for (int i = 0; i < itemStacks.length; i++) {
final int slot = convertToPacketSlot(i);
convertedSlots[slot] = items[i];
convertedSlots[slot] = itemStacks[i];
}
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
@ -411,7 +406,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
final boolean hotBarClick = convertToPacketSlot(slot) < 9;
final InventoryClickResult clickResult = clickProcessor.shiftClick(null, player, slot, clicked, cursor,
new InventoryClickLoopHandler(0, items.length, 1,
new InventoryClickLoopHandler(0, itemStacks.length, 1,
i -> {
if (hotBarClick) {
return i < 9 ? i + 9 : i - 9;
@ -486,9 +481,9 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
final ItemStack cursor = getCursorItem();
final InventoryClickResult clickResult = clickProcessor.doubleClick(null, player, slot, cursor,
new InventoryClickLoopHandler(0, items.length, 1,
new InventoryClickLoopHandler(0, itemStacks.length, 1,
i -> i < 9 ? i + 9 : i - 9,
index -> items[index],
index -> itemStacks[index],
this::setItemStack));
if (clickResult == null)