Work on inventory transactions

work in progress
This commit is contained in:
AJ Ferguson 2019-10-20 13:25:41 -08:00
parent 3067c72746
commit 88648f705e
22 changed files with 1214 additions and 269 deletions

View file

@ -27,6 +27,7 @@ package org.geysermc.connector.inventory;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
import com.nukkitx.math.vector.Vector3i;
import lombok.Getter;
import lombok.Setter;
@ -42,9 +43,6 @@ public class Inventory {
@Getter
protected WindowType windowType;
@Getter
protected int size;
@Getter
@Setter
protected String title;
@ -53,20 +51,31 @@ public class Inventory {
@Setter
protected ItemStack[] items;
public Inventory(int id, WindowType windowType, int size) {
this("Inventory", id, windowType, size);
@Getter
@Setter
protected Vector3i holderPosition = Vector3i.ZERO;
@Getter
@Setter
protected long holderId = -1;
protected short transactionId = 1;
public Inventory(int id, WindowType windowType) {
this("Inventory", id, windowType);
}
public Inventory(String title, int id, WindowType windowType, int size) {
public Inventory(String title, int id, WindowType windowType) {
this.title = title;
this.id = id;
this.windowType = windowType;
this.size = size;
this.items = new ItemStack[size];
}
public ItemStack getItem(int slot) {
return items[slot];
}
public short getNextTransactionId() {
return transactionId++;
}
}

View file

@ -35,12 +35,22 @@ public class PlayerInventory extends Inventory {
@Setter
private int heldItemSlot;
public PlayerInventory() {
super(0, null, 45);
@Getter
private ItemStack cursor;
public PlayerInventory() {
super(0, null);
items = new ItemStack[45];
heldItemSlot = 0;
}
public void setCursor(ItemStack stack) {
if (stack != null && stack.getId() == 0)
stack = null;
cursor = stack;
}
public ItemStack getItemInHand() {
return items[heldItemSlot];
}

View file

@ -99,6 +99,14 @@ public class GeyserSession implements Player {
@Setter
private BlockFace blockDiggingFace = BlockFace.DOWN;
@Getter
@Setter
private int lastClickedSlot;
@Getter
@Setter
private int reopeningWindow = -1;
public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession) {
this.connector = connector;
this.upstream = new UpstreamSession(bedrockServerSession);

View file

@ -25,15 +25,12 @@
package org.geysermc.connector.network.session.cache;
import com.github.steveice10.packetlib.packet.Packet;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class InventoryCache {
@ -47,9 +44,6 @@ public class InventoryCache {
@Getter
private Map<Integer, Inventory> inventories = new HashMap<Integer, Inventory>();
@Getter
private Map<Integer, List<Packet>> cachedPackets = new HashMap<Integer, List<Packet>>();
public InventoryCache(GeyserSession session) {
this.session = session;
}
@ -65,10 +59,4 @@ public class InventoryCache {
public void uncacheInventory(int id) {
inventories.remove(id);
}
public void cachePacket(int id, Packet packet) {
List<Packet> packets = cachedPackets.getOrDefault(id, new ArrayList<Packet>());
packets.add(packet);
cachedPackets.put(id, packets);
}
}

View file

@ -25,6 +25,7 @@
package org.geysermc.connector.network.translators;
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
import com.github.steveice10.mc.protocol.packet.ingame.server.*;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.*;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerActionAckPacket;
@ -36,9 +37,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerD
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerScoreboardObjectivePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerTeamPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerUpdateScorePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.*;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.*;
import com.nukkitx.nbt.CompoundTagBuilder;
import com.nukkitx.nbt.NbtUtils;
@ -48,25 +47,23 @@ import com.nukkitx.protocol.bedrock.packet.*;
import lombok.Getter;
import org.geysermc.connector.network.translators.bedrock.*;
import org.geysermc.connector.network.translators.block.BlockTranslator;
import org.geysermc.connector.network.translators.inventory.GenericInventoryTranslator;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.network.translators.inventory.*;
import org.geysermc.connector.network.translators.item.ItemTranslator;
import org.geysermc.connector.network.translators.java.*;
import org.geysermc.connector.network.translators.java.entity.*;
import org.geysermc.connector.network.translators.java.entity.player.*;
import org.geysermc.connector.network.translators.java.entity.spawn.*;
import org.geysermc.connector.network.translators.java.inventory.OpenWindowPacketTranslator;
import org.geysermc.connector.network.translators.java.scoreboard.JavaDisplayScoreboardTranslator;
import org.geysermc.connector.network.translators.java.scoreboard.JavaScoreboardObjectiveTranslator;
import org.geysermc.connector.network.translators.java.scoreboard.JavaTeamTranslator;
import org.geysermc.connector.network.translators.java.scoreboard.JavaUpdateScoreTranslator;
import org.geysermc.connector.network.translators.java.window.JavaOpenWindowTranslator;
import org.geysermc.connector.network.translators.java.window.JavaSetSlotTranslator;
import org.geysermc.connector.network.translators.java.window.JavaWindowItemsTranslator;
import org.geysermc.connector.network.translators.java.window.*;
import org.geysermc.connector.network.translators.java.world.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class TranslatorsInit {
@ -77,7 +74,7 @@ public class TranslatorsInit {
private static BlockTranslator blockTranslator;
@Getter
private static InventoryTranslator inventoryTranslator = new GenericInventoryTranslator();
private static Map<WindowType, InventoryTranslator> inventoryTranslators = new HashMap<WindowType, InventoryTranslator>();
private static final CompoundTag EMPTY_TAG = CompoundTagBuilder.builder().buildRootTag();
public static final byte[] EMPTY_LEVEL_CHUNK_DATA;
@ -132,9 +129,6 @@ public class TranslatorsInit {
Registry.registerJava(ServerNotifyClientPacket.class, new JavaNotifyClientTranslator());
Registry.registerJava(ServerChunkDataPacket.class, new JavaChunkDataTranslator());
Registry.registerJava(ServerEntityDestroyPacket.class, new JavaEntityDestroyTranslator());
Registry.registerJava(ServerWindowItemsPacket.class, new JavaWindowItemsTranslator());
Registry.registerJava(ServerOpenWindowPacket.class, new JavaOpenWindowTranslator());
Registry.registerJava(ServerSetSlotPacket.class, new JavaSetSlotTranslator());
Registry.registerJava(ServerScoreboardObjectivePacket.class, new JavaScoreboardObjectiveTranslator());
Registry.registerJava(ServerDisplayScoreboardPacket.class, new JavaDisplayScoreboardTranslator());
Registry.registerJava(ServerUpdateScorePacket.class, new JavaUpdateScoreTranslator());
@ -142,7 +136,12 @@ public class TranslatorsInit {
Registry.registerJava(ServerBlockChangePacket.class, new JavaBlockChangeTranslator());
Registry.registerJava(ServerMultiBlockChangePacket.class, new JavaMultiBlockChangeTranslator());
Registry.registerJava(ServerOpenWindowPacket.class, new OpenWindowPacketTranslator());
Registry.registerJava(ServerWindowItemsPacket.class, new JavaWindowItemsTranslator());
Registry.registerJava(ServerOpenWindowPacket.class, new JavaOpenWindowTranslator());
Registry.registerJava(ServerSetSlotPacket.class, new JavaSetSlotTranslator());
Registry.registerJava(ServerCloseWindowPacket.class, new JavaCloseWindowTranslator());
Registry.registerJava(ServerConfirmTransactionPacket.class, new JavaConfirmTransactionTranslator());
Registry.registerJava(ServerWindowPropertyPacket.class, new JavaWindowPropertyTranslator());
Registry.registerBedrock(AnimatePacket.class, new BedrockAnimateTranslator());
Registry.registerBedrock(CommandRequestPacket.class, new BedrockCommandRequestTranslator());
@ -153,6 +152,7 @@ public class TranslatorsInit {
Registry.registerBedrock(SetLocalPlayerAsInitializedPacket.class, new BedrockPlayerInitializedTranslator());
Registry.registerBedrock(InteractPacket.class, new BedrockInteractTranslator());
Registry.registerBedrock(TextPacket.class, new BedrockTextTranslator());
Registry.registerBedrock(ContainerClosePacket.class, new BedrockContainerCloseTranslator());
itemTranslator = new ItemTranslator();
blockTranslator = new BlockTranslator();
@ -161,11 +161,17 @@ public class TranslatorsInit {
}
private static void registerInventoryTranslators() {
/*inventoryTranslators.put(WindowType.GENERIC_9X1, new GenericInventoryTranslator());
inventoryTranslators.put(WindowType.GENERIC_9X2, new GenericInventoryTranslator());
inventoryTranslators.put(WindowType.GENERIC_9X3, new GenericInventoryTranslator());
inventoryTranslators.put(WindowType.GENERIC_9X4, new GenericInventoryTranslator());
inventoryTranslators.put(WindowType.GENERIC_9X5, new GenericInventoryTranslator());
inventoryTranslators.put(WindowType.GENERIC_9X6, new GenericInventoryTranslator());*/
inventoryTranslators.put(null, new PlayerInventoryTranslator()); //player inventory
inventoryTranslators.put(WindowType.GENERIC_9X1, new ChestInventoryTranslator(9));
inventoryTranslators.put(WindowType.GENERIC_9X2, new ChestInventoryTranslator(18));
inventoryTranslators.put(WindowType.GENERIC_9X3, new ChestInventoryTranslator(27));
inventoryTranslators.put(WindowType.GENERIC_9X4, new ChestInventoryTranslator(36));
inventoryTranslators.put(WindowType.GENERIC_9X5, new ChestInventoryTranslator(45));
inventoryTranslators.put(WindowType.GENERIC_9X6, new ChestInventoryTranslator(54));
inventoryTranslators.put(WindowType.GENERIC_3X3, new DispenserInventoryTranslator());
inventoryTranslators.put(WindowType.HOPPER, new HopperInventoryTranslator());
inventoryTranslators.put(WindowType.FURNACE, new FurnaceInventoryTranslator());
inventoryTranslators.put(WindowType.BLAST_FURNACE, new FurnaceInventoryTranslator());
inventoryTranslators.put(WindowType.SMOKER, new FurnaceInventoryTranslator());
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket;
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils;
public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerClosePacket> {
@Override
public void translate(ContainerClosePacket packet, GeyserSession session) {
byte windowId = packet.getWindowId() == -1 ? 0 : packet.getWindowId(); //player inventory
if (session.getReopeningWindow() != -1) {
Inventory inventory = session.getInventoryCache().getInventories().get(session.getReopeningWindow());
session.setReopeningWindow(-1);
if (inventory != null) {
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
translator.openInventory(session, inventory);
translator.updateInventory(session, inventory);
return;
}
}
ClientCloseWindowPacket closeWindowPacket = new ClientCloseWindowPacket(windowId);
session.getDownstream().getSession().send(closeWindowPacket);
InventoryUtils.closeInventory(session, windowId);
}
}

View file

@ -25,6 +25,10 @@
package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.data.game.window.*;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.nukkitx.math.vector.Vector3f;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction;
@ -33,18 +37,266 @@ import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket;
import com.nukkitx.math.vector.Vector3f;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.data.InventorySource;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import com.nukkitx.protocol.bedrock.packet.InventoryTransactionPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.PlayerEntity;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class BedrockInventoryTransactionTranslator extends PacketTranslator<InventoryTransactionPacket> {
@Override
public void translate(InventoryTransactionPacket packet, GeyserSession session) {
switch (packet.getTransactionType()) {
case NORMAL:
Inventory inventory = session.getInventoryCache().getOpenInventory();
if (inventory == null)
inventory = session.getInventory();
InventoryTranslator translator;
translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
//find the world interaction and/or cursor action if present
InventoryAction worldAction = null;
InventoryAction cursorAction = null;
for (InventoryAction action : packet.getActions()) {
if (action.getSource().getType() == InventorySource.Type.WORLD_INTERACTION) {
if (worldAction == null) {
worldAction = action;
} else {
return;
}
} else if (action.getSource().getContainerId() == ContainerId.CURSOR) {
if (cursorAction == null) {
cursorAction = action;
} else {
return;
}
}
}
if (packet.getActions().size() == 2) {
if (worldAction != null && worldAction.getSource().getFlag() == InventorySource.Flag.DROP_ITEM) {
//find container action
InventoryAction containerAction = null;
for (InventoryAction action : packet.getActions()) {
if (action.getSource().getType() == InventorySource.Type.CONTAINER || action.getSource().getType() == InventorySource.Type.UNTRACKED_INTERACTION_UI) {
containerAction = action;
break;
}
}
if (containerAction != null) {
//quick dropping from hotbar?
if (session.getInventoryCache().getOpenInventory() == null && containerAction.getSource().getContainerId() == ContainerId.INVENTORY) {
if (containerAction.getSlot() == session.getInventory().getHeldItemSlot()) {
ClientPlayerActionPacket actionPacket = new ClientPlayerActionPacket(
containerAction.getToItem().getCount() == 0 ? PlayerAction.DROP_ITEM_STACK : PlayerAction.DROP_ITEM,
new Position(0, 0, 0), BlockFace.DOWN);
session.getDownstream().getSession().send(actionPacket);
return;
}
}
boolean leftClick = containerAction.getToItem().getCount() == 0;
if (containerAction.getSource().getContainerId() != ContainerId.CURSOR) { //dropping directly from inventory
int javaSlot = translator.bedrockSlotToJava(containerAction);
ClientWindowActionPacket dropPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
javaSlot, null, WindowAction.DROP_ITEM,
leftClick ? DropItemParam.DROP_SELECTED_STACK : DropItemParam.DROP_FROM_SELECTED);
session.getDownstream().getSession().send(dropPacket);
return;
} else { //clicking outside of inventory
ClientWindowActionPacket dropPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
-999, null, WindowAction.CLICK_ITEM,
leftClick ? ClickItemParam.LEFT_CLICK : ClickItemParam.RIGHT_CLICK);
session.getDownstream().getSession().send(dropPacket);
return;
}
}
} else if (cursorAction != null) {
//find container action
InventoryAction containerAction = null;
for (InventoryAction action : packet.getActions()) {
if (action != cursorAction && (action.getSource().getType() == InventorySource.Type.CONTAINER || action.getSource().getType() == InventorySource.Type.UNTRACKED_INTERACTION_UI)) {
containerAction = action;
break;
}
}
if (containerAction != null) {
if (InventoryUtils.canCombine(cursorAction.getFromItem(), cursorAction.getToItem())
&& cursorAction.getToItem().getCount() > cursorAction.getFromItem().getCount()) { //fill stack
int javaSlot = session.getLastClickedSlot();
ClientWindowActionPacket fillStackPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
javaSlot, null, WindowAction.FILL_STACK, FillStackParam.FILL);
session.getDownstream().getSession().send(fillStackPacket);
translator.updateInventory(session, inventory); //bedrock fill stack can sometimes differ from java version, refresh and let server change slots
System.out.println(fillStackPacket);
return;
} else {
//left/right click
int javaSlot = translator.bedrockSlotToJava(containerAction);
boolean rightClick;
if (cursorAction.getFromItem().getCount() == 0) { //picking up item
rightClick = containerAction.getToItem().getCount() != 0;
} else { //releasing item
rightClick = cursorAction.getToItem().getCount() != 0 && cursorAction.getFromItem().getCount() - cursorAction.getToItem().getCount() == 1;
}
ItemStack translatedCursor = TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getFromItem());
boolean refresh = !Objects.equals(session.getInventory().getCursor(), translatedCursor.getId() == 0 ? null : translatedCursor); //refresh slot if there is a cursor mismatch
ClientWindowActionPacket clickPacket = new ClientWindowActionPacket(inventory.getId(),
inventory.getNextTransactionId(), javaSlot,
refresh ? new ItemStack(1, 127, new CompoundTag("")) : InventoryUtils.fixNbt(TranslatorsInit.getItemTranslator().translateToJava(containerAction.getFromItem())), //send invalid item stack to refresh slot
WindowAction.CLICK_ITEM, rightClick ? ClickItemParam.RIGHT_CLICK : ClickItemParam.LEFT_CLICK);
System.out.println(clickPacket);
session.getDownstream().getSession().send(clickPacket);
inventory.getItems()[javaSlot] = TranslatorsInit.getItemTranslator().translateToJava(containerAction.getToItem());
translator.updateSlot(session, inventory, javaSlot);
session.getInventory().setCursor(TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getToItem()));
session.setLastClickedSlot(javaSlot);
return;
}
}
} else if (packet.getActions().stream().allMatch(p -> p.getSource().getType() == InventorySource.Type.CONTAINER || p.getSource().getType() == InventorySource.Type.UNTRACKED_INTERACTION_UI)) {
//either moving 1 item or swapping 2 slots (touchscreen or one slot shift click)
InventoryAction fromAction;
InventoryAction toAction;
//find source slot
if (packet.getActions().get(0).getFromItem().getCount() > packet.getActions().get(0).getToItem().getCount()) {
fromAction = packet.getActions().get(0);
toAction = packet.getActions().get(1);
} else {
fromAction = packet.getActions().get(1);
toAction = packet.getActions().get(0);
}
int fromSlot = translator.bedrockSlotToJava(fromAction);
int toSlot = translator.bedrockSlotToJava(toAction);
//check if dealing with output only slot like furnace. this is to handle a situation where the output slot was partially emptied without right clicking (touchscreen or full inventory)
//this is only possible by shift clicking
if (translator.isOutputSlot(fromAction) && fromAction.getToItem().getCount() != 0) {
ClientWindowActionPacket shiftClickPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
fromSlot, InventoryUtils.fixNbt(inventory.getItem(fromSlot)), WindowAction.SHIFT_CLICK_ITEM, ShiftClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(shiftClickPacket);
inventory.getItems()[toSlot] = TranslatorsInit.getItemTranslator().translateToJava(toAction.getToItem());
inventory.getItems()[fromSlot] = TranslatorsInit.getItemTranslator().translateToJava(fromAction.getToItem());
return;
} else {
//pickup fromAction item
ClientWindowActionPacket leftClick1Packet = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
fromSlot, InventoryUtils.fixNbt(TranslatorsInit.getItemTranslator().translateToJava(fromAction.getFromItem())), WindowAction.CLICK_ITEM,
ClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(leftClick1Packet);
System.out.println(leftClick1Packet);
//release fromAction item into toAction slot
ClientWindowActionPacket leftClick2Packet = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
toSlot, InventoryUtils.fixNbt(TranslatorsInit.getItemTranslator().translateToJava(toAction.getFromItem())), WindowAction.CLICK_ITEM,
ClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(leftClick2Packet);
System.out.println(leftClick2Packet);
//test if swapping two items or moving one item
//if swapping then complete it
if (fromAction.getToItem().getId() != 0) {
ClientWindowActionPacket leftClick3Packet = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
fromSlot, null, WindowAction.CLICK_ITEM,
ClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(leftClick3Packet);
}
inventory.getItems()[toSlot] = TranslatorsInit.getItemTranslator().translateToJava(toAction.getToItem());
inventory.getItems()[fromSlot] = TranslatorsInit.getItemTranslator().translateToJava(fromAction.getToItem());
return;
}
}
} else if (packet.getActions().size() > 2) {
//shift click or fill stack?
ItemData firstItem;
if (packet.getActions().get(0).getFromItem().getId() != 0) {
firstItem = packet.getActions().get(0).getFromItem();
} else {
firstItem = packet.getActions().get(0).getToItem();
}
List<InventoryAction> sourceActions = new ArrayList<>(packet.getActions().size());
List<InventoryAction> destActions = new ArrayList<>(packet.getActions().size());
boolean sameItems = true;
for (InventoryAction action : packet.getActions()) {
if (action.getFromItem().getCount() > action.getToItem().getCount()) {
if (!InventoryUtils.canCombine(action.getFromItem(), firstItem))
sameItems = false;
sourceActions.add(action);
} else {
if (!InventoryUtils.canCombine(action.getToItem(), firstItem))
sameItems = false;
destActions.add(action);
}
}
if (sameItems) {
if (sourceActions.size() == 1) { //shift click
InventoryAction sourceAction = sourceActions.get(0);
//in java edition, shift clicked item must move across hotbar and main inventory
if (sourceAction.getSource().getContainerId() == ContainerId.INVENTORY) {
for (InventoryAction action : packet.getActions()) {
if (action != sourceAction && action.getSource().getContainerId() == ContainerId.INVENTORY) {
if ((sourceAction.getSlot() < 9 && action.getSlot() < 9) || (sourceAction.getSlot() >= 9 && action.getSlot() >= 9)) {
//shift click not compatible with java edition. refresh inventory and abort
translator.updateInventory(session, inventory);
return;
}
}
}
}
int javaSlot = translator.bedrockSlotToJava(sourceAction);
ClientWindowActionPacket shiftClickPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
javaSlot, InventoryUtils.fixNbt(inventory.getItem(javaSlot)), WindowAction.SHIFT_CLICK_ITEM, ShiftClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(shiftClickPacket);
return;
} else if (destActions.size() == 1) { //fill stack
InventoryAction destAction = destActions.get(0);
int javaSlot;
if (destAction != cursorAction) { //if touchscreen
javaSlot = translator.bedrockSlotToJava(destAction);
ClientWindowActionPacket leftClickPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
javaSlot, InventoryUtils.fixNbt(inventory.getItem(javaSlot)), WindowAction.CLICK_ITEM, ClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(leftClickPacket);
} else {
javaSlot = session.getLastClickedSlot();
}
ClientWindowActionPacket fillStackPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
javaSlot, null, WindowAction.FILL_STACK, FillStackParam.FILL);
session.getDownstream().getSession().send(fillStackPacket);
if (destAction != cursorAction) { //if touchscreen
ClientWindowActionPacket leftClickPacket = new ClientWindowActionPacket(inventory.getId(), inventory.getNextTransactionId(),
javaSlot, null, WindowAction.CLICK_ITEM, ClickItemParam.LEFT_CLICK);
session.getDownstream().getSession().send(leftClickPacket);
inventory.getItems()[javaSlot] = TranslatorsInit.getItemTranslator().translateToJava(destAction.getToItem());
}
translator.updateInventory(session, inventory);
return;
}
}
}
//refresh inventory, transaction was not translated
translator.updateInventory(session, inventory);
break;
case INVENTORY_MISMATCH:
InventorySlotPacket cursorPacket = new InventorySlotPacket();
cursorPacket.setContainerId(ContainerId.CURSOR);
cursorPacket.setSlot(TranslatorsInit.getItemTranslator().translateToBedrock(session.getInventory().getCursor()));
session.getUpstream().sendPacket(cursorPacket);
Inventory inv = session.getInventoryCache().getOpenInventory();
if (inv == null)
inv = session.getInventory();
TranslatorsInit.getInventoryTranslators().get(inv.getWindowType()).updateInventory(session, inv);
case ITEM_USE:
if (packet.getActionType() == 1) {
ClientPlayerUseItemPacket useItemPacket = new ClientPlayerUseItemPacket(Hand.MAIN_HAND);

View file

@ -0,0 +1,163 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.*;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
public class ChestInventoryTranslator extends InventoryTranslator {
public ChestInventoryTranslator(int size) {
super(size);
}
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
Vector3i position = session.getPlayerEntity().getPosition().toInt();
position = position.add(Vector3i.UP);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(54 << 4)); //chest
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
if (size > 27) {
Vector3i pairPosition = position.add(Vector3i.UNIT_X);
blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(pairPosition);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(54 << 4));
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
CompoundTag tag = CompoundTag.EMPTY.toBuilder()
.stringTag("id", "Chest")
.intTag("x", position.getX())
.intTag("y", position.getY())
.intTag("z", position.getZ())
.intTag("pairx", pairPosition.getX())
.intTag("pairz", pairPosition.getZ()).buildRootTag();
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
dataPacket.setData(tag);
dataPacket.setBlockPosition(position);
session.getUpstream().sendPacket(dataPacket);
tag = CompoundTag.EMPTY.toBuilder()
.stringTag("id", "Chest")
.intTag("x", pairPosition.getX())
.intTag("y", pairPosition.getY())
.intTag("z", pairPosition.getZ())
.intTag("pairx", position.getX())
.intTag("pairz", position.getZ()).buildRootTag();
dataPacket = new BlockEntityDataPacket();
dataPacket.setData(tag);
dataPacket.setBlockPosition(pairPosition);
session.getUpstream().sendPacket(dataPacket);
}
inventory.setHolderPosition(position);
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) ContainerType.CONTAINER.id());
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.getUpstream().sendPacket(containerOpenPacket);
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
Vector3i holderPos = inventory.getHolderPosition();
Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
BlockEntry realBlock = session.getChunkCache().getBlockAt(pos);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(holderPos);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
session.getUpstream().sendPacket(blockPacket);
if (this.size > 27) {
holderPos = holderPos.add(Vector3i.UNIT_X);
pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
realBlock = session.getChunkCache().getBlockAt(pos);
blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(holderPos);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
session.getUpstream().sendPacket(blockPacket);
}
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
}
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
//need to pad empty slots for 1x9, 2x9, 4x9, and 5x9
int paddedSize;
if (this.size > 27) {
paddedSize = 54;
} else {
paddedSize = 27;
}
ItemData[] bedrockItems = new ItemData[paddedSize];
for (int i = 0; i < bedrockItems.length; i++) {
if (i <= this.size) {
bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
} else {
bedrockItems[i] = ItemData.AIR;
}
}
InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getId());
contentPacket.setContents(bedrockItems);
session.getUpstream().sendPacket(contentPacket);
Inventory playerInventory = session.getInventory();
for (int i = 0; i < 36; i++) {
playerInventory.getItems()[i + 9] = inventory.getItems()[i + this.size];
}
TranslatorsInit.getInventoryTranslators().get(playerInventory.getWindowType()).updateInventory(session, playerInventory);
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
public class DispenserInventoryTranslator extends InventoryTranslator {
public DispenserInventoryTranslator() {
super(9);
}
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
Vector3i position = session.getPlayerEntity().getPosition().toInt();
position = position.add(Vector3i.UP);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(23 << 4)); //dispenser
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
inventory.setHolderPosition(position);
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) ContainerType.DISPENSER.id());
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.getUpstream().sendPacket(containerOpenPacket);
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
Vector3i holderPos = inventory.getHolderPosition();
Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
BlockEntry realBlock = session.getChunkCache().getBlockAt(pos);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(holderPos);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
session.getUpstream().sendPacket(blockPacket);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
}

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.*;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
public class FurnaceInventoryTranslator extends InventoryTranslator {
public FurnaceInventoryTranslator() {
super(3);
}
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
Vector3i position = session.getPlayerEntity().getPosition().toInt();
position = position.add(Vector3i.UP);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(61 << 4)); //furnace
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
inventory.setHolderPosition(position);
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) ContainerType.FURNACE.id());
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.getUpstream().sendPacket(containerOpenPacket);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
//bedrock protocol library is currently missing property mappings for windows. only the furnace arrow will update for now
ContainerSetDataPacket dataPacket = new ContainerSetDataPacket();
dataPacket.setWindowId((byte) inventory.getId());
switch (key) {
case 0:
dataPacket.setProperty(ContainerSetDataPacket.Property.FURNACE_LIT_TIME);
break;
case 1:
dataPacket.setProperty(ContainerSetDataPacket.Property.FURNACE_LIT_DURATION);
break;
case 2:
dataPacket.setProperty(ContainerSetDataPacket.Property.FURNACE_TICK_COUNT);
break;
default:
return;
}
dataPacket.setValue((short) value);
session.getUpstream().sendPacket(dataPacket);
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
Vector3i holderPos = inventory.getHolderPosition();
Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
BlockEntry realBlock = session.getChunkCache().getBlockAt(pos);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(holderPos);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
session.getUpstream().sendPacket(blockPacket);
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return action.getSlot() == 2;
}
}

