2020-12-23 06:21:00 +00:00
|
|
|
/*
|
2021-01-01 19:22:26 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-12-23 06:21:00 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.inventory.translators;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.window.WindowType;
|
2021-01-12 01:06:55 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientRenameItemPacket;
|
|
|
|
import com.nukkitx.nbt.NbtMap;
|
2021-02-18 00:39:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.*;
|
2021-01-12 01:06:55 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.CraftResultsDeprecatedStackRequestActionData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequestActionType;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
|
2020-12-23 06:21:00 +00:00
|
|
|
import org.geysermc.connector.inventory.AnvilContainer;
|
2021-01-12 01:06:55 +00:00
|
|
|
import org.geysermc.connector.inventory.GeyserItemStack;
|
2020-12-23 06:21:00 +00:00
|
|
|
import org.geysermc.connector.inventory.Inventory;
|
|
|
|
import org.geysermc.connector.inventory.PlayerInventory;
|
2021-01-12 01:06:55 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-12-23 06:21:00 +00:00
|
|
|
import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot;
|
|
|
|
import org.geysermc.connector.network.translators.inventory.updater.UIInventoryUpdater;
|
2021-01-12 01:06:55 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemTranslator;
|
2020-12-23 06:21:00 +00:00
|
|
|
|
|
|
|
public class AnvilInventoryTranslator extends AbstractBlockInventoryTranslator {
|
|
|
|
public AnvilInventoryTranslator() {
|
2021-02-12 19:39:41 +00:00
|
|
|
super(3, "minecraft:anvil[facing=north]", ContainerType.ANVIL, UIInventoryUpdater.INSTANCE,
|
|
|
|
"minecraft:chipped_anvil", "minecraft:damaged_anvil");
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 01:06:55 +00:00
|
|
|
/* 1.16.100 support start */
|
|
|
|
@Override
|
|
|
|
@Deprecated
|
|
|
|
public boolean shouldHandleRequestFirst(StackRequestActionData action, Inventory inventory) {
|
|
|
|
return action.getType() == StackRequestActionType.CRAFT_NON_IMPLEMENTED_DEPRECATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Deprecated
|
2021-02-18 00:39:10 +00:00
|
|
|
public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
|
2021-01-12 01:06:55 +00:00
|
|
|
if (!(request.getActions()[1] instanceof CraftResultsDeprecatedStackRequestActionData)) {
|
|
|
|
// Just silently log an error
|
|
|
|
session.getConnector().getLogger().debug("Something isn't quite right with taking an item out of an anvil.");
|
|
|
|
return translateRequest(session, inventory, request);
|
|
|
|
}
|
|
|
|
CraftResultsDeprecatedStackRequestActionData actionData = (CraftResultsDeprecatedStackRequestActionData) request.getActions()[1];
|
|
|
|
ItemData resultItem = actionData.getResultItems()[0];
|
|
|
|
if (resultItem.getTag() != null) {
|
|
|
|
NbtMap displayTag = resultItem.getTag().getCompound("display");
|
|
|
|
if (displayTag != null && displayTag.containsKey("Name")) {
|
|
|
|
ItemData sourceSlot = inventory.getItem(0).getItemData(session);
|
|
|
|
|
|
|
|
if (sourceSlot.getTag() != null) {
|
|
|
|
NbtMap oldDisplayTag = sourceSlot.getTag().getCompound("display");
|
|
|
|
if (oldDisplayTag != null && oldDisplayTag.containsKey("Name")) {
|
|
|
|
if (!displayTag.getString("Name").equals(oldDisplayTag.getString("Name"))) {
|
|
|
|
// Name has changed
|
|
|
|
sendRenamePacket(session, inventory, resultItem, displayTag.getString("Name"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No display tag on the old item
|
|
|
|
sendRenamePacket(session, inventory, resultItem, displayTag.getString("Name"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// New NBT tag
|
|
|
|
sendRenamePacket(session, inventory, resultItem, displayTag.getString("Name"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return translateRequest(session, inventory, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendRenamePacket(GeyserSession session, Inventory inventory, ItemData outputItem, String name) {
|
|
|
|
session.sendDownstreamPacket(new ClientRenameItemPacket(name));
|
|
|
|
inventory.setItem(2, GeyserItemStack.from(ItemTranslator.translateToJava(outputItem)), session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1.16.100 support end */
|
|
|
|
|
2020-12-23 06:21:00 +00:00
|
|
|
@Override
|
|
|
|
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
|
2021-02-13 20:29:54 +00:00
|
|
|
switch (slotInfoData.getContainer()) {
|
|
|
|
case ANVIL_INPUT:
|
|
|
|
return 0;
|
|
|
|
case ANVIL_MATERIAL:
|
|
|
|
return 1;
|
|
|
|
case ANVIL_RESULT:
|
|
|
|
case CREATIVE_OUTPUT:
|
|
|
|
return 2;
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
return super.bedrockSlotToJava(slotInfoData);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockContainerSlot javaSlotToBedrockContainer(int slot) {
|
|
|
|
switch (slot) {
|
|
|
|
case 0:
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.ANVIL_INPUT, 1);
|
|
|
|
case 1:
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.ANVIL_MATERIAL, 2);
|
|
|
|
case 2:
|
|
|
|
return new BedrockContainerSlot(ContainerSlotType.ANVIL_RESULT, 50);
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrockContainer(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int javaSlotToBedrock(int slot) {
|
|
|
|
switch (slot) {
|
|
|
|
case 0:
|
|
|
|
return 1;
|
|
|
|
case 1:
|
|
|
|
return 2;
|
|
|
|
case 2:
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
return super.javaSlotToBedrock(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Inventory createInventory(String name, int windowId, WindowType windowType, PlayerInventory playerInventory) {
|
2021-03-04 19:04:11 +00:00
|
|
|
return new AnvilContainer(name, windowId, this.size, windowType, playerInventory);
|
2020-12-23 06:21:00 +00:00
|
|
|
}
|
|
|
|
}
|