Implement some more windows

anvil, brewing stand, crafting table (still no crafting yet), shulker box
This commit is contained in:
AJ Ferguson 2019-11-05 14:17:22 -09:00
parent 5797dc2c7e
commit 625817c282
14 changed files with 421 additions and 353 deletions

View File

@ -43,6 +43,7 @@ import com.nukkitx.nbt.CompoundTagBuilder;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTOutputStream;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.packet.*;
import lombok.Getter;
import org.geysermc.connector.network.translators.bedrock.*;
@ -169,10 +170,19 @@ public class TranslatorsInit {
inventoryTranslators.put(WindowType.GENERIC_9X4, new DoubleChestInventoryTranslator(36));
inventoryTranslators.put(WindowType.GENERIC_9X5, new DoubleChestInventoryTranslator(45));
inventoryTranslators.put(WindowType.GENERIC_9X6, new DoubleChestInventoryTranslator(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());
inventoryTranslators.put(WindowType.BREWING_STAND, new BrewingStandInventoryTranslator());
inventoryTranslators.put(WindowType.ANVIL, new AnvilInventoryTranslator());
//inventoryTranslators.put(WindowType.ENCHANTMENT, new EnchantmentInventoryTranslator()); //TODO
InventoryTranslator furnace = new FurnaceInventoryTranslator();
inventoryTranslators.put(WindowType.FURNACE, furnace);
inventoryTranslators.put(WindowType.BLAST_FURNACE, furnace);
inventoryTranslators.put(WindowType.SMOKER, furnace);
inventoryTranslators.put(WindowType.GENERIC_3X3, new BlockInventoryTranslator(9, 23 << 4, ContainerType.DISPENSER));
inventoryTranslators.put(WindowType.HOPPER, new BlockInventoryTranslator(5, 154 << 4, ContainerType.HOPPER));
inventoryTranslators.put(WindowType.SHULKER_BOX, new BlockInventoryTranslator(36, 205 << 4, ContainerType.CONTAINER));
inventoryTranslators.put(WindowType.CRAFTING, new BlockInventoryTranslator(10, 58 << 4, ContainerType.WORKBENCH));
//inventoryTranslators.put(WindowType.BEACON, new BlockInventoryTranslator(1, 138 << 4, ContainerType.BEACON)); //TODO
}
}

View File

