2019-07-29 22:20:48 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2019-07-29 22:20:48 +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.translator.inventory;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
2022-01-30 16:15:07 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
|
2021-01-13 01:37:12 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient;
|
2021-03-15 18:49:44 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.IntTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
2021-01-13 01:37:12 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.*;
|
2020-01-31 01:05:57 +00:00
|
|
|
import lombok.AllArgsConstructor;
|
2021-12-04 20:35:01 +00:00
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.ItemStackRequest;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.ItemStackRequestSlotData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.*;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponse;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponseContainer;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponseSlot;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponseStatus;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ItemStackResponsePacket;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2022-01-30 16:15:07 +00:00
|
|
|
import org.geysermc.geyser.inventory.*;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.inventory.click.Click;
|
|
|
|
import org.geysermc.geyser.inventory.click.ClickPlan;
|
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-01-30 16:15:07 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.inventory.chest.DoubleChestInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.chest.SingleChestInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.furnace.BlastFurnaceInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.furnace.FurnaceInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.furnace.SmokerInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
|
|
|
import org.geysermc.geyser.util.ItemUtils;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
import java.util.*;
|
2020-01-31 01:05:57 +00:00
|
|
|
|
|
|
|
@AllArgsConstructor
|
2019-07-29 22:20:48 +00:00
|
|
|
public abstract class InventoryTranslator {
|
2020-05-25 01:07:05 +00:00
|
|
|
|
2020-12-26 17:20:59 +00:00
|
|
|
public static final InventoryTranslator PLAYER_INVENTORY_TRANSLATOR = new PlayerInventoryTranslator();
|
2021-12-04 20:35:01 +00:00
|
|
|
private static final Map<ContainerType, InventoryTranslator> INVENTORY_TRANSLATORS = new EnumMap<>(ContainerType.class) {
|
2020-05-25 01:07:05 +00:00
|
|
|
{
|
2020-12-23 06:21:00 +00:00
|
|
|
/* Chest UIs */
|
2021-11-14 05:07:24 +00:00
|
|
|
put(ContainerType.GENERIC_9X1, new SingleChestInventoryTranslator(9));
|
|
|
|
put(ContainerType.GENERIC_9X2, new SingleChestInventoryTranslator(18));
|
|
|
|
put(ContainerType.GENERIC_9X3, new SingleChestInventoryTranslator(27));
|
|
|
|
put(ContainerType.GENERIC_9X4, new DoubleChestInventoryTranslator(36));
|
|
|
|
put(ContainerType.GENERIC_9X5, new DoubleChestInventoryTranslator(45));
|
|
|
|
put(ContainerType.GENERIC_9X6, new DoubleChestInventoryTranslator(54));
|
2020-05-25 01:07:05 +00:00
|
|
|
|
2020-12-23 06:21:00 +00:00
|
|
|
/* Furnaces */
|
2021-11-14 05:07:24 +00:00
|
|
|
put(ContainerType.FURNACE, new FurnaceInventoryTranslator());
|
|
|
|
put(ContainerType.BLAST_FURNACE, new BlastFurnaceInventoryTranslator());
|
|
|
|
put(ContainerType.SMOKER, new SmokerInventoryTranslator());
|
2020-05-25 01:07:05 +00:00
|
|
|
|
2020-12-23 06:21:00 +00:00
|
|
|
/* Specific Inventories */
|
2021-11-14 05:07:24 +00:00
|
|
|
put(ContainerType.ANVIL, new AnvilInventoryTranslator());
|
|
|
|
put(ContainerType.BEACON, new BeaconInventoryTranslator());
|
|
|
|
put(ContainerType.BREWING_STAND, new BrewingInventoryTranslator());
|
|
|
|
put(ContainerType.CARTOGRAPHY, new CartographyInventoryTranslator());
|
|
|
|
put(ContainerType.CRAFTING, new CraftingInventoryTranslator());
|
|
|
|
put(ContainerType.ENCHANTMENT, new EnchantingInventoryTranslator());
|
|
|
|
put(ContainerType.HOPPER, new HopperInventoryTranslator());
|
|
|
|
put(ContainerType.GENERIC_3X3, new Generic3X3InventoryTranslator());
|
|
|
|
put(ContainerType.GRINDSTONE, new GrindstoneInventoryTranslator());
|
|
|
|
put(ContainerType.LOOM, new LoomInventoryTranslator());
|
|
|
|
put(ContainerType.MERCHANT, new MerchantInventoryTranslator());
|
|
|
|
put(ContainerType.SHULKER_BOX, new ShulkerInventoryTranslator());
|
|
|
|
put(ContainerType.SMITHING, new SmithingInventoryTranslator());
|
|
|
|
put(ContainerType.STONECUTTER, new StonecutterInventoryTranslator());
|
2020-12-23 06:21:00 +00:00
|
|
|
|
2020-12-28 05:29:27 +00:00
|
|
|
/* Lectern */
|
2021-11-14 05:07:24 +00:00
|
|
|
put(ContainerType.LECTERN, new LecternInventoryTranslator());
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
public static final int PLAYER_INVENTORY_SIZE = 36;
|
|
|
|
public static final int PLAYER_INVENTORY_OFFSET = 9;
|
2021-02-25 21:12:58 +00:00
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
public final int size;
|
|
|
|
|
2022-11-14 20:12:46 +00:00
|
|
|
public abstract boolean prepareInventory(GeyserSession session, Inventory inventory);
|
2021-11-22 19:52:26 +00:00
|
|
|
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 abstract void updateInventory(GeyserSession session, Inventory inventory);
|
|
|
|
public abstract void updateSlot(GeyserSession session, Inventory inventory, int slot);
|
2022-12-21 00:47:45 +00:00
|
|
|
public abstract int bedrockSlotToJava(ItemStackRequestSlotData slotInfoData);
|
2021-03-12 16:51:37 +00:00
|
|
|
public abstract int javaSlotToBedrock(int javaSlot);
|
|
|
|
public abstract BedrockContainerSlot javaSlotToBedrockContainer(int javaSlot);
|
2019-12-01 09:26:11 +00:00
|
|
|
public abstract SlotType getSlotType(int javaSlot);
|
2021-11-14 05:07:24 +00:00
|
|
|
public abstract Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory);
|
2020-10-16 23:25:05 +00:00
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
/**
|
|
|
|
* Used for crafting-related transactions. Will override in PlayerInventoryTranslator and CraftingInventoryTranslator.
|
|
|
|
*/
|
|
|
|
public int getGridSize() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-24 23:29:25 +00:00
|
|
|
/**
|
|
|
|
* Should be overwritten in cases where specific inventories should reject an item being in a specific spot.
|
|
|
|
* For examples, looms use this to reject items that are dyes in Bedrock but not in Java.
|
|
|
|
*
|
2020-12-26 21:41:50 +00:00
|
|
|
* The source/destination slot will be -1 if the cursor is the slot
|
2020-12-25 01:43:24 +00:00
|
|
|
*
|
2020-12-24 23:29:25 +00:00
|
|
|
* @return true if this transfer should be rejected
|
|
|
|
*/
|
2022-01-11 21:56:11 +00:00
|
|
|
protected boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, ContainerSlotType bedrockSourceContainer,
|
2021-03-08 19:17:31 +00:00
|
|
|
int javaSourceSlot, ContainerSlotType bedrockDestinationContainer, int javaDestinationSlot) {
|
2020-12-24 23:29:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-23 06:21:00 +00:00
|
|
|
/**
|
|
|
|
* Should be overrided if this request matches a certain criteria and shouldn't be treated normally.
|
|
|
|
* E.G. anvil renaming or enchanting
|
|
|
|
*/
|
2022-12-21 00:47:45 +00:00
|
|
|
protected boolean shouldHandleRequestFirst(ItemStackRequestAction action, Inventory inventory) {
|
2020-12-23 06:21:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-21 00:47:45 +00:00
|
|
|
* If {@link #shouldHandleRequestFirst(ItemStackRequestAction, Inventory)} returns true, this will be called
|
2020-12-23 06:21:00 +00:00
|
|
|
*/
|
2022-10-30 03:02:11 +00:00
|
|
|
protected ItemStackResponse translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2021-03-12 16:51:37 +00:00
|
|
|
return rejectRequest(request);
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
public final void translateRequests(GeyserSession session, Inventory inventory, List<ItemStackRequest> requests) {
|
2021-01-14 04:59:11 +00:00
|
|
|
boolean refresh = false;
|
2020-10-16 23:25:05 +00:00
|
|
|
ItemStackResponsePacket responsePacket = new ItemStackResponsePacket();
|
2021-02-18 00:39:10 +00:00
|
|
|
for (ItemStackRequest request : requests) {
|
2022-10-30 03:02:11 +00:00
|
|
|
ItemStackResponse response;
|
2020-10-16 23:25:05 +00:00
|
|
|
if (request.getActions().length > 0) {
|
2022-12-21 00:47:45 +00:00
|
|
|
ItemStackRequestAction firstAction = request.getActions()[0];
|
2020-12-24 03:53:54 +00:00
|
|
|
if (shouldHandleRequestFirst(firstAction, inventory)) {
|
2020-12-23 06:21:00 +00:00
|
|
|
// Some special request that shouldn't be processed normally
|
2021-01-14 04:59:11 +00:00
|
|
|
response = translateSpecialRequest(session, inventory, request);
|
2020-10-16 23:25:05 +00:00
|
|
|
} else {
|
2021-09-10 18:10:56 +00:00
|
|
|
response = switch (firstAction.getType()) {
|
|
|
|
case CRAFT_RECIPE -> translateCraftingRequest(session, inventory, request);
|
|
|
|
case CRAFT_RECIPE_AUTO -> translateAutoCraftingRequest(session, inventory, request);
|
|
|
|
case CRAFT_CREATIVE ->
|
|
|
|
// This is also used for pulling items out of creative
|
|
|
|
translateCreativeRequest(session, inventory, request);
|
|
|
|
default -> translateRequest(session, inventory, request);
|
|
|
|
};
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-14 04:59:11 +00:00
|
|
|
response = rejectRequest(request);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
2021-03-13 23:07:43 +00:00
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
if (response.getResult() != ItemStackResponseStatus.OK) {
|
2021-03-13 23:07:43 +00:00
|
|
|
// Sync our copy of the inventory with Bedrock's to prevent desyncs
|
2021-01-14 04:59:11 +00:00
|
|
|
refresh = true;
|
|
|
|
}
|
2021-03-13 23:07:43 +00:00
|
|
|
|
2021-01-14 04:59:11 +00:00
|
|
|
responsePacket.getEntries().add(response);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
session.sendUpstreamPacket(responsePacket);
|
2021-01-14 04:59:11 +00:00
|
|
|
|
|
|
|
if (refresh) {
|
|
|
|
InventoryUtils.updateCursor(session);
|
|
|
|
updateInventory(session, inventory);
|
|
|
|
}
|
2022-01-11 03:55:27 +00:00
|
|
|
|
|
|
|
// We're done with our batch of inventory requests so this hack should be reset
|
|
|
|
inventory.resetNextStateId();
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2020-10-16 23:25:05 +00:00
|
|
|
ClickPlan plan = new ClickPlan(session, this, inventory);
|
2020-12-22 02:09:14 +00:00
|
|
|
IntSet affectedSlots = new IntOpenHashSet();
|
2022-12-21 00:47:45 +00:00
|
|
|
for (ItemStackRequestAction action : request.getActions()) {
|
2020-10-16 23:25:05 +00:00
|
|
|
GeyserItemStack cursor = session.getPlayerInventory().getCursor();
|
|
|
|
switch (action.getType()) {
|
|
|
|
case TAKE:
|
|
|
|
case PLACE: {
|
2022-12-21 00:47:45 +00:00
|
|
|
TransferItemStackRequestAction transferAction = (TransferItemStackRequestAction) action;
|
2020-12-18 02:47:18 +00:00
|
|
|
if (!(checkNetId(session, inventory, transferAction.getSource()) && checkNetId(session, inventory, transferAction.getDestination()))) {
|
2021-11-20 21:34:30 +00:00
|
|
|
if (session.getGeyser().getConfig().isDebugMode()) {
|
2022-08-09 18:06:53 +00:00
|
|
|
session.getGeyser().getLogger().error("DEBUG: About to reject TAKE/PLACE request made by " + session.bedrockUsername());
|
2021-03-13 23:07:43 +00:00
|
|
|
dumpStackRequestDetails(session, inventory, transferAction.getSource(), transferAction.getDestination());
|
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
return rejectRequest(request);
|
2020-12-18 02:47:18 +00:00
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
|
2020-12-25 01:43:24 +00:00
|
|
|
int sourceSlot = bedrockSlotToJava(transferAction.getSource());
|
2020-12-24 23:29:25 +00:00
|
|
|
int destSlot = bedrockSlotToJava(transferAction.getDestination());
|
2022-01-30 16:15:07 +00:00
|
|
|
boolean isSourceCursor = isCursor(transferAction.getSource());
|
|
|
|
boolean isDestCursor = isCursor(transferAction.getDestination());
|
2020-12-24 23:29:25 +00:00
|
|
|
|
2021-03-08 19:17:31 +00:00
|
|
|
if (shouldRejectItemPlace(session, inventory, transferAction.getSource().getContainer(),
|
2022-01-30 16:15:07 +00:00
|
|
|
isSourceCursor ? -1 : sourceSlot,
|
|
|
|
transferAction.getDestination().getContainer(), isDestCursor ? -1 : destSlot)) {
|
2020-12-24 23:29:25 +00:00
|
|
|
// This item would not be here in Java
|
|
|
|
return rejectRequest(request, false);
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
if (isSourceCursor && isDestCursor) { //???
|
2020-10-16 23:25:05 +00:00
|
|
|
return rejectRequest(request);
|
2022-01-30 16:15:07 +00:00
|
|
|
} else if (isSourceCursor) { //releasing cursor
|
2020-10-16 23:25:05 +00:00
|
|
|
int sourceAmount = cursor.getAmount();
|
|
|
|
if (transferAction.getCount() == sourceAmount) { //release all
|
|
|
|
plan.add(Click.LEFT, destSlot);
|
|
|
|
} else { //release some
|
|
|
|
for (int i = 0; i < transferAction.getCount(); i++) {
|
|
|
|
plan.add(Click.RIGHT, destSlot);
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 16:15:07 +00:00
|
|
|
} else if (isDestCursor) { //picking up into cursor
|
2020-10-16 23:25:05 +00:00
|
|
|
GeyserItemStack sourceItem = plan.getItem(sourceSlot);
|
|
|
|
int sourceAmount = sourceItem.getAmount();
|
|
|
|
if (cursor.isEmpty()) { //picking up into empty cursor
|
|
|
|
if (transferAction.getCount() == sourceAmount) { //pickup all
|
|
|
|
plan.add(Click.LEFT, sourceSlot);
|
|
|
|
} else if (transferAction.getCount() == sourceAmount - (sourceAmount / 2)) { //larger half; simple right click
|
|
|
|
plan.add(Click.RIGHT, sourceSlot);
|
|
|
|
} else { //pickup some; not a simple right click
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //first pickup all
|
|
|
|
for (int i = 0; i < sourceAmount - transferAction.getCount(); i++) {
|
|
|
|
plan.add(Click.RIGHT, sourceSlot); //release extra items back into source slot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { //pickup into non-empty cursor
|
|
|
|
if (!InventoryUtils.canStack(cursor, plan.getItem(sourceSlot))) { //doesn't make sense, reject
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
2020-12-27 03:44:48 +00:00
|
|
|
if (transferAction.getCount() != sourceAmount) {
|
|
|
|
int tempSlot = findTempSlot(inventory, cursor, false, sourceSlot);
|
|
|
|
if (tempSlot == -1) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, tempSlot); //place cursor into temp slot
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //pickup source items into cursor
|
|
|
|
for (int i = 0; i < transferAction.getCount(); i++) {
|
|
|
|
plan.add(Click.RIGHT, tempSlot); //partially transfer source items into temp slot (original cursor)
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //return remaining source items
|
|
|
|
plan.add(Click.LEFT, tempSlot); //retrieve original cursor items from temp slot
|
|
|
|
} else {
|
|
|
|
if (getSlotType(sourceSlot).equals(SlotType.NORMAL)) {
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //release cursor onto source slot
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //pickup combined cursor and source
|
2020-12-24 23:29:25 +00:00
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
} else { //transfer from one slot to another
|
2020-12-27 03:44:48 +00:00
|
|
|
int tempSlot = -1;
|
2021-01-13 01:37:12 +00:00
|
|
|
if (!plan.getCursor().isEmpty()) {
|
2020-12-27 03:44:48 +00:00
|
|
|
tempSlot = findTempSlot(inventory, cursor, false, sourceSlot, destSlot);
|
|
|
|
if (tempSlot == -1) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, tempSlot); //place cursor into temp slot
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
2021-01-13 01:37:12 +00:00
|
|
|
|
|
|
|
transferSlot(plan, sourceSlot, destSlot, transferAction.getCount());
|
|
|
|
|
2020-12-27 03:44:48 +00:00
|
|
|
if (tempSlot != -1) {
|
|
|
|
plan.add(Click.LEFT, tempSlot); //retrieve original cursor
|
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-03-08 19:17:31 +00:00
|
|
|
case SWAP: {
|
2022-12-21 00:47:45 +00:00
|
|
|
SwapAction swapAction = (SwapAction) action;
|
|
|
|
ItemStackRequestSlotData source = swapAction.getSource();
|
|
|
|
ItemStackRequestSlotData destination = swapAction.getDestination();
|
2022-01-11 21:56:11 +00:00
|
|
|
|
|
|
|
if (!(checkNetId(session, inventory, source) && checkNetId(session, inventory, destination))) {
|
2021-11-20 21:34:30 +00:00
|
|
|
if (session.getGeyser().getConfig().isDebugMode()) {
|
2022-08-09 18:06:53 +00:00
|
|
|
session.getGeyser().getLogger().error("DEBUG: About to reject SWAP request made by " + session.bedrockUsername());
|
2022-01-11 21:56:11 +00:00
|
|
|
dumpStackRequestDetails(session, inventory, source, destination);
|
2021-03-13 23:07:43 +00:00
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
return rejectRequest(request);
|
2021-02-21 04:52:49 +00:00
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
|
2022-01-11 21:56:11 +00:00
|
|
|
int sourceSlot = bedrockSlotToJava(source);
|
|
|
|
int destSlot = bedrockSlotToJava(destination);
|
|
|
|
boolean isSourceCursor = isCursor(source);
|
|
|
|
boolean isDestCursor = isCursor(destination);
|
2021-03-08 19:17:31 +00:00
|
|
|
|
2022-01-11 21:56:11 +00:00
|
|
|
if (shouldRejectItemPlace(session, inventory, source.getContainer(),
|
2021-03-08 19:17:31 +00:00
|
|
|
isSourceCursor ? -1 : sourceSlot,
|
2022-01-11 21:56:11 +00:00
|
|
|
destination.getContainer(), isDestCursor ? -1 : destSlot)) {
|
2021-03-08 19:17:31 +00:00
|
|
|
// This item would not be here in Java
|
|
|
|
return rejectRequest(request, false);
|
|
|
|
}
|
|
|
|
|
2022-01-11 21:56:11 +00:00
|
|
|
if (!isSourceCursor && destination.getContainer() == ContainerSlotType.HOTBAR || destination.getContainer() == ContainerSlotType.HOTBAR_AND_INVENTORY) {
|
|
|
|
// Tell the server we're pressing one of the hotbar keys to save clicks
|
2022-01-20 23:09:35 +00:00
|
|
|
Click click = InventoryUtils.getClickForHotbarSwap(destination.getSlot());
|
2022-01-11 21:56:11 +00:00
|
|
|
if (click != null) {
|
|
|
|
plan.add(click, sourceSlot);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:17:31 +00:00
|
|
|
if (isSourceCursor && isDestCursor) { //???
|
2020-10-16 23:25:05 +00:00
|
|
|
return rejectRequest(request);
|
2021-03-08 19:17:31 +00:00
|
|
|
} else if (isSourceCursor) { //swap cursor
|
2020-10-16 23:25:05 +00:00
|
|
|
if (InventoryUtils.canStack(cursor, plan.getItem(destSlot))) { //TODO: cannot simply swap if cursor stacks with slot (temp slot)
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, destSlot);
|
2021-03-08 19:17:31 +00:00
|
|
|
} else if (isDestCursor) { //swap cursor
|
2020-10-16 23:25:05 +00:00
|
|
|
if (InventoryUtils.canStack(cursor, plan.getItem(sourceSlot))) { //TODO
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, sourceSlot);
|
|
|
|
} else {
|
|
|
|
if (!cursor.isEmpty()) { //TODO: (temp slot)
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
if (sourceSlot == destSlot) { //doesn't make sense
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
if (InventoryUtils.canStack(plan.getItem(sourceSlot), plan.getItem(destSlot))) { //TODO: (temp slot)
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //pickup source into cursor
|
|
|
|
plan.add(Click.LEFT, destSlot); //swap cursor with dest slot
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //release cursor onto source
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DROP: {
|
2022-12-21 00:47:45 +00:00
|
|
|
DropAction dropAction = (DropAction) action;
|
2020-10-16 23:25:05 +00:00
|
|
|
if (!checkNetId(session, inventory, dropAction.getSource()))
|
|
|
|
return rejectRequest(request);
|
|
|
|
|
|
|
|
if (isCursor(dropAction.getSource())) { //clicking outside of window
|
2021-01-15 04:45:59 +00:00
|
|
|
int sourceAmount = plan.getCursor().getAmount();
|
|
|
|
if (dropAction.getCount() == sourceAmount) { //drop all
|
|
|
|
plan.add(Click.LEFT_OUTSIDE, Click.OUTSIDE_SLOT);
|
|
|
|
} else { //drop some
|
|
|
|
for (int i = 0; i < dropAction.getCount(); i++) {
|
|
|
|
plan.add(Click.RIGHT_OUTSIDE, Click.OUTSIDE_SLOT); //drop one until goal is met
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { //dropping from inventory
|
|
|
|
int sourceSlot = bedrockSlotToJava(dropAction.getSource());
|
|
|
|
int sourceAmount = plan.getItem(sourceSlot).getAmount();
|
|
|
|
if (dropAction.getCount() == sourceAmount && sourceAmount > 1) { //dropping all? (prefer DROP_ONE if only one)
|
|
|
|
plan.add(Click.DROP_ALL, sourceSlot);
|
|
|
|
} else { //drop some
|
|
|
|
for (int i = 0; i < dropAction.getCount(); i++) {
|
|
|
|
plan.add(Click.DROP_ONE, sourceSlot); //drop one until goal is met
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-02-25 21:12:58 +00:00
|
|
|
case CONSUME: { // Tends to be called for UI inventories
|
2020-12-27 03:55:14 +00:00
|
|
|
if (inventory instanceof CartographyContainer) {
|
|
|
|
// TODO add this for more inventories? Only seems to glitch out the cartography table, though.
|
2022-12-21 00:47:45 +00:00
|
|
|
ConsumeAction consumeData = (ConsumeAction) action;
|
2021-03-10 19:43:54 +00:00
|
|
|
|
2020-12-27 03:55:14 +00:00
|
|
|
int sourceSlot = bedrockSlotToJava(consumeData.getSource());
|
2021-03-10 19:43:54 +00:00
|
|
|
if ((sourceSlot == 0 && inventory.getItem(1).isEmpty()) || (sourceSlot == 1 && inventory.getItem(0).isEmpty())) {
|
|
|
|
// Java doesn't allow an item to be renamed; this is why one of the slots could remain empty for Bedrock
|
|
|
|
// We check this now since setting the inventory slots here messes up shouldRejectItemPlace
|
2020-12-27 03:55:14 +00:00
|
|
|
return rejectRequest(request, false);
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:43:54 +00:00
|
|
|
if (sourceSlot == 1) {
|
|
|
|
// Decrease the item count, but only after both slots are checked.
|
|
|
|
// Otherwise, the slot 1 check will fail
|
|
|
|
GeyserItemStack item = inventory.getItem(sourceSlot);
|
|
|
|
item.setAmount(item.getAmount() - consumeData.getCount());
|
|
|
|
if (item.isEmpty()) {
|
|
|
|
inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session);
|
|
|
|
}
|
|
|
|
|
|
|
|
GeyserItemStack itemZero = inventory.getItem(0);
|
|
|
|
itemZero.setAmount(itemZero.getAmount() - consumeData.getCount());
|
|
|
|
if (itemZero.isEmpty()) {
|
|
|
|
inventory.setItem(0, GeyserItemStack.EMPTY, session);
|
|
|
|
}
|
2020-12-27 03:55:14 +00:00
|
|
|
}
|
|
|
|
affectedSlots.add(sourceSlot);
|
|
|
|
}
|
2020-12-22 03:44:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-12-08 14:23:23 +00:00
|
|
|
case CRAFT_RECIPE: // Called by stonecutters 1.18+
|
2021-02-25 21:12:58 +00:00
|
|
|
case CRAFT_RECIPE_AUTO: // Called by villagers
|
|
|
|
case CRAFT_NON_IMPLEMENTED_DEPRECATED: // Tends to be called for UI inventories
|
|
|
|
case CRAFT_RESULTS_DEPRECATED: // Tends to be called for UI inventories
|
2021-10-19 17:16:46 +00:00
|
|
|
case CRAFT_RECIPE_OPTIONAL: // Anvils and cartography tables will handle this
|
|
|
|
case CRAFT_LOOM: // Looms 1.17.40+
|
|
|
|
case CRAFT_REPAIR_AND_DISENCHANT: { // Grindstones 1.17.40+
|
2020-12-23 06:21:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
default:
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
plan.execute(false);
|
2020-12-22 02:09:14 +00:00
|
|
|
affectedSlots.addAll(plan.getAffectedSlots());
|
|
|
|
return acceptRequest(request, makeContainerEntries(session, inventory, affectedSlots));
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2020-10-16 23:25:05 +00:00
|
|
|
int resultSize = 0;
|
2021-02-21 04:52:49 +00:00
|
|
|
int timesCrafted;
|
2020-10-16 23:25:05 +00:00
|
|
|
CraftState craftState = CraftState.START;
|
|
|
|
|
|
|
|
int leftover = 0;
|
|
|
|
ClickPlan plan = new ClickPlan(session, this, inventory);
|
2022-01-30 16:15:07 +00:00
|
|
|
// Track all the crafting table slots to report back the contents of the slots after crafting
|
|
|
|
IntSet affectedSlots = new IntOpenHashSet();
|
2022-12-21 00:47:45 +00:00
|
|
|
for (ItemStackRequestAction action : request.getActions()) {
|
2020-10-16 23:25:05 +00:00
|
|
|
switch (action.getType()) {
|
|
|
|
case CRAFT_RECIPE: {
|
|
|
|
if (craftState != CraftState.START) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.RECIPE_ID;
|
2021-01-04 02:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
case CRAFT_RESULTS_DEPRECATED: {
|
2022-12-21 00:47:45 +00:00
|
|
|
CraftResultsDeprecatedAction deprecatedCraftAction = (CraftResultsDeprecatedAction) action;
|
2020-10-16 23:25:05 +00:00
|
|
|
if (craftState != CraftState.RECIPE_ID) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.DEPRECATED;
|
|
|
|
|
|
|
|
if (deprecatedCraftAction.getResultItems().length != 1) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
resultSize = deprecatedCraftAction.getResultItems()[0].getCount();
|
2021-01-04 02:54:26 +00:00
|
|
|
timesCrafted = deprecatedCraftAction.getTimesCrafted();
|
|
|
|
if (resultSize <= 0 || timesCrafted <= 0) {
|
2020-10-16 23:25:05 +00:00
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CONSUME: {
|
|
|
|
if (craftState != CraftState.DEPRECATED && craftState != CraftState.INGREDIENTS) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.INGREDIENTS;
|
2022-12-21 00:47:45 +00:00
|
|
|
affectedSlots.add(bedrockSlotToJava(((ConsumeAction) action).getSource()));
|
2020-10-16 23:25:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TAKE:
|
|
|
|
case PLACE: {
|
2022-12-21 00:47:45 +00:00
|
|
|
TransferItemStackRequestAction transferAction = (TransferItemStackRequestAction) action;
|
2020-10-16 23:25:05 +00:00
|
|
|
if (craftState != CraftState.INGREDIENTS && craftState != CraftState.TRANSFER) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.TRANSFER;
|
|
|
|
|
|
|
|
if (transferAction.getSource().getContainer() != ContainerSlotType.CREATIVE_OUTPUT) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
if (transferAction.getCount() <= 0) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sourceSlot = bedrockSlotToJava(transferAction.getSource());
|
2021-01-04 02:54:26 +00:00
|
|
|
int destSlot = bedrockSlotToJava(transferAction.getDestination());
|
|
|
|
|
2020-10-16 23:25:05 +00:00
|
|
|
if (isCursor(transferAction.getDestination())) {
|
|
|
|
plan.add(Click.LEFT, sourceSlot);
|
|
|
|
craftState = CraftState.DONE;
|
|
|
|
} else {
|
|
|
|
if (leftover != 0) {
|
|
|
|
if (transferAction.getCount() > leftover) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
if (transferAction.getCount() == leftover) {
|
|
|
|
plan.add(Click.LEFT, destSlot);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < transferAction.getCount(); i++) {
|
|
|
|
plan.add(Click.RIGHT, destSlot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
leftover -= transferAction.getCount();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int remainder = transferAction.getCount() % resultSize;
|
|
|
|
int timesToCraft = transferAction.getCount() / resultSize;
|
|
|
|
for (int i = 0; i < timesToCraft; i++) {
|
|
|
|
plan.add(Click.LEFT, sourceSlot);
|
|
|
|
plan.add(Click.LEFT, destSlot);
|
|
|
|
}
|
|
|
|
if (remainder > 0) {
|
|
|
|
plan.add(Click.LEFT, 0);
|
|
|
|
for (int i = 0; i < remainder; i++) {
|
|
|
|
plan.add(Click.RIGHT, destSlot);
|
|
|
|
}
|
|
|
|
leftover = resultSize - remainder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
plan.execute(false);
|
2022-01-30 16:15:07 +00:00
|
|
|
affectedSlots.addAll(plan.getAffectedSlots());
|
|
|
|
return acceptRequest(request, makeContainerEntries(session, inventory, affectedSlots));
|
2021-01-13 01:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateAutoCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2022-01-30 16:15:07 +00:00
|
|
|
final int gridSize = getGridSize();
|
|
|
|
if (gridSize == -1) {
|
2021-01-13 01:37:12 +00:00
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
2022-01-30 16:15:07 +00:00
|
|
|
int gridDimensions = gridSize == 4 ? 2 : 3;
|
2021-01-13 01:37:12 +00:00
|
|
|
|
|
|
|
Ingredient[] ingredients = new Ingredient[0];
|
|
|
|
ItemStack output = null;
|
|
|
|
int recipeWidth = 0;
|
|
|
|
int ingRemaining = 0;
|
|
|
|
int ingredientIndex = -1;
|
|
|
|
|
|
|
|
Int2IntMap consumedSlots = new Int2IntOpenHashMap();
|
|
|
|
int prioritySlot = -1;
|
2021-02-21 04:52:49 +00:00
|
|
|
int tempSlot;
|
2021-01-13 01:37:12 +00:00
|
|
|
|
|
|
|
int resultSize;
|
|
|
|
int timesCrafted = 0;
|
|
|
|
Int2ObjectMap<Int2IntMap> ingredientMap = new Int2ObjectOpenHashMap<>();
|
|
|
|
CraftState craftState = CraftState.START;
|
|
|
|
|
|
|
|
ClickPlan plan = new ClickPlan(session, this, inventory);
|
|
|
|
requestLoop:
|
2022-12-21 00:47:45 +00:00
|
|
|
for (ItemStackRequestAction action : request.getActions()) {
|
2021-01-13 01:37:12 +00:00
|
|
|
switch (action.getType()) {
|
|
|
|
case CRAFT_RECIPE_AUTO: {
|
2022-12-21 00:47:45 +00:00
|
|
|
AutoCraftRecipeAction autoCraftAction = (AutoCraftRecipeAction) action;
|
2021-07-14 13:33:34 +00:00
|
|
|
// TODO autoCraftAction#getTimesCrafted 1.17.10 ???
|
2021-01-13 01:37:12 +00:00
|
|
|
if (craftState != CraftState.START) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.RECIPE_ID;
|
|
|
|
|
|
|
|
int recipeId = autoCraftAction.getRecipeNetworkId();
|
2022-02-21 21:11:51 +00:00
|
|
|
GeyserRecipe recipe = session.getCraftingRecipes().get(recipeId);
|
2021-01-13 01:37:12 +00:00
|
|
|
if (recipe == null) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
if (!plan.getCursor().isEmpty()) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
//reject if crafting grid is not clear
|
|
|
|
for (int i = 1; i <= gridSize; i++) {
|
|
|
|
if (!inventory.getItem(i).isEmpty()) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 21:11:51 +00:00
|
|
|
if (recipe.isShaped()) {
|
|
|
|
GeyserShapedRecipe shapedRecipe = (GeyserShapedRecipe) recipe;
|
|
|
|
ingredients = shapedRecipe.ingredients();
|
|
|
|
recipeWidth = shapedRecipe.width();
|
|
|
|
output = shapedRecipe.result();
|
|
|
|
if (recipeWidth > gridDimensions || shapedRecipe.height() > gridDimensions) {
|
|
|
|
return rejectRequest(request);
|
2021-09-10 18:10:56 +00:00
|
|
|
}
|
2022-02-21 21:11:51 +00:00
|
|
|
} else {
|
|
|
|
GeyserShapelessRecipe shapelessRecipe = (GeyserShapelessRecipe) recipe;
|
|
|
|
ingredients = shapelessRecipe.ingredients();
|
|
|
|
recipeWidth = gridDimensions;
|
|
|
|
output = shapelessRecipe.result();
|
|
|
|
if (ingredients.length > gridSize) {
|
|
|
|
return rejectRequest(request);
|
2021-09-10 18:10:56 +00:00
|
|
|
}
|
2021-01-13 01:37:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CRAFT_RESULTS_DEPRECATED: {
|
2022-12-21 00:47:45 +00:00
|
|
|
CraftResultsDeprecatedAction deprecatedCraftAction = (CraftResultsDeprecatedAction) action;
|
2021-01-13 01:37:12 +00:00
|
|
|
if (craftState != CraftState.RECIPE_ID) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.DEPRECATED;
|
|
|
|
|
|
|
|
if (deprecatedCraftAction.getResultItems().length != 1) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
resultSize = deprecatedCraftAction.getResultItems()[0].getCount();
|
|
|
|
timesCrafted = deprecatedCraftAction.getTimesCrafted();
|
|
|
|
if (resultSize <= 0 || timesCrafted <= 0) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CONSUME: {
|
2022-12-21 00:47:45 +00:00
|
|
|
ConsumeAction consumeAction = (ConsumeAction) action;
|
2021-01-13 01:37:12 +00:00
|
|
|
if (craftState != CraftState.DEPRECATED && craftState != CraftState.INGREDIENTS) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.INGREDIENTS;
|
|
|
|
|
|
|
|
if (ingRemaining == 0) {
|
|
|
|
while (++ingredientIndex < ingredients.length) {
|
|
|
|
if (ingredients[ingredientIndex].getOptions().length != 0) {
|
|
|
|
ingRemaining = timesCrafted;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ingRemaining -= consumeAction.getCount();
|
|
|
|
if (ingRemaining < 0)
|
|
|
|
return rejectRequest(request);
|
|
|
|
|
|
|
|
int javaSlot = bedrockSlotToJava(consumeAction.getSource());
|
|
|
|
consumedSlots.merge(javaSlot, consumeAction.getCount(), Integer::sum);
|
|
|
|
|
|
|
|
int gridSlot = 1 + ingredientIndex + ((ingredientIndex / recipeWidth) * (gridDimensions - recipeWidth));
|
|
|
|
Int2IntMap sources = ingredientMap.computeIfAbsent(gridSlot, k -> new Int2IntOpenHashMap());
|
|
|
|
sources.put(javaSlot, consumeAction.getCount());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TAKE:
|
|
|
|
case PLACE: {
|
2022-12-21 00:47:45 +00:00
|
|
|
TransferItemStackRequestAction transferAction = (TransferItemStackRequestAction) action;
|
2021-01-13 01:37:12 +00:00
|
|
|
if (craftState != CraftState.INGREDIENTS && craftState != CraftState.TRANSFER) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
craftState = CraftState.TRANSFER;
|
|
|
|
|
|
|
|
if (transferAction.getSource().getContainer() != ContainerSlotType.CREATIVE_OUTPUT) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
if (transferAction.getCount() <= 0) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
int javaSlot = bedrockSlotToJava(transferAction.getDestination());
|
|
|
|
if (isCursor(transferAction.getDestination())) { //TODO
|
|
|
|
if (timesCrafted > 1) {
|
|
|
|
tempSlot = findTempSlot(inventory, GeyserItemStack.from(output), true);
|
|
|
|
if (tempSlot == -1) {
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break requestLoop;
|
|
|
|
} else if (inventory.getItem(javaSlot).getAmount() == consumedSlots.get(javaSlot)) {
|
|
|
|
prioritySlot = bedrockSlotToJava(transferAction.getDestination());
|
|
|
|
break requestLoop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final int maxLoops = Math.min(64, timesCrafted);
|
|
|
|
for (int loops = 0; loops < maxLoops; loops++) {
|
|
|
|
boolean done = true;
|
|
|
|
for (Int2ObjectMap.Entry<Int2IntMap> entry : ingredientMap.int2ObjectEntrySet()) {
|
|
|
|
Int2IntMap sources = entry.getValue();
|
|
|
|
if (sources.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
done = false;
|
|
|
|
int gridSlot = entry.getIntKey();
|
|
|
|
if (!plan.getItem(gridSlot).isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int sourceSlot;
|
|
|
|
if (loops == 0 && sources.containsKey(prioritySlot)) {
|
|
|
|
sourceSlot = prioritySlot;
|
|
|
|
} else {
|
|
|
|
sourceSlot = sources.keySet().iterator().nextInt();
|
|
|
|
}
|
|
|
|
int transferAmount = sources.remove(sourceSlot);
|
|
|
|
transferSlot(plan, sourceSlot, gridSlot, transferAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
//TODO: sometimes the server does not agree on this slot?
|
|
|
|
plan.add(Click.LEFT_SHIFT, 0, true);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inventory.setItem(0, GeyserItemStack.from(output), session);
|
|
|
|
plan.execute(true);
|
|
|
|
return acceptRequest(request, makeContainerEntries(session, inventory, plan.getAffectedSlots()));
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 16:51:37 +00:00
|
|
|
/**
|
|
|
|
* Handled in {@link PlayerInventoryTranslator}
|
|
|
|
*/
|
2022-10-30 03:02:11 +00:00
|
|
|
protected ItemStackResponse translateCreativeRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2020-10-16 23:25:05 +00:00
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:37:12 +00:00
|
|
|
private void transferSlot(ClickPlan plan, int sourceSlot, int destSlot, int transferAmount) {
|
|
|
|
boolean tempSwap = !plan.getCursor().isEmpty();
|
|
|
|
int sourceAmount = plan.getItem(sourceSlot).getAmount();
|
|
|
|
if (transferAmount == sourceAmount) { //transfer all
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //pickup source
|
|
|
|
plan.add(Click.LEFT, destSlot); //let go of all items and done
|
|
|
|
} else { //transfer some
|
|
|
|
//try to transfer items with least clicks possible
|
|
|
|
int halfSource = sourceAmount - (sourceAmount / 2); //larger half
|
|
|
|
int holding;
|
|
|
|
if (!tempSwap && transferAmount <= halfSource) { //faster to take only half. CURSOR MUST BE EMPTY
|
|
|
|
plan.add(Click.RIGHT, sourceSlot);
|
|
|
|
holding = halfSource;
|
|
|
|
} else { //need all
|
|
|
|
plan.add(Click.LEFT, sourceSlot);
|
|
|
|
holding = sourceAmount;
|
|
|
|
}
|
|
|
|
if (!tempSwap && transferAmount > holding / 2) { //faster to release extra items onto source or dest slot?
|
|
|
|
for (int i = 0; i < holding - transferAmount; i++) {
|
|
|
|
plan.add(Click.RIGHT, sourceSlot); //prepare cursor
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, destSlot); //release cursor onto dest slot
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < transferAmount; i++) {
|
|
|
|
plan.add(Click.RIGHT, destSlot); //right click until transfer goal is met
|
|
|
|
}
|
|
|
|
plan.add(Click.LEFT, sourceSlot); //return extra items to source slot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
protected static ItemStackResponse acceptRequest(ItemStackRequest request, List<ItemStackResponseContainer> containerEntries) {
|
|
|
|
return new ItemStackResponse(ItemStackResponseStatus.OK, request.getRequestId(), containerEntries);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 21:12:58 +00:00
|
|
|
/**
|
|
|
|
* Reject an incorrect ItemStackRequest.
|
|
|
|
*/
|
2022-10-30 03:02:11 +00:00
|
|
|
protected static ItemStackResponse rejectRequest(ItemStackRequest request) {
|
2020-12-20 00:19:44 +00:00
|
|
|
return rejectRequest(request, true);
|
|
|
|
}
|
|
|
|
|
2021-02-25 21:12:58 +00:00
|
|
|
/**
|
|
|
|
* Reject an incorrect ItemStackRequest.
|
|
|
|
*
|
|
|
|
* @param throwError whether this request was truly erroneous (true), or known as an outcome and should not be treated
|
|
|
|
* as bad (false).
|
|
|
|
*/
|
2022-10-30 03:02:11 +00:00
|
|
|
protected static ItemStackResponse rejectRequest(ItemStackRequest request, boolean throwError) {
|
2021-11-20 21:34:30 +00:00
|
|
|
if (throwError && GeyserImpl.getInstance().getConfig().isDebugMode()) {
|
2021-01-29 05:36:43 +00:00
|
|
|
new Throwable("DEBUGGING: ItemStackRequest rejected " + request.toString()).printStackTrace();
|
2020-12-20 00:19:44 +00:00
|
|
|
}
|
2022-12-21 00:47:45 +00:00
|
|
|
return new ItemStackResponse(ItemStackResponseStatus.ERROR, request.getRequestId(), Collections.emptyList());
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 23:07:43 +00:00
|
|
|
/**
|
|
|
|
* Print out the contents of an ItemStackRequest, should the net ID check fail.
|
|
|
|
*/
|
2022-12-21 00:47:45 +00:00
|
|
|
protected void dumpStackRequestDetails(GeyserSession session, Inventory inventory, ItemStackRequestSlotData source, ItemStackRequestSlotData destination) {
|
2021-11-20 21:34:30 +00:00
|
|
|
session.getGeyser().getLogger().error("Source: " + source.toString() + " Result: " + checkNetId(session, inventory, source));
|
|
|
|
session.getGeyser().getLogger().error("Destination: " + destination.toString() + " Result: " + checkNetId(session, inventory, destination));
|
|
|
|
session.getGeyser().getLogger().error("Geyser's record of source slot: " + inventory.getItem(bedrockSlotToJava(source)));
|
|
|
|
session.getGeyser().getLogger().error("Geyser's record of destination slot: " + inventory.getItem(bedrockSlotToJava(destination)));
|
2021-03-13 23:07:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
public boolean checkNetId(GeyserSession session, Inventory inventory, ItemStackRequestSlotData slotInfoData) {
|
2021-01-09 02:01:31 +00:00
|
|
|
int netId = slotInfoData.getStackNetworkId();
|
2021-02-21 04:52:49 +00:00
|
|
|
// "In my testing, sometimes the client thinks the netId of an item in the crafting grid is 1, even though we never said it was.
|
|
|
|
// I think it only happens when we manually set the grid but that was my quick fix"
|
2021-01-09 02:01:31 +00:00
|
|
|
if (netId < 0 || netId == 1)
|
2020-10-16 23:25:05 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
GeyserItemStack currentItem = isCursor(slotInfoData) ? session.getPlayerInventory().getCursor() : inventory.getItem(bedrockSlotToJava(slotInfoData));
|
2021-01-09 02:01:31 +00:00
|
|
|
return currentItem.getNetId() == netId;
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 03:44:48 +00:00
|
|
|
/**
|
|
|
|
* Try to find a slot that can temporarily store the given item.
|
|
|
|
* Only looks in the main inventory and hotbar (excluding offhand).
|
|
|
|
* Only slots that are empty or contain a different type of item are valid.
|
|
|
|
*
|
|
|
|
* @return java id for the temporary slot, or -1 if no viable slot was found
|
|
|
|
*/
|
|
|
|
//TODO: compatibility for simulated inventory (ClickPlan)
|
|
|
|
private static int findTempSlot(Inventory inventory, GeyserItemStack item, boolean emptyOnly, int... slotBlacklist) {
|
2022-08-29 16:26:30 +00:00
|
|
|
int offset = inventory.getJavaId() == 0 ? 1 : 0; //offhand is not a viable temp slot
|
2020-12-27 03:44:48 +00:00
|
|
|
HashSet<GeyserItemStack> itemBlacklist = new HashSet<>(slotBlacklist.length + 1);
|
|
|
|
itemBlacklist.add(item);
|
|
|
|
|
|
|
|
IntSet potentialSlots = new IntOpenHashSet(36);
|
|
|
|
for (int i = inventory.getSize() - (36 + offset); i < inventory.getSize() - offset; i++) {
|
|
|
|
potentialSlots.add(i);
|
|
|
|
}
|
|
|
|
for (int i : slotBlacklist) {
|
|
|
|
potentialSlots.remove(i);
|
|
|
|
GeyserItemStack blacklistedItem = inventory.getItem(i);
|
|
|
|
if (!blacklistedItem.isEmpty()) {
|
|
|
|
itemBlacklist.add(blacklistedItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i : potentialSlots) {
|
|
|
|
GeyserItemStack testItem = inventory.getItem(i);
|
|
|
|
if ((emptyOnly && !testItem.isEmpty())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean viable = true;
|
|
|
|
for (GeyserItemStack blacklistedItem : itemBlacklist) {
|
|
|
|
if (InventoryUtils.canStack(testItem, blacklistedItem)) {
|
|
|
|
viable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!viable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
//could not find a viable temp slot
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
protected final List<ItemStackResponseContainer> makeContainerEntries(GeyserSession session, Inventory inventory, IntSet affectedSlots) {
|
|
|
|
Map<ContainerSlotType, List<ItemStackResponseSlot>> containerMap = new HashMap<>();
|
2022-01-30 16:15:07 +00:00
|
|
|
// Manually call iterator to prevent Integer boxing
|
|
|
|
IntIterator it = affectedSlots.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
int slot = it.nextInt();
|
2020-10-16 23:25:05 +00:00
|
|
|
BedrockContainerSlot bedrockSlot = javaSlotToBedrockContainer(slot);
|
2022-12-21 00:47:45 +00:00
|
|
|
List<ItemStackResponseSlot> list = containerMap.computeIfAbsent(bedrockSlot.container(), k -> new ArrayList<>());
|
2022-01-16 19:42:17 +00:00
|
|
|
list.add(makeItemEntry(session, bedrockSlot.slot(), inventory.getItem(slot)));
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
List<ItemStackResponseContainer> containerEntries = new ArrayList<>();
|
|
|
|
for (Map.Entry<ContainerSlotType, List<ItemStackResponseSlot>> entry : containerMap.entrySet()) {
|
|
|
|
containerEntries.add(new ItemStackResponseContainer(entry.getKey(), entry.getValue()));
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
ItemStackResponseSlot cursorEntry = makeItemEntry(session, 0, session.getPlayerInventory().getCursor());
|
|
|
|
containerEntries.add(new ItemStackResponseContainer(ContainerSlotType.CURSOR, Collections.singletonList(cursorEntry)));
|
2020-10-16 23:25:05 +00:00
|
|
|
|
|
|
|
return containerEntries;
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
private static ItemStackResponseSlot makeItemEntry(GeyserSession session, int bedrockSlot, GeyserItemStack itemStack) {
|
|
|
|
ItemStackResponseSlot itemEntry;
|
2020-10-16 23:25:05 +00:00
|
|
|
if (!itemStack.isEmpty()) {
|
2021-03-15 18:49:44 +00:00
|
|
|
// As of 1.16.210: Bedrock needs confirmation on what the current item durability is.
|
|
|
|
// If 0 is sent, then Bedrock thinks the item is not damaged
|
|
|
|
int durability = 0;
|
|
|
|
if (itemStack.getNbt() != null) {
|
|
|
|
Tag damage = itemStack.getNbt().get("Damage");
|
|
|
|
if (damage instanceof IntTag) {
|
2021-07-13 01:19:40 +00:00
|
|
|
durability = ItemUtils.getCorrectBedrockDurability(session, itemStack.getJavaId(), ((IntTag) damage).getValue());
|
2021-03-15 18:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
itemEntry = new ItemStackResponseSlot((byte) bedrockSlot, (byte) bedrockSlot, (byte) itemStack.getAmount(), itemStack.getNetId(), "", durability);
|
2020-10-16 23:25:05 +00:00
|
|
|
} else {
|
2022-12-21 00:47:45 +00:00
|
|
|
itemEntry = new ItemStackResponseSlot((byte) bedrockSlot, (byte) bedrockSlot, (byte) 0, 0, "", 0);
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
return itemEntry;
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:47:45 +00:00
|
|
|
protected static boolean isCursor(ItemStackRequestSlotData slotInfoData) {
|
2020-10-16 23:25:05 +00:00
|
|
|
return slotInfoData.getContainer() == ContainerSlotType.CURSOR;
|
|
|
|
}
|
|
|
|
|
2021-12-04 20:35:01 +00:00
|
|
|
/**
|
|
|
|
* Gets the {@link InventoryTranslator} for the given {@link ContainerType}.
|
|
|
|
* Returns {@link #PLAYER_INVENTORY_TRANSLATOR} if type is null.
|
|
|
|
*
|
|
|
|
* @param type the type
|
|
|
|
* @return the InventoryType for the given ContainerType.
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public static InventoryTranslator inventoryTranslator(@Nullable ContainerType type) {
|
|
|
|
if (type == null) {
|
|
|
|
return PLAYER_INVENTORY_TRANSLATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INVENTORY_TRANSLATORS.get(type);
|
|
|
|
}
|
|
|
|
|
2021-01-15 04:45:59 +00:00
|
|
|
protected enum CraftState {
|
2020-10-16 23:25:05 +00:00
|
|
|
START,
|
|
|
|
RECIPE_ID,
|
|
|
|
DEPRECATED,
|
|
|
|
INGREDIENTS,
|
|
|
|
TRANSFER,
|
|
|
|
DONE
|
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|