Skip to content

Commit

Permalink
Better net ID handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Dec 30, 2020
1 parent f4b1d47 commit 3c1a40c
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
// Shield code
if (session.getPlayerEntity().getEntityId() == entityId && metadata.getFlags().getFlag(EntityFlag.SNEAKING)) {
PlayerInventory playerInv = session.getPlayerInventory();
if ((playerInv.getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) ||
(playerInv.getOffhand().getId() == ItemRegistry.SHIELD.getJavaId())) {
if ((playerInv.getItemInHand().getJavaId() == ItemRegistry.SHIELD.getJavaId()) ||
(playerInv.getOffhand().getJavaId() == ItemRegistry.SHIELD.getJavaId())) {
ClientPlayerUseItemPacket useItemPacket;
metadata.getFlags().setFlag(EntityFlag.BLOCKING, true);
if (playerInv.getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) {
if (playerInv.getItemInHand().getJavaId() == ItemRegistry.SHIELD.getJavaId()) {
useItemPacket = new ClientPlayerUseItemPacket(Hand.MAIN_HAND);
}
// Else we just assume it's the offhand, to simplify logic and to assure the packet gets sent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import lombok.Getter;
import lombok.NonNull;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;

/**
Expand All @@ -53,11 +54,11 @@ public GeyserItemStack getItem(int slot) {
}

@Override
public void setItem(int slot, @NonNull GeyserItemStack item) {
public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
if (slot < this.size) {
super.setItem(slot, item);
super.setItem(slot, newItem, session);
} else {
playerInventory.setItem(slot - this.size + InventoryTranslator.PLAYER_INVENTORY_OFFSET, item);
playerInventory.setItem(slot - this.size + InventoryTranslator.PLAYER_INVENTORY_OFFSET, newItem, session);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,34 @@
public class GeyserItemStack {
public static final GeyserItemStack EMPTY = new GeyserItemStack(0, 0, null);

private final int id;
private final int javaId;
private int amount;
private CompoundTag nbt;
private int netId;
private boolean netIdWasUpdated;

public GeyserItemStack(int id) {
this(id, 1);
public GeyserItemStack(int javaId) {
this(javaId, 1);
}

public GeyserItemStack(int id, int amount) {
this(id, amount, null);
public GeyserItemStack(int javaId, int amount) {
this(javaId, amount, null);
}

public GeyserItemStack(int id, int amount, CompoundTag nbt) {
this(id, amount, nbt, 1);
public GeyserItemStack(int javaId, int amount, CompoundTag nbt) {
this(javaId, amount, nbt, 1);
}

public GeyserItemStack(int id, int amount, CompoundTag nbt, int netId) {
this.id = id;
public GeyserItemStack(int javaId, int amount, CompoundTag nbt, int netId) {
this.javaId = javaId;
this.amount = amount;
this.nbt = nbt;
this.netId = netId;
this.netIdWasUpdated = !this.isEmpty();
}

public int getId() {
return isEmpty() ? 0 : id;
public int getJavaId() {
return isEmpty() ? 0 : javaId;
}

public int getAmount() {
Expand All @@ -75,6 +77,11 @@ public CompoundTag getNbt() {
return isEmpty() ? null : nbt;
}

public void setNetId(int netId) {
this.netId = netId;
this.netIdWasUpdated = true;
}

public int getNetId() {
return isEmpty() ? 0 : netId;
}
Expand All @@ -96,7 +103,7 @@ public static GeyserItemStack from(ItemStack itemStack, int netId) {
}

public ItemStack getItemStack() {
return isEmpty() ? null : new ItemStack(id, amount, nbt);
return isEmpty() ? null : new ItemStack(javaId, amount, nbt);
}

public ItemData getItemData(GeyserSession session) {
Expand All @@ -106,18 +113,18 @@ public ItemData getItemData(GeyserSession session) {
}

public ItemEntry getItemEntry() {
return ItemRegistry.ITEM_ENTRIES.get(getId());
return ItemRegistry.ITEM_ENTRIES.get(getJavaId());
}

public boolean isEmpty() {
return amount <= 0 || id == 0;
return amount <= 0 || javaId == 0;
}

public GeyserItemStack copy() {
return copy(amount);
}

public GeyserItemStack copy(int newAmount) {
return isEmpty() ? EMPTY : new GeyserItemStack(id, newAmount, nbt == null ? null : nbt.clone(), netId);
return isEmpty() ? EMPTY : new GeyserItemStack(javaId, newAmount, nbt == null ? null : nbt.clone(), netId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.geysermc.connector.network.session.GeyserSession;

import java.util.Arrays;

Expand Down Expand Up @@ -76,8 +77,16 @@ public GeyserItemStack getItem(int slot) {
return items[slot];
}

public void setItem(int slot, @NonNull GeyserItemStack item) {
items[slot] = item;
public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
GeyserItemStack oldItem = items[slot];
if (!newItem.isEmpty()) {
if (newItem.getItemData(session).equals(oldItem.getItemData(session), false, false, false)) {
newItem.setNetId(oldItem.getNetId());
} else {
newItem.setNetId(session.getNextItemNetId());
}
}
items[slot] = newItem;
}

public short getNextTransactionId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ else if (packet.getItemInHand() != null && ItemRegistry.BUCKETS.contains(packet.
break;
case 1:
// Handled in Entity.java
if (session.getPlayerInventory().getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) {
if (session.getPlayerInventory().getItemInHand().getJavaId() == ItemRegistry.SHIELD.getJavaId()) {
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void translate(EntityEventPacket packet, GeyserSession session) {
VillagerTrade[] trades = merchantInventory.getVillagerTrades();
if (trades != null && packet.getData() >= 0 && packet.getData() < trades.length) {
VillagerTrade trade = merchantInventory.getVillagerTrades()[packet.getData()];
openInventory.setItem(2, GeyserItemStack.from(trade.getOutput()));
openInventory.setItem(2, GeyserItemStack.from(trade.getOutput()), session);
villager.getMetadata().put(EntityData.TRADE_XP, trade.getXp() + villager.getMetadata().getInt(EntityData.TRADE_XP));
villager.updateBedrockMetadata(session);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void translate(InteractPacket packet, GeyserSession session) {

switch (packet.getAction()) {
case INTERACT:
if (session.getPlayerInventory().getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) {
if (session.getPlayerInventory().getItemInHand().getJavaId() == ItemRegistry.SHIELD.getJavaId()) {
break;
}
ClientPlayerInteractEntityPacket interactPacket = new ClientPlayerInteractEntityPacket((int) entity.getEntityId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
GeyserItemStack newItem = sourceItem.copy();
if (sourceIsCursor) {
GeyserItemStack destItem = inventory.getItem(destSlot);
if (destItem.getId() == sourceItem.getId()) {
if (destItem.getJavaId() == sourceItem.getJavaId()) {
// Combining items
int itemsLeftOver = destItem.getAmount() + transferAction.getCount();
if (itemsLeftOver > MAX_ITEM_STACK_SIZE) {
Expand All @@ -235,7 +235,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
}
} else {
// Delete the source since we're moving it
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY);
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session);
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
sourceSlot,
new ItemStack(0)
Expand All @@ -252,13 +252,13 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
if (sourceIsCursor) {
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
} else {
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY);
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session);
}
}
if (destIsCursor) {
session.getPlayerInventory().setCursor(newItem);
} else {
inventory.setItem(destSlot, newItem);
inventory.setItem(destSlot, newItem, session);
}
GeyserItemStack itemToUpdate = destIsCursor ? sourceItem : newItem;
// The Java server doesn't care about what's in the mouse in creative mode, so we just need to track
Expand Down Expand Up @@ -386,7 +386,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
);
System.out.println(creativeActionPacket);
session.sendDownstreamPacket(creativeActionPacket);
inventory.setItem(sourceSlot, oldDestinationItem);
inventory.setItem(sourceSlot, oldDestinationItem, session);
}
if (isCursor(swapAction.getDestination())) {
session.getPlayerInventory().setCursor(oldSourceItem);
Expand All @@ -397,7 +397,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
);
System.out.println(creativeActionPacket);
session.sendDownstreamPacket(creativeActionPacket);
inventory.setItem(destSlot, oldSourceItem);
inventory.setItem(destSlot, oldSourceItem, session);
}

} else if (isCursor(swapAction.getSource()) && isCursor(swapAction.getDestination())) { //???
Expand Down Expand Up @@ -499,7 +499,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
);
session.sendDownstreamPacket(destroyItemPacket);
System.out.println(destroyItemPacket);
inventory.setItem(javaSlot, GeyserItemStack.EMPTY);
inventory.setItem(javaSlot, GeyserItemStack.EMPTY, session);
affectedSlots.add(javaSlot);
} else {
// Just sync up the item on our end, since the server doesn't care what's in our cursor
Expand All @@ -522,7 +522,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
GeyserItemStack item = inventory.getItem(sourceSlot);
item.setAmount(item.getAmount() - consumeData.getCount());
if (item.isEmpty()) {
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY);
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session);
}
affectedSlots.add(sourceSlot);
}
Expand Down Expand Up @@ -718,16 +718,16 @@ public ItemStackResponsePacket.Response translateCreativeRequest(GeyserSession s
session.getPlayerInventory().setCursor(GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId()));
return acceptRequest(request, Collections.singletonList(
new ItemStackResponsePacket.ContainerEntry(ContainerSlotType.CURSOR,
Collections.singletonList(makeItemEntry(session, 0, session.getPlayerInventory().getCursor())))));
Collections.singletonList(makeItemEntry(0, session.getPlayerInventory().getCursor())))));
} else {
int javaSlot = bedrockSlotToJava(transferAction.getDestination());
GeyserItemStack existingItem = inventory.getItem(javaSlot);
if (existingItem.getId() == javaCreativeItem.getId()) {
if (existingItem.getJavaId() == javaCreativeItem.getId()) {
// Adding more to an existing item
existingItem.setAmount(existingItem.getAmount() + transferAction.getCount());
javaCreativeItem = existingItem.getItemStack();
} else {
inventory.setItem(javaSlot, GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId()));
inventory.setItem(javaSlot, GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId()), session);
}
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
javaSlot,
Expand Down Expand Up @@ -765,8 +765,8 @@ public static ItemStackResponsePacket.Response rejectRequest(ItemStackRequestPac
public boolean checkNetId(GeyserSession session, Inventory inventory, StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getStackNetworkId() < 0)
return true;
if (slotInfoData.getContainer() == ContainerSlotType.CURSOR) //TODO: temporary
return true;
// if (slotInfoData.getContainer() == ContainerSlotType.CURSOR) //TODO: temporary
// return true;

GeyserItemStack currentItem = isCursor(slotInfoData) ? session.getPlayerInventory().getCursor() : inventory.getItem(bedrockSlotToJava(slotInfoData));
return currentItem.getNetId() == slotInfoData.getStackNetworkId();
Expand Down Expand Up @@ -826,26 +826,24 @@ public List<ItemStackResponsePacket.ContainerEntry> makeContainerEntries(GeyserS
for (int slot : affectedSlots) {
BedrockContainerSlot bedrockSlot = javaSlotToBedrockContainer(slot);
List<ItemStackResponsePacket.ItemEntry> list = containerMap.computeIfAbsent(bedrockSlot.getContainer(), k -> new ArrayList<>());
list.add(makeItemEntry(session, bedrockSlot.getSlot(), inventory.getItem(slot)));
list.add(makeItemEntry(bedrockSlot.getSlot(), inventory.getItem(slot)));
}

List<ItemStackResponsePacket.ContainerEntry> containerEntries = new ArrayList<>();
for (Map.Entry<ContainerSlotType, List<ItemStackResponsePacket.ItemEntry>> entry : containerMap.entrySet()) {
containerEntries.add(new ItemStackResponsePacket.ContainerEntry(entry.getKey(), entry.getValue()));
}

ItemStackResponsePacket.ItemEntry cursorEntry = makeItemEntry(session, 0, session.getPlayerInventory().getCursor());
ItemStackResponsePacket.ItemEntry cursorEntry = makeItemEntry(0, session.getPlayerInventory().getCursor());
containerEntries.add(new ItemStackResponsePacket.ContainerEntry(ContainerSlotType.CURSOR, Collections.singletonList(cursorEntry)));

return containerEntries;
}

public static ItemStackResponsePacket.ItemEntry makeItemEntry(GeyserSession session, int bedrockSlot, GeyserItemStack itemStack) {
public static ItemStackResponsePacket.ItemEntry makeItemEntry(int bedrockSlot, GeyserItemStack itemStack) {
ItemStackResponsePacket.ItemEntry itemEntry;
if (!itemStack.isEmpty()) {
int newNetId = session.getNextItemNetId();
itemStack.setNetId(newNetId);
itemEntry = new ItemStackResponsePacket.ItemEntry((byte) bedrockSlot, (byte) bedrockSlot, (byte) itemStack.getAmount(), newNetId, "");
itemEntry = new ItemStackResponsePacket.ItemEntry((byte) bedrockSlot, (byte) bedrockSlot, (byte) itemStack.getAmount(), itemStack.getNetId(), "");
} else {
itemEntry = new ItemStackResponsePacket.ItemEntry((byte) bedrockSlot, (byte) bedrockSlot, (byte) 0, 0, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void setItem(int slot, GeyserItemStack item) {
if (simulating) {
simulatedItems.put(slot, item);
} else {
inventory.setItem(slot, item);
inventory.setItem(slot, item, session);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession se
inputCopy.getNbt().put(blockEntityTag);
}
// Set the new item as the output
inventory.setItem(3, inputCopy);
inventory.setItem(3, inputCopy, session);

return translateRequest(session, inventory, request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession se
}
CraftResultsDeprecatedStackRequestActionData craftData = (CraftResultsDeprecatedStackRequestActionData) data;
// Get the ID of the item we are cutting
int id = inventory.getItem(0).getId();
int id = inventory.getItem(0).getJavaId();
// Look up all possible options of cutting from this ID
IntList results = session.getStonecutterRecipes().get(id);
if (results == null) {
Expand All @@ -77,9 +77,9 @@ public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession se
ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), results.indexOf(javaOutput.getId()));
System.out.println(packet.toString());
session.sendDownstreamPacket(packet);
if (inventory.getItem(1).getId() != javaOutput.getId()) {
if (inventory.getItem(1).getJavaId() != javaOutput.getId()) {
// We don't know there is an output here, so we tell ourselves that there is
inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getNextItemNetId()));
inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getNextItemNetId()), session);
}
return translateRequest(session, inventory, request);
}
Expand Down
Loading

0 comments on commit 3c1a40c

Please sign in to comment.