Added API to remap non-existing items to stone

This commit is contained in:
RaphiMC 2023-01-18 13:05:31 +01:00
parent a7427ea6d5
commit f4df276f32
9 changed files with 294 additions and 216 deletions

View File

@ -6,4 +6,4 @@ org.gradle.configureondemand=true
# Project properties
maven_name=ViaLegacy
maven_group=net.raphimc
maven_version=2.2.2
maven_version=2.2.3

View File

@ -18,63 +18,115 @@
package net.raphimc.vialegacy.api.remapper;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.rewriter.RewriterBase;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.fastutil.ints.IntArrayList;
import com.viaversion.viaversion.libs.fastutil.ints.IntList;
import com.viaversion.viaversion.libs.fastutil.objects.ObjectArrayList;
import com.viaversion.viaversion.libs.fastutil.objects.ObjectList;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.*;
import java.util.List;
public abstract class AbstractItemRewriter {
public abstract class LegacyItemRewriter<P extends Protocol> extends RewriterBase<P> implements ItemRewriter<P> {
private final ObjectList<RewriteEntry> REWRITE_ENTRIES = new ObjectArrayList<>();
protected final String HACK_TAG_NAME;
private final ObjectList<RewriteEntry> rewriteEntries = new ObjectArrayList<>();
private final IntList nonExistentItems = new IntArrayList();
protected final String tagName;
protected final String protocolName;
private final boolean jsonName;
public AbstractItemRewriter(final String protocolName, final boolean jsonName) {
this.HACK_TAG_NAME = protocolName.replace(".", "_") + "_ProtocolHack_" + System.currentTimeMillis();
public LegacyItemRewriter(final P protocol, final String protocolName) {
super(protocol);
this.tagName = protocolName.replace(".", "_") + "_ProtocolHack_" + System.currentTimeMillis();
this.protocolName = protocolName;
this.jsonName = jsonName;
}
protected void registerRemappedItem(final int oldItemId, final int newItemId, final String newItemName) {
registerRemappedItem(oldItemId, newItemId, -1, newItemName);
protected void addRemappedItem(final int oldItemId, final int newItemId, final String newItemName) {
this.addRemappedItem(oldItemId, newItemId, -1, newItemName);
}
protected void registerRemappedItem(final int oldItemId, final int newItemId, final int newItemMeta, final String newItemName) {
registerRemappedItem(oldItemId, -1, newItemId, newItemMeta, newItemName);
protected void addRemappedItem(final int oldItemId, final int newItemId, final int newItemMeta, final String newItemName) {
this.addRemappedItem(oldItemId, -1, newItemId, newItemMeta, newItemName);
}
protected void registerRemappedItem(final int oldItemId, final int oldItemMeta, final int newItemId, final int newItemMeta, final String newItemName) {
REWRITE_ENTRIES.add(new RewriteEntry(oldItemId, (short) oldItemMeta, newItemId, (short) newItemMeta, newItemName));
protected void addRemappedItem(final int oldItemId, final int oldItemMeta, final int newItemId, final int newItemMeta, final String newItemName) {
this.rewriteEntries.add(new RewriteEntry(oldItemId, (short) oldItemMeta, newItemId, (short) newItemMeta, newItemName));
}
public void rewriteRead(final Item item) {
if (item == null) return;
protected void addNonExistentItems(final int... itemIds) {
for (int itemId : itemIds) {
this.nonExistentItems.add(itemId);
}
}
for (RewriteEntry rewriteEntry : REWRITE_ENTRIES) {
protected void addNonExistentItemRange(final int startItemId, final int endItemId) {
for (int i = startItemId; i <= endItemId; i++) {
this.nonExistentItems.add(i);
}
}
public void registerCreativeInventoryAction(final ServerboundPacketType packetType, final Type<Item> type) {
this.protocol.registerServerbound(packetType, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.SHORT); // slot
map(type); // item
handler(itemToServerHandler(type));
}
});
}
@Override
public Item handleItemToClient(final Item item) {
if (item == null) return null;
for (RewriteEntry rewriteEntry : this.rewriteEntries) {
if (rewriteEntry.rewrites(item)) {
setRemappedNameRead(item, rewriteEntry.newItemName);
this.setRemappedNameRead(item, rewriteEntry.newItemName);
if (rewriteEntry.newItemMeta != -1) {
item.setData(rewriteEntry.newItemMeta);
}
item.setIdentifier(rewriteEntry.newItemID);
}
}
return item;
}
public void rewriteWrite(final Item item) {
if (item == null) return;
@Override
public Item handleItemToServer(final Item item) {
if (item == null) return null;
setRemappedTagWrite(item);
if (this.nonExistentItems.contains(item.identifier())) {
item.setIdentifier(1);
item.setData((short) 0);
return item;
}
this.setRemappedTagWrite(item);
return item;
}
private PacketHandler itemToClientHandler(Type<Item> type) {
return wrapper -> handleItemToClient(wrapper.get(type, 0));
}
private PacketHandler itemToServerHandler(Type<Item> type) {
return wrapper -> handleItemToServer(wrapper.get(type, 0));
}
private void setRemappedNameRead(final Item item, final String name) {
//Set protocol hack tag for later remapping
CompoundTag protocolHackTag = (item.tag() != null && item.tag().contains(HACK_TAG_NAME) ? item.tag().get(HACK_TAG_NAME) : new CompoundTag());
if (item.tag() == null || !item.tag().contains(HACK_TAG_NAME)) {
final CompoundTag protocolHackTag = (item.tag() != null && item.tag().contains(tagName) ? item.tag().get(tagName) : new CompoundTag());
if (item.tag() == null || !item.tag().contains(tagName)) {
protocolHackTag.put("Id", new IntTag(item.identifier()));
protocolHackTag.put("Meta", new ShortTag(item.data()));
}
@ -86,7 +138,7 @@ public abstract class AbstractItemRewriter {
item.setTag(tag);
protocolHackTag.put("RemoveTag", new IntTag(0));
}
tag.put(HACK_TAG_NAME, protocolHackTag);
tag.put(tagName, protocolHackTag);
//Set name/lore of item
CompoundTag display = tag.get("display");
@ -102,29 +154,21 @@ public abstract class AbstractItemRewriter {
display.put("Lore", lore);
protocolHackTag.put("RemoveLore", new IntTag(0));
}
if (this.jsonName) {
lore.add(new StringTag(this.messageToJson("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")").toString()));
} else {
lore.add(new StringTag("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")"));
}
lore.add(new StringTag("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")"));
protocolHackTag.put("RemoveLastLore", new IntTag(0));
} else {
if (this.jsonName) {
display.put("Name", new StringTag(this.messageToJson("§r" + this.protocolName + " " + name).toString()));
} else {
display.put("Name", new StringTag("§r" + this.protocolName + " " + name));
}
display.put("Name", new StringTag("§r" + this.protocolName + " " + name));
protocolHackTag.put("RemoveDisplayName", new IntTag(0));
}
}
private void setRemappedTagWrite(final Item item) {
if (item.tag() == null) return;
if (!item.tag().contains(HACK_TAG_NAME)) return;
if (!item.tag().contains(tagName)) return;
CompoundTag tag = item.tag();
CompoundTag protocolHackTag = tag.get(HACK_TAG_NAME);
tag.remove(HACK_TAG_NAME);
final CompoundTag tag = item.tag();
final CompoundTag protocolHackTag = tag.get(tagName);
tag.remove(tagName);
item.setIdentifier(((IntTag) protocolHackTag.get("Id")).asInt());
item.setData(((ShortTag) protocolHackTag.get("Meta")).asShort());
@ -148,12 +192,6 @@ public abstract class AbstractItemRewriter {
}
}
private JsonObject messageToJson(final String s) {
JsonObject ob = new JsonObject();
ob.addProperty("text", s);
return ob;
}
public static class RewriteEntry {

View File

@ -48,6 +48,7 @@ import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.api.splitter.PreNettySplitter;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.providers.EncryptionProvider;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.rewriter.*;
@ -62,13 +63,14 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Chunk
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulk1_7_6Type;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.MetaType1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
import java.util.logging.Level;
public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1_6_4, ClientboundPackets1_7_2, ServerboundPackets1_6_4, ServerboundPackets1_7_2> {
public static ItemRewriter ITEM_REWRITER = new ItemRewriter();
private final LegacyItemRewriter<Protocol1_7_2_5to1_6_4> itemRewriter = new ItemRewriter(this);
public Protocol1_7_2_5to1_6_4() {
super(ClientboundPackets1_6_4.class, ClientboundPackets1_7_2.class, ServerboundPackets1_6_4.class, ServerboundPackets1_7_2.class);
@ -76,6 +78,8 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
@Override
protected void registerPackets() {
this.itemRewriter.register();
this.registerClientbound(State.STATUS, ClientboundPackets1_6_4.DISCONNECT.getId(), ClientboundStatusPackets.STATUS_RESPONSE.getId(), new PacketRemapper() {
@Override
public void registerMap() {
@ -213,7 +217,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
map(Type.INT); // entity id
map(Type.SHORT); // slot
map(Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteRead(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
}
});
this.registerClientbound(ClientboundPackets1_6_4.RESPAWN, new PacketRemapper() {
@ -392,7 +396,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
map(Type.BYTE); // pitch
handler(wrapper -> {
final Item currentItem = new DataItem(wrapper.read(Type.UNSIGNED_SHORT), (byte) 1, (short) 0, null); // item
ITEM_REWRITER.rewriteRead(currentItem);
itemRewriter.handleItemToClient(currentItem);
wrapper.write(Type.SHORT, (short) currentItem.identifier());
});
map(Types1_6_4.METADATA_LIST, Types1_7_6.METADATA_LIST); // metadata
@ -726,7 +730,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
map(Type.BYTE); // window id
map(Type.SHORT); // slot
map(Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteRead(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
}
});
this.registerClientbound(ClientboundPackets1_6_4.WINDOW_ITEMS, new PacketRemapper() {
@ -736,7 +740,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
handler(wrapper -> {
final Item[] items = wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM_ARRAY); // items
for (Item item : items) {
ITEM_REWRITER.rewriteRead(item);
itemRewriter.handleItemToClient(item);
}
});
}
@ -867,10 +871,10 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
wrapper.passthrough(Type.INT); // window id
final int count = wrapper.passthrough(Type.UNSIGNED_BYTE); // count
for (int i = 0; i < count; i++) {
ITEM_REWRITER.rewriteRead(wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM)); // item 1
ITEM_REWRITER.rewriteRead(wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM)); // item 3
itemRewriter.handleItemToClient(wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM)); // item 1
itemRewriter.handleItemToClient(wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM)); // item 3
if (wrapper.passthrough(Type.BOOLEAN)) { // has 3 items
ITEM_REWRITER.rewriteRead(wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM)); // item 2
itemRewriter.handleItemToClient(wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM)); // item 2
}
wrapper.passthrough(Type.BOOLEAN); // unavailable
}
@ -1012,7 +1016,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
map(Types1_7_6.POSITION_UBYTE); // position
map(Type.UNSIGNED_BYTE); // direction
map(Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
map(Type.UNSIGNED_BYTE); // offset x
map(Type.UNSIGNED_BYTE); // offset y
map(Type.UNSIGNED_BYTE); // offset z
@ -1027,15 +1031,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
map(Type.SHORT); // action
map(Type.BYTE); // mode
map(Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
}
});
this.registerServerbound(ServerboundPackets1_7_2.CREATIVE_INVENTORY_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.SHORT); // slot
map(Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
}
});
this.registerServerbound(ServerboundPackets1_7_2.UPDATE_SIGN, new PacketRemapper() {
@ -1127,7 +1123,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
case "MC|BEdit":
case "MC|BSign":
final Item item = wrapper.read(Types1_7_6.COMPRESSED_ITEM); // book
ITEM_REWRITER.rewriteWrite(item);
itemRewriter.handleItemToServer(item);
lengthPacketWrapper.write(Types1_7_6.COMPRESSED_ITEM, item);
lengthPacketWrapper.writeToBuffer(lengthBuffer);
@ -1168,7 +1164,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
private void rewriteMetadata(final List<Metadata> metadataList) {
for (Metadata metadata : metadataList) {
if (metadata.metaType().equals(MetaType1_6_4.Slot)) {
ITEM_REWRITER.rewriteRead(metadata.value());
itemRewriter.handleItemToClient(metadata.value());
}
metadata.setMetaType(MetaType1_7_6.byId(metadata.metaType().typeId()));
}
@ -1208,4 +1204,10 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol<ClientboundPackets1
}
}
@Nullable
@Override
public LegacyItemRewriter<Protocol1_7_2_5to1_6_4> getItemRewriter() {
return this.itemRewriter;
}
}

View File

@ -17,39 +17,47 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.rewriter;
import net.raphimc.vialegacy.api.remapper.AbstractItemRewriter;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.Protocol1_7_2_5to1_6_4;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ServerboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;
public class ItemRewriter extends AbstractItemRewriter {
public class ItemRewriter extends LegacyItemRewriter<Protocol1_7_2_5to1_6_4> {
public ItemRewriter() {
super("1.6.4", false);
registerRemappedItem(26, 355, "Bed Block");
registerRemappedItem(34, 33, "Piston Head");
registerRemappedItem(36, 33, "Piston Moving");
registerRemappedItem(55, 331, "Redstone Wire");
registerRemappedItem(59, 295, "Wheat Crops");
registerRemappedItem(63, 323, "Standing Sign");
registerRemappedItem(64, 324, "Oak Door Block");
registerRemappedItem(68, 323, "Wall Sign");
registerRemappedItem(71, 330, "Iron Door Block");
registerRemappedItem(74, 73, "Lit Redstone Ore");
registerRemappedItem(75, 76, "Unlit Redstone Torch");
registerRemappedItem(83, 338, "Sugar Cane Block");
registerRemappedItem(92, 354, "Cake Block");
registerRemappedItem(93, 356, "Unlit Redstone Repeater");
registerRemappedItem(94, 356, "Lit Redstone Repeater");
registerRemappedItem(95, 146, "Locked Chest");
registerRemappedItem(104, 361, "Pumpkin Stem");
registerRemappedItem(105, 362, "Melon Stem");
registerRemappedItem(115, 372, "Nether Wart Block");
registerRemappedItem(117, 379, "Brewing Stand Block");
registerRemappedItem(118, 380, "Cauldron Block");
registerRemappedItem(124, 123, "Lit Redstone Lamp");
registerRemappedItem(132, 287, "Tripwire");
registerRemappedItem(140, 390, "Flower Pot");
registerRemappedItem(144, 397, "Undefined Mob Head");
registerRemappedItem(149, 404, "Unlit Redstone Comparator");
registerRemappedItem(150, 404, "Lit Redstone Comparator");
public ItemRewriter(final Protocol1_7_2_5to1_6_4 protocol) {
super(protocol, "1.6.4");
addRemappedItem(26, 355, "Bed Block");
addRemappedItem(34, 33, "Piston Head");
addRemappedItem(36, 33, "Piston Moving");
addRemappedItem(55, 331, "Redstone Wire");
addRemappedItem(59, 295, "Wheat Crops");
addRemappedItem(63, 323, "Standing Sign");
addRemappedItem(64, 324, "Oak Door Block");
addRemappedItem(68, 323, "Wall Sign");
addRemappedItem(71, 330, "Iron Door Block");
addRemappedItem(74, 73, "Lit Redstone Ore");
addRemappedItem(75, 76, "Unlit Redstone Torch");
addRemappedItem(83, 338, "Sugar Cane Block");
addRemappedItem(92, 354, "Cake Block");
addRemappedItem(93, 356, "Unlit Redstone Repeater");
addRemappedItem(94, 356, "Lit Redstone Repeater");
addRemappedItem(95, 146, "Locked Chest");
addRemappedItem(104, 361, "Pumpkin Stem");
addRemappedItem(105, 362, "Melon Stem");
addRemappedItem(115, 372, "Nether Wart Block");
addRemappedItem(117, 379, "Brewing Stand Block");
addRemappedItem(118, 380, "Cauldron Block");
addRemappedItem(124, 123, "Lit Redstone Lamp");
addRemappedItem(132, 287, "Tripwire");
addRemappedItem(140, 390, "Flower Pot");
addRemappedItem(144, 397, "Undefined Mob Head");
addRemappedItem(149, 404, "Unlit Redstone Comparator");
addRemappedItem(150, 404, "Lit Redstone Comparator");
}
@Override
protected void registerPackets() {
this.registerCreativeInventoryAction(ServerboundPackets1_7_2.CREATIVE_INVENTORY_ACTION, Types1_7_6.COMPRESSED_ITEM);
}
}

View File

@ -49,6 +49,7 @@ import io.netty.buffer.Unpooled;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.ItemList1_6;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ClientboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ServerboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.data.Particle;
@ -65,13 +66,16 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.*;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Chunk1_7_6Type;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulk1_7_6Type;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_7_2, ClientboundPackets1_8, ServerboundPackets1_7_2, ServerboundPackets1_8> {
public static final ItemRewriter ITEM_REWRITER = new ItemRewriter();
private final LegacyItemRewriter<Protocol1_8to1_7_6_10> itemRewriter = new ItemRewriter(this);
private final ChatItemRewriter chatItemRewriter = new ChatItemRewriter(this);
private final MetadataRewriter metadataRewriter = new MetadataRewriter(this);
public Protocol1_8to1_7_6_10() {
super(ClientboundPackets1_7_2.class, ClientboundPackets1_8.class, ServerboundPackets1_7_2.class, ServerboundPackets1_8.class);
@ -97,6 +101,8 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
@Override
protected void registerPackets() {
this.itemRewriter.register();
this.registerClientbound(State.LOGIN, ClientboundLoginPackets.HELLO.getId(), ClientboundLoginPackets.HELLO.getId(), new PacketRemapper() {
@Override
public void registerMap() {
@ -140,7 +146,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
this.registerClientbound(ClientboundPackets1_7_2.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING, Type.STRING, msg -> TranslationRewriter.toClient(ChatItemRewriter.toClient(msg))); // message
map(Type.STRING, Type.STRING, msg -> TranslationRewriter.toClient(chatItemRewriter.remapShowItem(msg))); // message
create(Type.BYTE, (byte) 0); // position
}
});
@ -150,7 +156,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
map(Type.INT, Type.VAR_INT); // entity id
map(Type.SHORT); // slot
map(Types1_7_6.COMPRESSED_ITEM, Type.ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteRead(wrapper.get(Type.ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Type.ITEM, 0)));
}
});
this.registerClientbound(ClientboundPackets1_7_2.SPAWN_POSITION, new PacketRemapper() {
@ -233,11 +239,11 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
final short itemId = wrapper.read(Type.SHORT); // item in hand
final Item currentItem = new DataItem(itemId, (byte) 1, (short) 0, null);
ITEM_REWRITER.rewriteRead(currentItem);
itemRewriter.handleItemToClient(currentItem);
wrapper.write(Type.SHORT, (short) currentItem.identifier());
final List<Metadata> metadata = wrapper.read(Types1_7_6.METADATA_LIST); // metadata
MetadataRewriter.transform(Entity1_10Types.EntityType.PLAYER, metadata);
metadataRewriter.transform(Entity1_10Types.EntityType.PLAYER, metadata);
wrapper.write(Types1_8.METADATA_LIST, metadata);
tablistStorage.sendTempEntry(tempTabEntry);
@ -352,7 +358,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
tracker.updateEntityLocation(entityID, x, y, z, false);
tracker.updateEntityMetadata(entityID, metadataList);
MetadataRewriter.transform(entityType, metadataList);
metadataRewriter.transform(entityType, metadataList);
});
}
});
@ -566,7 +572,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
final int entityID = wrapper.get(Type.VAR_INT, 0);
if (tracker.getTrackedEntities().containsKey(entityID)) {
tracker.updateEntityMetadata(entityID, metadataList);
MetadataRewriter.transform(tracker.getTrackedEntities().get(entityID), metadataList);
metadataRewriter.transform(tracker.getTrackedEntities().get(entityID), metadataList);
if (metadataList.isEmpty()) wrapper.cancel();
} else {
wrapper.cancel();
@ -805,7 +811,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
int damage = 0;
if (parts.length > 2) damage = Integer.parseInt(parts[2]);
final DataItem item = new DataItem(id, (byte) 1, (short) damage, null);
ITEM_REWRITER.rewriteRead(item);
itemRewriter.handleItemToClient(item);
wrapper.write(Type.VAR_INT, item.identifier()); // particle data
if (item.data() != 0)
wrapper.write(Type.VAR_INT, (int) item.data()); // particle data
@ -922,7 +928,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
wrapper.write(Type.SHORT, slot);
});
map(Types1_7_6.COMPRESSED_ITEM, Type.ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteRead(wrapper.get(Type.ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Type.ITEM, 0)));
}
});
this.registerClientbound(ClientboundPackets1_7_2.WINDOW_ITEMS, new PacketRemapper() {
@ -940,7 +946,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
items[1] = new DataItem(351/*lapis_lazuli*/, (byte) 3, (short) 4, null);
}
for (Item item : items) {
ITEM_REWRITER.rewriteRead(item);
itemRewriter.handleItemToClient(item);
}
wrapper.write(Type.ITEM_ARRAY, items);
});
@ -1166,17 +1172,17 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
final int count = wrapper.passthrough(Type.UNSIGNED_BYTE); // count
for (int i = 0; i < count; i++) {
Item item = wrapper.read(Types1_7_6.COMPRESSED_ITEM);
ITEM_REWRITER.rewriteRead(item);
itemRewriter.handleItemToClient(item);
wrapper.write(Type.ITEM, item); // item 1
item = wrapper.read(Types1_7_6.COMPRESSED_ITEM);
ITEM_REWRITER.rewriteRead(item);
itemRewriter.handleItemToClient(item);
wrapper.write(Type.ITEM, item); // item 3
final boolean has3Items = wrapper.passthrough(Type.BOOLEAN); // has 3 items
if (has3Items) {
item = wrapper.read(Types1_7_6.COMPRESSED_ITEM);
ITEM_REWRITER.rewriteRead(item);
itemRewriter.handleItemToClient(item);
wrapper.write(Type.ITEM, item); // item 2
}
@ -1276,7 +1282,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
handler(wrapper -> {
final short direction = wrapper.get(Type.UNSIGNED_BYTE, 0);
final Item item = wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0);
ITEM_REWRITER.rewriteWrite(item);
itemRewriter.handleItemToServer(item);
if (item != null && item.identifier() == ItemList1_6.writtenBook.itemID && direction == 255) { // If placed item is a book then cancel it and send a MC|BOpen to the client
final PacketWrapper openBook = PacketWrapper.create(ClientboundPackets1_8.PLUGIN_MESSAGE, wrapper.user());
@ -1349,7 +1355,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
map(Type.SHORT); // transaction id
map(Type.BYTE); // action
map(Type.ITEM, Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
}
});
this.registerServerbound(ServerboundPackets1_8.CREATIVE_INVENTORY_ACTION, new PacketRemapper() {
@ -1357,7 +1363,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
public void registerMap() {
map(Type.SHORT); // slot
map(Type.ITEM, Types1_7_6.COMPRESSED_ITEM); // item
handler(wrapper -> ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0)));
}
});
this.registerServerbound(ServerboundPackets1_8.UPDATE_SIGN, new PacketRemapper() {
@ -1419,7 +1425,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
case "MC|BEdit":
case "MC|BSign":
final Item item = wrapper.read(Type.ITEM); // book
ITEM_REWRITER.rewriteWrite(item);
itemRewriter.handleItemToServer(item);
lengthPacketWrapper.write(Types1_7_6.COMPRESSED_ITEM, item);
lengthPacketWrapper.writeToBuffer(lengthBuffer);
@ -1587,4 +1593,14 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
userConnection.put(new ChunkTracker(userConnection));
}
@Nullable
@Override
public LegacyItemRewriter<Protocol1_8to1_7_6_10> getItemRewriter() {
return this.itemRewriter;
}
public MetadataRewriter getMetadataRewriter() {
return this.metadataRewriter;
}
}

View File

@ -29,7 +29,13 @@ import java.util.logging.Level;
public class MetadataRewriter {
public static void transform(Entity1_10Types.EntityType type, List<Metadata> list) {
private final Protocol1_8to1_7_6_10 protocol;
public MetadataRewriter(final Protocol1_8to1_7_6_10 protocol) {
this.protocol = protocol;
}
public void transform(Entity1_10Types.EntityType type, List<Metadata> list) {
for (Metadata entry : new ArrayList<>(list)) {
final MetaIndex1_8to1_7_6 metaIndex = MetaIndex1_8to1_7_6.searchIndex(type, entry.id());
try {
@ -85,7 +91,7 @@ public class MetadataRewriter {
entry.setValue(((Number) value).floatValue());
break;
case Slot:
Protocol1_8to1_7_6_10.ITEM_REWRITER.rewriteRead((Item) value);
this.protocol.getItemRewriter().handleItemToClient((Item) value);
break;
case String:
case Position:

View File

@ -27,7 +27,6 @@ import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
import net.raphimc.vialegacy.api.model.Location;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetaIndex1_8to1_7_6;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetadataRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.EntityTracker;
import java.util.*;
@ -306,7 +305,7 @@ public class HologramPartEntity {
for (final Map.Entry<MetaIndex1_8to1_7_6, Object> entry : this.metadata.entrySet()) {
metadataList.add(new Metadata(entry.getKey().getOldIndex(), entry.getKey().getOldType(), entry.getValue()));
}
MetadataRewriter.transform(this.entityType, metadataList);
this.user.getProtocolInfo().getPipeline().getProtocol(Protocol1_8to1_7_6_10.class).getMetadataRewriter().transform(this.entityType, metadataList);
return metadataList;
}

View File

@ -35,7 +35,7 @@ import net.raphimc.vialegacy.util.ViaStringTagReader1_11_2;
import java.io.IOException;
public class ChatItemRewriter extends ComponentRewriter {
public class ChatItemRewriter {
private static final Int2ObjectOpenHashMap<String> ID_TO_NAME = new Int2ObjectOpenHashMap<>(315, 0.99F);
@ -357,89 +357,93 @@ public class ChatItemRewriter extends ComponentRewriter {
ID_TO_NAME.put(2267, "record_wait");
}
private static final ComponentRewriter REMAP_SHOW_ITEM = new ComponentRewriter() {
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent);
final String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item")) return;
private final ComponentRewriter SHOW_ITEM;
final JsonElement value = hoverEvent.get("value");
if (value == null) return;
public ChatItemRewriter(final Protocol1_8to1_7_6_10 protocol) {
this.SHOW_ITEM = new ComponentRewriter(protocol) {
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent);
final String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item")) return;
final String text = findItemNBT(value);
if (text == null) return;
final JsonElement value = hoverEvent.get("value");
if (value == null) return;
final CompoundTag tag;
try {
tag = ViaStringTagReader1_11_2.getTagFromJson(text);
} catch (Exception e) {
ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
throw new RuntimeException(e);
final String text = findItemNBT(value);
if (text == null) return;
final CompoundTag tag;
try {
tag = ViaStringTagReader1_11_2.getTagFromJson(text);
} catch (Exception e) {
ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
throw new RuntimeException(e);
}
final CompoundTag itemTag = tag.get("tag");
final ShortTag idTag = tag.get("id");
final ShortTag damageTag = tag.get("Damage");
// Call item converter
final short damage = damageTag != null ? damageTag.asShort() : 0;
final short id = idTag != null ? idTag.asShort() : 1;
final Item item = new DataItem();
item.setIdentifier(id);
item.setData(damage);
item.setTag(itemTag);
handleItem(item);
// Serialize again
if (damage != item.data()) {
tag.put("Damage", new ShortTag(item.data()));
}
tag.put("id", new StringTag("minecraft:" + ID_TO_NAME.getOrDefault(item.identifier(), "stone")));
if (item.tag() != null) {
tag.put("tag", new CompoundTag(item.tag().getValue()));
}
final JsonArray array = new JsonArray();
final JsonObject object = new JsonObject();
array.add(object);
final String serializedNBT;
try {
serializedNBT = BinaryTagIO.writeString(tag);
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (IOException e) {
ViaLegacy.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text);
e.printStackTrace();
}
}
final CompoundTag itemTag = tag.get("tag");
final ShortTag idTag = tag.get("id");
final ShortTag damageTag = tag.get("Damage");
// Call item converter
final short damage = damageTag != null ? damageTag.asShort() : 0;
final short id = idTag != null ? idTag.asShort() : 1;
final Item item = new DataItem();
item.setIdentifier(id);
item.setData(damage);
item.setTag(itemTag);
handleItem(item);
// Serialize again
if (damage != item.data()) {
tag.put("Damage", new ShortTag(item.data()));
}
tag.put("id", new StringTag("minecraft:" + ID_TO_NAME.getOrDefault(item.identifier(), "stone")));
if (item.tag() != null) {
tag.put("tag", new CompoundTag(item.tag().getValue()));
private void handleItem(Item item) {
this.protocol.getItemRewriter().handleItemToClient(item);
}
final JsonArray array = new JsonArray();
final JsonObject object = new JsonObject();
array.add(object);
final String serializedNBT;
try {
serializedNBT = BinaryTagIO.writeString(tag);
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (IOException e) {
ViaLegacy.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text);
e.printStackTrace();
}
}
private void handleItem(Item item) {
Protocol1_8to1_7_6_10.ITEM_REWRITER.rewriteRead(item);
}
private String findItemNBT(JsonElement element) {
if (element.isJsonArray()) {
for (JsonElement jsonElement : element.getAsJsonArray()) {
String value = findItemNBT(jsonElement);
if (value != null) {
return value;
private String findItemNBT(JsonElement element) {
if (element.isJsonArray()) {
for (JsonElement jsonElement : element.getAsJsonArray()) {
String value = findItemNBT(jsonElement);
if (value != null) {
return value;
}
}
} else if (element.isJsonObject()) {
final JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text");
if (text != null) {
return text.getAsString();
}
} else if (element.isJsonPrimitive()) {
return element.getAsJsonPrimitive().getAsString();
}
} else if (element.isJsonObject()) {
final JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text");
if (text != null) {
return text.getAsString();
}
} else if (element.isJsonPrimitive()) {
return element.getAsJsonPrimitive().getAsString();
return null;
}
return null;
}
};
};
}
public static String toClient(final String text) {
return REMAP_SHOW_ITEM.processText(text).toString();
public String remapShowItem(final String text) {
return SHOW_ITEM.processText(text).toString();
}
}

View File

@ -23,38 +23,41 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.ItemList1_6;
import net.raphimc.vialegacy.api.remapper.AbstractItemRewriter;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.Protocol1_7_6_10to1_7_2_5;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher;
import java.util.UUID;
public class ItemRewriter extends AbstractItemRewriter {
public class ItemRewriter extends LegacyItemRewriter<Protocol1_8to1_7_6_10> {
public ItemRewriter() {
super("1.7", false);
registerRemappedItem(8, 326, "Water Block");
registerRemappedItem(9, 326, "Stationary Water Block");
registerRemappedItem(10, 327, "Lava Block");
registerRemappedItem(11, 327, "Stationary Lava Block");
registerRemappedItem(51, 385, "Fire");
registerRemappedItem(90, 399, "Nether portal");
registerRemappedItem(119, 381, "End portal");
registerRemappedItem(127, 351, 3, "Cocoa Block");
registerRemappedItem(141, 391, "Carrot Crops");
registerRemappedItem(142, 392, "Potato Crops");
registerRemappedItem(43, 44, "Double Stone Slab");
registerRemappedItem(125, 126, "Double Wood Slab");
public ItemRewriter(final Protocol1_8to1_7_6_10 protocol) {
super(protocol, "1.7");
addRemappedItem(8, 326, "Water Block");
addRemappedItem(9, 326, "Stationary Water Block");
addRemappedItem(10, 327, "Lava Block");
addRemappedItem(11, 327, "Stationary Lava Block");
addRemappedItem(51, 385, "Fire");
addRemappedItem(90, 399, "Nether portal");
addRemappedItem(119, 381, "End portal");
addRemappedItem(127, 351, 3, "Cocoa Block");
addRemappedItem(141, 391, "Carrot Crops");
addRemappedItem(142, 392, "Potato Crops");
addRemappedItem(43, 44, "Double Stone Slab");
addRemappedItem(125, 126, "Double Wood Slab");
addNonExistentItems(2);
}
@Override
public void rewriteRead(Item item) {
super.rewriteRead(item);
if (item == null) return;
public Item handleItemToClient(Item item) {
super.handleItemToClient(item);
if (item == null) return null;
if (item.identifier() == ItemList1_6.skull.itemID && item.data() == 3 && item.tag() != null) { // player_skull
if (!item.tag().contains("SkullOwner")) return;
if (!item.tag().contains("SkullOwner")) return item;
String skullOwnerName = null;
if (item.tag().get("SkullOwner") instanceof StringTag) {
@ -71,26 +74,28 @@ public class ItemRewriter extends AbstractItemRewriter {
if (skullOwnerName != null) {
final GameProfileFetcher gameProfileFetcher = Via.getManager().getProviders().get(GameProfileFetcher.class);
if (!ViaLegacy.getConfig().isLegacySkullLoading()) return;
if (!ViaLegacy.getConfig().isLegacySkullLoading()) return item;
if (gameProfileFetcher.isUUIDLoaded(skullOwnerName)) {
final UUID uuid = gameProfileFetcher.getMojangUUID(skullOwnerName);
if (gameProfileFetcher.isGameProfileLoaded(uuid)) {
final GameProfile skullProfile = gameProfileFetcher.getGameProfile(uuid);
if (skullProfile == null || skullProfile.isOffline()) return;
if (skullProfile == null || skullProfile.isOffline()) return item;
item.tag().put("SkullOwner", Protocol1_7_6_10to1_7_2_5.writeGameProfileToTag(skullProfile));
return;
return item;
}
}
gameProfileFetcher.getMojangUUIDAsync(skullOwnerName).thenAccept(gameProfileFetcher::getGameProfile);
}
}
return item;
}
@Override
public void rewriteWrite(Item item) {
if (item == null) return;
public Item handleItemToServer(Item item) {
if (item == null) return null;
NOT_VALID:
if (item.identifier() == ItemList1_6.skull.itemID && item.data() == 3 && item.tag() != null) { // player_skull
@ -100,7 +105,7 @@ public class ItemRewriter extends AbstractItemRewriter {
}
}
super.rewriteWrite(item);
return super.handleItemToServer(item);
}
}