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.protocol.java.inventory;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2021-01-09 05:38:53 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
|
|
|
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.clientbound.inventory.ClientboundContainerSetSlotPacket;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerId;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.CraftingData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemDescriptorWithCount;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.CraftingDataPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.InventorySlotPacket;
|
2022-07-10 19:52:11 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.inventory.GeyserItemStack;
|
|
|
|
import org.geysermc.geyser.inventory.Inventory;
|
2022-02-21 21:11:51 +00:00
|
|
|
import org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe;
|
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.translator.inventory.InventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.PlayerInventoryTranslator;
|
|
|
|
import org.geysermc.geyser.translator.inventory.item.ItemTranslator;
|
2022-01-30 16:15:07 +00:00
|
|
|
import org.geysermc.geyser.translator.protocol.PacketTranslator;
|
|
|
|
import org.geysermc.geyser.translator.protocol.Translator;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
2019-10-20 21:25:41 +00:00
|
|
|
|
2021-01-09 02:01:31 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.UUID;
|
2021-01-14 04:40:01 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-01-09 02:01:31 +00:00
|
|
|
|
2021-11-13 03:44:15 +00:00
|
|
|
@Translator(packet = ClientboundContainerSetSlotPacket.class)
|
2021-11-13 04:01:45 +00:00
|
|
|
public class JavaContainerSetSlotTranslator extends PacketTranslator<ClientboundContainerSetSlotPacket> {
|
2019-07-29 22:20:48 +00:00
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void translate(GeyserSession session, ClientboundContainerSetSlotPacket packet) {
|
2021-11-14 05:07:24 +00:00
|
|
|
if (packet.getContainerId() == 255) { //cursor
|
2021-08-17 00:39:29 +00:00
|
|
|
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
|
|
|
|
session.getPlayerInventory().setCursor(newItem, session);
|
|
|
|
InventoryUtils.updateCursor(session);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-31 01:05:57 +00:00
|
|
|
|
2021-08-17 00:39:29 +00:00
|
|
|
//TODO: support window id -2, should update player inventory
|
2021-11-14 05:07:24 +00:00
|
|
|
Inventory inventory = InventoryUtils.getInventory(session, packet.getContainerId());
|
2021-08-17 00:39:29 +00:00
|
|
|
if (inventory == null)
|
|
|
|
return;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2021-08-17 00:39:29 +00:00
|
|
|
InventoryTranslator translator = session.getInventoryTranslator();
|
|
|
|
if (translator != null) {
|
|
|
|
if (session.getCraftingGridFuture() != null) {
|
|
|
|
session.getCraftingGridFuture().cancel(false);
|
|
|
|
}
|
2022-07-10 19:52:11 +00:00
|
|
|
|
|
|
|
int slot = packet.getSlot();
|
|
|
|
if (slot >= inventory.getSize()) {
|
|
|
|
GeyserImpl geyser = session.getGeyser();
|
2022-08-09 18:06:53 +00:00
|
|
|
geyser.getLogger().warning("ClientboundContainerSetSlotPacket sent to " + session.bedrockUsername()
|
2022-07-10 19:52:11 +00:00
|
|
|
+ " that exceeds inventory size!");
|
|
|
|
if (geyser.getConfig().isDebugMode()) {
|
|
|
|
geyser.getLogger().debug(packet);
|
|
|
|
geyser.getLogger().debug(inventory);
|
|
|
|
}
|
|
|
|
// 1.19.0 behavior: the state ID will not be set due to exception
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCraftingGrid(session, slot, packet.getItem(), inventory, translator);
|
2021-01-09 05:38:53 +00:00
|
|
|
|
2021-08-17 00:39:29 +00:00
|
|
|
GeyserItemStack newItem = GeyserItemStack.from(packet.getItem());
|
2021-11-14 05:07:24 +00:00
|
|
|
if (packet.getContainerId() == 0 && !(translator instanceof PlayerInventoryTranslator)) {
|
2021-08-17 00:39:29 +00:00
|
|
|
// In rare cases, the window ID can still be 0 but Java treats it as valid
|
2022-07-10 19:52:11 +00:00
|
|
|
session.getPlayerInventory().setItem(slot, newItem, session);
|
|
|
|
InventoryTranslator.PLAYER_INVENTORY_TRANSLATOR.updateSlot(session, session.getPlayerInventory(), slot);
|
2021-08-17 00:39:29 +00:00
|
|
|
} else {
|
2022-07-10 19:52:11 +00:00
|
|
|
inventory.setItem(slot, newItem, session);
|
|
|
|
translator.updateSlot(session, inventory, slot);
|
2021-01-09 05:38:53 +00:00
|
|
|
}
|
2022-07-10 19:52:11 +00:00
|
|
|
|
|
|
|
// Intentional behavior here below the cursor; Minecraft 1.18.1 also does this.
|
|
|
|
int stateId = packet.getStateId();
|
|
|
|
session.setEmulatePost1_16Logic(stateId > 0 || stateId != inventory.getStateId());
|
|
|
|
inventory.setStateId(stateId);
|
2021-08-17 00:39:29 +00:00
|
|
|
}
|
2021-01-09 05:38:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
/**
|
|
|
|
* Checks for a changed output slot in the crafting grid, and ensures Bedrock sees the recipe.
|
|
|
|
*/
|
|
|
|
private static void updateCraftingGrid(GeyserSession session, int slot, ItemStack item, Inventory inventory, InventoryTranslator translator) {
|
|
|
|
if (slot != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int gridSize = translator.getGridSize();
|
|
|
|
if (gridSize == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-09 05:38:53 +00:00
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
if (item == null || item.getId() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-09 05:38:53 +00:00
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
session.setCraftingGridFuture(session.scheduleInEventLoop(() -> {
|
2021-01-09 05:38:53 +00:00
|
|
|
int offset = gridSize == 4 ? 28 : 32;
|
|
|
|
int gridDimensions = gridSize == 4 ? 2 : 3;
|
2021-01-09 13:10:27 +00:00
|
|
|
int firstRow = -1, height = -1;
|
|
|
|
int firstCol = -1, width = -1;
|
|
|
|
for (int row = 0; row < gridDimensions; row++) {
|
|
|
|
for (int col = 0; col < gridDimensions; col++) {
|
|
|
|
if (!inventory.getItem(col + (row * gridDimensions) + 1).isEmpty()) {
|
|
|
|
if (firstRow == -1) {
|
|
|
|
firstRow = row;
|
|
|
|
firstCol = col;
|
|
|
|
} else {
|
|
|
|
firstCol = Math.min(firstCol, col);
|
|
|
|
}
|
|
|
|
height = Math.max(height, row);
|
|
|
|
width = Math.max(width, col);
|
|
|
|
}
|
2021-01-09 05:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 13:10:27 +00:00
|
|
|
//empty grid
|
|
|
|
if (firstRow == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
height += -firstRow + 1;
|
|
|
|
width += -firstCol + 1;
|
|
|
|
|
2022-01-30 16:15:07 +00:00
|
|
|
if (InventoryUtils.getValidRecipe(session, item, inventory::getItem, gridDimensions, firstRow,
|
|
|
|
height, firstCol, width) != null) {
|
|
|
|
// Recipe is already present on the client; don't send packet
|
|
|
|
return;
|
2021-01-09 05:38:53 +00:00
|
|
|
}
|
2021-01-09 02:01:31 +00:00
|
|
|
|
2021-01-09 21:45:32 +00:00
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
int newRecipeId = session.getLastRecipeNetId().incrementAndGet();
|
|
|
|
|
|
|
|
ItemData[] ingredients = new ItemData[height * width];
|
2021-01-09 05:38:53 +00:00
|
|
|
//construct ingredient list and clear slots on client
|
2021-01-09 21:45:32 +00:00
|
|
|
Ingredient[] javaIngredients = new Ingredient[height * width];
|
|
|
|
int index = 0;
|
|
|
|
for (int row = firstRow; row < height + firstRow; row++) {
|
|
|
|
for (int col = firstCol; col < width + firstCol; col++) {
|
|
|
|
GeyserItemStack geyserItemStack = inventory.getItem(col + (row * gridDimensions) + 1);
|
|
|
|
ingredients[index] = geyserItemStack.getItemData(session);
|
|
|
|
ItemStack[] itemStacks = new ItemStack[] {geyserItemStack.isEmpty() ? null : geyserItemStack.getItemStack(1)};
|
|
|
|
javaIngredients[index] = new Ingredient(itemStacks);
|
|
|
|
|
|
|
|
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
|
|
|
slotPacket.setContainerId(ContainerId.UI);
|
|
|
|
slotPacket.setSlot(col + (row * gridDimensions) + offset);
|
|
|
|
slotPacket.setItem(ItemData.AIR);
|
|
|
|
session.sendUpstreamPacket(slotPacket);
|
|
|
|
index++;
|
|
|
|
}
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
2021-01-09 05:38:53 +00:00
|
|
|
|
2021-01-09 21:45:32 +00:00
|
|
|
// Cache this recipe so we know the client has received it
|
2022-02-21 21:11:51 +00:00
|
|
|
session.getCraftingRecipes().put(newRecipeId, new GeyserShapedRecipe(width, height, javaIngredients, item));
|
2021-01-09 21:45:32 +00:00
|
|
|
|
2021-01-09 05:38:53 +00:00
|
|
|
CraftingDataPacket craftPacket = new CraftingDataPacket();
|
|
|
|
craftPacket.getCraftingData().add(CraftingData.fromShaped(
|
|
|
|
uuid.toString(),
|
2021-01-09 21:45:32 +00:00
|
|
|
width,
|
|
|
|
height,
|
2022-09-15 01:17:08 +00:00
|
|
|
Arrays.stream(ingredients).map(ItemDescriptorWithCount::fromItem).toList(),
|
2022-01-30 16:15:07 +00:00
|
|
|
Collections.singletonList(ItemTranslator.translateToBedrock(session, item)),
|
2021-01-09 05:38:53 +00:00
|
|
|
uuid,
|
|
|
|
"crafting_table",
|
|
|
|
0,
|
2021-01-09 21:45:32 +00:00
|
|
|
newRecipeId
|
2021-01-09 05:38:53 +00:00
|
|
|
));
|
|
|
|
craftPacket.setCleanRecipes(false);
|
|
|
|
session.sendUpstreamPacket(craftPacket);
|
|
|
|
|
2021-01-09 21:45:32 +00:00
|
|
|
index = 0;
|
|
|
|
for (int row = firstRow; row < height + firstRow; row++) {
|
|
|
|
for (int col = firstCol; col < width + firstCol; col++) {
|
|
|
|
InventorySlotPacket slotPacket = new InventorySlotPacket();
|
|
|
|
slotPacket.setContainerId(ContainerId.UI);
|
|
|
|
slotPacket.setSlot(col + (row * gridDimensions) + offset);
|
|
|
|
slotPacket.setItem(ingredients[index]);
|
|
|
|
session.sendUpstreamPacket(slotPacket);
|
|
|
|
index++;
|
|
|
|
}
|
2021-01-09 05:38:53 +00:00
|
|
|
}
|
2022-01-30 16:15:07 +00:00
|
|
|
}, 150, TimeUnit.MILLISECONDS));
|
2021-01-09 13:10:27 +00:00
|
|
|
}
|
2019-07-29 22:20:48 +00:00
|
|
|
}
|