2020-05-17 04:52:39 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +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:
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* 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.
|
2020-05-17 04:52:39 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-05-17 04:52:39 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.inventory;
|
2020-05-17 04:52:39 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
|
2022-02-11 01:14:52 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSelectTradePacket;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.math.vector.Vector3f;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.entity.EntityLinkData;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
|
2022-12-21 00:47:45 +00:00
|
|
|
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.AutoCraftRecipeAction;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.CraftRecipeAction;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.response.ItemStackResponse;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.SetEntityLinkPacket;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.entity.EntityDefinitions;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.entity.type.Entity;
|
|
|
|
import org.geysermc.geyser.inventory.*;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.inventory.updater.InventoryUpdater;
|
|
|
|
import org.geysermc.geyser.inventory.updater.UIInventoryUpdater;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2022-02-11 01:14:52 +00:00
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-05-17 04:52:39 +00:00
|
|
|
|
|
|
|
public class MerchantInventoryTranslator extends BaseInventoryTranslator {
|
|
|
|
private final InventoryUpdater updater;
|
|
|
|
|
|
|
|
public MerchantInventoryTranslator() {
|
|
|
|
super(3);
|
2020-12-23 06:21:00 +00:00
|
|
|
this.updater = UIInventoryUpdater.INSTANCE;
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slot) {
|
|
|
|
case 0 -> 4;
|
|
|
|
case 1 -> 5;
|
|
|
|
case 2 -> 50;
|
|
|
|
default -> super.javaSlotToBedrock(slot);
|
|
|
|
};
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-16 23:25:05 +00:00
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slot) {
|
2022-12-23 17:06:41 +00:00
|
|
|
case 0 -> new BedrockContainerSlot(ContainerSlotType.TRADE2_INGREDIENT_1, 4);
|
|
|
|
case 1 -> new BedrockContainerSlot(ContainerSlotType.TRADE2_INGREDIENT_2, 5);
|
2021-09-10 18:10:56 +00:00
|
|
|
case 2 -> new BedrockContainerSlot(ContainerSlotType.TRADE2_RESULT, 50);
|
|
|
|
default -> super.javaSlotToBedrockContainer(slot);
|
|
|
|
};
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-12-21 00:47:45 +00:00
|
|
|
public int bedrockSlotToJava(ItemStackRequestSlotData slotInfoData) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (slotInfoData.getContainer()) {
|
2022-12-23 17:06:41 +00:00
|
|
|
case TRADE2_INGREDIENT_1 -> 0;
|
|
|
|
case TRADE2_INGREDIENT_2 -> 1;
|
|
|
|
case TRADE2_RESULT, CREATED_OUTPUT -> 2;
|
2021-09-10 18:10:56 +00:00
|
|
|
default -> super.bedrockSlotToJava(slotInfoData);
|
|
|
|
};
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:15:29 +00:00
|
|
|
@Override
|
|
|
|
public SlotType getSlotType(int javaSlot) {
|
|
|
|
if (javaSlot == 2) {
|
|
|
|
return SlotType.OUTPUT;
|
|
|
|
}
|
|
|
|
return SlotType.NORMAL;
|
|
|
|
}
|
|
|
|
|
2020-05-17 04:52:39 +00:00
|
|
|
@Override
|
2022-11-14 20:12:46 +00:00
|
|
|
public boolean prepareInventory(GeyserSession session, Inventory inventory) {
|
2020-10-16 23:25:05 +00:00
|
|
|
MerchantContainer merchantInventory = (MerchantContainer) inventory;
|
|
|
|
if (merchantInventory.getVillager() == null) {
|
|
|
|
long geyserId = session.getEntityCache().getNextEntityId().incrementAndGet();
|
|
|
|
Vector3f pos = session.getPlayerEntity().getPosition().sub(0, 3, 0);
|
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
Entity villager = new Entity(session, 0, geyserId, null, EntityDefinitions.VILLAGER, pos, Vector3f.ZERO, 0f, 0f, 0f) {
|
|
|
|
@Override
|
|
|
|
protected void initializeMetadata() {
|
2022-10-30 00:23:21 +00:00
|
|
|
dirtyMetadata.put(EntityDataTypes.SCALE, 0f);
|
2022-10-30 03:02:11 +00:00
|
|
|
dirtyMetadata.put(EntityDataTypes.WIDTH, 0f);
|
|
|
|
dirtyMetadata.put(EntityDataTypes.HEIGHT, 0f);
|
2021-11-18 03:02:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
villager.spawnEntity();
|
2020-10-16 23:25:05 +00:00
|
|
|
|
|
|
|
SetEntityLinkPacket linkPacket = new SetEntityLinkPacket();
|
|
|
|
EntityLinkData.Type type = EntityLinkData.Type.PASSENGER;
|
|
|
|
linkPacket.setEntityLink(new EntityLinkData(session.getPlayerEntity().getGeyserId(), geyserId, type, true, false));
|
|
|
|
session.sendUpstreamPacket(linkPacket);
|
|
|
|
|
|
|
|
merchantInventory.setVillager(villager);
|
|
|
|
}
|
2022-11-14 20:12:46 +00:00
|
|
|
|
|
|
|
return true;
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void openInventory(GeyserSession session, Inventory inventory) {
|
2021-11-13 04:01:45 +00:00
|
|
|
//Handled in JavaMerchantOffersTranslator
|
2021-03-08 19:17:31 +00:00
|
|
|
//TODO: send a blank inventory here in case the villager doesn't send a TradeList packet
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void closeInventory(GeyserSession session, Inventory inventory) {
|
2020-10-16 23:25:05 +00:00
|
|
|
MerchantContainer merchantInventory = (MerchantContainer) inventory;
|
|
|
|
if (merchantInventory.getVillager() != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
merchantInventory.getVillager().despawnEntity();
|
2020-10-16 23:25:05 +00:00
|
|
|
}
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 01:14:52 +00:00
|
|
|
@Override
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2022-02-11 01:14:52 +00:00
|
|
|
// Behavior as of 1.18.10.
|
|
|
|
// We set the net ID to the trade index + 1. This doesn't appear to cause issues and means we don't have to
|
|
|
|
// store a map of net ID to trade index on our end.
|
2022-12-21 00:47:45 +00:00
|
|
|
int tradeChoice = ((CraftRecipeAction) request.getActions()[0]).getRecipeNetworkId() - 1;
|
2022-02-13 02:44:51 +00:00
|
|
|
return handleTrade(session, inventory, request, tradeChoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-30 03:02:11 +00:00
|
|
|
public ItemStackResponse translateAutoCraftingRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2022-02-13 02:44:51 +00:00
|
|
|
// 1.18.10 update - seems impossible to call without consoles/controller input
|
|
|
|
// We set the net ID to the trade index + 1. This doesn't appear to cause issues and means we don't have to
|
|
|
|
// store a map of net ID to trade index on our end.
|
2022-12-21 00:47:45 +00:00
|
|
|
int tradeChoice = ((AutoCraftRecipeAction) request.getActions()[0]).getRecipeNetworkId() - 1;
|
2022-02-13 02:44:51 +00:00
|
|
|
return handleTrade(session, inventory, request, tradeChoice);
|
|
|
|
}
|
|
|
|
|
2022-10-30 03:02:11 +00:00
|
|
|
private ItemStackResponse handleTrade(GeyserSession session, Inventory inventory, ItemStackRequest request, int tradeChoice) {
|
2022-02-11 01:14:52 +00:00
|
|
|
ServerboundSelectTradePacket packet = new ServerboundSelectTradePacket(tradeChoice);
|
2023-10-12 13:02:57 +00:00
|
|
|
session.sendDownstreamGamePacket(packet);
|
2022-02-11 01:14:52 +00:00
|
|
|
|
2022-07-14 01:31:10 +00:00
|
|
|
if (session.isEmulatePost1_13Logic()) {
|
2022-02-11 01:14:52 +00:00
|
|
|
// 1.18 Java cooperates nicer than older versions
|
|
|
|
if (inventory instanceof MerchantContainer merchantInventory) {
|
|
|
|
merchantInventory.onTradeSelected(session, tradeChoice);
|
|
|
|
}
|
|
|
|
return translateRequest(session, inventory, request);
|
|
|
|
} else {
|
|
|
|
// 1.18 servers works fine without a workaround, but ViaVersion needs to work around 1.13 servers,
|
|
|
|
// so we need to work around that with the delay. Specifically they force a window refresh after a
|
|
|
|
// trade packet has been sent.
|
|
|
|
session.scheduleInEventLoop(() -> {
|
|
|
|
if (inventory instanceof MerchantContainer merchantInventory) {
|
|
|
|
merchantInventory.onTradeSelected(session, tradeChoice);
|
|
|
|
// Ignore output since we don't want to send a delayed response packet back to the client
|
|
|
|
translateRequest(session, inventory, request);
|
|
|
|
|
|
|
|
// Resync items once more
|
|
|
|
updateInventory(session, inventory);
|
|
|
|
InventoryUtils.updateCursor(session);
|
|
|
|
}
|
|
|
|
}, 100, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
// Revert this request, for now
|
|
|
|
return rejectRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 04:52:39 +00:00
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateInventory(GeyserSession session, Inventory inventory) {
|
2020-05-17 04:52:39 +00:00
|
|
|
updater.updateInventory(this, session, inventory);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
|
2020-05-17 04:52:39 +00:00
|
|
|
updater.updateSlot(this, session, inventory, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-14 05:07:24 +00:00
|
|
|
public Inventory createInventory(String name, int windowId, ContainerType containerType, PlayerInventory playerInventory) {
|
|
|
|
return new MerchantContainer(name, windowId, this.size, containerType, playerInventory);
|
2020-05-17 04:52:39 +00:00
|
|
|
}
|
|
|
|
}
|