View file

@ -1,74 +0,0 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
public class GenericInventoryTranslator extends InventoryTranslator {
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
// TODO: Add code here
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) 0);
containerOpenPacket.setBlockPosition(Vector3i.ZERO);
session.getUpstream().sendPacket(containerOpenPacket);
}
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
ItemData[] bedrockItems = new ItemData[inventory.getItems().length];
for (int i = 0; i < bedrockItems.length; i++) {
bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getId());
contentPacket.setContents(bedrockItems);
session.getUpstream().sendPacket(contentPacket);
}
@Override
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(inventory.getId());
slotPacket.setSlot(TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[slot]));
slotPacket.setInventorySlot(slot);
session.getUpstream().sendPacket(slotPacket);
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
public class HopperInventoryTranslator extends InventoryTranslator {
public HopperInventoryTranslator() {
super(5);
}
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
Vector3i position = session.getPlayerEntity().getPosition().toInt();
position = position.add(Vector3i.UP);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(154 << 4)); //hopper
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
inventory.setHolderPosition(position);
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) ContainerType.HOPPER.id());
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.getUpstream().sendPacket(containerOpenPacket);
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
Vector3i holderPos = inventory.getHolderPosition();
Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
BlockEntry realBlock = session.getChunkCache().getBlockAt(pos);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(holderPos);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(realBlock.getBedrockId() << 4 | realBlock.getBedrockData()));
session.getUpstream().sendPacket(blockPacket);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
}

