2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +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
|
|
|
|
*/
|
|
|
|
|
2019-07-29 22:20:48 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
2020-09-15 00:52:16 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientMoveItemToHotbarPacket;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NbtMap;
|
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
|
|
|
import com.nukkitx.nbt.NbtType;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerId;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket;
|
2020-09-15 00:52:16 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.PlayerHotbarPacket;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-09-15 00:52:16 +00:00
|
|
|
import org.geysermc.connector.common.ChatColor;
|
2021-02-25 23:17:43 +00:00
|
|
|
import org.geysermc.connector.inventory.Container;
|
|
|
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
|
|
|
import org.geysermc.connector.inventory.PlayerInventory;
|
2019-07-29 22:20:48 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
|
2021-02-25 05:14:01 +00:00
|
|
|
import org.geysermc.connector.network.translators.inventory.translators.LecternInventoryTranslator;
|
2020-12-23 17:32:39 +00:00
|
|
|
import org.geysermc.connector.network.translators.inventory.translators.chest.DoubleChestInventoryTranslator;
|
2020-09-15 00:52:16 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
2020-05-25 01:07:05 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemRegistry;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2020-05-06 21:05:03 +00:00
|
|
|
import java.util.Collections;
|
2020-01-31 01:05:57 +00:00
|
|
|
import java.util.Objects;
|
2019-07-29 22:20:48 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
public class InventoryUtils {
|
2020-10-16 23:25:05 +00:00
|
|
|
public static final ItemStack REFRESH_ITEM = new ItemStack(1, 127, new CompoundTag(""));
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-10-21 03:41:46 +00:00
|
|
|
public static void openInventory(GeyserSession session, Inventory inventory) {
|
2021-02-25 05:14:01 +00:00
|
|
|
session.setOpenInventory(inventory);
|
|
|
|
if (session.isClosingInventory()) {
|
|
|
|
//Wait for close confirmation from client before opening the new inventory.
|
|
|
|
//Handled in BedrockContainerCloseTranslator
|
|
|
|
inventory.setPending(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
displayInventory(session, inventory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void displayInventory(GeyserSession session, Inventory inventory) {
|
2020-12-26 17:20:59 +00:00
|
|
|
InventoryTranslator translator = session.getInventoryTranslator();
|
2019-10-20 21:25:41 +00:00
|
|
|
if (translator != null) {
|
|
|
|
translator.prepareInventory(session, inventory);
|
2021-02-25 05:14:01 +00:00
|
|
|
if (translator instanceof DoubleChestInventoryTranslator && !((Container) inventory).isUsingRealBlock()) {
|
2021-02-25 23:17:43 +00:00
|
|
|
GeyserConnector.getInstance().getGeneralThreadPool().schedule(() ->
|
2021-02-25 05:14:01 +00:00
|
|
|
session.addInventoryTask(() -> {
|
|
|
|
Inventory openInv = session.getOpenInventory();
|
|
|
|
if (openInv != null && openInv.getId() == inventory.getId()) {
|
|
|
|
translator.openInventory(session, inventory);
|
|
|
|
translator.updateInventory(session, inventory);
|
2021-03-04 17:26:16 +00:00
|
|
|
} else if (openInv != null && openInv.isPending()) {
|
|
|
|
// Presumably, this inventory is no longer relevant, and the client doesn't care about it
|
|
|
|
displayInventory(session, openInv);
|
2021-02-25 05:14:01 +00:00
|
|
|
}
|
2021-02-25 23:17:43 +00:00
|
|
|
}), 200, TimeUnit.MILLISECONDS);
|
2019-10-20 21:25:41 +00:00
|
|
|
} else {
|
|
|
|
translator.openInventory(session, inventory);
|
2019-10-21 03:41:46 +00:00
|
|
|
translator.updateInventory(session, inventory);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
2021-02-25 23:17:43 +00:00
|
|
|
} else {
|
|
|
|
// Precaution - as of 1.16 every inventory should be translated so this shouldn't happen
|
|
|
|
session.setOpenInventory(null);
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 05:14:01 +00:00
|
|
|
public static void closeInventory(GeyserSession session, int windowId, boolean confirm) {
|
2021-01-01 02:39:54 +00:00
|
|
|
session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY, session);
|
2020-10-16 23:25:05 +00:00
|
|
|
updateCursor(session);
|
|
|
|
|
|
|
|
Inventory inventory = getInventory(session, windowId);
|
|
|
|
if (inventory != null) {
|
2020-12-26 17:20:59 +00:00
|
|
|
InventoryTranslator translator = session.getInventoryTranslator();
|
2020-10-16 23:25:05 +00:00
|
|
|
translator.closeInventory(session, inventory);
|
2021-02-25 05:14:01 +00:00
|
|
|
if (confirm && !inventory.isPending() && !(translator instanceof LecternInventoryTranslator)) {
|
|
|
|
session.setClosingInventory(true);
|
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
2020-12-26 17:20:59 +00:00
|
|
|
session.setInventoryTranslator(InventoryTranslator.PLAYER_INVENTORY_TRANSLATOR);
|
2020-10-16 23:25:05 +00:00
|
|
|
session.setOpenInventory(null);
|
2020-06-02 16:48:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
public static Inventory getInventory(GeyserSession session, int windowId) {
|
|
|
|
if (windowId == 0) {
|
|
|
|
return session.getPlayerInventory();
|
|
|
|
} else {
|
|
|
|
Inventory openInventory = session.getOpenInventory();
|
|
|
|
if (openInventory != null && windowId == openInventory.getId()) {
|
|
|
|
return openInventory;
|
|
|
|
}
|
|
|
|
return null;
|
2020-06-02 16:48:26 +00:00
|
|
|
}
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
2020-01-31 01:05:57 +00:00
|
|
|
|
|
|
|
public static void updateCursor(GeyserSession session) {
|
|
|
|
InventorySlotPacket cursorPacket = new InventorySlotPacket();
|
2020-07-08 16:36:26 +00:00
|
|
|
cursorPacket.setContainerId(ContainerId.UI);
|
2020-02-26 07:31:55 +00:00
|
|
|
cursorPacket.setSlot(0);
|
2020-10-16 23:25:05 +00:00
|
|
|
cursorPacket.setItem(session.getPlayerInventory().getCursor().getItemData(session));
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(cursorPacket);
|
2020-01-31 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
public static boolean canStack(GeyserItemStack item1, GeyserItemStack item2) {
|
|
|
|
if (item1.isEmpty() || item2.isEmpty())
|
|
|
|
return false;
|
2020-12-30 00:59:22 +00:00
|
|
|
return item1.getJavaId() == item2.getJavaId() && Objects.equals(item1.getNbt(), item2.getNbt());
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 01:05:57 +00:00
|
|
|
public static boolean canStack(ItemStack item1, ItemStack item2) {
|
|
|
|
if (item1 == null || item2 == null)
|
|
|
|
return false;
|
|
|
|
return item1.getId() == item2.getId() && Objects.equals(item1.getNbt(), item2.getNbt());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canStack(ItemData item1, ItemData item2) {
|
|
|
|
if (item1 == null || item2 == null)
|
|
|
|
return false;
|
|
|
|
return item1.equals(item2, false, true, true);
|
|
|
|
}
|
2020-05-06 21:05:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a barrier block with custom name and lore to explain why
|
|
|
|
* part of the inventory is unusable.
|
2020-08-08 22:56:15 +00:00
|
|
|
*
|
|
|
|
* @param description the description
|
|
|
|
* @return the unusable space block
|
2020-05-06 21:05:03 +00:00
|
|
|
*/
|
|
|
|
public static ItemData createUnusableSpaceBlock(String description) {
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMapBuilder root = NbtMap.builder();
|
|
|
|
NbtMapBuilder display = NbtMap.builder();
|
2020-05-06 21:05:03 +00:00
|
|
|
|
2020-07-05 23:35:51 +00:00
|
|
|
// Not ideal to use log here but we dont get a session
|
|
|
|
display.putString("Name", ChatColor.RESET + LanguageUtils.getLocaleStringLog("geyser.inventory.unusable_item.name"));
|
2020-07-05 20:58:43 +00:00
|
|
|
display.putList("Lore", NbtType.STRING, Collections.singletonList(ChatColor.RESET + ChatColor.DARK_PURPLE + description));
|
2020-05-06 21:05:03 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
root.put("display", display.build());
|
2021-04-06 04:14:06 +00:00
|
|
|
return ItemData.builder()
|
|
|
|
.id(ItemRegistry.ITEM_ENTRIES.get(ItemRegistry.BARRIER_INDEX).getBedrockId())
|
|
|
|
.count(1)
|
|
|
|
.tag(root.build()).build();
|
2020-05-06 21:05:03 +00:00
|
|
|
}
|
2020-09-15 00:52:16 +00:00
|
|
|
|
2021-05-09 19:44:41 +00:00
|
|
|
/**
|
|
|
|
* See {@link #findOrCreateItem(GeyserSession, String)}. This is for finding a specified {@link ItemStack}.
|
|
|
|
*
|
|
|
|
* @param session the Bedrock client's session
|
|
|
|
* @param itemStack the item to try to find a match for. NBT will also be accounted for.
|
|
|
|
*/
|
|
|
|
public static void findOrCreateItem(GeyserSession session, ItemStack itemStack) {
|
|
|
|
PlayerInventory inventory = session.getPlayerInventory();
|
|
|
|
|
|
|
|
if (itemStack == null || itemStack.getId() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check hotbar for item
|
|
|
|
for (int i = 36; i < 45; i++) {
|
|
|
|
GeyserItemStack geyserItem = inventory.getItem(i);
|
|
|
|
if (geyserItem.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If this is the item we're looking for
|
|
|
|
if (geyserItem.getJavaId() == itemStack.getId() && Objects.equals(geyserItem.getNbt(), itemStack.getNbt())) {
|
|
|
|
setHotbarItem(session, i);
|
|
|
|
// Don't check inventory if item was in hotbar
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check inventory for item
|
|
|
|
for (int i = 9; i < 36; i++) {
|
|
|
|
GeyserItemStack geyserItem = inventory.getItem(i);
|
|
|
|
if (geyserItem.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If this is the item we're looking for
|
|
|
|
if (geyserItem.getJavaId() == itemStack.getId() && Objects.equals(geyserItem.getNbt(), itemStack.getNbt())) {
|
|
|
|
ClientMoveItemToHotbarPacket packetToSend = new ClientMoveItemToHotbarPacket(i); // https://wiki.vg/Protocol#Pick_Item
|
|
|
|
session.sendDownstreamPacket(packetToSend);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have not found the item, and we're in creative, ask for the item from the server.
|
|
|
|
if (session.getGameMode() == GameMode.CREATIVE) {
|
|
|
|
int slot = findEmptyHotbarSlot(inventory);
|
|
|
|
|
|
|
|
ClientCreativeInventoryActionPacket actionPacket = new ClientCreativeInventoryActionPacket(slot,
|
|
|
|
itemStack);
|
|
|
|
if ((slot - 36) != inventory.getHeldItemSlot()) {
|
|
|
|
setHotbarItem(session, slot);
|
|
|
|
}
|
|
|
|
session.sendDownstreamPacket(actionPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 00:52:16 +00:00
|
|
|
/**
|
|
|
|
* Attempt to find the specified item name in the session's inventory.
|
|
|
|
* If it is found and in the hotbar, set the user's held item to that slot.
|
|
|
|
* If it is found in another part of the inventory, move it.
|
|
|
|
* If it is not found and the user is in creative mode, create the item,
|
|
|
|
* overriding the current item slot if no other hotbar slots are empty, or otherwise selecting the empty slot.
|
|
|
|
*
|
|
|
|
* This attempts to mimic Java Edition behavior as best as it can.
|
|
|
|
* @param session the Bedrock client's session
|
|
|
|
* @param itemName the Java identifier of the item to search/select
|
|
|
|
*/
|
2020-12-29 23:09:42 +00:00
|
|
|
public static void findOrCreateItem(GeyserSession session, String itemName) {
|
2020-09-15 00:52:16 +00:00
|
|
|
// Get the inventory to choose a slot to pick
|
2020-10-16 23:25:05 +00:00
|
|
|
PlayerInventory inventory = session.getPlayerInventory();
|
2020-09-15 00:52:16 +00:00
|
|
|
|
2020-12-29 23:09:42 +00:00
|
|
|
if (itemName.equals("minecraft:air")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-15 00:52:16 +00:00
|
|
|
// Check hotbar for item
|
|
|
|
for (int i = 36; i < 45; i++) {
|
2020-10-16 23:25:05 +00:00
|
|
|
GeyserItemStack geyserItem = inventory.getItem(i);
|
|
|
|
if (geyserItem.isEmpty()) {
|
2020-09-15 00:52:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If this isn't the item we're looking for
|
2020-10-16 23:25:05 +00:00
|
|
|
if (!geyserItem.getItemEntry().getJavaIdentifier().equals(itemName)) {
|
2020-09-15 00:52:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHotbarItem(session, i);
|
|
|
|
// Don't check inventory if item was in hotbar
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check inventory for item
|
|
|
|
for (int i = 9; i < 36; i++) {
|
2020-10-16 23:25:05 +00:00
|
|
|
GeyserItemStack geyserItem = inventory.getItem(i);
|
|
|
|
if (geyserItem.isEmpty()) {
|
2020-09-15 00:52:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If this isn't the item we're looking for
|
2020-10-16 23:25:05 +00:00
|
|
|
if (!geyserItem.getItemEntry().getJavaIdentifier().equals(itemName)) {
|
2020-09-15 00:52:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientMoveItemToHotbarPacket packetToSend = new ClientMoveItemToHotbarPacket(i); // https://wiki.vg/Protocol#Pick_Item
|
|
|
|
session.sendDownstreamPacket(packetToSend);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have not found the item, and we're in creative, ask for the item from the server.
|
|
|
|
if (session.getGameMode() == GameMode.CREATIVE) {
|
2021-05-09 19:44:41 +00:00
|
|
|
int slot = findEmptyHotbarSlot(inventory);
|
2020-09-15 00:52:16 +00:00
|
|
|
|
2020-12-21 01:40:21 +00:00
|
|
|
ItemEntry entry = ItemRegistry.getItemEntry(itemName);
|
|
|
|
if (entry != null) {
|
|
|
|
ClientCreativeInventoryActionPacket actionPacket = new ClientCreativeInventoryActionPacket(slot,
|
|
|
|
new ItemStack(entry.getJavaId()));
|
2020-12-23 17:32:39 +00:00
|
|
|
if ((slot - 36) != inventory.getHeldItemSlot()) {
|
2020-12-21 01:40:21 +00:00
|
|
|
setHotbarItem(session, slot);
|
|
|
|
}
|
|
|
|
session.sendDownstreamPacket(actionPacket);
|
|
|
|
} else {
|
|
|
|
session.getConnector().getLogger().debug("Cannot find item for block " + itemName);
|
2020-09-15 00:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 19:44:41 +00:00
|
|
|
/**
|
|
|
|
* @return the first empty slot found in this inventory, or else the player's currently held slot.
|
|
|
|
*/
|
|
|
|
private static int findEmptyHotbarSlot(PlayerInventory inventory) {
|
|
|
|
int slot = inventory.getHeldItemSlot() + 36;
|
|
|
|
if (!inventory.getItemInHand().isEmpty()) { // Otherwise we should just use the current slot
|
|
|
|
for (int i = 36; i < 45; i++) {
|
|
|
|
if (inventory.getItem(i).isEmpty()) {
|
|
|
|
slot = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2020-09-15 00:52:16 +00:00
|
|
|
/**
|
|
|
|
* Changes the held item slot to the specified slot
|
|
|
|
* @param session GeyserSession
|
|
|
|
* @param slot inventory slot to be selected
|
|
|
|
*/
|
|
|
|
private static void setHotbarItem(GeyserSession session, int slot) {
|
|
|
|
PlayerHotbarPacket hotbarPacket = new PlayerHotbarPacket();
|
|
|
|
hotbarPacket.setContainerId(0);
|
|
|
|
// Java inventory slot to hotbar slot ID
|
|
|
|
hotbarPacket.setSelectedHotbarSlot(slot - 36);
|
|
|
|
hotbarPacket.setSelectHotbarSlot(true);
|
|
|
|
session.sendUpstreamPacket(hotbarPacket);
|
2020-09-18 03:07:20 +00:00
|
|
|
// No need to send a Java packet as Bedrock sends a confirmation packet back that we translate
|
2020-09-15 00:52:16 +00:00
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|