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) {
closeWindowPacket.windowId = 0;
} else {
closeWindowPacket.windowId = openInventory.getWindowId();
closeWindowPacket.windowId = (byte) openInventory.getInventoryType().getWindowType();
openInventory.removeViewer(this);
refreshOpenInventory(null);
}

View File

@ -15,16 +15,17 @@ import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable {
private static AtomicInteger lastInventoryId = new AtomicInteger();
private static volatile byte lastInventoryId;
private int id;
private InventoryType inventoryType;
private String title;
private int size;
private int offset;
private ItemStack[] itemStacks;
@ -39,17 +40,22 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
this.inventoryType = inventoryType;
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();
}
}
private static int generateId() {
return lastInventoryId.incrementAndGet();
private static byte generateId() {
byte newInventoryId = ++lastInventoryId;
if (newInventoryId < 0)
newInventoryId = 1;
return newInventoryId;
}
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.network.packet.client.play.ClientClickWindowPacket;
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.WindowConfirmationPacket;
public class WindowListener {
@ -26,10 +26,10 @@ public class WindowListener {
// System.out.println("Window id: " + windowId + " | slot: " + slot + " | button: " + button + " | mode: " + mode);
ConfirmTransactionPacket confirmTransactionPacket = new ConfirmTransactionPacket();
confirmTransactionPacket.windowId = windowId;
confirmTransactionPacket.actionNumber = actionNumber;
confirmTransactionPacket.accepted = true; // Change depending on output
WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket();
windowConfirmationPacket.windowId = windowId;
windowConfirmationPacket.actionNumber = actionNumber;
windowConfirmationPacket.accepted = true; // Change depending on output
switch (mode) {
case 0:
@ -83,7 +83,7 @@ public class WindowListener {
setSlotPacket.itemStack = cursorItem;
player.getPlayerConnection().sendPacket(setSlotPacket);
player.getPlayerConnection().sendPacket(confirmTransactionPacket);
player.getPlayerConnection().sendPacket(windowConfirmationPacket);
}
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 int windowId;
public byte windowId;
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(windowId);
writer.writeByte(windowId);
}
@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.ServerPacketIdentifier;
public class ConfirmTransactionPacket implements ServerPacket {
public class WindowConfirmationPacket implements ServerPacket {
public int windowId;
public byte windowId;
public short actionNumber;
public boolean accepted;
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(windowId);
writer.writeByte(windowId);
writer.writeShort(actionNumber);
writer.writeBoolean(accepted);
}