mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-18 00:25:30 +01:00
Inventory code cleanup
This commit is contained in:
parent
785e002a50
commit
0478b696f4
@ -161,9 +161,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
// Clear the item array
|
// Clear the item array
|
||||||
for (int i = 0; i < getSize(); i++) {
|
Arrays.fill(itemStacks, ItemStack.AIR);
|
||||||
setItemStackInternal(i, ItemStack.AIR);
|
|
||||||
}
|
|
||||||
// Send the cleared inventory to viewers
|
// Send the cleared inventory to viewers
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
@ -295,7 +293,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
* @param itemStack the item to insert
|
* @param itemStack the item to insert
|
||||||
*/
|
*/
|
||||||
private synchronized void safeItemInsert(int slot, @NotNull ItemStack itemStack) {
|
private synchronized void safeItemInsert(int slot, @NotNull ItemStack itemStack) {
|
||||||
setItemStackInternal(slot, itemStack);
|
this.itemStacks[slot] = itemStack;
|
||||||
SetSlotPacket setSlotPacket = new SetSlotPacket();
|
SetSlotPacket setSlotPacket = new SetSlotPacket();
|
||||||
setSlotPacket.windowId = getWindowId();
|
setSlotPacket.windowId = getWindowId();
|
||||||
setSlotPacket.slot = (short) slot;
|
setSlotPacket.slot = (short) slot;
|
||||||
@ -303,19 +301,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
sendPacketToViewers(setSlotPacket);
|
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}.
|
* Creates a complete new {@link WindowItemsPacket}.
|
||||||
*
|
*
|
||||||
@ -604,22 +589,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
return !clickResult.isCancel();
|
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
|
* Used to update the inventory for a specific player in order to fix his cancelled actions
|
||||||
*
|
*
|
||||||
|
@ -34,7 +34,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
public static final int INVENTORY_SIZE = 46;
|
public static final int INVENTORY_SIZE = 46;
|
||||||
|
|
||||||
protected final Player player;
|
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 ItemStack cursorItem = ItemStack.AIR;
|
||||||
|
|
||||||
private final List<InventoryCondition> inventoryConditions = new CopyOnWriteArrayList<>();
|
private final List<InventoryCondition> inventoryConditions = new CopyOnWriteArrayList<>();
|
||||||
@ -44,17 +44,17 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
|
|
||||||
public PlayerInventory(@NotNull Player player) {
|
public PlayerInventory(@NotNull Player player) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
Arrays.fill(items, ItemStack.AIR);
|
Arrays.fill(itemStacks, ItemStack.AIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ItemStack getItemStack(int slot) {
|
public @NotNull ItemStack getItemStack(int slot) {
|
||||||
return this.items[slot];
|
return this.itemStacks[slot];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ItemStack[] getItemStacks() {
|
public @NotNull ItemStack[] getItemStacks() {
|
||||||
return items.clone();
|
return itemStacks.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -109,9 +109,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
// Clear the item array
|
// Clear the item array
|
||||||
for (int i = 0; i < getSize(); i++) {
|
Arrays.fill(itemStacks, ItemStack.AIR);
|
||||||
setItemStackInternal(i, ItemStack.AIR);
|
|
||||||
}
|
|
||||||
// Send the cleared inventory to the inventory's owner
|
// Send the cleared inventory to the inventory's owner
|
||||||
update();
|
update();
|
||||||
|
|
||||||
@ -265,7 +264,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.items[slot] = itemStack;
|
this.itemStacks[slot] = itemStack;
|
||||||
|
|
||||||
// Sync equipment
|
// Sync equipment
|
||||||
if (equipmentSlot != null) {
|
if (equipmentSlot != null) {
|
||||||
@ -278,10 +277,6 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
//refreshSlot((short) slot);
|
//refreshSlot((short) slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setItemStackInternal(int slot, @NotNull ItemStack itemStack) {
|
|
||||||
items[slot] = itemStack;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets an item from a packet slot.
|
* Sets an item from a packet slot.
|
||||||
*
|
*
|
||||||
@ -303,7 +298,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
*/
|
*/
|
||||||
protected ItemStack getItemStack(int slot, int offset) {
|
protected ItemStack getItemStack(int slot, int offset) {
|
||||||
final int convertedSlot = convertPlayerInventorySlot(slot, 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() {
|
private WindowItemsPacket createWindowItemsPacket() {
|
||||||
ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE];
|
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);
|
final int slot = convertToPacketSlot(i);
|
||||||
convertedSlots[slot] = items[i];
|
convertedSlots[slot] = itemStacks[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
|
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
|
||||||
@ -411,7 +406,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
|
|
||||||
final boolean hotBarClick = convertToPacketSlot(slot) < 9;
|
final boolean hotBarClick = convertToPacketSlot(slot) < 9;
|
||||||
final InventoryClickResult clickResult = clickProcessor.shiftClick(null, player, slot, clicked, cursor,
|
final InventoryClickResult clickResult = clickProcessor.shiftClick(null, player, slot, clicked, cursor,
|
||||||
new InventoryClickLoopHandler(0, items.length, 1,
|
new InventoryClickLoopHandler(0, itemStacks.length, 1,
|
||||||
i -> {
|
i -> {
|
||||||
if (hotBarClick) {
|
if (hotBarClick) {
|
||||||
return i < 9 ? i + 9 : i - 9;
|
return i < 9 ? i + 9 : i - 9;
|
||||||
@ -486,9 +481,9 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
final ItemStack cursor = getCursorItem();
|
final ItemStack cursor = getCursorItem();
|
||||||
|
|
||||||
final InventoryClickResult clickResult = clickProcessor.doubleClick(null, player, slot, cursor,
|
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,
|
i -> i < 9 ? i + 9 : i - 9,
|
||||||
index -> items[index],
|
index -> itemStacks[index],
|
||||||
this::setItemStack));
|
this::setItemStack));
|
||||||
|
|
||||||
if (clickResult == null)
|
if (clickResult == null)
|
||||||
|
Loading…
Reference in New Issue
Block a user