View file

@ -25,14 +25,72 @@
package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.inventory.PlayerInventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
public abstract class InventoryTranslator {
public final int size;
InventoryTranslator(int size) {
this.size = size;
}
public abstract void prepareInventory(GeyserSession session, Inventory inventory);
public abstract void openInventory(GeyserSession session, Inventory inventory);
public abstract void updateInventory(GeyserSession session, Inventory inventory);
public abstract void updateSlot(GeyserSession session, Inventory inventory, int slot);
public abstract void closeInventory(GeyserSession session, Inventory inventory);
public abstract void updateProperty(GeyserSession session, Inventory inventory, int key, int value);
public void updateInventory(GeyserSession session, Inventory inventory) {
ItemData[] bedrockItems = new ItemData[this.size];
for (int i = 0; i < bedrockItems.length; i++) {
bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
InventoryContentPacket contentPacket = new InventoryContentPacket();
contentPacket.setContainerId(inventory.getId());
contentPacket.setContents(bedrockItems);
session.getUpstream().sendPacket(contentPacket);
Inventory playerInventory = session.getInventory();
for (int i = 0; i < 36; i++) {
playerInventory.getItems()[i + 9] = inventory.getItems()[i + this.size];
}
TranslatorsInit.getInventoryTranslators().get(playerInventory.getWindowType()).updateInventory(session, playerInventory);
}
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
if (slot >= this.size) {
Inventory playerInventory = session.getInventory();
playerInventory.getItems()[(slot + 9) - this.size] = inventory.getItem(slot);
TranslatorsInit.getInventoryTranslators().get(playerInventory.getWindowType()).updateSlot(session, playerInventory, (slot + 9) - this.size);
} else {
InventorySlotPacket slotPacket = new InventorySlotPacket();
slotPacket.setContainerId(inventory.getId());
slotPacket.setInventorySlot(slot);
slotPacket.setSlot(TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[slot]));
session.getUpstream().sendPacket(slotPacket);
}
}
public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot();
if (action.getSource().getContainerId() == ContainerId.INVENTORY) {
//hotbar
if (slotnum >= 9) {
return slotnum + this.size - 9;
} else {
return slotnum + this.size + 27;
}
} else {
return slotnum;
}
}
public abstract boolean isOutputSlot(InventoryAction action);
}

