3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-19 14:30:16 +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; package com.viaversion.viaversion.api.minecraft.item;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.gson.annotations.SerializedName;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Objects; public interface Item {
public class Item { int identifier();
@SerializedName(value = "identifier", alternate = "id")
private int identifier;
private byte amount;
private short data;
private CompoundTag tag;
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) { default void setData(short data) {
this.identifier = identifier; throw new UnsupportedOperationException();
this.amount = amount;
this.data = data;
this.tag = tag;
} }
public Item(Item toCopy) { @Nullable CompoundTag tag();
this(toCopy.getIdentifier(), toCopy.getAmount(), toCopy.getData(), toCopy.getTag());
}
public int getIdentifier() { void setTag(@Nullable CompoundTag tag);
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 +
'}';
}
} }

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.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
@ -274,6 +275,15 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
return null; 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. * 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 @Override
public void register() { public final void register() {
registerPackets(); registerPackets();
registerRewrites(); registerRewrites();
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -17,6 +17,7 @@
*/ */
package com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8; package com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -52,7 +53,7 @@ public class HandItemCache extends BukkitRunnable {
} }
public static Item convert(ItemStack itemInHand) { 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);
return new Item(itemInHand.getTypeId(), (byte) itemInHand.getAmount(), itemInHand.getDurability(), 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.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; 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.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.api.type.types.version.Types1_9;
import com.viaversion.viaversion.protocols.protocol1_10to1_9_3.packets.InventoryPackets; 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; return metaList;
} }
}; };
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_10To1_9_3_4() { public Protocol1_10To1_9_3_4() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class); 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 @Override
protected void registerPackets() { protected void registerPackets() {
itemRewriter.register();
InventoryPackets.register(this);
// Named sound effect // Named sound effect
registerClientbound(State.PLAY, 0x19, 0x19, new PacketRemapper() { 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) { public void init(UserConnection userConnection) {
userConnection.put(new ResourcePackTracker()); 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.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_10To1_9_3_4 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, item -> { super(protocol);
}, InventoryPackets::toServerItem);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
} }
public static void toServerItem(Item item) { @Override
if (item == null) return; public void registerPackets() {
boolean newItem = item.getIdentifier() >= 213 && item.getIdentifier() <= 217; 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 if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1); item.setIdentifier((short) 1);
item.setData((short) 0); item.setData((short) 0);
} }
return item;
} }
} }

Datei anzeigen

@ -18,19 +18,26 @@
package com.viaversion.viaversion.protocols.protocol1_11_1to1_11; package com.viaversion.viaversion.protocols.protocol1_11_1to1_11;
import com.viaversion.viaversion.api.protocol.AbstractProtocol; 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_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.ClientboundPackets1_9_3;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_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> { 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() { public Protocol1_11_1To1_11() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class); super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class);
} }
@Override @Override
protected void registerPackets() { 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.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_11_1To1_11 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, item -> { super(protocol);
}, InventoryPackets::toServerItem);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
} }
public static void toServerItem(Item item) { @Override
if (item == null) return; public void registerPackets() {
boolean newItem = item.getIdentifier() == 452; 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 if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1); item.setIdentifier((short) 1);
item.setData((short) 0); item.setData((short) 0);
} }
return null;
} }
} }

Datei anzeigen

