2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.util;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
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;
|
2022-01-30 16:15:07 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient;
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundPickItemPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket;
|
2020-01-31 01:05:57 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2023-12-05 23:54:42 +00:00
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.math.vector.Vector3i;
|
|
|
|
import org.cloudburstmc.nbt.NbtMap;
|
|
|
|
import org.cloudburstmc.nbt.NbtMapBuilder;
|
|
|
|
import org.cloudburstmc.nbt.NbtType;
|
2023-06-03 09:47:50 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerId;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.InventorySlotPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.PlayerHotbarPacket;
|
2022-11-24 02:33:55 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.Container;
|
|
|
|
import org.geysermc.geyser.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
|
|
|
import org.geysermc.geyser.inventory.PlayerInventory;
|
2022-01-20 23:09:35 +00:00
|
|
|
import org.geysermc.geyser.inventory.click.Click;
|
2022-02-21 21:11:51 +00:00
|
|
|
import org.geysermc.geyser.inventory.recipe.GeyserRecipe;
|
|
|
|
import org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe;
|
|
|
|
import org.geysermc.geyser.inventory.recipe.GeyserShapelessRecipe;
|
2022-12-29 20:10:40 +00:00
|
|
|
import org.geysermc.geyser.item.Items;
|
2022-11-15 16:50:58 +00:00
|
|
|
import org.geysermc.geyser.level.BedrockDimension;
|
2022-02-21 21:11:51 +00:00
|
|
|
import org.geysermc.geyser.registry.Registries;
|
|
|
|
import org.geysermc.geyser.registry.type.ItemMapping;
|
2023-12-05 23:54:42 +00:00
|
|
|
import org.geysermc.geyser.registry.type.ItemMappings;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.ChatColor;
|
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
|
|
|
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.LecternInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.chest.DoubleChestInventoryTranslator;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
import java.util.Arrays;
|
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;
|
2021-07-13 01:19:40 +00:00
|
|
|
import java.util.function.IntFunction;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
|
|
|
public class InventoryUtils {
|
2021-07-13 01:19:40 +00:00
|
|
|
/**
|
|
|
|
* Stores the last used recipe network ID. Since 1.16.200 (and for server-authoritative inventories),
|
|
|
|
* each recipe needs a unique network ID (or else in .200 the client crashes).
|
|
|
|
*/
|
|
|
|
public static int LAST_RECIPE_NET_ID;
|
|
|
|
|
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
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void openInventory(GeyserSession session, Inventory inventory) {
|
2021-02-25 05:14:01 +00:00
|
|
|
session.setOpenInventory(inventory);
|
2021-12-07 19:51:58 +00:00
|
|
|
if (session.isClosingInventory() || !session.getUpstream().isInitialized()) {
|
|
|
|
// Wait for close confirmation from client before opening the new inventory.
|
|
|
|
// Handled in BedrockContainerCloseTranslator
|
|
|
|
// or - client hasn't yet loaded in; wait until inventory is shown
|
2021-02-25 05:14:01 +00:00
|
|
|
inventory.setPending(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
displayInventory(session, inventory);
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void displayInventory(GeyserSession session, Inventory inventory) {
|
2020-12-26 17:20:59 +00:00
|
|
|
InventoryTranslator translator = session.getInventoryTranslator();
|
2022-11-14 20:12:46 +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-08-18 00:57:46 +00:00
|
|
|
session.scheduleInEventLoop(() -> {
|
2021-08-17 00:39:29 +00:00
|
|
|
Inventory openInv = session.getOpenInventory();
|
2022-08-29 16:26:30 +00:00
|
|
|
if (openInv != null && openInv.getJavaId() == inventory.getJavaId()) {
|
2021-08-17 00:39:29 +00:00
|
|
|
translator.openInventory(session, inventory);
|
|
|
|
translator.updateInventory(session, inventory);
|
2023-03-03 14:09:52 +00:00
|
|
|
openInv.setDisplayed(true);
|
2021-08-17 00:39:29 +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);
|
|
|
|
}
|
|
|
|
}, 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);
|
2023-03-03 14:09:52 +00:00
|
|
|
inventory.setDisplayed(true);
|
2019-10-20 21:25:41 +00:00
|
|
|
}
|
2021-02-25 23:17:43 +00:00
|
|
|
} else {
|
|
|
|
session.setOpenInventory(null);
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:26:30 +00:00
|
|
|
public static void closeInventory(GeyserSession session, int javaId, 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);
|
|
|
|
|
2022-08-29 16:26:30 +00:00
|
|
|
Inventory inventory = getInventory(session, javaId);
|
2020-10-16 23:25:05 +00:00
|
|
|
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);
|
2023-03-03 14:09:52 +00:00
|
|
|
if (confirm && inventory.isDisplayed() && !inventory.isPending() && !(translator instanceof LecternInventoryTranslator)) {
|
2021-02-25 05:14:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-05 23:54:42 +00:00
|
|
|
public static @Nullable Inventory getInventory(GeyserSession session, int javaId) {
|
2022-08-29 16:26:30 +00:00
|
|
|
if (javaId == 0) {
|
2020-10-16 23:25:05 +00:00
|
|
|
return session.getPlayerInventory();
|
|
|
|
} else {
|
|
|
|
Inventory openInventory = session.getOpenInventory();
|
2022-08-29 16:26:30 +00:00
|
|
|
if (openInventory != null && javaId == openInventory.getJavaId()) {
|
2020-10-16 23:25:05 +00:00
|
|
|
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
|
|
|
|
2022-11-15 16:50:58 +00:00
|
|
|
/**
|
|
|
|
* Finds a usable block space in the world to place a fake inventory block, and returns the position.
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public static Vector3i findAvailableWorldSpace(GeyserSession session) {
|
|
|
|
// Check if a fake block can be placed, either above the player or beneath.
|
|
|
|
BedrockDimension dimension = session.getChunkCache().getBedrockDimension();
|
|
|
|
int minY = dimension.minY(), maxY = minY + dimension.height();
|
|
|
|
Vector3i flatPlayerPosition = session.getPlayerEntity().getPosition().toInt();
|
|
|
|
Vector3i position = flatPlayerPosition.add(Vector3i.UP);
|
|
|
|
if (position.getY() < minY) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (position.getY() >= maxY) {
|
|
|
|
position = flatPlayerPosition.sub(0, 4, 0);
|
|
|
|
if (position.getY() >= maxY) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void updateCursor(GeyserSession session) {
|
2020-01-31 01:05:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-25 03:49:10 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if an item stack represents air or has no count.
|
|
|
|
*/
|
|
|
|
public static boolean isEmpty(@Nullable ItemStack itemStack) {
|
2022-12-29 20:10:40 +00:00
|
|
|
return itemStack == null || itemStack.getId() == Items.AIR.javaId() || itemStack.getAmount() <= 0;
|
2022-02-25 03:49:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2021-07-13 01:19:40 +00:00
|
|
|
public static IntFunction<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
|
2021-11-20 23:29:46 +00:00
|
|
|
display.putString("Name", ChatColor.RESET + GeyserLocale.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-07-13 01:19:40 +00:00
|
|
|
return protocolVersion -> ItemData.builder()
|
2022-12-21 00:47:45 +00:00
|
|
|
.definition(getUnusableSpaceBlockDefinition(protocolVersion))
|
2021-04-06 04:14:06 +00:00
|
|
|
.count(1)
|
|
|
|
.tag(root.build()).build();
|
2020-05-06 21:05:03 +00:00
|
|
|
}
|
2020-09-15 00:52:16 +00:00
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
private static ItemDefinition getUnusableSpaceBlockDefinition(int protocolVersion) {
|
2023-12-05 23:54:42 +00:00
|
|
|
ItemMappings mappings = Registries.ITEMS.forVersion(protocolVersion);
|
2022-11-24 02:33:55 +00:00
|
|
|
String unusableSpaceBlock = GeyserImpl.getInstance().getConfig().getUnusableSpaceBlock();
|
2023-12-05 23:54:42 +00:00
|
|
|
ItemDefinition itemDefinition = mappings.getDefinition(unusableSpaceBlock);
|
2023-08-07 00:22:05 +00:00
|
|
|
|
|
|
|
if (itemDefinition == null) {
|
|
|
|
GeyserImpl.getInstance().getLogger().error("Invalid value " + unusableSpaceBlock + ". Resorting to barrier block.");
|
2023-12-05 23:54:42 +00:00
|
|
|
return mappings.getStoredItems().barrier().getBedrockDefinition();
|
2023-08-07 00:22:05 +00:00
|
|
|
} else {
|
|
|
|
return itemDefinition;
|
2022-11-24 02:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 02:42:17 +00:00
|
|
|
public static IntFunction<ItemData> getUpgradeTemplate() {
|
|
|
|
return protocolVersion -> ItemData.builder()
|
|
|
|
.definition(Registries.ITEMS.forVersion(protocolVersion).getStoredItems().upgradeTemplate().getBedrockDefinition())
|
|
|
|
.count(1).build();
|
|
|
|
}
|
|
|
|
|
2021-05-09 19:44:41 +00:00
|
|
|
/**
|
2021-11-22 19:52:26 +00:00
|
|
|
* See {@link #findOrCreateItem(GeyserSession, String)}. This is for finding a specified {@link ItemStack}.
|
2021-05-09 19:44:41 +00:00
|
|
|
*
|
|
|
|
* @param session the Bedrock client's session
|
|
|
|
* @param itemStack the item to try to find a match for. NBT will also be accounted for.
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void findOrCreateItem(GeyserSession session, ItemStack itemStack) {
|
2021-05-09 19:44:41 +00:00
|
|
|
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())) {
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundPickItemPacket packetToSend = new ServerboundPickItemPacket(i); // https://wiki.vg/Protocol#Pick_Item
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(packetToSend);
|
2021-05-09 19:44:41 +00:00
|
|
|
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);
|
|
|
|
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundSetCreativeModeSlotPacket actionPacket = new ServerboundSetCreativeModeSlotPacket(slot,
|
2021-05-09 19:44:41 +00:00
|
|
|
itemStack);
|
|
|
|
if ((slot - 36) != inventory.getHeldItemSlot()) {
|
|
|
|
setHotbarItem(session, slot);
|
|
|
|
}
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(actionPacket);
|
2021-05-09 19:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2023-12-05 23:54:42 +00:00
|
|
|
* <p>
|
2020-09-15 00:52:16 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2021-11-22 19:52:26 +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
|
2022-12-29 20:10:40 +00:00
|
|
|
if (!geyserItem.asItem().javaIdentifier().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
|
2022-12-29 20:10:40 +00:00
|
|
|
if (!geyserItem.asItem().javaIdentifier().equals(itemName)) {
|
2020-09-15 00:52:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundPickItemPacket packetToSend = new ServerboundPickItemPacket(i); // https://wiki.vg/Protocol#Pick_Item
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(packetToSend);
|
2020-09-15 00:52:16 +00:00
|
|
|
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
|
|
|
|
2022-12-29 20:10:40 +00:00
|
|
|
ItemMapping mapping = session.getItemMappings().getMapping(itemName); // TODO
|
2021-07-13 01:19:40 +00:00
|
|
|
if (mapping != null) {
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundSetCreativeModeSlotPacket actionPacket = new ServerboundSetCreativeModeSlotPacket(slot,
|
2022-12-29 20:10:40 +00:00
|
|
|
new ItemStack(mapping.getJavaItem().javaId()));
|
2020-12-23 17:32:39 +00:00
|
|
|
if ((slot - 36) != inventory.getHeldItemSlot()) {
|
2020-12-21 01:40:21 +00:00
|
|
|
setHotbarItem(session, slot);
|
|
|
|
}
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(actionPacket);
|
2020-12-21 01:40:21 +00:00
|
|
|
} else {
|
2021-11-20 21:34:30 +00:00
|
|
|
session.getGeyser().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
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
private static void setHotbarItem(GeyserSession session, int slot) {
|
2020-09-15 00:52:16 +00:00
|
|
|
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
|
|
|
}
|
2022-01-20 23:09:35 +00:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static Click getClickForHotbarSwap(int slot) {
|
|
|
|
return switch (slot) {
|
|
|
|
case 0 -> Click.SWAP_TO_HOTBAR_1;
|
|
|
|
case 1 -> Click.SWAP_TO_HOTBAR_2;
|
|
|
|
case 2 -> Click.SWAP_TO_HOTBAR_3;
|
|
|
|
case 3 -> Click.SWAP_TO_HOTBAR_4;
|
|
|
|
case 4 -> Click.SWAP_TO_HOTBAR_5;
|
|
|
|
case 5 -> Click.SWAP_TO_HOTBAR_6;
|
|
|
|
case 6 -> Click.SWAP_TO_HOTBAR_7;
|
|
|
|
case 7 -> Click.SWAP_TO_HOTBAR_8;
|
|
|
|
case 8 -> Click.SWAP_TO_HOTBAR_9;
|
|
|
|
default -> null;
|
|
|
|
};
|
|
|
|
}
|
2022-01-30 16:15:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test all known recipes to find a valid match
|
|
|
|
*
|
|
|
|
* @param output if not null, the recipe has to output this item
|
|
|
|
*/
|
|
|
|
@Nullable
|
2022-02-21 21:11:51 +00:00
|
|
|
public static GeyserRecipe getValidRecipe(final GeyserSession session, final @Nullable ItemStack output, final IntFunction<GeyserItemStack> inventoryGetter,
|
2022-01-30 16:15:07 +00:00
|
|
|
final int gridDimensions, final int firstRow, final int height, final int firstCol, final int width) {
|
|
|
|
int nonAirCount = 0; // Used for shapeless recipes for amount of items needed in recipe
|
|
|
|
for (int row = firstRow; row < height + firstRow; row++) {
|
|
|
|
for (int col = firstCol; col < width + firstCol; col++) {
|
|
|
|
if (!inventoryGetter.apply(col + (row * gridDimensions) + 1).isEmpty()) {
|
|
|
|
nonAirCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recipes:
|
2022-02-21 21:11:51 +00:00
|
|
|
for (GeyserRecipe recipe : session.getCraftingRecipes().values()) {
|
|
|
|
if (recipe.isShaped()) {
|
|
|
|
GeyserShapedRecipe shapedRecipe = (GeyserShapedRecipe) recipe;
|
|
|
|
if (output != null && !shapedRecipe.result().equals(output)) {
|
2022-01-30 16:15:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-21 21:11:51 +00:00
|
|
|
Ingredient[] ingredients = shapedRecipe.ingredients();
|
|
|
|
if (shapedRecipe.width() != width || shapedRecipe.height() != height || width * height != ingredients.length) {
|
2022-01-30 16:15:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!testShapedRecipe(ingredients, inventoryGetter, gridDimensions, firstRow, height, firstCol, width)) {
|
|
|
|
Ingredient[] mirroredIngredients = new Ingredient[ingredients.length];
|
|
|
|
for (int row = 0; row < height; row++) {
|
|
|
|
for (int col = 0; col < width; col++) {
|
|
|
|
mirroredIngredients[col + (row * width)] = ingredients[(width - 1 - col) + (row * width)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Arrays.equals(ingredients, mirroredIngredients) ||
|
|
|
|
!testShapedRecipe(mirroredIngredients, inventoryGetter, gridDimensions, firstRow, height, firstCol, width)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 21:11:51 +00:00
|
|
|
} else {
|
|
|
|
GeyserShapelessRecipe data = (GeyserShapelessRecipe) recipe;
|
|
|
|
if (output != null && !data.result().equals(output)) {
|
2022-01-30 16:15:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-21 21:11:51 +00:00
|
|
|
if (nonAirCount != data.ingredients().length) {
|
2022-01-30 16:15:07 +00:00
|
|
|
// There is an amount of items on the crafting table that is not the same as the ingredient count so this is invalid
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-21 21:11:51 +00:00
|
|
|
for (int i = 0; i < data.ingredients().length; i++) {
|
|
|
|
Ingredient ingredient = data.ingredients()[i];
|
2022-01-30 16:15:07 +00:00
|
|
|
for (ItemStack itemStack : ingredient.getOptions()) {
|
|
|
|
boolean inventoryHasItem = false;
|
|
|
|
// Iterate only over the crafting table to find this item
|
|
|
|
crafting:
|
|
|
|
for (int row = firstRow; row < height + firstRow; row++) {
|
|
|
|
for (int col = firstCol; col < width + firstCol; col++) {
|
|
|
|
GeyserItemStack geyserItemStack = inventoryGetter.apply(col + (row * gridDimensions) + 1);
|
|
|
|
if (geyserItemStack.isEmpty()) {
|
2023-12-05 23:54:42 +00:00
|
|
|
//noinspection ConstantValue
|
2022-01-30 16:15:07 +00:00
|
|
|
inventoryHasItem = itemStack == null || itemStack.getId() == 0;
|
|
|
|
if (inventoryHasItem) {
|
|
|
|
break crafting;
|
|
|
|
}
|
|
|
|
} else if (itemStack.equals(geyserItemStack.getItemStack(1))) {
|
|
|
|
inventoryHasItem = true;
|
|
|
|
break crafting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inventoryHasItem) {
|
|
|
|
continue recipes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 21:11:51 +00:00
|
|
|
return recipe;
|
2022-01-30 16:15:07 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-05 23:54:42 +00:00
|
|
|
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
2022-01-30 16:15:07 +00:00
|
|
|
private static boolean testShapedRecipe(final Ingredient[] ingredients, final IntFunction<GeyserItemStack> inventoryGetter,
|
|
|
|
final int gridDimensions, final int firstRow, final int height, final int firstCol, final int width) {
|
|
|
|
int ingredientIndex = 0;
|
|
|
|
for (int row = firstRow; row < height + firstRow; row++) {
|
|
|
|
for (int col = firstCol; col < width + firstCol; col++) {
|
|
|
|
GeyserItemStack geyserItemStack = inventoryGetter.apply(col + (row * gridDimensions) + 1);
|
|
|
|
Ingredient ingredient = ingredients[ingredientIndex++];
|
|
|
|
if (ingredient.getOptions().length == 0) {
|
|
|
|
if (!geyserItemStack.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
boolean inventoryHasItem = false;
|
|
|
|
for (ItemStack item : ingredient.getOptions()) {
|
|
|
|
if (Objects.equals(geyserItemStack.getItemStack(1), item)) {
|
|
|
|
inventoryHasItem = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inventoryHasItem) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|