216 lines
9.3 KiB
Java
216 lines
9.3 KiB
Java
package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
|
|
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
|
import com.github.steveice10.opennbt.tag.builtin.LongTag;
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
|
import us.myles.ViaVersion.api.minecraft.item.Item;
|
|
import us.myles.ViaVersion.api.remapper.PacketRemapper;
|
|
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
|
|
import us.myles.ViaVersion.api.type.Type;
|
|
import us.myles.ViaVersion.api.type.types.UUIDIntArrayType;
|
|
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14;
|
|
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15;
|
|
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
|
|
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16;
|
|
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16;
|
|
|
|
import java.util.UUID;
|
|
|
|
public class InventoryPackets {
|
|
|
|
public static void register(Protocol1_16To1_15_2 protocol) {
|
|
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
|
|
|
|
protocol.registerOutgoing(ClientboundPackets1_15.OPEN_WINDOW, new PacketRemapper() {
|
|
@Override
|
|
public void registerMap() {
|
|
map(Type.VAR_INT); // Window Id
|
|
map(Type.VAR_INT); // Window Type
|
|
map(Type.COMPONENT); // Window Title
|
|
|
|
handler(wrapper -> {
|
|
InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class);
|
|
int windowId = wrapper.get(Type.VAR_INT, 0);
|
|
int windowType = wrapper.get(Type.VAR_INT, 1);
|
|
if (windowType >= 20) { // smithing added with id 20
|
|
wrapper.set(Type.VAR_INT, 1, ++windowType);
|
|
}
|
|
inventoryTracker.setInventory((short) windowId);
|
|
});
|
|
}
|
|
});
|
|
|
|
protocol.registerOutgoing(ClientboundPackets1_15.CLOSE_WINDOW, new PacketRemapper() {
|
|
@Override
|
|
public void registerMap() {
|
|
map(Type.UNSIGNED_BYTE);
|
|
|
|
handler(wrapper -> {
|
|
InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class);
|
|
inventoryTracker.setInventory((short) -1);
|
|
});
|
|
}
|
|
});
|
|
|
|
protocol.registerOutgoing(ClientboundPackets1_15.WINDOW_PROPERTY, new PacketRemapper() {
|
|
@Override
|
|
public void registerMap() {
|
|
map(Type.UNSIGNED_BYTE); // Window Id
|
|
map(Type.SHORT); // Property
|
|
map(Type.SHORT); // Value
|
|
|
|
handler(wrapper -> {
|
|
short property = wrapper.get(Type.SHORT, 0);
|
|
if (property >= 4 && property <= 6) { // Enchantment id
|
|
short enchantmentId = wrapper.get(Type.SHORT, 1);
|
|
if (enchantmentId >= 11) { // soul_speed added with id 11
|
|
wrapper.set(Type.SHORT, 1, ++enchantmentId);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
itemRewriter.registerSetCooldown(ClientboundPackets1_15.COOLDOWN);
|
|
itemRewriter.registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
|
|
itemRewriter.registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
|
|
itemRewriter.registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
|
|
itemRewriter.registerAdvancements(ClientboundPackets1_15.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
|
|
|
|
protocol.registerOutgoing(ClientboundPackets1_15.ENTITY_EQUIPMENT, new PacketRemapper() {
|
|
@Override
|
|
public void registerMap() {
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
handler(wrapper -> {
|
|
int slot = wrapper.read(Type.VAR_INT);
|
|
wrapper.write(Type.BYTE, (byte) slot);
|
|
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
|
|
});
|
|
}
|
|
});
|
|
|
|
new RecipeRewriter1_14(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES);
|
|
|
|
itemRewriter.registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
|
|
itemRewriter.registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
|
|
|
|
protocol.registerIncoming(ServerboundPackets1_16.CLOSE_WINDOW, new PacketRemapper() {
|
|
@Override
|
|
public void registerMap() {
|
|
map(Type.UNSIGNED_BYTE);
|
|
|
|
handler(wrapper -> {
|
|
InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class);
|
|
inventoryTracker.setInventory((short) -1);
|
|
});
|
|
}
|
|
});
|
|
|
|
protocol.registerIncoming(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() {
|
|
@Override
|
|
public void registerMap() {
|
|
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void toClient(Item item) {
|
|
if (item == null) return;
|
|
|
|
if (item.getIdentifier() == 771 && item.getTag() != null) {
|
|
CompoundTag tag = item.getTag();
|
|
Tag ownerTag = tag.get("SkullOwner");
|
|
if (ownerTag instanceof CompoundTag) {
|
|
CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
|
|
Tag idTag = ownerCompundTag.get("Id");
|
|
if (idTag instanceof StringTag) {
|
|
UUID id = UUID.fromString((String) idTag.getValue());
|
|
ownerCompundTag.put(new IntArrayTag("Id", UUIDIntArrayType.uuidToIntArray(id)));
|
|
}
|
|
}
|
|
}
|
|
|
|
oldToNewAttributes(item);
|
|
item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getNewItemId(item.getIdentifier()));
|
|
}
|
|
|
|
public static void toServer(Item item) {
|
|
if (item == null) return;
|
|
|
|
item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getOldItemId(item.getIdentifier()));
|
|
|
|
if (item.getIdentifier() == 771 && item.getTag() != null) {
|
|
CompoundTag tag = item.getTag();
|
|
Tag ownerTag = tag.get("SkullOwner");
|
|
if (ownerTag instanceof CompoundTag) {
|
|
CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
|
|
Tag idTag = ownerCompundTag.get("Id");
|
|
if (idTag instanceof IntArrayTag) {
|
|
UUID id = UUIDIntArrayType.uuidFromIntArray((int[]) idTag.getValue());
|
|
ownerCompundTag.put(new StringTag("Id", id.toString()));
|
|
}
|
|
}
|
|
}
|
|
|
|
newToOldAttributes(item);
|
|
}
|
|
|
|
public static void oldToNewAttributes(Item item) {
|
|
if (item.getTag() == null) return;
|
|
|
|
ListTag attributes = item.getTag().get("AttributeModifiers");
|
|
if (attributes == null) return;
|
|
|
|
for (Tag tag : attributes) {
|
|
CompoundTag attribute = (CompoundTag) tag;
|
|
rewriteAttributeName(attribute, "AttributeName", false);
|
|
rewriteAttributeName(attribute, "Name", false);
|
|
Tag leastTag = attribute.get("UUIDLeast");
|
|
if (leastTag != null) {
|
|
Tag mostTag = attribute.get("UUIDMost");
|
|
int[] uuidIntArray = UUIDIntArrayType.bitsToIntArray(((Number) leastTag.getValue()).longValue(), ((Number) mostTag.getValue()).longValue());
|
|
attribute.put(new IntArrayTag("UUID", uuidIntArray));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void newToOldAttributes(Item item) {
|
|
if (item.getTag() == null) return;
|
|
|
|
ListTag attributes = item.getTag().get("AttributeModifiers");
|
|
if (attributes == null) return;
|
|
|
|
for (Tag tag : attributes) {
|
|
CompoundTag attribute = (CompoundTag) tag;
|
|
rewriteAttributeName(attribute, "AttributeName", true);
|
|
rewriteAttributeName(attribute, "Name", true);
|
|
IntArrayTag uuidTag = attribute.get("UUID");
|
|
if (uuidTag != null) {
|
|
UUID uuid = UUIDIntArrayType.uuidFromIntArray(uuidTag.getValue());
|
|
attribute.put(new LongTag("UUIDLeast", uuid.getLeastSignificantBits()));
|
|
attribute.put(new LongTag("UUIDMost", uuid.getMostSignificantBits()));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void rewriteAttributeName(CompoundTag compoundTag, String entryName, boolean inverse) {
|
|
StringTag attributeNameTag = compoundTag.get(entryName);
|
|
if (attributeNameTag == null) return;
|
|
|
|
String attributeName = attributeNameTag.getValue();
|
|
if (inverse && !attributeName.startsWith("minecraft:")) {
|
|
attributeName = "minecraft:" + attributeName;
|
|
}
|
|
|
|
String mappedAttribute = (inverse ? Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings().inverse()
|
|
: Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings()).get(attributeName);
|
|
if (mappedAttribute == null) return;
|
|
|
|
attributeNameTag.setValue(mappedAttribute);
|
|
}
|
|
}
|