ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java

307 lines
15 KiB
Java
Raw Normal View History

/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets;
2018-10-24 19:05:14 +02:00
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
2018-11-29 16:31:39 +01:00
import com.google.common.collect.Sets;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
2021-04-26 21:16:10 +02:00
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter;
2018-10-24 19:05:14 +02:00
2018-11-29 16:31:39 +01:00
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
2018-11-29 16:31:39 +01:00
2018-10-24 19:05:14 +02:00
public class InventoryPackets {
2019-10-04 13:01:33 +02:00
private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_14To1_13_2.class.getSimpleName();
2018-11-29 16:31:39 +01:00
private static final Set<String> REMOVED_RECIPE_TYPES = Sets.newHashSet("crafting_special_banneraddpattern", "crafting_special_repairitem");
private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() {
@Override
protected void handleTranslate(JsonObject object, String translate) {
super.handleTranslate(object, translate);
// Mojang decided to remove .name from inventory titles
if (translate.startsWith("block.") && translate.endsWith(".name")) {
object.addProperty("translate", translate.substring(0, translate.length() - 5));
}
}
};
2018-10-24 19:05:14 +02:00
2018-10-27 13:25:42 +02:00
public static void register(Protocol protocol) {
2019-10-04 13:01:33 +02:00
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
2018-10-24 19:05:14 +02:00
2020-08-16 16:24:06 +02:00
itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
itemRewriter.registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
2020-02-08 21:33:44 +01:00
protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {
2018-10-27 13:25:42 +02:00
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
2019-01-09 18:26:08 +01:00
String type = wrapper.read(Type.STRING);
JsonElement title = wrapper.read(Type.COMPONENT);
COMPONENT_REWRITER.processText(title);
2019-01-09 18:26:08 +01:00
Short slots = wrapper.read(Type.UNSIGNED_BYTE);
if (type.equals("EntityHorse")) {
2019-04-05 18:48:38 +02:00
wrapper.setId(0x1F);
2019-01-09 18:26:08 +01:00
int entityId = wrapper.read(Type.INT);
wrapper.write(Type.UNSIGNED_BYTE, windowId);
2019-01-09 18:26:08 +01:00
wrapper.write(Type.VAR_INT, slots.intValue());
wrapper.write(Type.INT, entityId);
} else {
2019-04-05 18:48:38 +02:00
wrapper.setId(0x2E);
wrapper.write(Type.VAR_INT, windowId.intValue());
2019-01-09 18:26:08 +01:00
int typeId = -1;
switch (type) {
case "minecraft:crafting_table":
2019-03-13 17:53:17 +01:00
typeId = 11;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:furnace":
2019-03-13 17:53:17 +01:00
typeId = 13;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:dropper":
case "minecraft:dispenser":
2019-03-13 17:53:17 +01:00
typeId = 6;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:enchanting_table":
2019-03-13 17:53:17 +01:00
typeId = 12;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:brewing_stand":
2019-03-13 17:53:17 +01:00
typeId = 10;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:villager":
2019-03-13 17:53:17 +01:00
typeId = 18;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:beacon":
2019-03-13 17:53:17 +01:00
typeId = 8;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:anvil":
2019-03-13 17:53:17 +01:00
typeId = 7;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:hopper":
2019-03-13 17:53:17 +01:00
typeId = 15;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:shulker_box":
2019-03-13 17:53:17 +01:00
typeId = 19;
2019-01-09 18:26:08 +01:00
break;
case "minecraft:container":
case "minecraft:chest":
default:
if (slots > 0 && slots <= 54) {
typeId = slots / 9 - 1;
}
break;
2019-01-09 18:26:08 +01:00
}
if (typeId == -1) {
Via.getPlatform().getLogger().warning("Can't open inventory for 1.14 player! Type: " + type + " Size: " + slots);
}
wrapper.write(Type.VAR_INT, typeId);
wrapper.write(Type.COMPONENT, title);
2019-01-09 18:26:08 +01:00
}
2018-10-27 13:25:42 +02:00
}
});
}
});
itemRewriter.registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
2018-10-27 13:25:42 +02:00
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
2018-10-27 13:25:42 +02:00
@Override
public void registerMap() {
map(Type.STRING); // Channel
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String channel = wrapper.get(Type.STRING, 0);
if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
2019-04-05 18:48:38 +02:00
wrapper.setId(0x27);
2019-01-09 18:26:08 +01:00
wrapper.resetReader();
wrapper.read(Type.STRING); // Remove channel
int windowId = wrapper.read(Type.INT);
Refactor entity tracking and meta handling This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs. ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes. ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries. This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers). All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
tracker.setLatestTradeWindowId(windowId);
2019-01-09 18:26:08 +01:00
wrapper.write(Type.VAR_INT, windowId);
2018-10-27 13:25:42 +02:00
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Output Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) {
// Second Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
wrapper.passthrough(Type.BOOLEAN); // Trade disabled
wrapper.passthrough(Type.INT); // Number of tools uses
wrapper.passthrough(Type.INT); // Maximum number of trade uses
2019-03-13 18:07:09 +01:00
wrapper.write(Type.INT, 0);
wrapper.write(Type.INT, 0);
wrapper.write(Type.FLOAT, 0f);
2018-10-27 13:25:42 +02:00
}
2019-03-13 18:07:09 +01:00
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.BOOLEAN, false);
2018-12-15 20:57:17 +01:00
} else if (channel.equals("minecraft:book_open") || channel.equals("book_open")) {
int hand = wrapper.read(Type.VAR_INT);
wrapper.clearPacket();
2019-04-05 18:48:38 +02:00
wrapper.setId(0x2D);
2018-12-15 20:57:17 +01:00
wrapper.write(Type.VAR_INT, hand);
2018-10-27 13:25:42 +02:00
}
}
});
}
});
itemRewriter.registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
2018-10-27 13:25:42 +02:00
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol, InventoryPackets::toClient);
protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
2018-10-27 13:25:42 +02:00
@Override
public void registerMap() {
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
int deleted = 0;
for (int i = 0; i < size; i++) {
String id = wrapper.read(Type.STRING); // Recipe Identifier
String type = wrapper.read(Type.STRING);
if (REMOVED_RECIPE_TYPES.contains(type)) {
deleted++;
continue;
2018-10-27 13:25:42 +02:00
}
wrapper.write(Type.STRING, type);
wrapper.write(Type.STRING, id);
recipeRewriter.handle(wrapper, type);
2018-10-27 13:25:42 +02:00
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
2018-10-27 13:25:42 +02:00
});
}
});
2018-10-24 19:05:14 +02:00
itemRewriter.registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
2018-10-27 13:25:42 +02:00
protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Selecting trade now moves the items, we need to resync the inventory
PacketWrapper resyncPacket = wrapper.create(0x08);
Refactor entity tracking and meta handling This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs. ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes. ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries. This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers). All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId())); // 0 - Window ID
resyncPacket.write(Type.SHORT, ((short) -999)); // 1 - Slot
resyncPacket.write(Type.BYTE, (byte) 2); // 2 - Button - End left click
resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt())); // 3 - Action number
resyncPacket.write(Type.VAR_INT, 5); // 4 - Mode - Drag
2021-03-14 16:57:37 +01:00
CompoundTag tag = new CompoundTag();
tag.put("force_resync", new DoubleTag(Double.NaN)); // Tags with NaN are not equal
resyncPacket.write(Type.FLAT_VAR_INT_ITEM, new Item(1, (byte) 1, (short) 0, tag)); // 5 - Clicked Item
resyncPacket.sendToServer(Protocol1_14To1_13_2.class, true, false);
}
});
}
});
itemRewriter.registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
2020-09-21 09:53:04 +02:00
itemRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
2018-10-27 13:25:42 +02:00
}
public static void toClient(Item item) {
if (item == null) return;
2020-08-16 16:24:06 +02:00
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier()));
2019-01-06 20:27:41 +01:00
if (item.getTag() == null) return;
// Display Lore now uses JSON
Tag displayTag = item.getTag().get("display");
if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore");
if (loreTag instanceof ListTag) {
ListTag lore = (ListTag) loreTag;
2021-03-14 16:57:37 +01:00
display.put(NBT_TAG_NAME + "|Lore", new ListTag(lore.clone().getValue())); // Save old lore
for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) {
2020-11-23 17:28:07 +01:00
String jsonText = ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue(), true);
((StringTag) loreEntry).setValue(jsonText);
2019-01-06 20:27:41 +01:00
}
}
}
}
2018-10-27 13:25:42 +02:00
}
public static void toServer(Item item) {
if (item == null) return;
2020-08-16 16:24:06 +02:00
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier()));
2019-01-06 20:27:41 +01:00
if (item.getTag() == null) return;
// Display Name now uses JSON
Tag displayTag = item.getTag().get("display");
if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore");
if (loreTag instanceof ListTag) {
ListTag lore = (ListTag) loreTag;
ListTag savedLore = display.remove(NBT_TAG_NAME + "|Lore");
if (savedLore != null) {
2021-03-14 16:57:37 +01:00
display.put("Lore", new ListTag(savedLore.getValue()));
} else {
for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) {
2020-11-23 17:28:07 +01:00
((StringTag) loreEntry).setValue(ChatRewriter.jsonToLegacyText(((StringTag) loreEntry).getValue()));
2019-01-06 20:27:41 +01:00
}
}
}
}
}
2018-10-27 13:25:42 +02:00
}
}