Fixed inventory id generation

This commit is contained in:
Felix Cravic 2020-04-28 18:55:09 +02:00
parent f5212e3bf6
commit e022881554
5 changed files with 25 additions and 19 deletions

View File

@ -799,7 +799,7 @@ public class Player extends LivingEntity {
if (openInventory == null) { if (openInventory == null) {
closeWindowPacket.windowId = 0; closeWindowPacket.windowId = 0;
} else { } else {
closeWindowPacket.windowId = openInventory.getWindowId(); closeWindowPacket.windowId = (byte) openInventory.getInventoryType().getWindowType();
openInventory.removeViewer(this); openInventory.removeViewer(this);
refreshOpenInventory(null); refreshOpenInventory(null);
} }

View File

@ -15,16 +15,17 @@ import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable { public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable {
private static AtomicInteger lastInventoryId = new AtomicInteger(); private static volatile byte lastInventoryId;
private int id; private int id;
private InventoryType inventoryType; private InventoryType inventoryType;
private String title; private String title;
private int size;
private int offset; private int offset;
private ItemStack[] itemStacks; private ItemStack[] itemStacks;
@ -39,17 +40,22 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
this.inventoryType = inventoryType; this.inventoryType = inventoryType;
this.title = title; this.title = title;
this.offset = inventoryType.getAdditionalSlot(); this.size = inventoryType.getAdditionalSlot();
this.itemStacks = new ItemStack[inventoryType.getAdditionalSlot()]; this.offset = size;
for (int i = 0; i < itemStacks.length; i++) { this.itemStacks = new ItemStack[size];
for (int i = 0; i < size; i++) {
itemStacks[i] = ItemStack.getAirItem(); itemStacks[i] = ItemStack.getAirItem();
} }
} }
private static int generateId() { private static byte generateId() {
return lastInventoryId.incrementAndGet(); byte newInventoryId = ++lastInventoryId;
if (newInventoryId < 0)
newInventoryId = 1;
return newInventoryId;
} }
public InventoryType getInventoryType() { public InventoryType getInventoryType() {

View File

@ -7,8 +7,8 @@ import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.client.play.ClientClickWindowPacket; import net.minestom.server.network.packet.client.play.ClientClickWindowPacket;
import net.minestom.server.network.packet.client.play.ClientCloseWindow; import net.minestom.server.network.packet.client.play.ClientCloseWindow;
import net.minestom.server.network.packet.server.play.ConfirmTransactionPacket;
import net.minestom.server.network.packet.server.play.SetSlotPacket; import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowConfirmationPacket;
public class WindowListener { public class WindowListener {
@ -26,10 +26,10 @@ public class WindowListener {
// System.out.println("Window id: " + windowId + " | slot: " + slot + " | button: " + button + " | mode: " + mode); // System.out.println("Window id: " + windowId + " | slot: " + slot + " | button: " + button + " | mode: " + mode);
ConfirmTransactionPacket confirmTransactionPacket = new ConfirmTransactionPacket(); WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket();
confirmTransactionPacket.windowId = windowId; windowConfirmationPacket.windowId = windowId;
confirmTransactionPacket.actionNumber = actionNumber; windowConfirmationPacket.actionNumber = actionNumber;
confirmTransactionPacket.accepted = true; // Change depending on output windowConfirmationPacket.accepted = true; // Change depending on output
switch (mode) { switch (mode) {
case 0: case 0:
@ -83,7 +83,7 @@ public class WindowListener {
setSlotPacket.itemStack = cursorItem; setSlotPacket.itemStack = cursorItem;
player.getPlayerConnection().sendPacket(setSlotPacket); player.getPlayerConnection().sendPacket(setSlotPacket);
player.getPlayerConnection().sendPacket(confirmTransactionPacket); player.getPlayerConnection().sendPacket(windowConfirmationPacket);
} }
public static void closeWindowListener(ClientCloseWindow packet, Player player) { public static void closeWindowListener(ClientCloseWindow packet, Player player) {

View File

@ -6,11 +6,11 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier;
public class CloseWindowPacket implements ServerPacket { public class CloseWindowPacket implements ServerPacket {
public int windowId; public byte windowId;
@Override @Override
public void write(PacketWriter writer) { public void write(PacketWriter writer) {
writer.writeVarInt(windowId); writer.writeByte(windowId);
} }
@Override @Override

View File

@ -4,15 +4,15 @@ import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
public class ConfirmTransactionPacket implements ServerPacket { public class WindowConfirmationPacket implements ServerPacket {
public int windowId; public byte windowId;
public short actionNumber; public short actionNumber;
public boolean accepted; public boolean accepted;
@Override @Override
public void write(PacketWriter writer) { public void write(PacketWriter writer) {
writer.writeVarInt(windowId); writer.writeByte(windowId);
writer.writeShort(actionNumber); writer.writeShort(actionNumber);
writer.writeBoolean(accepted); writer.writeBoolean(accepted);
} }