Skip to content

Commit

Permalink
Merge branch 'server-inventory' of https://github.com/GeyserMC/Geyser
Browse files Browse the repository at this point in the history
…into server-inventory
  • Loading branch information
Camotoy committed Jan 1, 2021
2 parents 2a5c134 + b7b3278 commit 7a82852
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class GeyserItemStack {
private int amount;
private CompoundTag nbt;
private int netId;
private boolean netIdWasUpdated;

public GeyserItemStack(int javaId) {
this(javaId, 1);
Expand All @@ -61,7 +60,6 @@ public GeyserItemStack(int javaId, int amount, CompoundTag nbt, int netId) {
this.amount = amount;
this.nbt = nbt;
this.netId = netId;
this.netIdWasUpdated = !this.isEmpty();
}

public int getJavaId() {
Expand All @@ -78,7 +76,6 @@ public CompoundTag getNbt() {

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

public int getNetId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ public GeyserItemStack getItem(int slot) {

public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
GeyserItemStack oldItem = items[slot];
updateItemNetId(oldItem, newItem, session);
items[slot] = newItem;
}

protected static void updateItemNetId(GeyserItemStack oldItem, GeyserItemStack newItem, GeyserSession session) {
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 @@ -28,6 +28,7 @@
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.geysermc.connector.network.session.GeyserSession;

public class PlayerInventory extends Inventory {

Expand All @@ -40,7 +41,6 @@ public class PlayerInventory extends Inventory {
private int heldItemSlot;

@Getter
@Setter
@NonNull
private GeyserItemStack cursor = GeyserItemStack.EMPTY;

Expand All @@ -49,6 +49,11 @@ public PlayerInventory() {
heldItemSlot = 0;
}

public void setCursor(@NonNull GeyserItemStack newCursor, GeyserSession session) {
updateItemNetId(cursor, newCursor, session);
cursor = newCursor;
}

public GeyserItemStack getItemInHand() {
return items[36 + heldItemSlot];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
} else {
// Cursor will be emptied
destItem.setAmount(itemsLeftOver);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
}
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
destSlot,
Expand All @@ -250,13 +250,13 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
if (sourceItem.isEmpty()) {
// Item is basically deleted
if (sourceIsCursor) {
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
} else {
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session);
}
}
if (destIsCursor) {
session.getPlayerInventory().setCursor(newItem);
session.getPlayerInventory().setCursor(newItem, session);
} else {
inventory.setItem(destSlot, newItem, session);
}
Expand Down Expand Up @@ -340,7 +340,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
//try to transfer items with least clicks possible
int halfSource = sourceAmount - (sourceAmount / 2); //larger half
int holding;
if (transferAction.getCount() <= halfSource) { //faster to take only half
if (plan.getCursor().isEmpty() && transferAction.getCount() <= halfSource) { //faster to take only half. CURSOR MUST BE EMPTY
plan.add(Click.RIGHT, sourceSlot);
holding = halfSource;
} else { //need all
Expand Down Expand Up @@ -376,7 +376,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
GeyserItemStack oldDestinationItem = inventory.getItem(destSlot);
if (isCursor(swapAction.getSource())) {
oldSourceItem = session.getPlayerInventory().getCursor();
session.getPlayerInventory().setCursor(oldDestinationItem);
session.getPlayerInventory().setCursor(oldDestinationItem, session);
} else {
int sourceSlot = bedrockSlotToJava(swapAction.getSource());
oldSourceItem = inventory.getItem(sourceSlot);
Expand All @@ -389,7 +389,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
inventory.setItem(sourceSlot, oldDestinationItem, session);
}
if (isCursor(swapAction.getDestination())) {
session.getPlayerInventory().setCursor(oldSourceItem);
session.getPlayerInventory().setCursor(oldSourceItem, session);
} else {
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
destSlot,
Expand Down Expand Up @@ -445,7 +445,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
cursorItem.setAmount(cursorItem.getAmount() - dropAction.getCount());
if (cursorItem.isEmpty()) {
// Cursor item no longer exists
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
}
droppingItem.setAmount(dropAction.getCount());
ClientCreativeInventoryActionPacket packet = new ClientCreativeInventoryActionPacket(
Expand Down Expand Up @@ -503,7 +503,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
affectedSlots.add(javaSlot);
} else {
// Just sync up the item on our end, since the server doesn't care what's in our cursor
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
}
break;
}
Expand Down Expand Up @@ -715,7 +715,7 @@ public ItemStackResponsePacket.Response translateCreativeRequest(GeyserSession s
ItemStack javaCreativeItem = ItemTranslator.translateToJava(creativeItem);

if (isCursor(transferAction.getDestination())) {
session.getPlayerInventory().setCursor(GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId()));
session.getPlayerInventory().setCursor(GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId()), session);
return acceptRequest(request, Collections.singletonList(
new ItemStackResponsePacket.ContainerEntry(ContainerSlotType.CURSOR,
Collections.singletonList(makeItemEntry(0, session.getPlayerInventory().getCursor())))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public ClickPlan(GeyserSession session, InventoryTranslator translator, Inventor
this.simulating = true;
}

private void resetSimulation() {
this.simulatedItems.clear();
this.simulatedCursor = session.getPlayerInventory().getCursor().copy();
}

public void add(Click click, int slot) {
if (!simulating)
throw new UnsupportedOperationException("ClickPlan already executed");
Expand All @@ -77,7 +82,8 @@ public void add(Click click, int slot) {
}

public void execute(boolean refresh) {
simulating = false;
//update geyser inventory after simulation to avoid net id desync
resetSimulation();
ListIterator<ClickAction> planIter = plan.listIterator();
while (planIter.hasNext()) {
ClickAction action = planIter.next();
Expand All @@ -92,7 +98,7 @@ public void execute(boolean refresh) {
} else if (action.click.windowAction == WindowAction.DROP_ITEM || action.slot == Click.OUTSIDE_SLOT) {
clickedItemStack = null;
} else {
clickedItemStack = inventory.getItem(action.slot).getItemStack();
clickedItemStack = getItem(action.slot).getItemStack();
}

short actionId = inventory.getNextTransactionId();
Expand All @@ -113,6 +119,12 @@ public void execute(boolean refresh) {
}
System.out.println(clickPacket);
}

session.getPlayerInventory().setCursor(simulatedCursor, session);
for (Int2ObjectMap.Entry<GeyserItemStack> simulatedSlot : simulatedItems.int2ObjectEntrySet()) {
inventory.setItem(simulatedSlot.getIntKey(), simulatedSlot.getValue(), session);
}
simulating = false;
}

public GeyserItemStack getItem(int slot) {
Expand All @@ -135,7 +147,7 @@ private void setCursor(GeyserItemStack item) {
if (simulating) {
simulatedCursor = item;
} else {
session.getPlayerInventory().setCursor(item);
session.getPlayerInventory().setCursor(item, session);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void translate(ServerConfirmTransactionPacket packet, GeyserSession sessi
if (!packet.isAccepted()) {
ClientConfirmTransactionPacket confirmPacket = new ClientConfirmTransactionPacket(packet.getWindowId(), packet.getActionId(), true);
session.sendDownstreamPacket(confirmPacket);
System.out.println(packet);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ public void translate(ServerSetSlotPacket packet, GeyserSession session) {
session.addInventoryTask(() -> {
if (packet.getWindowId() == 255) { //cursor
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
GeyserItemStack oldItem = session.getPlayerInventory().getCursor();
if (newItem.getItemData(session).equals(oldItem.getItemData(session))) {
newItem.setNetId(oldItem.getNetId());
} else {
newItem.setNetId(session.getNextItemNetId());
}
session.getPlayerInventory().setCursor(newItem);
session.getPlayerInventory().setCursor(newItem, session);
InventoryUtils.updateCursor(session);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void openInventory(GeyserSession session, Inventory inventory) {
}

public static void closeInventory(GeyserSession session, int windowId) {
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
updateCursor(session);

Inventory inventory = getInventory(session, windowId);
Expand Down

0 comments on commit 7a82852

Please sign in to comment.