@ -139,9 +139,9 @@ public class EntityIdRewriter {
public static void toClientItem(Item item, boolean backwards) { public static void toClientItem(Item item, boolean backwards) {
if (hasEntityTag(item)) { 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) { public static void toServerItem(Item item) {
@ -151,7 +151,7 @@ public class EntityIdRewriter {
public static void toServerItem(Item item, boolean backwards) { public static void toServerItem(Item item, boolean backwards) {
if (!hasEntityTag(item)) return; if (!hasEntityTag(item)) return;
CompoundTag entityTag = item.getTag().get("EntityTag"); CompoundTag entityTag = item.tag().get("EntityTag");
Tag idTag = entityTag.get("id"); Tag idTag = entityTag.get("id");
if (idTag instanceof StringTag) { if (idTag instanceof StringTag) {
StringTag id = (StringTag) idTag; StringTag id = (StringTag) idTag;
@ -163,9 +163,9 @@ public class EntityIdRewriter {
} }
private static boolean hasEntityTag(Item item) { 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; if (tag == null) return false;
Tag entityTag = tag.get("EntityTag"); 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.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; 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.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.api.type.types.version.Types1_9;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping; 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 EntityRewriter entityRewriter = new MetadataRewriter1_11To1_10(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_11To1_10() { public Protocol1_11To1_10() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class); 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 @Override
protected void registerPackets() { protected void registerPackets() {
entityRewriter.register(); entityRewriter.register();
itemRewriter.register();
InventoryPackets.register(this);
registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() { registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {
@Override @Override
@ -388,4 +389,9 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return entityRewriter; 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.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types; 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.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.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; 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 @Override
protected void handleMetadata(int entityId, com.viaversion.viaversion.api.minecraft.entities.EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) { 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 // Apply rewrite
EntityIdRewriter.toClientItem((Item) metadata.getValue()); 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.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_11To1_10 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, EntityIdRewriter::toClientItem, InventoryPackets::toServerItem); super(protocol);
}
itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM); @Override
itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY); public void registerPackets() {
itemRewriter.registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM); 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 // Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() { 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); registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, 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); EntityIdRewriter.toServerItem(item);
if (item == null) return; if (item == null) return null;
boolean newItem = item.getIdentifier() >= 218 && item.getIdentifier() <= 234; boolean newItem = item.identifier() >= 218 && item.identifier() <= 234;
newItem |= item.getIdentifier() == 449 || item.getIdentifier() == 450; newItem |= item.identifier() == 449 || item.identifier() == 450;
if (newItem) { // Replace server-side unknown items if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1); item.setIdentifier((short) 1);
item.setData((short) 0); item.setData((short) 0);
} }
return item;
} }
} }

Datei anzeigen

@ -23,14 +23,14 @@ public class BedRewriter {
public static void toClientItem(Item item) { public static void toClientItem(Item item) {
if (item == null) return; if (item == null) return;
if (item.getIdentifier() == 355 && item.getData() == 0) { if (item.identifier() == 355 && item.data() == 0) {
item.setData((short) 14); item.setData((short) 14);
} }
} }
public static void toServerItem(Item item) { public static void toServerItem(Item item) {
if (item == null) return; if (item == null) return;
if (item.getIdentifier() == 355 && item.getData() == 14) { if (item.identifier() == 355 && item.data() == 14) {
item.setData((short) 0); 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.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; 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.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_12; import com.viaversion.viaversion.api.type.types.version.Types1_12;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; 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> { 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 EntityRewriter metadataRewriter = new MetadataRewriter1_12To1_11_1(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_12To1_11_1() { public Protocol1_12To1_11_1() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_12.class, ServerboundPackets1_9_3.class, ServerboundPackets1_12.class); 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 @Override
protected void registerPackets() { protected void registerPackets() {
metadataRewriter.register(); metadataRewriter.register();
itemRewriter.register();
InventoryPackets.register(this);
registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() { registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {
@Override @Override
@ -268,4 +269,9 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return metadataRewriter; 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.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types;
import com.viaversion.viaversion.api.minecraft.entities.EntityType; 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.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.BedRewriter; 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 @Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) { 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 // Apply rewrite
BedRewriter.toClientItem((Item) metadata.getValue()); 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.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_12To1_11_1 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, BedRewriter::toClientItem, InventoryPackets::toServerItem); super(protocol);
}
itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM); @Override
itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY); public void registerPackets() {
itemRewriter.registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM); 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 // Plugin message Packet -> Trading
protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() { protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
@ -113,17 +116,20 @@ public class InventoryPackets {
); );
// Creative Inventory Action // 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); BedRewriter.toServerItem(item);
if (item == null) return;
boolean newItem = item.getIdentifier() >= 235 && item.getIdentifier() <= 252; boolean newItem = item.identifier() >= 235 && item.identifier() <= 252;
newItem |= item.getIdentifier() == 453; newItem |= item.identifier() == 453;
if (newItem) { // Replace server-side unknown items if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1); item.setIdentifier((short) 1);
item.setData((short) 0); 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.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; 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); 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 EntityRewriter entityRewriter = new MetadataRewriter1_13_1To1_13(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_13_1To1_13() { public Protocol1_13_1To1_13() {
super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); 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 @Override
protected void registerPackets() { protected void registerPackets() {
entityRewriter.register(); entityRewriter.register();
itemRewriter.register();
EntityPackets.register(this); EntityPackets.register(this);
InventoryPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
registerServerbound(ServerboundPackets1_13.TAB_COMPLETE, new PacketRemapper() { registerServerbound(ServerboundPackets1_13.TAB_COMPLETE, new PacketRemapper() {
@ -77,12 +79,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
public void registerMap() { public void registerMap() {
map(Type.FLAT_ITEM); map(Type.FLAT_ITEM);
map(Type.BOOLEAN); map(Type.BOOLEAN);
handler(new PacketHandler() { handler(wrapper -> {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.get(Type.FLAT_ITEM, 0); Item item = wrapper.get(Type.FLAT_ITEM, 0);
InventoryPackets.toServer(item); itemRewriter.handleItemToServer(item);
}
}); });
handler(new PacketHandler() { handler(new PacketHandler() {
@Override @Override
@ -166,4 +165,9 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return entityRewriter; 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.minecraft.metadata.types.MetaType1_13;
import com.viaversion.viaversion.api.type.types.Particle; 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.Protocol1_13_1To1_13;
import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List; 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) { protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
// 1.13 changed item to flat item (no data) // 1.13 changed item to flat item (no data)
if (metadata.metaType() == MetaType1_13.Slot) { if (metadata.metaType() == MetaType1_13.Slot) {
InventoryPackets.toClient((Item) metadata.getValue()); protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_13.BlockID) { } else if (metadata.metaType() == MetaType1_13.BlockID) {
// Convert to new block id // Convert to new block id
int data = (int) metadata.getValue(); int data = (int) metadata.getValue();

Datei anzeigen

@ -17,7 +17,6 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets; 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.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; 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.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter; 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) { public InventoryPackets(Protocol1_13_1To1_13 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
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); @Override
itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN); 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() { protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override @Override
@ -52,14 +55,14 @@ public class InventoryPackets {
int size = wrapper.passthrough(Type.UNSIGNED_BYTE); int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// Input Item // Input Item
toClient(wrapper.passthrough(Type.FLAT_ITEM)); handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
// Output 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 boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) { if (secondItem) {
// Second Item // Second Item
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM)); handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
} }
wrapper.passthrough(Type.BOOLEAN); // Trade disabled 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() { protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
@ -90,19 +93,9 @@ public class InventoryPackets {
} }
}); });
itemRewriter.registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM); registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM); registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_ITEM, Type.FLOAT); 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()));
} }
} }

Datei anzeigen

@ -25,6 +25,7 @@ import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; 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.minecraft.item.Item;
import com.viaversion.viaversion.api.platform.providers.ViaProviders; import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractProtocol; 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.PacketRemapper;
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; 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 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 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 EntityRewriter entityRewriter = new MetadataRewriter1_13To1_12_2(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
static { static {
SCOREBOARD_TEAM_NAME_REWRITE.put('0', 'g'); SCOREBOARD_TEAM_NAME_REWRITE.put('0', 'g');
@ -157,11 +160,10 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
@Override @Override
protected void registerPackets() { protected void registerPackets() {
entityRewriter.register(); entityRewriter.register();
itemRewriter.register();
// Register grouped packet changes
EntityPackets.register(this); EntityPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
InventoryPackets.register(this);
registerClientbound(State.LOGIN, 0x0, 0x0, new PacketRemapper() { registerClientbound(State.LOGIN, 0x0, 0x0, new PacketRemapper() {
@Override @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 Item[] clone = ingredient.clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) { for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue; 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_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; break;
} }
case "crafting_shaped": { 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 Item[] clone = ingredient.clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) { for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue; 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_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; break;
} }
case "smelting": { 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 Item[] clone = entry.getValue().getIngredient().clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) { for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue; 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_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.FLOAT, entry.getValue().getExperience());
wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime()); wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime());
break; 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)); // Title
ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); // Description ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); // Description
Item icon = wrapper.read(Type.ITEM); Item icon = wrapper.read(Type.ITEM);
InventoryPackets.toClient(icon); itemRewriter.handleItemToClient(icon);
wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item wrapper.write(Type.FLAT_ITEM, icon); // Translate item to flat item
wrapper.passthrough(Type.VAR_INT); // Frame type wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags 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); Item item = wrapper.read(Type.FLAT_ITEM);
boolean isSigning = wrapper.read(Type.BOOLEAN); 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.STRING, isSigning ? "MC|BSign" : "MC|BEdit"); // Channel
wrapper.write(Type.ITEM, item); wrapper.write(Type.ITEM, item);
@ -1108,4 +1110,9 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return entityRewriter; 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.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.viaversion.viaversion.api.Via; 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.item.Item;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO; import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import com.viaversion.viaversion.api.protocol.Protocol; 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.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.ComponentRewriter;
import java.io.IOException; import java.io.IOException;
@ -40,6 +40,7 @@ public class ComponentRewriter1_13 extends ComponentRewriter {
} }
public ComponentRewriter1_13() { public ComponentRewriter1_13() {
super(Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class));
} }
@Override @Override
@ -70,14 +71,14 @@ public class ComponentRewriter1_13 extends ComponentRewriter {
// Call item converter // Call item converter
short damage = damageTag != null ? damageTag.asShort() : 0; short damage = damageTag != null ? damageTag.asShort() : 0;
Item item = new Item(); Item item = new DataItem();
item.setData(damage); item.setData(damage);
item.setTag(itemTag); item.setTag(itemTag);
handleItem(item); protocol.getItemRewriter().handleItemToClient(item);
// Serialize again // Serialize again
if (damage != item.getData()) { if (damage != item.data()) {
tag.put("Damage", new ShortTag(item.getData())); tag.put("Damage", new ShortTag(item.data()));
} }
if (itemTag != null) { if (itemTag != null) {
tag.put("tag", itemTag); 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) { protected String findItemNBT(JsonElement element) {
if (element.isJsonArray()) { if (element.isJsonArray()) {
for (JsonElement jsonElement : element.getAsJsonArray()) { for (JsonElement jsonElement : element.getAsJsonArray()) {

Datei anzeigen

@ -18,10 +18,11 @@
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data; package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data;
import com.viaversion.viaversion.api.Via; 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.item.Item;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.Particle; 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 com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets;
import java.util.ArrayList; import java.util.ArrayList;
@ -143,14 +144,14 @@ public class ParticleRewriter {
public Particle handler(Particle particle, Integer[] data) { public Particle handler(Particle particle, Integer[] data) {
Item item; Item item;
if (data.length == 1) 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) 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 else
return particle; return particle;
// Transform to new Item // 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. particle.getArguments().add(new Particle.ParticleData(Type.FLAT_ITEM, item)); // Item Slot The item that will be used.
return particle; return particle;

Datei anzeigen

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

Datei anzeigen

@ -29,8 +29,8 @@ import com.viaversion.viaversion.rewriter.RecipeRewriter;
*/ */
public class RecipeRewriter1_13_2 extends RecipeRewriter { public class RecipeRewriter1_13_2 extends RecipeRewriter {
public RecipeRewriter1_13_2(Protocol protocol, ItemRewriter.RewriteFunction rewriter) { public RecipeRewriter1_13_2(Protocol protocol) {
super(protocol, rewriter); super(protocol);
recipeHandlers.put("crafting_shapeless", this::handleCraftingShapeless); recipeHandlers.put("crafting_shapeless", this::handleCraftingShapeless);
recipeHandlers.put("crafting_shaped", this::handleCraftingShaped); recipeHandlers.put("crafting_shaped", this::handleCraftingShaped);
recipeHandlers.put("smelting", this::handleSmelting); recipeHandlers.put("smelting", this::handleSmelting);
@ -40,10 +40,10 @@ public class RecipeRewriter1_13_2 extends RecipeRewriter {
wrapper.passthrough(Type.STRING); // Group wrapper.passthrough(Type.STRING); // Group
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) { 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.FLOAT); // EXP
wrapper.passthrough(Type.VAR_INT); // Cooking time 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++) { for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) { 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 { 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++) { for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) { 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.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.EntityTypeRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; 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.protocols.protocol1_13to1_12_2.packets.WorldPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; 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) // 1.13 changed item to flat item (no data)
if (metadata.metaType() == MetaType1_13.Slot) { if (metadata.metaType() == MetaType1_13.Slot) {
metadata.setMetaType(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) { } else if (metadata.metaType() == MetaType1_13.BlockID) {
// Convert to new block id // Convert to new block id
metadata.setValue(WorldPackets.toNewId((int) metadata.getValue())); metadata.setValue(WorldPackets.toNewId((int) metadata.getValue()));

Datei anzeigen

@ -49,12 +49,15 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Optional; 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(); private static final String NBT_TAG_NAME = "ViaVersion|" + Protocol1_13To1_12_2.class.getSimpleName();
public static void register(Protocol1_13To1_12_2 protocol) { public InventoryPackets(Protocol1_13To1_12_2 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
}
@Override
public void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_12_1.SET_SLOT, new PacketRemapper() { protocol.registerClientbound(ClientboundPackets1_12_1.SET_SLOT, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
@ -62,7 +65,7 @@ public class InventoryPackets {
map(Type.SHORT); // 1 - Slot ID map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM, Type.FLAT_ITEM); // 2 - Slot Value 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() { 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.UNSIGNED_BYTE); // 0 - Window ID
map(Type.ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values 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() { protocol.registerClientbound(ClientboundPackets1_12_1.WINDOW_PROPERTY, new PacketRemapper() {
@ -142,18 +145,18 @@ public class InventoryPackets {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// Input Item // Input Item
Item input = wrapper.read(Type.ITEM); Item input = wrapper.read(Type.ITEM);
InventoryPackets.toClient(input); handleItemToClient(input);
wrapper.write(Type.FLAT_ITEM, input); wrapper.write(Type.FLAT_ITEM, input);
// Output Item // Output Item
Item output = wrapper.read(Type.ITEM); Item output = wrapper.read(Type.ITEM);
InventoryPackets.toClient(output); handleItemToClient(output);
wrapper.write(Type.FLAT_ITEM, output); wrapper.write(Type.FLAT_ITEM, output);
boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) { if (secondItem) {
// Second Item // Second Item
Item second = wrapper.read(Type.ITEM); Item second = wrapper.read(Type.ITEM);
InventoryPackets.toClient(second); handleItemToClient(second);
wrapper.write(Type.FLAT_ITEM, second); wrapper.write(Type.FLAT_ITEM, second);
} }
@ -203,7 +206,7 @@ public class InventoryPackets {
map(Type.VAR_INT); // 1 - Slot ID map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM, Type.FLAT_ITEM); // 2 - Item 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.VAR_INT); // 4 - Mode
map(Type.FLAT_ITEM, Type.ITEM); // 5 - Clicked Item 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.SHORT); // 0 - Slot
map(Type.FLAT_ITEM, Type.ITEM); // 1 - Clicked Item map(Type.FLAT_ITEM, Type.ITEM); // 1 - Clicked Item
handler(itemRewriter.itemToServerHandler(Type.ITEM)); handler(itemToServerHandler(Type.ITEM));
} }
}); });
} }
// TODO CLEANUP / SMARTER REWRITE SYSTEM @Override
// TODO Rewrite identifiers public Item handleItemToClient(Item item) {
public static void toClient(Item item) { if (item == null) return null;
if (item == null) return; CompoundTag tag = item.tag();
CompoundTag tag = item.getTag();
// Save original id // 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 // NBT Additions
if (isDamageable(item.getIdentifier())) { if (isDamageable(item.identifier())) {
if (tag == null) item.setTag(tag = new CompoundTag()); 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()); if (tag == null) item.setTag(tag = new CompoundTag());
tag.put("map", new IntTag(item.getData())); tag.put("map", new IntTag(item.data()));
} }
// NBT Changes // NBT Changes
if (tag != null) { if (tag != null) {
// Invert banner/shield color id // Invert banner/shield color id
boolean banner = item.getIdentifier() == 425; boolean banner = item.identifier() == 425;
if (banner || item.getIdentifier() == 442) { if (banner || item.identifier() == 442) {
if (tag.get("BlockEntityTag") instanceof CompoundTag) { if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag"); CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) { if (blockEntityTag.get("Base") instanceof IntTag) {
@ -409,7 +411,7 @@ public class InventoryPackets {
tag.put("CanDestroy", newCanDestroy); tag.put("CanDestroy", newCanDestroy);
} }
// Handle SpawnEggs // Handle SpawnEggs
if (item.getIdentifier() == 383) { if (item.identifier() == 383) {
if (tag.get("EntityTag") instanceof CompoundTag) { if (tag.get("EntityTag") instanceof CompoundTag) {
CompoundTag entityTag = tag.get("EntityTag"); CompoundTag entityTag = tag.get("EntityTag");
if (entityTag.get("id") instanceof StringTag) { if (entityTag.get("id") instanceof StringTag) {
@ -437,17 +439,17 @@ public class InventoryPackets {
} }
if (!Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId)) { 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()); if (tag == null) item.setTag(tag = new CompoundTag());
tag.put(NBT_TAG_NAME, new IntTag(originalId)); // Data will be lost, saving original id 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 rawId = 32 << 4; // Dead Bush
} else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) { } else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) {
rawId &= ~0xF; // Remove data rawId &= ~0xF; // Remove data
} else { } else {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { 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 rawId = 16; // Stone
} }
@ -455,6 +457,7 @@ public class InventoryPackets {
item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId)); item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId));
item.setData((short) 0); item.setData((short) 0);
return item;
} }
public static String getNewPluginChannelId(String old) { public static String getNewPluginChannelId(String old) {
@ -485,13 +488,14 @@ public class InventoryPackets {
} }
} }
public static void toServer(Item item) { @Override
if (item == null) return; public Item handleItemToServer(Item item) {
if (item == null) return null;
Integer rawId = null; Integer rawId = null;
boolean gotRawIdFromTag = false; boolean gotRawIdFromTag = false;
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
// Use tag to get original ID and data // Use tag to get original ID and data
if (tag != null) { if (tag != null) {
@ -505,7 +509,7 @@ public class InventoryPackets {
} }
if (rawId == null) { 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) { if (oldId != -1) {
// Handle spawn eggs // Handle spawn eggs
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId); Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
@ -526,7 +530,7 @@ public class InventoryPackets {
if (rawId == null) { if (rawId == null) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { 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 rawId = 0x10000; // Stone
} }
@ -536,7 +540,7 @@ public class InventoryPackets {
// NBT changes // NBT changes
if (tag != null) { if (tag != null) {
if (isDamageable(item.getIdentifier())) { if (isDamageable(item.identifier())) {
if (tag.get("Damage") instanceof IntTag) { if (tag.get("Damage") instanceof IntTag) {
if (!gotRawIdFromTag) { if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("Damage").getValue()); 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 (tag.get("map") instanceof IntTag) {
if (!gotRawIdFromTag) { if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("map").getValue()); 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) { if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag"); CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) { if (blockEntityTag.get("Base") instanceof IntTag) {
@ -669,6 +673,7 @@ public class InventoryPackets {
tag.put("CanDestroy", newCanDestroy); tag.put("CanDestroy", newCanDestroy);
} }
} }
return item;
} }
public static String getOldPluginChannelId(String newId) { 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.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_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(); public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_14To1_13_2(this); private final EntityRewriter metadataRewriter = new MetadataRewriter1_14To1_13_2(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
public Protocol1_14To1_13_2() { public Protocol1_14To1_13_2() {
super(ClientboundPackets1_13.class, ClientboundPackets1_14.class, ServerboundPackets1_13.class, ServerboundPackets1_14.class); 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 @Override
protected void registerPackets() { protected void registerPackets() {
metadataRewriter.register(); metadataRewriter.register();
itemRewriter.register();
InventoryPackets.register(this);
EntityPackets.register(this); EntityPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
PlayerPackets.register(this); PlayerPackets.register(this);
@ -162,4 +164,9 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return metadataRewriter; return metadataRewriter;
} }
@Override
public ItemRewriter getItemRewriter() {
return itemRewriter;
}
} }

Datei anzeigen

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

Datei anzeigen

@ -26,8 +26,8 @@ import com.viaversion.viaversion.rewriter.ItemRewriter;
public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 { public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 {
public RecipeRewriter1_14(Protocol protocol, ItemRewriter.RewriteFunction rewriter) { public RecipeRewriter1_14(Protocol protocol) {
super(protocol, rewriter); super(protocol);
recipeHandlers.put("stonecutting", this::handleStonecutting); recipeHandlers.put("stonecutting", this::handleStonecutting);
recipeHandlers.put("blasting", this::handleSmelting); recipeHandlers.put("blasting", this::handleSmelting);
@ -39,9 +39,9 @@ public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 {
wrapper.passthrough(Type.STRING); wrapper.passthrough(Type.STRING);
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) { 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_13Types;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types;
import com.viaversion.viaversion.api.minecraft.entities.EntityType; 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.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_14; 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.Type;
import com.viaversion.viaversion.api.type.types.Particle; 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.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.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
import com.viaversion.viaversion.rewriter.EntityRewriter; 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); EntityTracker1_14 tracker = tracker(connection);
if (metadata.metaType() == MetaType1_14.Slot) { if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue()); protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) { } else if (metadata.metaType() == MetaType1_14.BlockID) {
// Convert to new block id // Convert to new block id
int data = (int) metadata.getValue(); int data = (int) metadata.getValue();
@ -123,11 +123,11 @@ public class MetadataRewriter1_14To1_13_2 extends EntityRewriter<Protocol1_14To1
int armorType = (int) metadata.getValue(); int armorType = (int) metadata.getValue();
Item armorItem = null; Item armorItem = null;
if (armorType == 1) { //iron armor 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 } 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 } 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); 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.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via; 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.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; 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.Set;
import java.util.concurrent.ThreadLocalRandom; 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 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 Set<String> REMOVED_RECIPE_TYPES = Sets.newHashSet("crafting_special_banneraddpattern", "crafting_special_repairitem");
private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() { private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() {
@ -59,11 +59,14 @@ public class InventoryPackets {
} }
}; };
public static void register(Protocol protocol) { public InventoryPackets(Protocol1_14To1_13_2 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
}
itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN); @Override
itemRewriter.registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); 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() { protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {
@Override @Override
@ -141,8 +144,8 @@ public class InventoryPackets {
} }
}); });
itemRewriter.registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM); registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() { protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override @Override
@ -165,14 +168,14 @@ public class InventoryPackets {
int size = wrapper.passthrough(Type.UNSIGNED_BYTE); int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// Input Item // Input Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Output 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 boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
if (secondItem) { if (secondItem) {
// Second Item // Second Item
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
} }
wrapper.passthrough(Type.BOOLEAN); // Trade disabled 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() { protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override @Override
public void registerMap() { 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() { protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {
@Override @Override
@ -242,26 +245,27 @@ public class InventoryPackets {
resyncPacket.write(Type.VAR_INT, 5); // 4 - Mode - Drag resyncPacket.write(Type.VAR_INT, 5); // 4 - Mode - Drag
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
tag.put("force_resync", new DoubleTag(Double.NaN)); // Tags with NaN are not equal 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); 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) { @Override
if (item == null) return; public Item handleItemToClient(Item item) {
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier())); 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 // Display Lore now uses JSON
Tag displayTag = item.getTag().get("display"); Tag displayTag = item.tag().get("display");
if (displayTag instanceof CompoundTag) { if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag; CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore"); Tag loreTag = display.get("Lore");
@ -276,16 +280,18 @@ public class InventoryPackets {
} }
} }
} }
return item;
} }
public static void toServer(Item item) { @Override
if (item == null) return; public Item handleItemToServer(Item item) {
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier())); 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 // Display Name now uses JSON
Tag displayTag = item.getTag().get("display"); Tag displayTag = item.tag().get("display");
if (displayTag instanceof CompoundTag) { if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag; CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore"); Tag loreTag = display.get("Lore");
@ -303,5 +309,6 @@ public class InventoryPackets {
} }
} }
} }
return item;
} }
} }

Datei anzeigen

@ -56,12 +56,12 @@ public class PlayerPackets {
@Override @Override
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
Item item = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); 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 // 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 (Via.getConfig().isTruncate1_14Books()) {
if (item == null) return; if (item == null) return;
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
if (tag == null) return; if (tag == null) return;
Tag pages = tag.get("pages"); 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.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; 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(); public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_15To1_14_4(this); private final EntityRewriter metadataRewriter = new MetadataRewriter1_15To1_14_4(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter; private TagRewriter tagRewriter;
public Protocol1_15To1_14_4() { public Protocol1_15To1_14_4() {
@ -50,11 +52,11 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol<ClientboundPackets1_1
@Override @Override
protected void registerPackets() { protected void registerPackets() {
metadataRewriter.register(); metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this); EntityPackets.register(this);
PlayerPackets.register(this); PlayerPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
InventoryPackets.register(this);
SoundRewriter soundRewriter = new SoundRewriter(this); SoundRewriter soundRewriter = new SoundRewriter(this);
soundRewriter.registerSound(ClientboundPackets1_14.ENTITY_SOUND); // Entity Sound Effect (added somewhere in 1.14) 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() { registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {
@Override @Override
public void registerMap() { 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() { public EntityRewriter getEntityRewriter() {
return metadataRewriter; 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.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.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.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List; import java.util.List;
@ -41,7 +40,7 @@ public class MetadataRewriter1_15To1_14_4 extends EntityRewriter<Protocol1_15To1
@Override @Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception { public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
if (metadata.metaType() == MetaType1_14.Slot) { if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue()); protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) { } else if (metadata.metaType() == MetaType1_14.BlockID) {
// Convert to new block id // Convert to new block id
int data = (int) metadata.getValue(); int data = (int) metadata.getValue();

Datei anzeigen

@ -17,7 +17,6 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets; 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.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_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.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_15To1_14_4 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
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 static void toClient(Item item) { @Override
if (item == null) return; public void registerPackets() {
item.setIdentifier(Protocol1_15To1_14_4.MAPPINGS.getNewItemId(item.getIdentifier())); 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) { new RecipeRewriter1_14(protocol).registerDefaultHandler(ClientboundPackets1_14.DECLARE_RECIPES);
if (item == null) return;
item.setIdentifier(Protocol1_15To1_14_4.MAPPINGS.getOldItemId(item.getIdentifier())); 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); int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == 32) { } 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.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data.MappingData; 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(); public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this); private final EntityRewriter metadataRewriter = new MetadataRewriter1_16_2To1_16_1(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter; private TagRewriter tagRewriter;
public Protocol1_16_2To1_16_1() { public Protocol1_16_2To1_16_1() {
@ -49,10 +51,10 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1
@Override @Override
protected void registerPackets() { protected void registerPackets() {
metadataRewriter.register(); metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this); EntityPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
InventoryPackets.register(this);
tagRewriter = new TagRewriter(this); tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY); tagRewriter.register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY);
@ -126,4 +128,9 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol<ClientboundPackets1
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return metadataRewriter; 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.minecraft.metadata.types.MetaType1_14;
import com.viaversion.viaversion.api.type.types.Particle; 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.Protocol1_16_2To1_16_1;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List; import java.util.List;
@ -41,7 +40,7 @@ public class MetadataRewriter1_16_2To1_16_1 extends EntityRewriter<Protocol1_16_
@Override @Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception { public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
if (metadata.metaType() == MetaType1_14.Slot) { if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue()); protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) { } else if (metadata.metaType() == MetaType1_14.BlockID) {
int data = (int) metadata.getValue(); int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));

Datei anzeigen

@ -17,7 +17,6 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets; 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.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
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.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.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_16_2To1_16_1 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
}
itemRewriter.registerSetCooldown(ClientboundPackets1_16.COOLDOWN); @Override
itemRewriter.registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); public void registerPackets() {
itemRewriter.registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); registerSetCooldown(ClientboundPackets1_16.COOLDOWN);
itemRewriter.registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM); registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerEntityEquipmentArray(ClientboundPackets1_16.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM); registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerAdvancements(ClientboundPackets1_16.ADVANCEMENTS, 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() { protocol.registerClientbound(ClientboundPackets1_16.UNLOCK_RECIPES, new PacketRemapper() {
@Override @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); registerClickWindow(ServerboundPackets1_16_2.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_16_2.CREATIVE_INVENTORY_ACTION, 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() { protocol.registerServerbound(ServerboundPackets1_16_2.EDIT_BOOK, new PacketRemapper() {
@Override @Override
public void registerMap() { 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); 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()));
} }
} }

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.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; 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); private static final UUID ZERO_UUID = new UUID(0, 0);
public static final MappingData MAPPINGS = new MappingData(); public static final MappingData MAPPINGS = new MappingData();
private final EntityRewriter metadataRewriter = new MetadataRewriter1_16To1_15_2(this); private final EntityRewriter metadataRewriter = new MetadataRewriter1_16To1_15_2(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter; private TagRewriter tagRewriter;
public Protocol1_16To1_15_2() { public Protocol1_16To1_15_2() {
@ -65,10 +67,10 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
@Override @Override
protected void registerPackets() { protected void registerPackets() {
metadataRewriter.register(); metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this); EntityPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
InventoryPackets.register(this);
tagRewriter = new TagRewriter(this); tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY); tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
@ -286,4 +288,9 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return metadataRewriter; 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.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; 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 class RecipeRewriter1_16 extends RecipeRewriter1_14 {
public RecipeRewriter1_16(Protocol protocol, ItemRewriter.RewriteFunction rewriter) { public RecipeRewriter1_16(Protocol protocol) {
super(protocol, rewriter); super(protocol);
recipeHandlers.put("smithing", this::handleSmithing); recipeHandlers.put("smithing", this::handleSmithing);
} }
public void handleSmithing(PacketWrapper wrapper) throws Exception { public void handleSmithing(PacketWrapper wrapper) throws Exception {
Item[] baseIngredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); Item[] baseIngredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
for (Item item : baseIngredients) { for (Item item : baseIngredients) {
rewriter.rewrite(item); rewrite(item);
} }
Item[] ingredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); Item[] ingredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
for (Item item : ingredients) { 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.minecraft.metadata.types.MetaType1_14;
import com.viaversion.viaversion.api.type.types.Particle; 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.Protocol1_16To1_15_2;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.EntityRewriter;
import java.util.List; import java.util.List;
@ -42,7 +41,7 @@ public class MetadataRewriter1_16To1_15_2 extends EntityRewriter<Protocol1_16To1
@Override @Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception { public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
if (metadata.metaType() == MetaType1_14.Slot) { if (metadata.metaType() == MetaType1_14.Slot) {
InventoryPackets.toClient((Item) metadata.getValue()); protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == MetaType1_14.BlockID) { } else if (metadata.metaType() == MetaType1_14.BlockID) {
int data = (int) metadata.getValue(); int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));

Datei anzeigen

@ -37,11 +37,14 @@ import com.viaversion.viaversion.rewriter.ItemRewriter;
import java.util.UUID; 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) { public InventoryPackets(Protocol1_16To1_15_2 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
}
@Override
public void registerPackets() {
protocol.registerClientbound(ClientboundPackets1_15.OPEN_WINDOW, new PacketRemapper() { protocol.registerClientbound(ClientboundPackets1_15.OPEN_WINDOW, new PacketRemapper() {
@Override @Override
public void registerMap() { public void registerMap() {
@ -92,11 +95,11 @@ public class InventoryPackets {
} }
}); });
itemRewriter.registerSetCooldown(ClientboundPackets1_15.COOLDOWN); registerSetCooldown(ClientboundPackets1_15.COOLDOWN);
itemRewriter.registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM); registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerAdvancements(ClientboundPackets1_15.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); registerAdvancements(ClientboundPackets1_15.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_15.ENTITY_EQUIPMENT, new PacketRemapper() { protocol.registerClientbound(ClientboundPackets1_15.ENTITY_EQUIPMENT, new PacketRemapper() {
@Override @Override
@ -106,15 +109,15 @@ public class InventoryPackets {
handler(wrapper -> { handler(wrapper -> {
int slot = wrapper.read(Type.VAR_INT); int slot = wrapper.read(Type.VAR_INT);
wrapper.write(Type.BYTE, (byte) slot); 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); registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_16.CLOSE_WINDOW, new PacketRemapper() { protocol.registerServerbound(ServerboundPackets1_16.CLOSE_WINDOW, new PacketRemapper() {
@Override @Override
@ -131,18 +134,19 @@ public class InventoryPackets {
protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() { protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() {
@Override @Override
public void registerMap() { 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) { @Override
if (item == null) return; public Item handleItemToClient(Item item) {
if (item == null) return null;
if (item.getIdentifier() == 771 && item.getTag() != null) { if (item.identifier() == 771 && item.tag() != null) {
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
Tag ownerTag = tag.get("SkullOwner"); Tag ownerTag = tag.get("SkullOwner");
if (ownerTag instanceof CompoundTag) { if (ownerTag instanceof CompoundTag) {
CompoundTag ownerCompundTag = (CompoundTag) ownerTag; CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
@ -155,16 +159,18 @@ public class InventoryPackets {
} }
oldToNewAttributes(item); 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) { @Override
if (item == null) return; 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) { if (item.identifier() == 771 && item.tag() != null) {
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
Tag ownerTag = tag.get("SkullOwner"); Tag ownerTag = tag.get("SkullOwner");
if (ownerTag instanceof CompoundTag) { if (ownerTag instanceof CompoundTag) {
CompoundTag ownerCompundTag = (CompoundTag) ownerTag; CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
@ -177,12 +183,13 @@ public class InventoryPackets {
} }
newToOldAttributes(item); newToOldAttributes(item);
return item;
} }
public static void oldToNewAttributes(Item 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; if (attributes == null) return;
for (Tag tag : attributes) { for (Tag tag : attributes) {
@ -199,9 +206,9 @@ public class InventoryPackets {
} }
public static void newToOldAttributes(Item item) { 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; if (attributes == null) return;
for (Tag tag : attributes) { 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.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.EntityRewriter; 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.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; 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); 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 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 EntityRewriter metadataRewriter = new MetadataRewriter1_17To1_16_4(this);
private final ItemRewriter itemRewriter = new InventoryPackets(this);
private TagRewriter tagRewriter; private TagRewriter tagRewriter;
public Protocol1_17To1_16_4() { public Protocol1_17To1_16_4() {
@ -54,9 +56,9 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
@Override @Override
protected void registerPackets() { protected void registerPackets() {
metadataRewriter.register(); metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this); EntityPackets.register(this);
InventoryPackets.register(this);
WorldPackets.register(this); WorldPackets.register(this);
tagRewriter = new TagRewriter(this); tagRewriter = new TagRewriter(this);
@ -250,4 +252,9 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol<ClientboundPackets1_1
public EntityRewriter getEntityRewriter() { public EntityRewriter getEntityRewriter() {
return metadataRewriter; 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.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_17; 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.Protocol1_17To1_16_4;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.InventoryPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.EntityRewriter;
public class MetadataRewriter1_17To1_16_4 extends 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 // Ticks frozen added with id 7
filter().filterFamily(Entity1_17Types.ENTITY).addIndex(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.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements;
import com.viaversion.viaversion.rewriter.ItemRewriter; 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) { public InventoryPackets(Protocol1_17To1_16_4 protocol) {
ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets::toClient, InventoryPackets::toServer); super(protocol);
}
itemRewriter.registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN); @Override
itemRewriter.registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY); public void registerPackets() {
itemRewriter.registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM); registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN);
itemRewriter.registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM); registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
itemRewriter.registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM); registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM); registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE); 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() { protocol.registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {
@Override @Override
public void registerMap() { 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; item = null;
} else { } else {
// Use the item sent // Use the item sent
toServer(item); handleItemToServer(item);
} }
wrapper.write(Type.FLAT_VAR_INT_ITEM, 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) { public static void toServer(Item item) {
if (item != null) { if (item != null) {
if (item.getIdentifier() == 383 && item.getData() == 0) { // Monster Egg if (item.identifier() == 383 && item.data() == 0) { // Monster Egg
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
int data = 0; int data = 0;
if (tag != null && tag.get("EntityTag") instanceof CompoundTag) { if (tag != null && tag.get("EntityTag") instanceof CompoundTag) {
CompoundTag entityTag = tag.get("EntityTag"); CompoundTag entityTag = tag.get("EntityTag");
@ -173,8 +173,8 @@ public class ItemRewriter {
item.setTag(tag); item.setTag(tag);
item.setData((short) data); item.setData((short) data);
} }
if (item.getIdentifier() == 373) { // Potion if (item.identifier() == 373) { // Potion
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
int data = 0; int data = 0;
if (tag != null && tag.get("Potion") instanceof StringTag) { if (tag != null && tag.get("Potion") instanceof StringTag) {
StringTag potion = tag.get("Potion"); StringTag potion = tag.get("Potion");
@ -188,8 +188,8 @@ public class ItemRewriter {
item.setData((short) data); item.setData((short) data);
} }
// Splash potion // Splash potion
if (item.getIdentifier() == 438) { if (item.identifier() == 438) {
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
int data = 0; int data = 0;
item.setIdentifier(373); // Potion item.setIdentifier(373); // Potion
if (tag != null && tag.get("Potion") instanceof StringTag) { if (tag != null && tag.get("Potion") instanceof StringTag) {
@ -204,9 +204,9 @@ public class ItemRewriter {
item.setData((short) data); item.setData((short) data);
} }
boolean newItem = item.getIdentifier() >= 198 && item.getIdentifier() <= 212; boolean newItem = item.identifier() >= 198 && item.identifier() <= 212;
newItem |= item.getIdentifier() == 397 && item.getData() == 5; newItem |= item.identifier() == 397 && item.data() == 5;
newItem |= item.getIdentifier() >= 432 && item.getIdentifier() <= 448; newItem |= item.identifier() >= 432 && item.identifier() <= 448;
if (newItem) { // Replace server-side unknown items if (newItem) { // Replace server-side unknown items
item.setIdentifier((short) 1); item.setIdentifier((short) 1);
item.setData((short) 0); item.setData((short) 0);
@ -215,11 +215,11 @@ public class ItemRewriter {
} }
public static void rewriteBookToServer(Item item) { public static void rewriteBookToServer(Item item) {
int id = item.getIdentifier(); int id = item.identifier();
if (id != 387) { if (id != 387) {
return; return;
} }
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
ListTag pages = tag.get("pages"); ListTag pages = tag.get("pages");
if (pages == null) { // is this even possible? if (pages == null) { // is this even possible?
return; return;
@ -251,13 +251,13 @@ public class ItemRewriter {
public static void toClient(Item item) { public static void toClient(Item item) {
if (item != null) { if (item != null) {
if (item.getIdentifier() == 383 && item.getData() != 0) { // Monster Egg if (item.identifier() == 383 && item.data() != 0) { // Monster Egg
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
if (tag == null) { if (tag == null) {
tag = new CompoundTag(); tag = new CompoundTag();
} }
CompoundTag entityTag = 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) { if (entityName != null) {
StringTag id = new StringTag(entityName); StringTag id = new StringTag(entityName);
entityTag.put("id", id); entityTag.put("id", id);
@ -266,23 +266,23 @@ public class ItemRewriter {
item.setTag(tag); item.setTag(tag);
item.setData((short) 0); item.setData((short) 0);
} }
if (item.getIdentifier() == 373) { // Potion if (item.identifier() == 373) { // Potion
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
if (tag == null) { if (tag == null) {
tag = new CompoundTag(); tag = new CompoundTag();
} }
if (item.getData() >= 16384) { if (item.data() >= 16384) {
item.setIdentifier(438); // splash id 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); StringTag potion = new StringTag("minecraft:" + name);
tag.put("Potion", potion); tag.put("Potion", potion);
item.setTag(tag); item.setTag(tag);
item.setData((short) 0); item.setData((short) 0);
} }
if (item.getIdentifier() == 387) { // WRITTEN_BOOK if (item.identifier() == 387) { // WRITTEN_BOOK
CompoundTag tag = item.getTag(); CompoundTag tag = item.tag();
if (tag == null) { if (tag == null) {
tag = new CompoundTag(); tag = new CompoundTag();
} }

Datei anzeigen

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

Datei anzeigen

@ -126,7 +126,7 @@ public class InventoryPackets {
short windowId = wrapper.get(Type.BYTE, 0); short windowId = wrapper.get(Type.BYTE, 0);
// Store item in slot // 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 // Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword(); entityTracker.syncShieldWithSword();
@ -177,7 +177,7 @@ public class InventoryPackets {
// Store items in slots // Store items in slots
if (showShieldWhenSwordInHand) { if (showShieldWhenSwordInHand) {
inventoryTracker.setItemId(windowId, i, stack == null ? 0 : stack.getIdentifier()); inventoryTracker.setItemId(windowId, i, stack == null ? 0 : stack.identifier());
} }
ItemRewriter.toClient(stack); ItemRewriter.toClient(stack);
@ -269,7 +269,7 @@ public class InventoryPackets {
short slotID = wrapper.get(Type.SHORT, 0); short slotID = wrapper.get(Type.SHORT, 0);
// Update item in slot // 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 // Sync shield item in offhand with main hand
entityTracker.syncShieldWithSword(); 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.Via;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types; 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.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9;
@ -117,7 +118,7 @@ public class SpawnPackets {
public void handle(PacketWrapper wrapper) throws Exception { public void handle(PacketWrapper wrapper) throws Exception {
wrapper.write(Type.VAR_INT, entityID); wrapper.write(Type.VAR_INT, entityID);
List<Metadata> meta = new ArrayList<>(); 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 ItemRewriter.toClient(item); // Rewrite so that it gets the right nbt
// TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS // TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS
Metadata potion = new Metadata(5, MetaType1_9.Slot, item); Metadata potion = new Metadata(5, MetaType1_9.Slot, item);
@ -308,7 +309,7 @@ public class SpawnPackets {
PacketWrapper packet = PacketWrapper.create(0x3C, null, wrapper.user()); PacketWrapper packet = PacketWrapper.create(0x3C, null, wrapper.user());
packet.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0)); packet.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
packet.write(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 { try {
packet.send(Protocol1_9To1_8.class); packet.send(Protocol1_9To1_8.class);
} catch (Exception e) { } 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.Via;
import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk1_8; 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.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
@ -314,7 +315,7 @@ public class WorldPackets {
// Method to identify the sword in hand // Method to identify the sword in hand
boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand() boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand()
: item != null && Protocol1_9To1_8.isSword(item.getIdentifier()); : item != null && Protocol1_9To1_8.isSword(item.identifier());
if (isSword) { if (isSword) {
if (hand == 0) { if (hand == 0) {
@ -325,7 +326,7 @@ public class WorldPackets {
if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) { if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
// Set shield in offhand when interacting with main hand // 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); tracker.setSecondHand(shield);
} }
} }

Datei anzeigen

@ -18,11 +18,12 @@
package com.viaversion.viaversion.protocols.protocol1_9to1_8.providers; package com.viaversion.viaversion.protocols.protocol1_9to1_8.providers;
import com.viaversion.viaversion.api.connection.UserConnection; 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.minecraft.item.Item;
import com.viaversion.viaversion.api.platform.providers.Provider; import com.viaversion.viaversion.api.platform.providers.Provider;
public class HandItemProvider implements Provider { public class HandItemProvider implements Provider {
public Item getHandItem(final UserConnection info) { 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.legacy.bossbar.BossStyle;
import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types.EntityType; 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.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; 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) { if (!swordInHand || this.itemInSecondHand == null) {
// Update shield in off hand depending if a sword is in the main hand // 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 (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) {
if ((data & 0x10) == 0x10) { if ((data & 0x10) == 0x10) {
if (validBlocking.contains(entityId)) { 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); setSecondHand(entityId, shield);
} else { } else {
setSecondHand(entityId, null); 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 itemType item meta type if needed
* @param blockType block meta type if needed * @param blockType block meta type if needed
* @param particleType particle 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) -> { filter().handler((event, meta) -> {
if (itemType != null && meta.metaType() == itemType) { if (itemType != null && meta.metaType() == itemType) {
itemRewriter.rewrite(meta.value()); protocol.getItemRewriter().handleItemToClient(meta.value());
} else if (blockType != null && meta.metaType() == blockType) { } else if (blockType != null && meta.metaType() == blockType) {
int data = meta.value(); int data = meta.value();
meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); 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.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.RewriterBase;
import com.viaversion.viaversion.api.type.Type; 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 abstract class ItemRewriter<T extends Protocol> extends RewriterBase<T> implements com.viaversion.viaversion.api.rewriter.ItemRewriter<T> {
public class ItemRewriter {
private final Protocol protocol;
private final RewriteFunction toClient;
private final RewriteFunction toServer;
public ItemRewriter(Protocol protocol, RewriteFunction toClient, RewriteFunction toServer) { public ItemRewriter(T protocol) {
this.protocol = protocol; super(protocol);
this.toClient = toClient; }
this.toServer = toServer;
// 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) { public void registerWindowItems(ClientboundPacketType packetType, Type<Item[]> type) {
@ -89,7 +105,7 @@ public class ItemRewriter {
do { do {
slot = wrapper.passthrough(Type.BYTE); slot = wrapper.passthrough(Type.BYTE);
// & 0x7F into an extra variable if slot is needed // & 0x7F into an extra variable if slot is needed
toClient.rewrite(wrapper.passthrough(type)); handleItemToClient(wrapper.passthrough(type));
} while ((slot & 0xFFFFFF80) != 0); } while ((slot & 0xFFFFFF80) != 0);
}); });
} }
@ -138,11 +154,11 @@ public class ItemRewriter {
int length = wrapper.passthrough(Type.VAR_INT); int length = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
wrapper.passthrough(Type.SHORT); // Slot wrapper.passthrough(Type.SHORT); // Slot
toServer.rewrite(wrapper.passthrough(type)); handleItemToServer(wrapper.passthrough(type));
} }
// Carried item // Carried item
toServer.rewrite(wrapper.passthrough(type)); handleItemToServer(wrapper.passthrough(type));
}); });
} }
}); });
@ -169,11 +185,11 @@ public class ItemRewriter {
wrapper.passthrough(Type.VAR_INT); wrapper.passthrough(Type.VAR_INT);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE); int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
toClient.rewrite(wrapper.passthrough(type)); // Input handleItemToClient(wrapper.passthrough(type)); // Input
toClient.rewrite(wrapper.passthrough(type)); // Output handleItemToClient(wrapper.passthrough(type)); // Output
if (wrapper.passthrough(Type.BOOLEAN)) { // Has second item 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 wrapper.passthrough(Type.BOOLEAN); // Trade disabled
@ -209,7 +225,7 @@ public class ItemRewriter {
if (wrapper.passthrough(Type.BOOLEAN)) { if (wrapper.passthrough(Type.BOOLEAN)) {
wrapper.passthrough(Type.COMPONENT); // Title wrapper.passthrough(Type.COMPONENT); // Title
wrapper.passthrough(Type.COMPONENT); // Description wrapper.passthrough(Type.COMPONENT); // Description
toClient.rewrite(wrapper.passthrough(type)); // Icon handleItemToClient(wrapper.passthrough(type)); // Icon
wrapper.passthrough(Type.VAR_INT); // Frame type wrapper.passthrough(Type.VAR_INT); // Frame type
int flags = wrapper.passthrough(Type.INT); // Flags int flags = wrapper.passthrough(Type.INT); // Flags
if ((flags & 1) != 0) { if ((flags & 1) != 0) {
@ -261,7 +277,7 @@ public class ItemRewriter {
int data = wrapper.passthrough(Type.VAR_INT); int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data)); wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == mappings.getItemId()) { } else if (id == mappings.getItemId()) {
toClient.rewrite(wrapper.passthrough(itemType)); handleItemToClient(wrapper.passthrough(itemType));
} }
int newId = protocol.getMappingData().getNewParticleId(id); int newId = protocol.getMappingData().getNewParticleId(id);
@ -276,22 +292,16 @@ public class ItemRewriter {
return wrapper -> { return wrapper -> {
Item[] items = wrapper.get(type, 0); Item[] items = wrapper.get(type, 0);
for (Item item : items) { for (Item item : items) {
toClient.rewrite(item); handleItemToClient(item);
} }
}; };
} }
public PacketHandler itemToClientHandler(Type<Item> type) { 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) { public PacketHandler itemToServerHandler(Type<Item> type) {
return wrapper -> toServer.rewrite(wrapper.get(type, 0)); return wrapper -> handleItemToServer(wrapper.get(type, 0));
}
@FunctionalInterface
public interface RewriteFunction {
void rewrite(Item item);
} }
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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