item painting. cursor net id

This commit is contained in:
AJ Ferguson 2020-12-31 17:39:54 -09:00
parent c4fc604e0c
commit b7b3278d8b
8 changed files with 38 additions and 25 deletions

View file

@ -43,7 +43,6 @@ public class GeyserItemStack {
private int amount;
private CompoundTag nbt;
private int netId;
private boolean netIdWasUpdated;
public GeyserItemStack(int javaId) {
this(javaId, 1);
@ -62,7 +61,6 @@ public class GeyserItemStack {
this.amount = amount;
this.nbt = nbt;
this.netId = netId;
this.netIdWasUpdated = !this.isEmpty();
}
public int getJavaId() {
@ -79,7 +77,6 @@ public class GeyserItemStack {
public void setNetId(int netId) {
this.netId = netId;
this.netIdWasUpdated = true;
}
public int getNetId() {

View file

@ -79,6 +79,11 @@ public class Inventory {
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());
@ -86,7 +91,6 @@ public class Inventory {
newItem.setNetId(session.getNextItemNetId());
}
}
items[slot] = newItem;
}
public short getNextTransactionId() {

View file

@ -28,6 +28,7 @@ package org.geysermc.connector.inventory;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.geysermc.connector.network.session.GeyserSession;
public class PlayerInventory extends Inventory {
@ -40,7 +41,6 @@ public class PlayerInventory extends Inventory {
private int heldItemSlot;
@Getter
@Setter
@NonNull
private GeyserItemStack cursor = GeyserItemStack.EMPTY;
@ -49,6 +49,11 @@ public class PlayerInventory extends Inventory {
heldItemSlot = 0;
}
public void setCursor(@NonNull GeyserItemStack newCursor, GeyserSession session) {
updateItemNetId(cursor, newCursor, session);
cursor = newCursor;
}
public GeyserItemStack getItemInHand() {
return items[36 + heldItemSlot];
}

View file

@ -223,7 +223,7 @@ public abstract class InventoryTranslator {
} else {
// Cursor will be emptied
destItem.setAmount(itemsLeftOver);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY);
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
}
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
destSlot,
@ -250,13 +250,13 @@ public abstract class InventoryTranslator {
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);
}
@ -340,7 +340,7 @@ public abstract class InventoryTranslator {
//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
@ -376,7 +376,7 @@ public abstract class InventoryTranslator {
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);
@ -389,7 +389,7 @@ public abstract class InventoryTranslator {
inventory.setItem(sourceSlot, oldDestinationItem, session);
}
if (isCursor(swapAction.getDestination())) {
session.getPlayerInventory().setCursor(oldSourceItem);
session.getPlayerInventory().setCursor(oldSourceItem, session);
} else {
ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket(
destSlot,
@ -445,7 +445,7 @@ public abstract class InventoryTranslator {
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(
@ -503,7 +503,7 @@ public abstract class InventoryTranslator {
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;
}
@ -715,7 +715,7 @@ public abstract class InventoryTranslator {
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())))));

View file

@ -63,6 +63,11 @@ public class ClickPlan {
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");
@ -77,7 +82,8 @@ public class ClickPlan {
}
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();
@ -92,7 +98,7 @@ public class ClickPlan {
} 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();
@ -113,6 +119,12 @@ public class ClickPlan {
}
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) {
@ -135,7 +147,7 @@ public class ClickPlan {
if (simulating) {
simulatedCursor = item;
} else {
session.getPlayerInventory().setCursor(item);
session.getPlayerInventory().setCursor(item, session);
}
}

View file

@ -41,6 +41,7 @@ public class JavaConfirmTransactionTranslator extends PacketTranslator<ServerCon
if (!packet.isAccepted()) {
ClientConfirmTransactionPacket confirmPacket = new ClientConfirmTransactionPacket(packet.getWindowId(), packet.getActionId(), true);
session.sendDownstreamPacket(confirmPacket);
System.out.println(packet);
}
});
}

View file

@ -43,13 +43,7 @@ public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket>
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;
}

View file

@ -79,7 +79,7 @@ public class InventoryUtils {
}
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);