View file

@ -0,0 +1,138 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.protocol.bedrock.data.*;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
public class PlayerInventoryTranslator extends InventoryTranslator {
public PlayerInventoryTranslator() {
super(45);
}
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
InventoryContentPacket inventoryContentPacket = new InventoryContentPacket();
inventoryContentPacket.setContainerId(ContainerId.INVENTORY);
ItemData[] contents = new ItemData[36];
// Inventory
for (int i = 9; i < 36; i++) {
contents[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
// Hotbar
for (int i = 36; i < 45; i++) {
contents[i - 36] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
inventoryContentPacket.setContents(contents);
session.getUpstream().sendPacket(inventoryContentPacket);
// Armor
InventoryContentPacket armorContentPacket = new InventoryContentPacket();
armorContentPacket.setContainerId(ContainerId.ARMOR);
contents = new ItemData[4];
for (int i = 5; i < 9; i++) {
contents[i - 5] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
armorContentPacket.setContents(contents);
session.getUpstream().sendPacket(armorContentPacket);
}
@Override
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
if (slot >= 5 && slot <= 44) {
InventorySlotPacket slotPacket = new InventorySlotPacket();
if (slot >= 9) {
slotPacket.setContainerId(ContainerId.INVENTORY);
if (slot >= 36) {
slotPacket.setInventorySlot(slot - 36);
} else {
slotPacket.setInventorySlot(slot);
}
} else {
slotPacket.setContainerId(ContainerId.ARMOR);
slotPacket.setInventorySlot(slot - 5);
}
slotPacket.setSlot(TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItem(slot)));
session.getUpstream().sendPacket(slotPacket);
} else if (slot == 0) {
//TODO: crafting output
}
}
@Override
public int bedrockSlotToJava(InventoryAction action) {
int slotnum = action.getSlot();
switch (action.getSource().getContainerId()) {
case ContainerId.INVENTORY:
// Inventory
if (slotnum >= 9 && slotnum <= 35) {
return slotnum;
}
// Hotbar
if (slotnum >= 0 && slotnum <= 8) {
return slotnum + 36;
}
break;
case ContainerId.ARMOR:
if (slotnum >= 0 && slotnum <= 3) {
return slotnum + 5;
}
break;
case ContainerId.CRAFTING_ADD_INGREDIENT:
case ContainerId.CRAFTING_REMOVE_INGREDIENT:
return slotnum + 1;
}
return slotnum;
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
}
}

View file

@ -91,7 +91,6 @@ public class ItemTranslator {
private CompoundTag translateToJavaNBT(com.nukkitx.nbt.tag.CompoundTag tag) {
CompoundTag javaTag = new CompoundTag(tag.getName());
Map<String, Tag> javaValue = javaTag.getValue();
if (tag.getValue() != null && !tag.getValue().isEmpty()) {
for (String str : tag.getValue().keySet()) {
com.nukkitx.nbt.tag.Tag bedrockTag = tag.get(str);
@ -99,7 +98,7 @@ public class ItemTranslator {
if (translatedTag == null)
continue;
javaValue.put(str, translatedTag);
javaTag.getValue().put(str, translatedTag);
}
}

View file

@ -1,22 +0,0 @@
package org.geysermc.connector.network.translators.java.inventory;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
public class OpenWindowPacketTranslator extends PacketTranslator<ServerOpenWindowPacket> {
@Override
public void translate(ServerOpenWindowPacket packet, GeyserSession session) {
System.out.println("debug: " + packet.getType());
InventoryTranslator translator = TranslatorsInit.getInventoryTranslator();
translator.openInventory(session, new Inventory(packet.getName(), packet.getWindowId(), packet.getType(), 54));
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.java.window;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerCloseWindowPacket;
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.utils.InventoryUtils;
public class JavaCloseWindowTranslator extends PacketTranslator<ServerCloseWindowPacket> {
@Override
public void translate(ServerCloseWindowPacket packet, GeyserSession session) {
ContainerClosePacket closePacket = new ContainerClosePacket();
closePacket.setWindowId((byte)packet.getWindowId());
session.getUpstream().sendPacket(closePacket);
InventoryUtils.closeInventory(session, packet.getWindowId());
}
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.java.window;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientConfirmTransactionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerConfirmTransactionPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils;
public class JavaConfirmTransactionTranslator extends PacketTranslator<ServerConfirmTransactionPacket> {
@Override
public void translate(ServerConfirmTransactionPacket packet, GeyserSession session) {
System.out.println(packet);
if (!packet.isAccepted()) {
ClientConfirmTransactionPacket confirmPacket = new ClientConfirmTransactionPacket(packet.getWindowId(), packet.getActionId(), true);
session.getDownstream().getSession().send(confirmPacket);
}
}
}

View file

@ -25,44 +25,57 @@
package org.geysermc.connector.network.translators.java.window;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket;
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.session.cache.InventoryCache;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.utils.InventoryUtils;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import java.util.Objects;
public class JavaSetSlotTranslator extends PacketTranslator<ServerSetSlotPacket> {
@Override
public void translate(ServerSetSlotPacket packet, GeyserSession session) {
InventoryCache inventoryCache = session.getInventoryCache();
if (!inventoryCache.getInventories().containsKey(packet.getWindowId())) {
inventoryCache.cachePacket(packet.getWindowId(), packet);
if (packet.getWindowId() == 255 && packet.getSlot() == -1) { //cursor
if (Objects.equals(session.getInventory().getCursor(), packet.getItem()))
return;
//bedrock client is bugged when changing the cursor. reopen inventory after changing it
if (packet.getItem() == null && session.getInventory().getCursor() != null) {
InventorySlotPacket cursorPacket = new InventorySlotPacket();
cursorPacket.setContainerId(ContainerId.CURSOR);
cursorPacket.setSlot(ItemData.AIR);
session.getUpstream().sendPacket(cursorPacket);
Inventory inventory = session.getInventoryCache().getOpenInventory();
if (inventory != null) {
session.setReopeningWindow(inventory.getId());
} else {
inventory = session.getInventory();
}
ContainerClosePacket closePacket = new ContainerClosePacket();
closePacket.setWindowId((byte)inventory.getId());
session.getUpstream().sendPacket(closePacket);
}
session.getInventory().setCursor(packet.getItem());
return;
}
Inventory inventory = inventoryCache.getInventories().get(packet.getWindowId());
if (packet.getWindowId() != 0 && inventory.getWindowType() == null)
Inventory inventory = session.getInventoryCache().getInventories().get(packet.getWindowId());
if (inventory == null || (packet.getWindowId() != 0 && inventory.getWindowType() == null))
return;
// Player inventory
if (packet.getWindowId() == 0) {
if (packet.getSlot() >= inventory.getItems().length)
return; // Most likely not a player inventory
ItemStack[] items = inventory.getItems();
items[packet.getSlot()] = packet.getItem();
inventory.setItems(items);
InventoryUtils.refreshPlayerInventory(session, inventory);
if (inventory.isOpen()) {
InventoryUtils.updateSlot(session, packet);
} else {
inventoryCache.cachePacket(packet.getWindowId(), packet);
}
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
if (translator != null) {
inventory.getItems()[packet.getSlot()] = packet.getItem();
translator.updateSlot(session, inventory, packet.getSlot());
}
}
}

View file

@ -30,26 +30,22 @@ import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.session.cache.InventoryCache;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import org.geysermc.connector.utils.InventoryUtils;
public class JavaWindowItemsTranslator extends PacketTranslator<ServerWindowItemsPacket> {
@Override
public void translate(ServerWindowItemsPacket packet, GeyserSession session) {
InventoryCache inventoryCache = session.getInventoryCache();
if (!inventoryCache.getInventories().containsKey(packet.getWindowId())) {
inventoryCache.cachePacket(packet.getWindowId(), packet);
Inventory inventory = session.getInventoryCache().getInventories().get(packet.getWindowId());
if (inventory == null || (packet.getWindowId() != 0 && inventory.getWindowType() == null))
return;
}
Inventory inventory = inventoryCache.getInventories().get(packet.getWindowId());
// Player inventory
if (packet.getWindowId() == 0) {
inventory.setItems(packet.getItems());
InventoryUtils.refreshPlayerInventory(session, inventory);
return;
inventory.setItems(packet.getItems());
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
if (translator != null) {
translator.updateInventory(session, inventory);
}
InventoryUtils.updateInventory(session, packet);
}
}

View file

@ -0,0 +1,24 @@
package org.geysermc.connector.network.translators.java.window;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowPropertyPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.session.cache.InventoryCache;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
public class JavaWindowPropertyTranslator extends PacketTranslator<ServerWindowPropertyPacket> {
@Override
public void translate(ServerWindowPropertyPacket packet, GeyserSession session) {
Inventory inventory = session.getInventoryCache().getInventories().get(packet.getWindowId());
if (inventory == null || (packet.getWindowId() != 0 && inventory.getWindowType() == null))
return;
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
if (translator != null) {
translator.updateProperty(session, inventory, packet.getRawProperty(), packet.getValue());
}
}
}

View file

@ -1,113 +1,63 @@
package org.geysermc.connector.utils;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket;
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket;
import com.github.steveice10.packetlib.packet.Packet;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import org.geysermc.api.Geyser;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
public class InventoryUtils {
public static void refreshPlayerInventory(GeyserSession session, Inventory inventory) {
InventoryContentPacket inventoryContentPacket = new InventoryContentPacket();
inventoryContentPacket.setContainerId(ContainerId.INVENTORY);
ItemData[] contents = new ItemData[40];
// Inventory
for (int i = 9; i < 36; i++) {
contents[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
// Hotbar
for (int i = 36; i < 45; i++) {
contents[i - 36] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
// Armor
for (int i = 5; i < 9; i++) {
contents[i + 31] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
}
inventoryContentPacket.setContents(contents);
session.getUpstream().sendPacket(inventoryContentPacket);
}
public static void openInventory(GeyserSession session, ServerOpenWindowPacket packet) {
Inventory inventory = new Inventory(packet.getWindowId(), packet.getType(), 45); // TODO: Find a way to set this value
session.getInventoryCache().getInventories().put(packet.getWindowId(), inventory);
session.getInventoryCache().setOpenInventory(inventory);
InventoryTranslator translator = TranslatorsInit.getInventoryTranslator();
translator.prepareInventory(session, inventory);
Geyser.getGeneralThreadPool().schedule(() -> {
List<Packet> packets = session.getInventoryCache().getCachedPackets().get(inventory.getId());
packets.forEach(itemPacket -> {
if (itemPacket != null) {
if (ServerWindowItemsPacket.class.isAssignableFrom(itemPacket.getClass())) {
updateInventory(session, (ServerWindowItemsPacket) itemPacket);
}
}
});
}, 200, TimeUnit.MILLISECONDS);
Inventory inventory = new Inventory(packet.getWindowId(), packet.getType());
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
if (translator != null) {
session.getInventoryCache().cacheInventory(inventory);
session.getInventoryCache().setOpenInventory(inventory);
translator.prepareInventory(session, inventory);
//TODO: find better way to handle double chest delay
if (inventory.getWindowType() == WindowType.GENERIC_9X4 || inventory.getWindowType() == WindowType.GENERIC_9X5 || inventory.getWindowType() == WindowType.GENERIC_9X6) {
Geyser.getGeneralThreadPool().schedule(() -> {
translator.openInventory(session, inventory);
translator.updateInventory(session, inventory);
}, 200, TimeUnit.MILLISECONDS);
} else {
translator.openInventory(session, inventory);
}
}
}
public static void updateInventory(GeyserSession session, ServerWindowItemsPacket packet) {
if (packet.getWindowId() == 0)
return;
if (session.getInventoryCache().getOpenInventory() == null || !session.getInventoryCache().getInventories().containsKey(packet.getWindowId()))
return;
Inventory openInventory = session.getInventoryCache().getOpenInventory();
if (packet.getWindowId() != openInventory.getId())
return;
InventoryTranslator translator = TranslatorsInit.getInventoryTranslator();
if (translator == null) {
session.getDownstream().getSession().send(new ClientCloseWindowPacket(packet.getWindowId()));
return;
public static void closeInventory(GeyserSession session, int windowId) {
if (windowId != 0) {
Inventory inventory = session.getInventoryCache().getInventories().get(windowId);
if (inventory != null) {
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
translator.closeInventory(session, inventory);
session.getInventoryCache().uncacheInventory(windowId);
}
session.getInventoryCache().setOpenInventory(null);
}
openInventory.setItems(packet.getItems());
translator.updateInventory(session, openInventory);
}
public static void updateSlot(GeyserSession session, ServerSetSlotPacket packet) {
if (packet.getWindowId() == 0)
return;
//currently, ItemStack.equals() does not check the item id
public static boolean canCombine(ItemData stack1, ItemData stack2) {
if (stack1 == null || stack2 == null)
return false;
return stack1.getId() == stack2.getId() && stack1.equals(stack2, false, true, true);
}
if (session.getInventoryCache().getOpenInventory() == null || !session.getInventoryCache().getInventories().containsKey(packet.getWindowId()))
return;
Inventory openInventory = session.getInventoryCache().getOpenInventory();
if (packet.getWindowId() != openInventory.getId())
return;
InventoryTranslator translator = TranslatorsInit.getInventoryTranslator();
if (translator == null) {
session.getDownstream().getSession().send(new ClientCloseWindowPacket(packet.getWindowId()));
return;
}
if (packet.getSlot() >= openInventory.getSize()) {
session.getDownstream().getSession().send(new ClientCloseWindowPacket(packet.getWindowId()));
return;
}
ItemStack[] items = openInventory.getItems();
items[packet.getSlot()] = packet.getItem();
translator.updateSlot(session, openInventory, packet.getSlot());
//NPE if nbt tag is null
public static ItemStack fixNbt(ItemStack stack) {
if (stack == null)
return null;
return new ItemStack(stack.getId(), stack.getAmount(), stack.getNbt() == null ? new CompoundTag("") : stack.getNbt());
}
}