3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-04 23:30:24 +01:00

Make Item an interface, more OOP for the ItemRewriter

Dieser Commit ist enthalten in:
KennyTV 2021-06-04 12:15:14 +02:00
Ursprung 05f9fc1ddc
Commit 2b8c5082ed
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
63 geänderte Dateien mit 715 neuen und 507 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,124 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* 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.
*/
package com.viaversion.viaversion.api.minecraft.item;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.gson.annotations.SerializedName;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Objects;
public class DataItem implements Item {
@SerializedName(value = "identifier", alternate = "id")
private int identifier;
private byte amount;
private short data;
private CompoundTag tag;
public DataItem() {
}
public DataItem(int identifier, byte amount, short data, @Nullable CompoundTag tag) {
this.identifier = identifier;
this.amount = amount;
this.data = data;
this.tag = tag;
}
public DataItem(Item toCopy) {
this(toCopy.identifier(), (byte) toCopy.amount(), toCopy.data(), toCopy.tag());
}
@Override
public int identifier() {
return identifier;
}
@Override
public void setIdentifier(int identifier) {
this.identifier = identifier;
}
@Override
public int amount() {
return amount;
}
@Override
public void setAmount(int amount) {
if (amount > Byte.MAX_VALUE && amount < Byte.MIN_VALUE) {
throw new IllegalArgumentException("Invalid item amount: " + amount);
}
this.amount = (byte) amount;
}
@Override
public short data() {
return data;
}
@Override
public void setData(short data) {
this.data = data;
}
@Override
public @Nullable CompoundTag tag() {
return tag;
}
@Override
public void setTag(@Nullable CompoundTag tag) {
this.tag = tag;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DataItem item = (DataItem) o;
if (identifier != item.identifier) return false;
if (amount != item.amount) return false;
if (data != item.data) return false;
return Objects.equals(tag, item.tag);
}
@Override
public int hashCode() {
int result = identifier;
result = 31 * result + (int) amount;
result = 31 * result + (int) data;
result = 31 * result + (tag != null ? tag.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Item{" +
"identifier=" + identifier +
", amount=" + amount +
", data=" + data +
", tag=" + tag +
'}';
}
}

Datei anzeigen

@ -23,91 +23,27 @@
package com.viaversion.viaversion.api.minecraft.item;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.gson.annotations.SerializedName;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Objects;
public interface Item {
public class Item {
@SerializedName(value = "identifier", alternate = "id")
private int identifier;
private byte amount;
private short data;
private CompoundTag tag;
int identifier();
public Item() {
void setIdentifier(int identifier);
int amount();
void setAmount(int amount);
default short data() {
return 0;
}
public Item(int identifier, byte amount, short data, @Nullable CompoundTag tag) {
this.identifier = identifier;
this.amount = amount;
this.data = data;
this.tag = tag;
default void setData(short data) {
throw new UnsupportedOperationException();
}
public Item(Item toCopy) {
this(toCopy.getIdentifier(), toCopy.getAmount(), toCopy.getData(), toCopy.getTag());
}
@Nullable CompoundTag tag();
public int getIdentifier() {
return identifier;
}
public void setIdentifier(int identifier) {
this.identifier = identifier;
}
public byte getAmount() {
return amount;
}
public void setAmount(byte amount) {
this.amount = amount;
}
public short getData() {
return data;
}
public void setData(short data) {
this.data = data;
}
public @Nullable CompoundTag getTag() {
return tag;
}
public void setTag(@Nullable CompoundTag tag) {
this.tag = tag;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
if (identifier != item.identifier) return false;
if (amount != item.amount) return false;
if (data != item.data) return false;
return Objects.equals(tag, item.tag);
}
@Override
public int hashCode() {
int result = identifier;
result = 31 * result + (int) amount;
result = 31 * result + (int) data;
result = 31 * result + (tag != null ? tag.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Item{" +
"identifier=" + identifier +
", amount=" + amount +
", data=" + data +
", tag=" + tag +
'}';
}
void setTag(@Nullable CompoundTag tag);
}

Datei anzeigen

@ -32,6 +32,7 @@ import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@ -274,6 +275,15 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
return null;
}
/**
* Returns the protocol's item rewriter if present.
*
* @return item rewriter
*/
default @Nullable ItemRewriter getItemRewriter() {
return null;
}
/**
* Returns whether this protocol is a base protocol.
*

Datei anzeigen

@ -0,0 +1,46 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* 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.
*/
package com.viaversion.viaversion.api.rewriter;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import org.checkerframework.checker.nullness.qual.Nullable;
public interface ItemRewriter<T extends Protocol> extends Rewriter<T> {
/**
* Returns the rewritten item, which may or may not be the same given Item instance.
*
* @param item item
* @return rewritten item
*/
@Nullable Item handleItemToClient(@Nullable Item item);
/**
* Returns the rewritten item, which may or may not be the same given Item instance.
*
* @param item item
* @return rewritten item
*/
@Nullable Item handleItemToServer(@Nullable Item item);
}

Datei anzeigen

@ -32,7 +32,7 @@ public abstract class RewriterBase<T extends Protocol> implements Rewriter<T> {
}
@Override
public void register() {
public final void register() {
registerPackets();
registerRewrites();
}

Datei anzeigen

@ -22,6 +22,7 @@
*/
package com.viaversion.viaversion.api.type.types.minecraft;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
import io.netty.buffer.ByteBuf;
@ -37,7 +38,7 @@ public class FlatItemType extends BaseItemType {
if (id < 0) {
return null;
} else {
Item item = new Item();
Item item = new DataItem();
item.setIdentifier(id);
item.setAmount(buffer.readByte());
item.setTag(Type.NBT.read(buffer));
@ -50,9 +51,9 @@ public class FlatItemType extends BaseItemType {
if (object == null) {
buffer.writeShort(-1);
} else {
buffer.writeShort(object.getIdentifier());
buffer.writeByte(object.getAmount());
Type.NBT.write(buffer, object.getTag());
buffer.writeShort(object.identifier());
buffer.writeByte(object.amount());
Type.NBT.write(buffer, object.tag());
}
}
}

Datei anzeigen

@ -22,6 +22,7 @@
*/
package com.viaversion.viaversion.api.type.types.minecraft;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import io.netty.buffer.ByteBuf;
@ -36,7 +37,7 @@ public class FlatVarIntItemType extends BaseItemType {
if (!present) {
return null;
} else {
Item item = new Item();
Item item = new DataItem();
item.setIdentifier(VAR_INT.readPrimitive(buffer));
item.setAmount(buffer.readByte());
item.setTag(NBT.read(buffer));
@ -50,9 +51,9 @@ public class FlatVarIntItemType extends BaseItemType {
buffer.writeBoolean(false);
} else {
buffer.writeBoolean(true);
VAR_INT.writePrimitive(buffer, object.getIdentifier());
buffer.writeByte(object.getAmount());
NBT.write(buffer, object.getTag());
VAR_INT.writePrimitive(buffer, object.identifier());
buffer.writeByte(object.amount());
NBT.write(buffer, object.tag());
}
}
}

Datei anzeigen

@ -22,6 +22,7 @@
*/
package com.viaversion.viaversion.api.type.types.minecraft;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import io.netty.buffer.ByteBuf;
@ -36,7 +37,7 @@ public class ItemType extends BaseItemType {
if (id < 0) {
return null;
} else {
Item item = new Item();
Item item = new DataItem();
item.setIdentifier(id);
item.setAmount(buffer.readByte());
item.setData(buffer.readShort());
@ -50,10 +51,10 @@ public class ItemType extends BaseItemType {
if (object == null) {
buffer.writeShort(-1);
} else {
buffer.writeShort(object.getIdentifier());
buffer.writeByte(object.getAmount());
buffer.writeShort(object.getData());
NBT.write(buffer, object.getTag());
buffer.writeShort(object.identifier());
buffer.writeByte(object.amount());
buffer.writeShort(object.data());
NBT.write(buffer, object.tag());
}
}
}

Datei anzeigen

@ -17,6 +17,7 @@
*/
package com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -52,7 +53,7 @@ public class HandItemCache extends BukkitRunnable {
}
public static Item convert(ItemStack itemInHand) {
if (itemInHand == null) return new Item(0, (byte) 0, (short) 0, null);
return new Item(itemInHand.getTypeId(), (byte) itemInHand.getAmount(), itemInHand.getDurability(), null);
if (itemInHand == null) return new DataItem(0, (byte) 0, (short) 0, null);
return new DataItem(itemInHand.getTypeId(), (byte) itemInHand.getAmount(), itemInHand.getDurability(), null);
}
}

Datei anzeigen

@ -25,6 +25,7 @@ import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_9;
import com.viaversion.viaversion.protocols.protocol1_10to1_9_3.packets.InventoryPackets;
@ -54,6 +55,7 @@ public class Protocol1_10To1_9_3_4 extends AbstractProtocol<ClientboundPackets1_
return metaList;
}
};
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_10To1_9_3_4() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class);
@ -61,8 +63,7 @@ public class Protocol1_10To1_9_3_4 extends AbstractProtocol<ClientboundPackets1_
@Override
protected void registerPackets() {
InventoryPackets.register(this);
itemRewriter.register();
// Named sound effect
registerClientbound(State.PLAY, 0x19, 0x19, new PacketRemapper() {
@ -196,4 +197,9 @@ public class Protocol1_10To1_9_3_4 extends AbstractProtocol<ClientboundPackets1_
public void init(UserConnection userConnection) {
userConnection.put(new ResourcePackTracker());
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -23,21 +23,25 @@ import com.viaversion.viaversion.protocols.protocol1_10to1_9_3.Protocol1_10To1_9
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_10To1_9_3_4> {
public static void register(Protocol1_10To1_9_3_4 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, item -> {
}, InventoryPackets::toServerItem);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
public InventoryPackets(Protocol1_10To1_9_3_4 protocol) {
super(protocol);
}
public static void toServerItem(Item item) {
if (item == null) return;
boolean newItem = item.getIdentifier() >= 213 && item.getIdentifier() <= 217;
@Override
public void registerPackets() {
registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
@Override
public Item handleItemToServer(Item item) {
if (item == null) return null;
boolean newItem = item.identifier() >= 213 && item.identifier() <= 217;
if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1);
item.setData((short) 0);
}
return item;
}
}

Datei anzeigen

@ -18,19 +18,26 @@
package com.viaversion.viaversion.protocols.protocol1_11_1to1_11;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.protocols.protocol1_11_1to1_11.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
public class Protocol1_11_1To1_11 extends AbstractProtocol<ClientboundPackets1_9_3, ClientboundPackets1_9_3, ServerboundPackets1_9_3, ServerboundPackets1_9_3> {
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_11_1To1_11() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class);
}
@Override
protected void registerPackets() {
InventoryPackets.register(this);
itemRewriter.register();
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -23,21 +23,25 @@ import com.viaversion.viaversion.protocols.protocol1_11_1to1_11.Protocol1_11_1To
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_11_1To1_11> {
public static void register(Protocol1_11_1To1_11 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, item -> {
}, InventoryPackets::toServerItem);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
public InventoryPackets(Protocol1_11_1To1_11 protocol) {
super(protocol);
}
public static void toServerItem(Item item) {
if (item == null) return;
boolean newItem = item.getIdentifier() == 452;
@Override
public void registerPackets() {
registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
@Override
public Item handleItemToServer(Item item) {
if (item == null) return null;
boolean newItem = item.identifier() == 452;
if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1);
item.setData((short) 0);
}
return null;
}
}

Datei anzeigen

@ -139,9 +139,9 @@ public class EntityIdRewriter {
public static void toClientItem(Item item, boolean backwards) {
if (hasEntityTag(item)) {
toClient(item.getTag().get("EntityTag"), backwards);
toClient(item.tag().get("EntityTag"), backwards);
}
if (item != null && item.getAmount() <= 0) item.setAmount((byte) 1);
if (item != null && item.amount() <= 0) item.setAmount(1);
}
public static void toServerItem(Item item) {
@ -151,7 +151,7 @@ public class EntityIdRewriter {
public static void toServerItem(Item item, boolean backwards) {
if (!hasEntityTag(item)) return;
CompoundTag entityTag = item.getTag().get("EntityTag");
CompoundTag entityTag = item.tag().get("EntityTag");
Tag idTag = entityTag.get("id");
if (idTag instanceof StringTag) {
StringTag id = (StringTag) idTag;
@ -163,9 +163,9 @@ public class EntityIdRewriter {
}
private static boolean hasEntityTag(Item item) {
if (item == null || item.getIdentifier() != 383) return false; // Monster Egg
if (item == null || item.identifier() != 383) return false; // Monster Egg
CompoundTag tag = item.getTag();
CompoundTag tag = item.tag();
if (tag == null) return false;
Tag entityTag = tag.get("EntityTag");

Datei anzeigen

@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_9;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping;
@ -51,6 +52,7 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
};
private final EntityRewriter entityRewriter = new MetadataRewriter1_11To1_10(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_11To1_10() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class);
@ -59,8 +61,7 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
@Override
protected void registerPackets() {
entityRewriter.register();
InventoryPackets.register(this);
itemRewriter.register();
registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {
@Override
@ -388,4 +389,9 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
public EntityRewriter getEntityRewriter() {
return entityRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -21,6 +21,7 @@ import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types.EntityType;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9;
@ -42,7 +43,7 @@ public class MetadataRewriter1_11To1_10 extends EntityRewriter<Protocol1_11To1_1
@Override
protected void handleMetadata(int entityId, com.viaversion.viaversion.api.minecraft.entities.EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
if (metadata.getValue() instanceof Item) {
if (metadata.getValue() instanceof DataItem) {
// Apply rewrite
EntityIdRewriter.toClientItem((Item) metadata.getValue());
}

Datei anzeigen

@ -28,14 +28,17 @@ import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPac
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_11To1_10> {
public static void register(Protocol1_11To1_10 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, EntityIdRewriter::toClientItem, InventoryPackets::toServerItem);
public InventoryPackets(Protocol1_11To1_10 protocol) {
super(protocol);
}
itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
itemRewriter.registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
@Override
public void registerPackets() {
registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
// Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
@ -68,19 +71,26 @@ public class InventoryPackets {
}
});
itemRewriter.registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
public static void toServerItem(Item item) {
@Override
public Item handleItemToClient(Item item) {
EntityIdRewriter.toClientItem(item);
return item;
}
@Override
public Item handleItemToServer(Item item) {
EntityIdRewriter.toServerItem(item);
if (item == null) return;
boolean newItem = item.getIdentifier() >= 218 && item.getIdentifier() <= 234;
newItem |= item.getIdentifier() == 449 || item.getIdentifier() == 450;
if (item == null) return null;
boolean newItem = item.identifier() >= 218 && item.identifier() <= 234;
newItem |= item.identifier() == 449 || item.identifier() == 450;
if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1);
item.setData((short) 0);
}
return item;
}
}

Datei anzeigen

@ -23,14 +23,14 @@ public class BedRewriter {
public static void toClientItem(Item item) {
if (item == null) return;
if (item.getIdentifier() == 355 && item.getData() == 0) {
if (item.identifier() == 355 && item.data() == 0) {
item.setData((short) 14);
}
}
public static void toServerItem(Item item) {
if (item == null) return;
if (item.getIdentifier() == 355 && item.getData() == 14) {
if (item.identifier() == 355 && item.data() == 14) {
item.setData((short) 0);
}
}

Datei anzeigen

@ -31,6 +31,7 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_12;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
@ -48,6 +49,7 @@ import com.viaversion.viaversion.rewriter.SoundRewriter;
public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9_3, ClientboundPackets1_12, ServerboundPackets1_9_3, ServerboundPackets1_12> {
private final EntityRewriter metadataRewriter = new MetadataRewriter1_12To1_11_1(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_12To1_11_1() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_12.class, ServerboundPackets1_9_3.class, ServerboundPackets1_12.class);
@ -56,8 +58,7 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9
@Override
protected void registerPackets() {
metadataRewriter.register();
InventoryPackets.register(this);
itemRewriter.register();
registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {
@Override
@ -268,4 +269,9 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_12to1_11_1.metadata;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.BedRewriter;
@ -36,7 +37,7 @@ public class MetadataRewriter1_12To1_11_1 extends EntityRewriter<Protocol1_12To1
@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
if (metadata.getValue() instanceof Item) {
if (metadata.getValue() instanceof DataItem) {
// Apply rewrite
BedRewriter.toClientItem((Item) metadata.getValue());
}

Datei anzeigen

@ -30,14 +30,17 @@ import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.Invent
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_12To1_11_1> {
public static void register(Protocol1_12To1_11_1 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, BedRewriter::toClientItem, InventoryPackets::toServerItem);
public InventoryPackets(Protocol1_12To1_11_1 protocol) {
super(protocol);
}
itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
itemRewriter.registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
@Override
public void registerPackets() {
registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
// Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
@ -113,17 +116,20 @@ public class InventoryPackets {
);
// Creative Inventory Action
itemRewriter.registerCreativeInvAction(ServerboundPackets1_12.CREATIVE_INVENTORY_ACTION, Type.ITEM);
registerCreativeInvAction(ServerboundPackets1_12.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
public static void toServerItem(Item item) {
@Override
public Item handleItemToServer(Item item) {
if (item == null) return null;
BedRewriter.toServerItem(item);
if (item == null) return;
boolean newItem = item.getIdentifier() >= 235 && item.getIdentifier() <= 252;
newItem |= item.getIdentifier() == 453;
boolean newItem = item.identifier() >= 235 && item.identifier() <= 252;
newItem |= item.identifier() == 453;
if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1);
item.setData((short) 0);
}
return item;
}
}

Datei anzeigen

@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13;
@ -45,6 +46,7 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
public static final MappingData MAPPINGS = new MappingDataBase("1.13", "1.13.2", true);
private final EntityRewriter entityRewriter = new MetadataRewriter1_13_1To1_13(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_13_1To1_13() {
super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class);
@ -53,9 +55,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
entityRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
InventoryPackets.register(this);
WorldPackets.register(this);
registerServerbound(ServerboundPackets1_13.TAB_COMPLETE, new PacketRemapper() {
@ -77,12 +79,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
public void registerMap() {
map(Type.FLAT_ITEM);
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
InventoryPackets.toServer(item);
}
handler(wrapper -> {
Item item = wrapper.get(Type.FLAT_ITEM, 0);
itemRewriter.handleItemToServer(item);
});
handler(new PacketHandler() {
@Override
@ -166,4 +165,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() {
return entityRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -25,7 +25,6 @@ import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_13;
import com.viaversion.viaversion.api.type.types.Particle;
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13;
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List;
@ -40,7 +39,7 @@ public class MetadataRewriter1_13_1To1_13 extends EntityRewriter<Protocol1_13_1T
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
// 1.13 changed item to flat item (no data)
if (metadata.metaType() == MetaType1_13.Slot) {
InventoryPackets.toClient((Item) metadata.getValue());
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_13.BlockID) {
// Convert to new block id
int data = (int) metadata.getValue();

Datei anzeigen

@ -17,7 +17,6 @@
*/
package com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
@ -29,14 +28,18 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewri
import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_13_1To1_13> {
public static void register(Protocol1_13_1To1_13 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
itemRewriter.registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_ITEM);
itemRewriter.registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_ITEM_ARRAY);
itemRewriter.registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_ITEM);
itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
public InventoryPackets(Protocol1_13_1To1_13 protocol) {
super(protocol);
}
@Override
public void registerPackets() {
registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_ITEM);
registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_ITEM_ARRAY);
registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_ITEM);
registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
@ -52,14 +55,14 @@ public class InventoryPackets {
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
toClient(wrapper.passthrough(Type.FLAT_ITEM));
handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
// Output Item
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM));
handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) {
// Second Item
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM));
handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
}
wrapper.passthrough(Type.BOOLEAN); // Trade disabled
@ -72,9 +75,9 @@ public class InventoryPackets {
}
});
itemRewriter.registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_ITEM);
registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol, InventoryPackets::toClient);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
@ -90,19 +93,9 @@ public class InventoryPackets {
}
});
itemRewriter.registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_ITEM, Type.FLOAT);
}
public static void toClient(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_13_1To1_13.MAPPINGS.getNewItemId(item.getIdentifier()));
}
public static void toServer(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_13_1To1_13.MAPPINGS.getOldItemId(item.getIdentifier()));
registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_ITEM, Type.FLOAT);
}
}

Datei anzeigen

@ -25,6 +25,7 @@ import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
@ -34,6 +35,7 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
@ -73,6 +75,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
private static final Map<Character, Character> SCOREBOARD_TEAM_NAME_REWRITE = new HashMap<>();
private static final Set<Character> FORMATTING_CODES = Sets.newHashSet('k', 'l', 'm', 'n', 'o', 'r');
private final EntityRewriter entityRewriter = new MetadataRewriter1_13To1_12_2(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
static {
SCOREBOARD_TEAM_NAME_REWRITE.put('0', 'g');
@ -157,11 +160,10 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
entityRewriter.register();
itemRewriter.register();
// Register grouped packet changes
EntityPackets.register(this);
WorldPackets.register(this);
InventoryPackets.register(this);
registerClientbound(State.LOGIN, 0x0, 0x0, new PacketRemapper() {
@Override
@ -516,11 +518,11 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
Item[] clone = ingredient.clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue;
clone[i] = new Item(clone[i]);
clone[i] = new DataItem(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
}
wrapper.write(Type.FLAT_ITEM, new Item(entry.getValue().getResult()));
wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
break;
}
case "crafting_shaped": {
@ -531,11 +533,11 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
Item[] clone = ingredient.clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue;
clone[i] = new Item(clone[i]);
clone[i] = new DataItem(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
}
wrapper.write(Type.FLAT_ITEM, new Item(entry.getValue().getResult()));
wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
break;
}
case "smelting": {
@ -543,10 +545,10 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
Item[] clone = entry.getValue().getIngredient().clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue;
clone[i] = new Item(clone[i]);
clone[i] = new DataItem(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
wrapper.write(Type.FLAT_ITEM, new Item(entry.getValue().getResult()));
wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
wrapper.write(Type.FLOAT, entry.getValue().getExperience());
wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime());
break;
@ -731,7 +733,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); // Title
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); // Description
Item icon = wrapper.read(Type.ITEM);
InventoryPackets.toClient(icon);
itemRewriter.handleItemToClient(icon);
wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item
wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags
@ -813,7 +815,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
Item item = wrapper.read(Type.FLAT_ITEM);
boolean isSigning = wrapper.read(Type.BOOLEAN);
InventoryPackets.toServer(item);
itemRewriter.handleItemToServer(item);
wrapper.write(Type.STRING, isSigning ? "MC|BSign" : "MC|BEdit"); // Channel
wrapper.write(Type.ITEM, item);
@ -1108,4 +1110,9 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() {
return entityRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -24,11 +24,11 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import java.io.IOException;
@ -40,6 +40,7 @@ public class ComponentRewriter1_13 extends ComponentRewriter {
}
public ComponentRewriter1_13() {
super(Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class));
}
@Override
@ -70,14 +71,14 @@ public class ComponentRewriter1_13 extends ComponentRewriter {
// Call item converter
short damage = damageTag != null ? damageTag.asShort() : 0;
Item item = new Item();
Item item = new DataItem();
item.setData(damage);
item.setTag(itemTag);
handleItem(item);
protocol.getItemRewriter().handleItemToClient(item);
// Serialize again
if (damage != item.getData()) {
tag.put("Damage", new ShortTag(item.getData()));
if (damage != item.data()) {
tag.put("Damage", new ShortTag(item.data()));
}
if (itemTag != null) {
tag.put("tag", itemTag);
@ -97,10 +98,6 @@ public class ComponentRewriter1_13 extends ComponentRewriter {
}
}
protected void handleItem(Item item) {
InventoryPackets.toClient(item);
}
protected String findItemNBT(JsonElement element) {
if (element.isJsonArray()) {
for (JsonElement jsonElement : element.getAsJsonArray()) {

Datei anzeigen

@ -18,10 +18,11 @@
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.Particle;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets;
import java.util.ArrayList;
@ -143,14 +144,14 @@ public class ParticleRewriter {
public Particle handler(Particle particle, Integer[] data) {
Item item;
if (data.length == 1)
item = new Item(data[0], (byte) 1, (short) 0, null);
item = new DataItem(data[0], (byte) 1, (short) 0, null);
else if (data.length == 2)
item = new Item(data[0], (byte) 1, data[1].shortValue(), null);
item = new DataItem(data[0], (byte) 1, data[1].shortValue(), null);
else
return particle;
// Transform to new Item
InventoryPackets.toClient(item);
Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class).getItemRewriter().handleItemToClient(item);
particle.getArguments().add(new Particle.ParticleData(Type.FLAT_ITEM, item)); // Item Slot The item that will be used.
return particle;

Datei anzeigen

@ -18,7 +18,7 @@
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data;
import com.google.gson.reflect.TypeToken;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.util.GsonUtil;
import java.io.IOException;
@ -55,9 +55,9 @@ public class RecipeData {
private int height;
private float experience;
private int cookingTime;
private Item[] ingredient;
private Item[][] ingredients;
private Item result;
private DataItem[] ingredient;
private DataItem[][] ingredients;
private DataItem result;
public String getType() {
return type;
@ -107,27 +107,27 @@ public class RecipeData {
this.cookingTime = cookingTime;
}
public Item[] getIngredient() {
public DataItem[] getIngredient() {
return ingredient;
}
public void setIngredient(Item[] ingredient) {
public void setIngredient(DataItem[] ingredient) {
this.ingredient = ingredient;
}
public Item[][] getIngredients() {
public DataItem[][] getIngredients() {
return ingredients;
}
public void setIngredients(Item[][] ingredients) {
public void setIngredients(DataItem[][] ingredients) {
this.ingredients = ingredients;
}
public Item getResult() {
public DataItem getResult() {
return result;
}
public void setResult(Item result) {
public void setResult(DataItem result) {
this.result = result;
}
}

Datei anzeigen

@ -29,8 +29,8 @@ import com.viaversion.viaversion.rewriter.RecipeRewriter;
*/
public class RecipeRewriter1_13_2 extends RecipeRewriter {
public RecipeRewriter1_13_2(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
public RecipeRewriter1_13_2(Protocol protocol) {
super(protocol);
recipeHandlers.put("crafting_shapeless", this::handleCraftingShapeless);
recipeHandlers.put("crafting_shaped", this::handleCraftingShaped);
recipeHandlers.put("smelting", this::handleSmelting);
@ -40,10 +40,10 @@ public class RecipeRewriter1_13_2 extends RecipeRewriter {
wrapper.passthrough(Type.STRING); // Group
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
rewrite(item);
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
wrapper.passthrough(Type.FLOAT); // EXP
wrapper.passthrough(Type.VAR_INT); // Cooking time
@ -55,10 +55,10 @@ public class RecipeRewriter1_13_2 extends RecipeRewriter {
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
rewrite(item);
}
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
public void handleCraftingShapeless(PacketWrapper wrapper) throws Exception {
@ -67,9 +67,9 @@ public class RecipeRewriter1_13_2 extends RecipeRewriter {
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
rewrite(item);
}
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
}

Datei anzeigen

@ -28,7 +28,6 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ParticleRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
@ -69,7 +68,7 @@ public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<Protocol1_13To1
// 1.13 changed item to flat item (no data)
if (metadata.metaType() == MetaType1_13.Slot) {
metadata.setMetaType(MetaType1_13.Slot);
InventoryPackets.toClient((Item) metadata.getValue());
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_13.BlockID) {
// Convert to new block id
metadata.setValue(WorldPackets.toNewId((int) metadata.getValue()));

Datei anzeigen

@ -49,12 +49,15 @@ import java.util.List;
import java.util.Locale;
import java.util.Optional;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_13To1_12_2> {
private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_13To1_12_2.class.getSimpleName();
public static void register(Protocol1_13To1_12_2 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
public InventoryPackets(Protocol1_13To1_12_2 protocol) {
super(protocol);
}
@Override
public void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_12_1.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
@ -62,7 +65,7 @@ public class InventoryPackets {
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM, Type.FLAT_ITEM); // 2 - Slot Value
handler(itemRewriter.itemToClientHandler(Type.FLAT_ITEM));
handler(itemToClientHandler(Type.FLAT_ITEM));
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.WINDOW_ITEMS, new PacketRemapper() {
@ -71,7 +74,7 @@ public class InventoryPackets {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values
handler(itemRewriter.itemArrayHandler(Type.FLAT_ITEM_ARRAY));
handler(itemArrayHandler(Type.FLAT_ITEM_ARRAY));
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.WINDOW_PROPERTY, new PacketRemapper() {
@ -142,18 +145,18 @@ public class InventoryPackets {
for (int i = 0; i < size; i++) {
// Input Item
Item input = wrapper.read(Type.ITEM);
InventoryPackets.toClient(input);
handleItemToClient(input);
wrapper.write(Type.FLAT_ITEM, input);
// Output Item
Item output = wrapper.read(Type.ITEM);
InventoryPackets.toClient(output);
handleItemToClient(output);
wrapper.write(Type.FLAT_ITEM, output);
boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) {
// Second Item
Item second = wrapper.read(Type.ITEM);
InventoryPackets.toClient(second);
handleItemToClient(second);
wrapper.write(Type.FLAT_ITEM, second);
}
@ -203,7 +206,7 @@ public class InventoryPackets {
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM, Type.FLAT_ITEM); // 2 - Item
handler(itemRewriter.itemToClientHandler(Type.FLAT_ITEM));
handler(itemToClientHandler(Type.FLAT_ITEM));
}
});
@ -218,7 +221,7 @@ public class InventoryPackets {
map(Type.VAR_INT); // 4 - Mode
map(Type.FLAT_ITEM, Type.ITEM); // 5 - Clicked Item
handler(itemRewriter.itemToServerHandler(Type.ITEM));
handler(itemToServerHandler(Type.ITEM));
}
});
@ -263,37 +266,36 @@ public class InventoryPackets {
map(Type.SHORT); // 0 - Slot
map(Type.FLAT_ITEM, Type.ITEM); // 1 - Clicked Item
handler(itemRewriter.itemToServerHandler(Type.ITEM));
handler(itemToServerHandler(Type.ITEM));
}
});
}
// TODO CLEANUP / SMARTER REWRITE SYSTEM
// TODO Rewrite identifiers
public static void toClient(Item item) {
if (item == null) return;
CompoundTag tag = item.getTag();
@Override
public Item handleItemToClient(Item item) {
if (item == null) return null;
CompoundTag tag = item.tag();
// Save original id
int originalId = (item.getIdentifier() << 16 | item.getData() & 0xFFFF);
int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
int rawId = (item.getIdentifier() << 4 | item.getData() & 0xF);
int rawId = (item.identifier() << 4 | item.data() & 0xF);
// NBT Additions
if (isDamageable(item.getIdentifier())) {
if (isDamageable(item.identifier())) {
if (tag == null) item.setTag(tag = new CompoundTag());
tag.put("Damage", new IntTag(item.getData()));
tag.put("Damage", new IntTag(item.data()));
}
if (item.getIdentifier() == 358) { // map
if (item.identifier() == 358) { // map
if (tag == null) item.setTag(tag = new CompoundTag());
tag.put("map", new IntTag(item.getData()));
tag.put("map", new IntTag(item.data()));
}
// NBT Changes
if (tag != null) {
// Invert banner/shield color id
boolean banner = item.getIdentifier() == 425;
if (banner || item.getIdentifier() == 442) {
boolean banner = item.identifier() == 425;
if (banner || item.identifier() == 442) {
if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) {
@ -409,7 +411,7 @@ public class InventoryPackets {
tag.put("CanDestroy", newCanDestroy);
}
// Handle SpawnEggs
if (item.getIdentifier() == 383) {
if (item.identifier() == 383) {
if (tag.get("EntityTag") instanceof CompoundTag) {
CompoundTag entityTag = tag.get("EntityTag");
if (entityTag.get("id") instanceof StringTag) {
@ -437,17 +439,17 @@ public class InventoryPackets {
}
if (!Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId)) {
if (!isDamageable(item.getIdentifier()) && item.getIdentifier() != 358) { // Map
if (!isDamageable(item.identifier()) && item.identifier() != 358) { // Map
if (tag == null) item.setTag(tag = new CompoundTag());
tag.put(NBT_TAG_NAME, new IntTag(originalId)); // Data will be lost, saving original id
}
if (item.getIdentifier() == 31 && item.getData() == 0) { // Shrub was removed
if (item.identifier() == 31 && item.data() == 0) { // Shrub was removed
rawId = 32 << 4; // Dead Bush
} else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) {
rawId &= ~0xF; // Remove data
} else {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.getIdentifier());
Via.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
}
rawId = 16; // Stone
}
@ -455,6 +457,7 @@ public class InventoryPackets {
item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId));
item.setData((short) 0);
return item;
}
public static String getNewPluginChannelId(String old) {
@ -485,13 +488,14 @@ public class InventoryPackets {
}
}
public static void toServer(Item item) {
if (item == null) return;
@Override
public Item handleItemToServer(Item item) {
if (item == null) return null;
Integer rawId = null;
boolean gotRawIdFromTag = false;
CompoundTag tag = item.getTag();
CompoundTag tag = item.tag();
// Use tag to get original ID and data
if (tag != null) {
@ -505,7 +509,7 @@ public class InventoryPackets {
}
if (rawId == null) {
int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.getIdentifier());
int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.identifier());
if (oldId != -1) {
// Handle spawn eggs
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
@ -526,7 +530,7 @@ public class InventoryPackets {
if (rawId == null) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Failed to get 1.12 item for " + item.getIdentifier());
Via.getPlatform().getLogger().warning("Failed to get 1.12 item for " + item.identifier());
}
rawId = 0x10000; // Stone
}
@ -536,7 +540,7 @@ public class InventoryPackets {
// NBT changes
if (tag != null) {
if (isDamageable(item.getIdentifier())) {
if (isDamageable(item.identifier())) {
if (tag.get("Damage") instanceof IntTag) {
if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("Damage").getValue());
@ -545,7 +549,7 @@ public class InventoryPackets {
}
}
if (item.getIdentifier() == 358) { // map
if (item.identifier() == 358) { // map
if (tag.get("map") instanceof IntTag) {
if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("map").getValue());
@ -554,7 +558,7 @@ public class InventoryPackets {
}
}
if (item.getIdentifier() == 442 || item.getIdentifier() == 425) { // shield / banner
if (item.identifier() == 442 || item.identifier() == 425) { // shield / banner
if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) {
@ -669,6 +673,7 @@ public class InventoryPackets {
tag.put("CanDestroy", newCanDestroy);
}
}
return item;
}
public static String getOldPluginChannelId(String newId) {

Datei anzeigen

@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
@ -44,6 +45,7 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_1
public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_14To1_13_2(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_14To1_13_2() {
super(ClientboundPackets1_13.class, ClientboundPackets1_14.class, ServerboundPackets1_13.class, ServerboundPackets1_14.class);
@ -52,8 +54,8 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
InventoryPackets.register(this);
EntityPackets.register(this);
WorldPackets.register(this);
PlayerPackets.register(this);
@ -162,4 +164,9 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -18,10 +18,8 @@
package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.InventoryPackets;
public class ComponentRewriter1_14 extends ComponentRewriter1_13 {
@ -29,11 +27,6 @@ public class ComponentRewriter1_14 extends ComponentRewriter1_13 {
super(protocol);
}
@Override
protected void handleItem(Item item) {
InventoryPackets.toClient(item);
}
@Override
protected void handleTranslate(JsonObject object, String translate) {
// Nothing

Datei anzeigen

@ -26,8 +26,8 @@ import com.viaversion.viaversion.rewriter.ItemRewriter;
public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 {
public RecipeRewriter1_14(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
public RecipeRewriter1_14(Protocol protocol) {
super(protocol);
recipeHandlers.put("stonecutting", this::handleStonecutting);
recipeHandlers.put("blasting", this::handleSmelting);
@ -39,9 +39,9 @@ public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 {
wrapper.passthrough(Type.STRING);
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
rewrite(item);
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
}

Datei anzeigen

@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.minecraft.VillagerData;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_14;
@ -30,7 +31,6 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.Particle;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
import com.viaversion.viaversion.rewriter.EntityRewriter;
@ -51,7 +51,7 @@ public class MetadataRewriter1_14To1_13_2 extends EntityRewriter<Protocol1_14To1
EntityTracker1_14 tracker = tracker(connection);
if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue());
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) {
// Convert to new block id
int data = (int) metadata.getValue();
@ -123,11 +123,11 @@ public class MetadataRewriter1_14To1_13_2 extends EntityRewriter<Protocol1_14To1
int armorType = (int) metadata.getValue();
Item armorItem = null;
if (armorType == 1) { //iron armor
armorItem = new Item(protocol.getMappingData().getNewItemId(727), (byte) 1, (short) 0, null);
armorItem = new DataItem(protocol.getMappingData().getNewItemId(727), (byte) 1, (short) 0, null);
} else if (armorType == 2) { //gold armor
armorItem = new Item(protocol.getMappingData().getNewItemId(728), (byte) 1, (short) 0, null);
armorItem = new DataItem(protocol.getMappingData().getNewItemId(728), (byte) 1, (short) 0, null);
} else if (armorType == 3) { //diamond armor
armorItem = new Item(protocol.getMappingData().getNewItemId(729), (byte) 1, (short) 0, null);
armorItem = new DataItem(protocol.getMappingData().getNewItemId(729), (byte) 1, (short) 0, null);
}
PacketWrapper equipmentPacket = PacketWrapper.create(0x46, null, connection);

Datei anzeigen

@ -26,8 +26,8 @@ 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.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
@ -45,7 +45,7 @@ import com.viaversion.viaversion.rewriter.RecipeRewriter;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_14To1_13_2> {
private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_14To1_13_2.class.getSimpleName();
private static final Set<String> REMOVED_RECIPE_TYPES = Sets.newHashSet("crafting_special_banneraddpattern", "crafting_special_repairitem");
private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() {
@ -59,11 +59,14 @@ public class InventoryPackets {
}
};
public static void register(Protocol protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
public InventoryPackets(Protocol1_14To1_13_2 protocol) {
super(protocol);
}
itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
itemRewriter.registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {
@Override
@ -141,8 +144,8 @@ public class InventoryPackets {
}
});
itemRewriter.registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
@ -165,14 +168,14 @@ public class InventoryPackets {
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Output Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
handleItemToClient(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));
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
wrapper.passthrough(Type.BOOLEAN); // Trade disabled
@ -197,9 +200,9 @@ public class InventoryPackets {
}
});
itemRewriter.registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol, InventoryPackets::toClient);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
@ -224,7 +227,7 @@ public class InventoryPackets {
});
itemRewriter.registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {
@Override
@ -242,26 +245,27 @@ public class InventoryPackets {
resyncPacket.write(Type.VAR_INT, 5); // 4 - Mode - Drag
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.write(Type.FLAT_VAR_INT_ITEM, new DataItem(1, (byte) 1, (short) 0, tag)); // 5 - Clicked Item
resyncPacket.scheduleSendToServer(Protocol1_14To1_13_2.class);
}
});
}
});
itemRewriter.registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
public static void toClient(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier()));
@Override
public Item handleItemToClient(Item item) {
if (item == null) return null;
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.identifier()));
if (item.getTag() == null) return;
if (item.tag() == null) return item;
// Display Lore now uses JSON
Tag displayTag = item.getTag().get("display");
Tag displayTag = item.tag().get("display");
if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore");
@ -276,16 +280,18 @@ public class InventoryPackets {
}
}
}
return item;
}
public static void toServer(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier()));
@Override
public Item handleItemToServer(Item item) {
if (item == null) return null;
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.identifier()));
if (item.getTag() == null) return;
if (item.tag() == null) return item;
// Display Name now uses JSON
Tag displayTag = item.getTag().get("display");
Tag displayTag = item.tag().get("display");
if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore");
@ -303,5 +309,6 @@ public class InventoryPackets {
}
}
}
return item;
}
}

Datei anzeigen

@ -56,12 +56,12 @@ public class PlayerPackets {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
InventoryPackets.toServer(item);
protocol.getItemRewriter().handleItemToServer(item);
// Client limit when editing a book was upped from 50 to 100 in 1.14, but some anti-exploit plugins ban with a size higher than the old client limit
if (Via.getConfig().isTruncate1_14Books()) {
if (item == null) return;
CompoundTag tag = item.getTag();
CompoundTag tag = item.tag();
if (tag == null) return;
Tag pages = tag.get("pages");

Datei anzeigen

@ -22,6 +22,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
@ -41,6 +42,7 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_15To1_14_4(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter;
public Protocol1_15To1_14_4() {
@ -50,11 +52,11 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
PlayerPackets.register(this);
WorldPackets.register(this);
InventoryPackets.register(this);
SoundRewriter soundRewriter = new SoundRewriter(this);
soundRewriter.registerSound(ClientboundPackets1_14.ENTITY_SOUND); // Entity Sound Effect (added somewhere in 1.14)
@ -65,7 +67,7 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
handler(wrapper -> itemRewriter.handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
@ -97,4 +99,9 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -27,7 +27,6 @@ import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_14;
import com.viaversion.viaversion.api.type.types.Particle;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List;
@ -41,7 +40,7 @@ public class MetadataRewriter1_15To1_14_4 extends EntityRewriter<Protocol1_15To1
@Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue());
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) {
// Convert to new block id
int data = (int) metadata.getValue();

Datei anzeigen

@ -17,7 +17,6 @@
*/
package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
@ -25,31 +24,24 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewri
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_15To1_14_4> {
public static void register(Protocol1_15To1_14_4 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
itemRewriter.registerSetCooldown(ClientboundPackets1_14.COOLDOWN);
itemRewriter.registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerTradeList(ClientboundPackets1_14.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerEntityEquipment(ClientboundPackets1_14.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
new RecipeRewriter1_14(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_14.DECLARE_RECIPES);
itemRewriter.registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
public InventoryPackets(Protocol1_15To1_14_4 protocol) {
super(protocol);
}
public static void toClient(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_15To1_14_4.MAPPINGS.getNewItemId(item.getIdentifier()));
}
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_14.COOLDOWN);
registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerTradeList(ClientboundPackets1_14.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerEntityEquipment(ClientboundPackets1_14.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
public static void toServer(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_15To1_14_4.MAPPINGS.getOldItemId(item.getIdentifier()));
new RecipeRewriter1_14(protocol).registerDefaultHandler(ClientboundPackets1_14.DECLARE_RECIPES);
registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
}
}

Datei anzeigen

@ -106,7 +106,7 @@ public class WorldPackets {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == 32) {
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
protocol.getItemRewriter().handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
}
});

Datei anzeigen

@ -22,6 +22,7 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data.MappingData;
@ -40,6 +41,7 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1
public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter;
public Protocol1_16_2To1_16_1() {
@ -49,10 +51,10 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
InventoryPackets.register(this);
tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY);
@ -126,4 +128,9 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -26,7 +26,6 @@ import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_14;
import com.viaversion.viaversion.api.type.types.Particle;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List;
@ -41,7 +40,7 @@ public class MetadataRewriter1_16_2To1_16_1 extends EntityRewriter<Protocol1_16_
@Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue());
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) {
int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));

Datei anzeigen

@ -17,7 +17,6 @@
*/
package com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1;
@ -26,17 +25,20 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPacke
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_16_2To1_16_1> {
public static void register(Protocol1_16_2To1_16_1 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
public InventoryPackets(Protocol1_16_2To1_16_1 protocol) {
super(protocol);
}
itemRewriter.registerSetCooldown(ClientboundPackets1_16.COOLDOWN);
itemRewriter.registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerEntityEquipmentArray(ClientboundPackets1_16.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerAdvancements(ClientboundPackets1_16.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_16.COOLDOWN);
registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerEntityEquipmentArray(ClientboundPackets1_16.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_16.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_16.UNLOCK_RECIPES, new PacketRemapper() {
@Override
@ -56,27 +58,17 @@ public class InventoryPackets {
}
});
new RecipeRewriter1_16(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_16.DECLARE_RECIPES);
new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_16.DECLARE_RECIPES);
itemRewriter.registerClickWindow(ServerboundPackets1_16_2.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_16_2.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
registerClickWindow(ServerboundPackets1_16_2.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_16_2.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_16_2.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
itemRewriter.registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
public static void toClient(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_16_2To1_16_1.MAPPINGS.getNewItemId(item.getIdentifier()));
}
public static void toServer(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_16_2To1_16_1.MAPPINGS.getOldItemId(item.getIdentifier()));
registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
}

Datei anzeigen

@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
@ -56,6 +57,7 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
private static final UUID ZERO_UUID = new UUID(0, 0);
public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_16To1_15_2(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter;
public Protocol1_16To1_15_2() {
@ -65,10 +67,10 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
InventoryPackets.register(this);
tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
@ -286,4 +288,9 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -22,24 +22,23 @@ import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class RecipeRewriter1_16 extends RecipeRewriter1_14 {
public RecipeRewriter1_16(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
public RecipeRewriter1_16(Protocol protocol) {
super(protocol);
recipeHandlers.put("smithing", this::handleSmithing);
}
public void handleSmithing(PacketWrapper wrapper) throws Exception {
Item[] baseIngredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
for (Item item : baseIngredients) {
rewriter.rewrite(item);
rewrite(item);
}
Item[] ingredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
for (Item item : ingredients) {
rewriter.rewrite(item);
rewrite(item);
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
}

Datei anzeigen

@ -26,7 +26,6 @@ import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_14;
import com.viaversion.viaversion.api.type.types.Particle;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List;
@ -42,7 +41,7 @@ public class MetadataRewriter1_16To1_15_2 extends EntityRewriter<Protocol1_16To1
@Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue());
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) {
int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));

Datei anzeigen

@ -37,11 +37,14 @@ import com.viaversion.viaversion.rewriter.ItemRewriter;
import java.util.UUID;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_16To1_15_2> {
public static void register(Protocol1_16To1_15_2 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
public InventoryPackets(Protocol1_16To1_15_2 protocol) {
super(protocol);
}
@Override
public void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_15.OPEN_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
@ -92,11 +95,11 @@ public class InventoryPackets {
}
});
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);
registerSetCooldown(ClientboundPackets1_15.COOLDOWN);
registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_15.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_15.ENTITY_EQUIPMENT, new PacketRemapper() {
@Override
@ -106,15 +109,15 @@ public class InventoryPackets {
handler(wrapper -> {
int slot = wrapper.read(Type.VAR_INT);
wrapper.write(Type.BYTE, (byte) slot);
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
});
}
});
new RecipeRewriter1_14(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES);
new RecipeRewriter1_14(protocol).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);
registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_16.CLOSE_WINDOW, new PacketRemapper() {
@Override
@ -131,18 +134,19 @@ public class InventoryPackets {
protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
itemRewriter.registerSpawnParticle(ClientboundPackets1_15.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
registerSpawnParticle(ClientboundPackets1_15.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
public static void toClient(Item item) {
if (item == null) return;
@Override
public Item handleItemToClient(Item item) {
if (item == null) return null;
if (item.getIdentifier() == 771 && item.getTag() != null) {
CompoundTag tag = item.getTag();
if (item.identifier() == 771 && item.tag() != null) {
CompoundTag tag = item.tag();
Tag ownerTag = tag.get("SkullOwner");
if (ownerTag instanceof CompoundTag) {
CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
@ -155,16 +159,18 @@ public class InventoryPackets {
}
oldToNewAttributes(item);
item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getNewItemId(item.getIdentifier()));
item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getNewItemId(item.identifier()));
return item;
}
public static void toServer(Item item) {
if (item == null) return;
@Override
public Item handleItemToServer(Item item) {
if (item == null) return null;
item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getOldItemId(item.getIdentifier()));
item.setIdentifier(Protocol1_16To1_15_2.MAPPINGS.getOldItemId(item.identifier()));
if (item.getIdentifier() == 771 && item.getTag() != null) {
CompoundTag tag = item.getTag();
if (item.identifier() == 771 && item.tag() != null) {
CompoundTag tag = item.tag();
Tag ownerTag = tag.get("SkullOwner");
if (ownerTag instanceof CompoundTag) {
CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
@ -177,12 +183,13 @@ public class InventoryPackets {
}
newToOldAttributes(item);
return item;
}
public static void oldToNewAttributes(Item item) {
if (item.getTag() == null) return;
if (item.tag() == null) return;
ListTag attributes = item.getTag().get("AttributeModifiers");
ListTag attributes = item.tag().get("AttributeModifiers");
if (attributes == null) return;
for (Tag tag : attributes) {
@ -199,9 +206,9 @@ public class InventoryPackets {
}
public static void newToOldAttributes(Item item) {
if (item.getTag() == null) return;
if (item.tag() == null) return;
ListTag attributes = item.getTag().get("AttributeModifiers");
ListTag attributes = item.tag().get("AttributeModifiers");
if (attributes == null) return;
for (Tag tag : attributes) {

Datei anzeigen

@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2;
@ -45,6 +46,7 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
public static final MappingData MAPPINGS = new MappingDataBase("1.16.2", "1.17", true);
private static final String[] NEW_GAME_EVENT_TAGS = {"minecraft:ignore_vibrations_sneaking", "minecraft:vibrations"};
private final EntityRewriter metadataRewriter = new MetadataRewriter1_17To1_16_4(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter;
public Protocol1_17To1_16_4() {
@ -54,9 +56,9 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
InventoryPackets.register(this);
WorldPackets.register(this);
tagRewriter = new TagRewriter(this);
@ -250,4 +252,9 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() {
return metadataRewriter;
}
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
}

Datei anzeigen

@ -22,7 +22,6 @@ import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_17;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
public class MetadataRewriter1_17To1_16_4 extends EntityRewriter {
@ -45,7 +44,7 @@ public class MetadataRewriter1_17To1_16_4 extends EntityRewriter {
}
}
});
registerDumMetaTypeHandler(MetaType1_17.ITEM, MetaType1_17.BLOCK_STATE, MetaType1_17.PARTICLE, InventoryPackets::toClient);
registerMetaTypeHandler(MetaType1_17.ITEM, MetaType1_17.BLOCK_STATE, MetaType1_17.PARTICLE);
// Ticks frozen added with id 7
filter().filterFamily(Entity1_17Types.ENTITY).addIndex(7);

Datei anzeigen

@ -30,27 +30,30 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPacke
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements;
import com.viaversion.viaversion.rewriter.ItemRewriter;
public class InventoryPackets {
public class InventoryPackets extends ItemRewriter<Protocol1_17To1_16_4> {
public static void register(Protocol1_17To1_16_4 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer);
public InventoryPackets(Protocol1_17To1_16_4 protocol) {
super(protocol);
}
itemRewriter.registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN);
itemRewriter.registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN);
registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
new RecipeRewriter1_16(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES);
new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
@ -80,7 +83,7 @@ public class InventoryPackets {
item = null;
} else {
// Use the item sent
toServer(item);
handleItemToServer(item);
}
wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
@ -133,16 +136,4 @@ public class InventoryPackets {
}
});
}
public static void toClient(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_17To1_16_4.MAPPINGS.getNewItemId(item.getIdentifier()));
}
public static void toServer(Item item) {
if (item == null) return;
item.setIdentifier(Protocol1_17To1_16_4.MAPPINGS.getOldItemId(item.getIdentifier()));
}
}

Datei anzeigen

@ -158,8 +158,8 @@ public class ItemRewriter {
public static void toServer(Item item) {
if (item != null) {
if (item.getIdentifier() == 383 && item.getData() == 0) { // Monster Egg
CompoundTag tag = item.getTag();
if (item.identifier() == 383 && item.data() == 0) { // Monster Egg
CompoundTag tag = item.tag();
int data = 0;
if (tag != null && tag.get("EntityTag") instanceof CompoundTag) {
CompoundTag entityTag = tag.get("EntityTag");
@ -173,8 +173,8 @@ public class ItemRewriter {
item.setTag(tag);
item.setData((short) data);
}
if (item.getIdentifier() == 373) { // Potion
CompoundTag tag = item.getTag();
if (item.identifier() == 373) { // Potion
CompoundTag tag = item.tag();
int data = 0;
if (tag != null && tag.get("Potion") instanceof StringTag) {
StringTag potion = tag.get("Potion");
@ -188,8 +188,8 @@ public class ItemRewriter {
item.setData((short) data);
}
// Splash potion
if (item.getIdentifier() == 438) {
CompoundTag tag = item.getTag();
if (item.identifier() == 438) {
CompoundTag tag = item.tag();
int data = 0;
item.setIdentifier(373); // Potion
if (tag != null && tag.get("Potion") instanceof StringTag) {
@ -204,9 +204,9 @@ public class ItemRewriter {
item.setData((short) data);
}
boolean newItem = item.getIdentifier() >= 198 && item.getIdentifier() <= 212;
newItem |= item.getIdentifier() == 397 && item.getData() == 5;
newItem |= item.getIdentifier() >= 432 && item.getIdentifier() <= 448;
boolean newItem = item.identifier() >= 198 && item.identifier() <= 212;
newItem |= item.identifier() == 397 && item.data() == 5;
newItem |= item.identifier() >= 432 && item.identifier() <= 448;
if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1);
item.setData((short) 0);
@ -215,11 +215,11 @@ public class ItemRewriter {
}
public static void rewriteBookToServer(Item item) {
int id = item.getIdentifier();
int id = item.identifier();
if (id != 387) {
return;
}
CompoundTag tag = item.getTag();
CompoundTag tag = item.tag();
ListTag pages = tag.get("pages");
if (pages == null) { // is this even possible?
return;
@ -251,13 +251,13 @@ public class ItemRewriter {
public static void toClient(Item item) {
if (item != null) {
if (item.getIdentifier() == 383 && item.getData() != 0) { // Monster Egg
CompoundTag tag = item.getTag();
if (item.identifier() == 383 && item.data() != 0) { // Monster Egg
CompoundTag tag = item.tag();
if (tag == null) {
tag = new CompoundTag();
}
CompoundTag entityTag = new CompoundTag();
String entityName = ENTTIY_ID_TO_NAME.get((int) item.getData());
String entityName = ENTTIY_ID_TO_NAME.get((int) item.data());
if (entityName != null) {
StringTag id = new StringTag(entityName);
entityTag.put("id", id);
@ -266,23 +266,23 @@ public class ItemRewriter {
item.setTag(tag);
item.setData((short) 0);
}
if (item.getIdentifier() == 373) { // Potion
CompoundTag tag = item.getTag();
if (item.identifier() == 373) { // Potion
CompoundTag tag = item.tag();
if (tag == null) {
tag = new CompoundTag();
}
if (item.getData() >= 16384) {
if (item.data() >= 16384) {
item.setIdentifier(438); // splash id
item.setData((short) (item.getData() - 8192));
item.setData((short) (item.data() - 8192));
}
String name = potionNameFromDamage(item.getData());
String name = potionNameFromDamage(item.data());
StringTag potion = new StringTag("minecraft:" + name);
tag.put("Potion", potion);
item.setTag(tag);
item.setData((short) 0);
}
if (item.getIdentifier() == 387) { // WRITTEN_BOOK
CompoundTag tag = item.getTag();
if (item.identifier() == 387) { // WRITTEN_BOOK
CompoundTag tag = item.tag();
if (tag == null) {
tag = new CompoundTag();
}

Datei anzeigen

@ -187,7 +187,7 @@ public class EntityPackets {
Item stack = wrapper.get(Type.ITEM, 0);
if (stack != null) {
if (Protocol1_9To1_8.isSword(stack.getIdentifier())) {
if (Protocol1_9To1_8.isSword(stack.identifier())) {
entityTracker.getValidBlocking().add(entityID);
return;
}

Datei anzeigen

@ -126,7 +126,7 @@ public class InventoryPackets {
short windowId = wrapper.get(Type.BYTE, 0);
// Store item in slot
inventoryTracker.setItemId(windowId, slotID, stack == null ? 0 : stack.getIdentifier());
inventoryTracker.setItemId(windowId, slotID, stack == null ? 0 : stack.identifier());
// Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword();
@ -177,7 +177,7 @@ public class InventoryPackets {
// Store items in slots
if (showShieldWhenSwordInHand) {
inventoryTracker.setItemId(windowId, i, stack == null ? 0 : stack.getIdentifier());
inventoryTracker.setItemId(windowId, i, stack == null ? 0 : stack.identifier());
}
ItemRewriter.toClient(stack);
@ -269,7 +269,7 @@ public class InventoryPackets {
short slotID = wrapper.get(Type.SHORT, 0);
// Update item in slot
inventoryTracker.setItemId((short) 0, slotID, stack == null ? 0 : stack.getIdentifier());
inventoryTracker.setItemId((short) 0, slotID, stack == null ? 0 : stack.identifier());
// Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword();

Datei anzeigen

@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_9to1_8.packets;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9;
@ -117,7 +118,7 @@ public class SpawnPackets {
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.VAR_INT, entityID);
List<Metadata> meta = new ArrayList<>();
Item item = new Item(373, (byte) 1, (short) data, null); // Potion
Item item = new DataItem(373, (byte) 1, (short) data, null); // Potion
ItemRewriter.toClient(item); // Rewrite so that it gets the right nbt
// TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS
Metadata potion = new Metadata(5, MetaType1_9.Slot, item);
@ -308,7 +309,7 @@ public class SpawnPackets {
PacketWrapper packet = PacketWrapper.create(0x3C, null, wrapper.user());
packet.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
packet.write(Type.VAR_INT, 0);
packet.write(Type.ITEM, new Item(item, (byte) 1, (short) 0, null));
packet.write(Type.ITEM, new DataItem(item, (byte) 1, (short) 0, null));
try {
packet.send(Protocol1_9To1_8.class);
} catch (Exception e) {

Datei anzeigen

@ -22,6 +22,7 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk1_8;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
@ -314,7 +315,7 @@ public class WorldPackets {
// Method to identify the sword in hand
boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand()
: item != null && Protocol1_9To1_8.isSword(item.getIdentifier());
: item != null && Protocol1_9To1_8.isSword(item.identifier());
if (isSword) {
if (hand == 0) {
@ -325,7 +326,7 @@ public class WorldPackets {
if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
// Set shield in offhand when interacting with main hand
Item shield = new Item(442, (byte) 1, (short) 0, null);
Item shield = new DataItem(442, (byte) 1, (short) 0, null);
tracker.setSecondHand(shield);
}
}

Datei anzeigen

@ -18,11 +18,12 @@
package com.viaversion.viaversion.protocols.protocol1_9to1_8.providers;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.platform.providers.Provider;
public class HandItemProvider implements Provider {
public Item getHandItem(final UserConnection info) {
return new Item(0, (byte) 0, (short) 0, null);
return new DataItem(0, (byte) 0, (short) 0, null);
}
}

Datei anzeigen

@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.legacy.bossbar.BossColor;
import com.viaversion.viaversion.api.legacy.bossbar.BossStyle;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types.EntityType;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9;
@ -114,7 +115,7 @@ public class EntityTracker1_9 extends EntityTrackerBase {
if (!swordInHand || this.itemInSecondHand == null) {
// Update shield in off hand depending if a sword is in the main hand
setSecondHand(swordInHand ? new Item(442, (byte) 1, (short) 0, null) : null);
setSecondHand(swordInHand ? new DataItem(442, (byte) 1, (short) 0, null) : null);
}
}
@ -201,7 +202,7 @@ public class EntityTracker1_9 extends EntityTrackerBase {
if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) {
if ((data & 0x10) == 0x10) {
if (validBlocking.contains(entityId)) {
Item shield = new Item(442, (byte) 1, (short) 0, null);
Item shield = new DataItem(442, (byte) 1, (short) 0, null);
setSecondHand(entityId, shield);
} else {
setSecondHand(entityId, null);

Datei anzeigen

@ -218,12 +218,11 @@ public abstract class EntityRewriter<T extends Protocol> extends RewriterBase<T>
* @param itemType item meta type if needed
* @param blockType block meta type if needed
* @param particleType particle meta type if needed
* @param itemRewriter itemrewriter if needed
*/
public void registerDumMetaTypeHandler(@Nullable MetaType itemType, @Nullable MetaType blockType, @Nullable MetaType particleType, ItemRewriter.@Nullable RewriteFunction itemRewriter) {
public void registerMetaTypeHandler(@Nullable MetaType itemType, @Nullable MetaType blockType, @Nullable MetaType particleType) {
filter().handler((event, meta) -> {
if (itemType != null && meta.metaType() == itemType) {
itemRewriter.rewrite(meta.value());
protocol.getItemRewriter().handleItemToClient(meta.value());
} else if (blockType != null && meta.metaType() == blockType) {
int data = meta.value();
meta.setValue(protocol.getMappingData().getNewBlockStateId(data));

Datei anzeigen

@ -24,18 +24,34 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.RewriterBase;
import com.viaversion.viaversion.api.type.Type;
import org.checkerframework.checker.nullness.qual.Nullable;
// If any of these methods become outdated, just create a new rewriter overriding the methods
public class ItemRewriter {
private final Protocol protocol;
private final RewriteFunction toClient;
private final RewriteFunction toServer;
public abstract class ItemRewriter<T extends Protocol> extends RewriterBase<T> implements com.viaversion.viaversion.api.rewriter.ItemRewriter<T> {
public ItemRewriter(Protocol protocol, RewriteFunction toClient, RewriteFunction toServer) {
this.protocol = protocol;
this.toClient = toClient;
this.toServer = toServer;
public ItemRewriter(T protocol) {
super(protocol);
}
// These two methods always return the same item instance *for now*
// It is made this way so it's easy to handle new instance creation/implementation changes
@Override
public @Nullable Item handleItemToClient(@Nullable Item item) {
if (item == null) return null;
if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) {
item.setIdentifier(protocol.getMappingData().getNewItemId(item.identifier()));
}
return item;
}
@Override
public @Nullable Item handleItemToServer(@Nullable Item item) {
if (item == null) return null;
if (protocol.getMappingData() != null && protocol.getMappingData().getItemMappings() != null) {
item.setIdentifier(protocol.getMappingData().getOldItemId(item.identifier()));
}
return item;
}
public void registerWindowItems(ClientboundPacketType packetType, Type<Item[]> type) {
@ -89,7 +105,7 @@ public class ItemRewriter {
do {
slot = wrapper.passthrough(Type.BYTE);
// & 0x7F into an extra variable if slot is needed
toClient.rewrite(wrapper.passthrough(type));
handleItemToClient(wrapper.passthrough(type));
} while ((slot & 0xFFFFFF80) != 0);
});
}
@ -138,11 +154,11 @@ public class ItemRewriter {
int length = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < length; i++) {
wrapper.passthrough(Type.SHORT); // Slot
toServer.rewrite(wrapper.passthrough(type));
handleItemToServer(wrapper.passthrough(type));
}
// Carried item
toServer.rewrite(wrapper.passthrough(type));
handleItemToServer(wrapper.passthrough(type));
});
}
});
@ -169,11 +185,11 @@ public class ItemRewriter {
wrapper.passthrough(Type.VAR_INT);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
toClient.rewrite(wrapper.passthrough(type)); // Input
toClient.rewrite(wrapper.passthrough(type)); // Output
handleItemToClient(wrapper.passthrough(type)); // Input
handleItemToClient(wrapper.passthrough(type)); // Output
if (wrapper.passthrough(Type.BOOLEAN)) { // Has second item
toClient.rewrite(wrapper.passthrough(type)); // Second Item
handleItemToClient(wrapper.passthrough(type)); // Second Item
}
wrapper.passthrough(Type.BOOLEAN); // Trade disabled
@ -209,7 +225,7 @@ public class ItemRewriter {
if (wrapper.passthrough(Type.BOOLEAN)) {
wrapper.passthrough(Type.COMPONENT); // Title
wrapper.passthrough(Type.COMPONENT); // Description
toClient.rewrite(wrapper.passthrough(type)); // Icon
handleItemToClient(wrapper.passthrough(type)); // Icon
wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags
if ((flags & 1) != 0) {
@ -261,7 +277,7 @@ public class ItemRewriter {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == mappings.getItemId()) {
toClient.rewrite(wrapper.passthrough(itemType));
handleItemToClient(wrapper.passthrough(itemType));
}
int newId = protocol.getMappingData().getNewParticleId(id);
@ -276,22 +292,16 @@ public class ItemRewriter {
return wrapper -> {
Item[] items = wrapper.get(type, 0);
for (Item item : items) {
toClient.rewrite(item);
handleItemToClient(item);
}
};
}
public PacketHandler itemToClientHandler(Type<Item> type) {
return wrapper -> toClient.rewrite(wrapper.get(type, 0));
return wrapper -> handleItemToClient(wrapper.get(type, 0));
}
public PacketHandler itemToServerHandler(Type<Item> type) {
return wrapper -> toServer.rewrite(wrapper.get(type, 0));
}
@FunctionalInterface
public interface RewriteFunction {
void rewrite(Item item);
return wrapper -> handleItemToServer(wrapper.get(type, 0));
}
}

Datei anzeigen

@ -17,11 +17,13 @@
*/
package com.viaversion.viaversion.rewriter;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.HashMap;
import java.util.Map;
@ -29,12 +31,10 @@ import java.util.Map;
public abstract class RecipeRewriter {
protected final Protocol protocol;
protected final ItemRewriter.RewriteFunction rewriter;
protected final Map<String, RecipeConsumer> recipeHandlers = new HashMap<>();
protected RecipeRewriter(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
protected RecipeRewriter(Protocol protocol) {
this.protocol = protocol;
this.rewriter = rewriter;
}
public void handle(PacketWrapper wrapper, String type) throws Exception {
@ -60,6 +60,12 @@ public abstract class RecipeRewriter {
});
}
protected void rewrite(@Nullable Item item) {
if (protocol.getItemRewriter() != null) {
protocol.getItemRewriter().handleItemToClient(item);
}
}
@FunctionalInterface
public interface RecipeConsumer {

Datei anzeigen

@ -17,11 +17,11 @@
*/
package com.viaversion.viaversion.common.type;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
public class ItemTypeTest {
@ -38,7 +38,7 @@ public class ItemTypeTest {
// Test item read
Assertions.assertEquals(
new Item((int) Short.MAX_VALUE, (byte) -128, (short) 257, null),
new DataItem((int) Short.MAX_VALUE, (byte) -128, (short) 257, null),
Type.ITEM.read(Unpooled.wrappedBuffer(new byte[]{
127, -1,
-128,
@ -47,7 +47,7 @@ public class ItemTypeTest {
}))
);
Assertions.assertEquals(
new Item(420, (byte) 53, (short) 0, null),
new DataItem(420, (byte) 53, (short) 0, null),
Type.FLAT_ITEM.read(Unpooled.wrappedBuffer(new byte[]{
1, (byte) 164,
53,
@ -55,7 +55,7 @@ public class ItemTypeTest {
}))
);
Assertions.assertEquals(
new Item(268435456, (byte) 127, (short) 0, null),
new DataItem(268435456, (byte) 127, (short) 0, null),
Type.FLAT_VAR_INT_ITEM.read(Unpooled.wrappedBuffer(new byte[]{
1,
-128, -128, -128, -128, 1,
@ -83,20 +83,20 @@ public class ItemTypeTest {
ByteBuf buf = Unpooled.buffer();
// Test item write
Type.ITEM.write(buf, new Item((int) Short.MAX_VALUE, (byte) -128, (short) 257, null));
Type.ITEM.write(buf, new DataItem((int) Short.MAX_VALUE, (byte) -128, (short) 257, null));
Assertions.assertArrayEquals(toBytes(buf), new byte[]{
127, -1,
-128,
1, 1,
0
});
Type.FLAT_ITEM.write(buf, new Item(420, (byte) 53, (short) 0, null));
Type.FLAT_ITEM.write(buf, new DataItem(420, (byte) 53, (short) 0, null));
Assertions.assertArrayEquals(toBytes(buf), new byte[]{
1, (byte) 164,
53,
0
});
Type.FLAT_VAR_INT_ITEM.write(buf, new Item(268435456, (byte) 127, (short) 0, null));
Type.FLAT_VAR_INT_ITEM.write(buf, new DataItem(268435456, (byte) 127, (short) 0, null));
Assertions.assertArrayEquals(toBytes(buf), new byte[]{
1,
-128, -128, -128, -128, 1,

Datei anzeigen

@ -17,6 +17,7 @@
*/
package com.viaversion.viaversion.sponge.listeners.protocol1_9to1_8;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.sponge.listeners.protocol1_9to1_8.sponge4.Sponge4ItemGrabber;
import com.viaversion.viaversion.sponge.listeners.protocol1_9to1_8.sponge5.Sponge5ItemGrabber;
@ -68,7 +69,7 @@ public class HandItemCache implements Runnable {
}
public static Item convert(ItemStack itemInHand) {
if (itemInHand == null) return new Item(0, (byte) 0, (short) 0, null);
if (itemInHand == null) return new DataItem(0, (byte) 0, (short) 0, null);
if (GET_DAMAGE == null) {
try {
GET_DAMAGE = itemInHand.getClass().getDeclaredField("field_77991_e");
@ -104,7 +105,7 @@ public class HandItemCache implements Runnable {
e.printStackTrace();
}
}
return new Item(id, (byte) itemInHand.getQuantity(), (short) damage, null);
return new DataItem(id, (byte) itemInHand.getQuantity(), (short) damage, null);
}
}