@ -38,7 +38,16 @@ public class BedrockContainerCloseTranslator extends PacketTranslator<ContainerC
@Override
public void translate(ContainerClosePacket packet, GeyserSession session) {
byte windowId = packet.getWindowId() == -1 ? 0 : packet.getWindowId(); //player inventory
byte windowId = packet.getWindowId();
if (windowId == -1) { //player inventory or crafting table
Inventory openInventory = session.getInventoryCache().getOpenInventory();
if (openInventory != null) {
windowId = (byte) openInventory.getId();
} else {
windowId = 0;
}
}
if (session.getReopeningWindow() != -1) {
Inventory inventory = session.getInventoryCache().getInventories().get(session.getReopeningWindow());
session.setReopeningWindow(-1);

View File

@ -26,6 +26,7 @@
package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.data.game.window.*;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientRenameItemPacket;
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;
@ -65,32 +66,60 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
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
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
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;
}
worldAction = action;
} else if (action.getSource().getContainerId() == ContainerId.CURSOR) {
if (cursorAction == null) {
cursorAction = action;
} else {
return;
}
cursorAction = action;
}
}
if (packet.getActions().size() == 2) {
List<InventoryAction> actions = packet.getActions();
if (inventory.getWindowType() == WindowType.ANVIL) {
InventoryAction anvilResult = null;
InventoryAction anvilInput = null;
for (InventoryAction action : packet.getActions()) {
if (action.getSource().getContainerId() == ContainerId.ANVIL_RESULT) {
anvilResult = action;
} else if (action.getSource().getContainerId() == ContainerId.CONTAINER_INPUT) {
anvilInput = action;
}
}
ItemData itemName = null;
if (anvilResult != null) {
itemName = anvilResult.getFromItem();
actions = new ArrayList<>(2);
for (InventoryAction action : packet.getActions()) { //packet sent by client when grabbing anvil output needs useless actions stripped
if (!(action.getSource().getContainerId() == ContainerId.CONTAINER_INPUT || action.getSource().getContainerId() == ContainerId.ANVIL_MATERIAL)) {
actions.add(action);
}
}
} else if (anvilInput != null) {
itemName = anvilInput.getToItem();
}
if (itemName != null) {
String rename;
com.nukkitx.nbt.tag.CompoundTag tag = itemName.getTag();
if (tag != null) {
rename = tag.getAsCompound("display").getAsString("Name");
} else {
rename = "";
}
ClientRenameItemPacket renameItemPacket = new ClientRenameItemPacket(rename);
session.getDownstream().getSession().send(renameItemPacket);
}
}
if (actions.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) {
for (InventoryAction action : actions) {
if (action != worldAction) {
containerAction = action;
break;
}
@ -125,8 +154,8 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
} 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)) {
for (InventoryAction action : actions) {
if (action != cursorAction) {
containerAction = action;
break;
}
@ -163,17 +192,17 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
return;
}
}
} else if (packet.getActions().stream().allMatch(p -> p.getSource().getType() == InventorySource.Type.CONTAINER || p.getSource().getType() == InventorySource.Type.UNTRACKED_INTERACTION_UI)) {
} else {
//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);
if (actions.get(0).getFromItem().getCount() > actions.get(0).getToItem().getCount()) {
fromAction = actions.get(0);
toAction = actions.get(1);
} else {
fromAction = packet.getActions().get(1);
toAction = packet.getActions().get(0);
fromAction = actions.get(1);
toAction = actions.get(0);
}
int fromSlot = translator.bedrockSlotToJava(fromAction);
int toSlot = translator.bedrockSlotToJava(toAction);
@ -211,18 +240,18 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
return;
}
}
} else if (packet.getActions().size() > 2) {
} else if (actions.size() > 2) {
//shift click or fill stack?
ItemData firstItem;
if (packet.getActions().get(0).getFromItem().getId() != 0) {
firstItem = packet.getActions().get(0).getFromItem();
if (actions.get(0).getFromItem().getId() != 0) {
firstItem = actions.get(0).getFromItem();
} else {
firstItem = packet.getActions().get(0).getToItem();
firstItem = actions.get(0).getToItem();
}
List<InventoryAction> sourceActions = new ArrayList<>(packet.getActions().size());
List<InventoryAction> destActions = new ArrayList<>(packet.getActions().size());
List<InventoryAction> sourceActions = new ArrayList<>(actions.size());
List<InventoryAction> destActions = new ArrayList<>(actions.size());
boolean sameItems = true;
for (InventoryAction action : packet.getActions()) {
for (InventoryAction action : actions) {
if (action.getFromItem().getCount() > action.getToItem().getCount()) {
if (!InventoryUtils.canCombine(action.getFromItem(), firstItem))
sameItems = false;
@ -238,7 +267,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
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()) {
for (InventoryAction action : actions) {
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

View File

@ -0,0 +1,60 @@
/*
* 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.ContainerId;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
public class AnvilInventoryTranslator extends BlockInventoryTranslator {
public AnvilInventoryTranslator() {
super(3, 145 << 4, ContainerType.ANVIL);
}
@Override
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 {
if (action.getSource().getContainerId() == ContainerId.ANVIL_RESULT) {
return 2;
} else {
return slotnum;
}
}
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return action.getSource().getContainerId() == ContainerId.ANVIL_RESULT;
}
}

View File

@ -29,7 +29,6 @@ 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.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
@ -38,9 +37,14 @@ 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);
public class BlockInventoryTranslator extends ContainerInventoryTranslator {
protected final int blockId;
protected final ContainerType containerType;
public BlockInventoryTranslator(int size, int blockId, ContainerType containerType) {
super(size);
this.blockId = blockId;
this.containerType = containerType;
}
@Override
@ -50,7 +54,7 @@ public class HopperInventoryTranslator extends InventoryTranslator {
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(154 << 4)); //hopper
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(blockId));
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
inventory.setHolderPosition(position);
@ -70,7 +74,7 @@ public class HopperInventoryTranslator extends InventoryTranslator {
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) ContainerType.HOPPER.id());
containerOpenPacket.setType((byte) containerType.id());
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.getUpstream().sendPacket(containerOpenPacket);
@ -87,13 +91,4 @@ public class HopperInventoryTranslator extends InventoryTranslator {
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,100 @@
/*
* 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.ContainerId;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
public class BrewingStandInventoryTranslator extends BlockInventoryTranslator {
public BrewingStandInventoryTranslator() {
super(5, 117 << 4, ContainerType.BREWING_STAND);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
//bedrock protocol library is currently missing property mappings for windows.
ContainerSetDataPacket dataPacket = new ContainerSetDataPacket();
dataPacket.setWindowId((byte) inventory.getId());
switch (key) {
case 0:
dataPacket.setProperty(ContainerSetDataPacket.Property.BREWING_STAND_BREW_TIME);
break;
case 1:
dataPacket.setProperty(ContainerSetDataPacket.Property.BREWING_STAND_FUEL_AMOUNT);
break;
default:
return;
}
dataPacket.setValue((short) value);
session.getUpstream().sendPacket(dataPacket);
}
@Override
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 {
switch (slotnum) {
case 0:
return 3;
case 1:
return 0;
case 2:
return 1;
case 3:
return 2;
default:
return slotnum;
}
}
}
@Override
public int javaSlotToBedrock(int slotnum) {
switch (slotnum) {
case 0:
return 1;
case 1:
return 2;
case 2:
return 3;
case 3:
return 0;
default:
return slotnum;
}
}
}

View File

@ -0,0 +1,103 @@
/*
* 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.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.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
public abstract class ContainerInventoryTranslator extends InventoryTranslator {
public ContainerInventoryTranslator(int size) {
super(size);
}
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
ItemData[] bedrockItems = new ItemData[this.size];
for (int i = 0; i < bedrockItems.length; i++) {
bedrockItems[javaSlotToBedrock(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);
}
@Override
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(javaSlotToBedrock(slot));
slotPacket.setSlot(TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[slot]));
session.getUpstream().sendPacket(slotPacket);
}
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
}
@Override
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;
}
}
@Override
public int javaSlotToBedrock(int slot) {
return slot;
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
}

View File

@ -1,99 +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.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.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
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);
CompoundTag tag = CompoundTag.EMPTY.toBuilder()
.intTag("x", position.getX())
.intTag("y", position.getY())
.intTag("z", position.getZ())
.stringTag("CustomName", inventory.getTitle()).buildRootTag();
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
dataPacket.setData(tag);
dataPacket.setBlockPosition(position);
session.getUpstream().sendPacket(dataPacket);
}
@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

@ -29,10 +29,8 @@ 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.ContainerType;
import com.nukkitx.protocol.bedrock.data.InventoryAction;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.inventory.Inventory;
@ -41,9 +39,9 @@ import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
public class DoubleChestInventoryTranslator extends InventoryTranslator {
public class DoubleChestInventoryTranslator extends BlockInventoryTranslator {
public DoubleChestInventoryTranslator(int size) {
super(size);
super(size, 54 << 4, ContainerType.CONTAINER);
}
@Override
@ -53,7 +51,7 @@ public class DoubleChestInventoryTranslator extends InventoryTranslator {
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(position);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(54 << 4)); //chest
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(blockId));
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
@ -73,7 +71,7 @@ public class DoubleChestInventoryTranslator extends InventoryTranslator {
blockPacket = new UpdateBlockPacket();
blockPacket.setDataLayer(0);
blockPacket.setBlockPosition(pairPosition);
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(54 << 4));
blockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(blockId));
blockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
session.getUpstream().sendPacket(blockPacket);
@ -93,16 +91,6 @@ public class DoubleChestInventoryTranslator extends InventoryTranslator {
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();
@ -124,15 +112,10 @@ public class DoubleChestInventoryTranslator extends InventoryTranslator {
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 4x9, and 5x9
final int paddedSize = 54;
ItemData[] bedrockItems = new ItemData[paddedSize];
//need to pad empty slots for 4x9 and 5x9
ItemData[] bedrockItems = new ItemData[54];
for (int i = 0; i < bedrockItems.length; i++) {
if (i <= this.size) {
bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
@ -151,9 +134,4 @@ public class DoubleChestInventoryTranslator extends InventoryTranslator {
}
TranslatorsInit.getInventoryTranslators().get(playerInventory.getWindowType()).updateInventory(session, playerInventory);
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
}

View File

@ -0,0 +1,41 @@
/*
* 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.ContainerType;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
public class EnchantmentInventoryTranslator extends BlockInventoryTranslator {
public EnchantmentInventoryTranslator() {
super(2, 116 << 4, ContainerType.ENCHANTMENT);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
}
}

View File

@ -25,56 +25,15 @@
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 com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
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 class FurnaceInventoryTranslator extends BlockInventoryTranslator {
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);
CompoundTag tag = CompoundTag.EMPTY.toBuilder()
.intTag("x", position.getX())
.intTag("y", position.getY())
.intTag("z", position.getZ())
.stringTag("CustomName", inventory.getTitle()).buildRootTag();
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
dataPacket.setData(tag);
dataPacket.setBlockPosition(position);
session.getUpstream().sendPacket(dataPacket);
}
@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);
super(3, 61 << 4, ContainerType.FURNACE);
}
@Override
@ -99,18 +58,6 @@ public class FurnaceInventoryTranslator extends InventoryTranslator {
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

@ -25,15 +25,9 @@
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;
@ -46,51 +40,9 @@ public abstract class InventoryTranslator {
public abstract void openInventory(GeyserSession session, Inventory inventory);
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 void updateInventory(GeyserSession session, Inventory inventory);
public abstract void updateSlot(GeyserSession session, Inventory inventory, int slot);
public abstract int bedrockSlotToJava(InventoryAction action);
public abstract int javaSlotToBedrock(int slot);
public abstract boolean isOutputSlot(InventoryAction action);
}

View File

@ -128,6 +128,11 @@ public class PlayerInventoryTranslator extends InventoryTranslator {
return slotnum;
}
@Override
public int javaSlotToBedrock(int slot) {
return slot;
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;

View File

@ -25,79 +25,22 @@
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 SingleChestInventoryTranslator extends InventoryTranslator {
public class SingleChestInventoryTranslator extends BlockInventoryTranslator {
public SingleChestInventoryTranslator(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);
inventory.setHolderPosition(position);
CompoundTag tag = CompoundTag.EMPTY.toBuilder()
.intTag("x", position.getX())
.intTag("y", position.getY())
.intTag("z", position.getZ())
.stringTag("CustomName", inventory.getTitle()).buildRootTag();
BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
dataPacket.setData(tag);
dataPacket.setBlockPosition(position);
session.getUpstream().sendPacket(dataPacket);
}
@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);
}
@Override
public void updateProperty(GeyserSession session, Inventory inventory, int key, int value) {
super(size, 54 << 4, ContainerType.CONTAINER);
}
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
//need to pad empty slots for 1x9 and 2x9
final int paddedSize = 27;
ItemData[] bedrockItems = new ItemData[paddedSize];
ItemData[] bedrockItems = new ItemData[27];
for (int i = 0; i < bedrockItems.length; i++) {
if (i <= this.size) {
bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]);
@ -116,9 +59,4 @@ public class SingleChestInventoryTranslator extends InventoryTranslator {
}
TranslatorsInit.getInventoryTranslators().get(playerInventory.getWindowType()).updateInventory(session, playerInventory);
}
@Override
public boolean isOutputSlot(InventoryAction action) {
return false;
}
}