2019-10-20 21:25:41 +00:00
|
|
|
/*
|
2020-01-31 01:05:57 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-10-20 21:25:41 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket;
|
2020-04-25 22:33:52 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ContainerId;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.InventoryActionData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.InventorySource;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.ItemData;
|
2019-10-20 21:25:41 +00:00
|
|
|
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;
|
2020-04-02 22:01:17 +00:00
|
|
|
import org.geysermc.connector.network.translators.Translators;
|
2020-02-26 07:31:55 +00:00
|
|
|
import org.geysermc.connector.network.translators.inventory.action.InventoryActionDataTranslator;
|
2020-01-31 01:05:57 +00:00
|
|
|
import org.geysermc.connector.utils.InventoryUtils;
|
2020-04-25 22:33:52 +00:00
|
|
|
import org.geysermc.connector.utils.Toolbox;
|
2020-01-31 01:05:57 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
2019-10-20 21:25:41 +00:00
|
|
|
|
|
|
|
public class PlayerInventoryTranslator extends InventoryTranslator {
|
2020-04-25 22:29:26 +00:00
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
public PlayerInventoryTranslator() {
|
2019-10-22 23:31:03 +00:00
|
|
|
super(46);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateInventory(GeyserSession session, Inventory inventory) {
|
2020-04-25 22:33:52 +00:00
|
|
|
updateCraftingGrid(session, inventory);
|
2020-01-31 01:05:57 +00:00
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
InventoryContentPacket inventoryContentPacket = new InventoryContentPacket();
|
|
|
|
inventoryContentPacket.setContainerId(ContainerId.INVENTORY);
|
|
|
|
ItemData[] contents = new ItemData[36];
|
|
|
|
// Inventory
|
|
|
|
for (int i = 9; i < 36; i++) {
|
2020-04-22 21:26:16 +00:00
|
|
|
contents[i] = Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(i));
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
// Hotbar
|
|
|
|
for (int i = 36; i < 45; i++) {
|
2020-04-22 21:26:16 +00:00
|
|
|
contents[i - 36] = Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(i));
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
inventoryContentPacket.setContents(contents);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(inventoryContentPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
|
|
|
|
// Armor
|
|
|
|
InventoryContentPacket armorContentPacket = new InventoryContentPacket();
|
|
|
|
armorContentPacket.setContainerId(ContainerId.ARMOR);
|
|
|
|
contents = new ItemData[4];
|
|
|
|
for (int i = 5; i < 9; i++) {
|
2020-04-22 21:26:16 +00:00
|
|
|
contents[i - 5] = Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(i));
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
armorContentPacket.setContents(contents);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(armorContentPacket);
|
2019-10-22 23:31:03 +00:00
|
|
|
|
|
|
|
// Offhand
|
|
|
|
InventoryContentPacket offhandPacket = new InventoryContentPacket();
|
|
|
|
offhandPacket.setContainerId(ContainerId.OFFHAND);
|
2020-04-22 21:26:16 +00:00
|
|
|
offhandPacket.setContents(new ItemData[]{Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(45))});
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(offhandPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 22:33:52 +00:00
|
|
|
/**
|
|
|
|
* Update the crafting grid for the player to hide/show the barriers in the creative inventory
|
|
|
|
* @param session Session of the player
|
|
|
|
* @param inventory Inventory of the player
|
|
|
|
*/
|
|
|
|
public static void updateCraftingGrid(GeyserSession session, Inventory inventory) {
|
|
|
|
// Crafting grid
|
|
|
|
for (int i = 1; i < 5; i++) {
|
|
|
|
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
|
|
|
slotPacket.setContainerId(ContainerId.CURSOR);
|
|
|
|
slotPacket.setSlot(i + 27);
|
|
|
|
|
|
|
|
if (session.getGameMode() == GameMode.CREATIVE) {
|
|
|
|
slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(session, new ItemStack(Toolbox.BARRIER_INDEX)));
|
|
|
|
}else{
|
|
|
|
slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(i)));
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(slotPacket);
|
2020-04-25 22:33:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
@Override
|
|
|
|
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
|
2020-01-31 01:05:57 +00:00
|
|
|
if (slot >= 1 && slot <= 44) {
|
2019-10-20 21:25:41 +00:00
|
|
|
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
|
|
|
if (slot >= 9) {
|
|
|
|
slotPacket.setContainerId(ContainerId.INVENTORY);
|
|
|
|
if (slot >= 36) {
|
2020-02-26 07:31:55 +00:00
|
|
|
slotPacket.setSlot(slot - 36);
|
2019-10-20 21:25:41 +00:00
|
|
|
} else {
|
2020-02-26 07:31:55 +00:00
|
|
|
slotPacket.setSlot(slot);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
2020-01-31 01:05:57 +00:00
|
|
|
} else if (slot >= 5) {
|
2019-10-20 21:25:41 +00:00
|
|
|
slotPacket.setContainerId(ContainerId.ARMOR);
|
2020-02-26 07:31:55 +00:00
|
|
|
slotPacket.setSlot(slot - 5);
|
2020-01-31 01:05:57 +00:00
|
|
|
} else {
|
|
|
|
slotPacket.setContainerId(ContainerId.CURSOR);
|
2020-02-26 07:31:55 +00:00
|
|
|
slotPacket.setSlot(slot + 27);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
2020-04-22 21:26:16 +00:00
|
|
|
slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(slot)));
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(slotPacket);
|
2019-10-22 23:31:03 +00:00
|
|
|
} else if (slot == 45) {
|
|
|
|
InventoryContentPacket offhandPacket = new InventoryContentPacket();
|
|
|
|
offhandPacket.setContainerId(ContainerId.OFFHAND);
|
2020-04-22 21:26:16 +00:00
|
|
|
offhandPacket.setContents(new ItemData[]{Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(slot))});
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(offhandPacket);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-26 07:31:55 +00:00
|
|
|
public int bedrockSlotToJava(InventoryActionData action) {
|
2019-10-20 21:25:41 +00:00
|
|
|
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;
|
2019-10-22 23:31:03 +00:00
|
|
|
case ContainerId.OFFHAND:
|
|
|
|
return 45;
|
2019-11-28 03:55:58 +00:00
|
|
|
case ContainerId.CURSOR:
|
|
|
|
if (slotnum >= 28 && 31 >= slotnum) {
|
|
|
|
return slotnum - 27;
|
|
|
|
} else if (slotnum == 50) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-01 09:26:11 +00:00
|
|
|
break;
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
return slotnum;
|
|
|
|
}
|
|
|
|
|
2019-11-05 23:17:22 +00:00
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
@Override
|
2019-12-01 09:26:11 +00:00
|
|
|
public SlotType getSlotType(int javaSlot) {
|
|
|
|
if (javaSlot == 0)
|
|
|
|
return SlotType.OUTPUT;
|
|
|
|
return SlotType.NORMAL;
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
@Override
|
2020-02-26 07:31:55 +00:00
|
|
|
public void translateActions(GeyserSession session, Inventory inventory, List<InventoryActionData> actions) {
|
2020-01-31 01:05:57 +00:00
|
|
|
if (session.getGameMode() == GameMode.CREATIVE) {
|
|
|
|
//crafting grid is not visible in creative mode in java edition
|
2020-02-26 07:31:55 +00:00
|
|
|
for (InventoryActionData action : actions) {
|
2020-01-31 01:05:57 +00:00
|
|
|
if (action.getSource().getContainerId() == ContainerId.CURSOR && (action.getSlot() >= 28 && 31 >= action.getSlot())) {
|
|
|
|
updateInventory(session, inventory);
|
|
|
|
InventoryUtils.updateCursor(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack javaItem;
|
2020-02-26 07:31:55 +00:00
|
|
|
for (InventoryActionData action : actions) {
|
2020-01-31 01:05:57 +00:00
|
|
|
switch (action.getSource().getContainerId()) {
|
|
|
|
case ContainerId.INVENTORY:
|
|
|
|
case ContainerId.ARMOR:
|
|
|
|
case ContainerId.OFFHAND:
|
|
|
|
int javaSlot = bedrockSlotToJava(action);
|
|
|
|
if (action.getToItem().getId() == 0) {
|
|
|
|
javaItem = new ItemStack(-1, 0, null);
|
|
|
|
} else {
|
2020-04-22 21:26:16 +00:00
|
|
|
javaItem = Translators.getItemTranslator().translateToJava(session, action.getToItem());
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
2020-01-31 08:15:18 +00:00
|
|
|
ClientCreativeInventoryActionPacket creativePacket = new ClientCreativeInventoryActionPacket(javaSlot, javaItem);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(creativePacket);
|
2020-01-31 01:05:57 +00:00
|
|
|
inventory.setItem(javaSlot, javaItem);
|
|
|
|
break;
|
|
|
|
case ContainerId.CURSOR:
|
|
|
|
if (action.getSlot() == 0) {
|
2020-04-22 21:26:16 +00:00
|
|
|
session.getInventory().setCursor(Translators.getItemTranslator().translateToJava(session, action.getToItem()));
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ContainerId.NONE:
|
|
|
|
if (action.getSource().getType() == InventorySource.Type.WORLD_INTERACTION
|
|
|
|
&& action.getSource().getFlag() == InventorySource.Flag.DROP_ITEM) {
|
2020-04-22 21:26:16 +00:00
|
|
|
javaItem = Translators.getItemTranslator().translateToJava(session, action.getToItem());
|
2020-01-31 08:15:18 +00:00
|
|
|
ClientCreativeInventoryActionPacket creativeDropPacket = new ClientCreativeInventoryActionPacket(-1, javaItem);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendDownstreamPacket(creativeDropPacket);
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-26 07:31:55 +00:00
|
|
|
InventoryActionDataTranslator.translate(this, session, inventory, actions);
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
@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) {
|
|
|
|
}
|
|
|
|
}
|