Handle color for items and entities, handle item translatable components

This commit is contained in:
KennyTV 2020-06-02 12:13:21 +02:00
parent 2698725401
commit 38ed381094
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
9 changed files with 85 additions and 36 deletions

View File

@ -5,19 +5,24 @@ import nl.matsv.viabackwards.api.data.MappedItem;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction; import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag; import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
public abstract class ItemRewriter<T extends BackwardsProtocol> extends ItemRewriterBase<T> { public abstract class ItemRewriter<T extends BackwardsProtocol> extends ItemRewriterBase<T> {
private final MappedItemFunction mappedItemFunction; private final MappedItemFunction mappedItemFunction;
private final TranslatableRewriter translatableRewriter;
protected ItemRewriter(T protocol, IdRewriteFunction oldRewriter, IdRewriteFunction newRewriter, MappedItemFunction mappedItemFunction) { protected ItemRewriter(T protocol, TranslatableRewriter translatableRewriter, IdRewriteFunction oldRewriter, IdRewriteFunction newRewriter, MappedItemFunction mappedItemFunction) {
super(protocol, oldRewriter, newRewriter, true); super(protocol, oldRewriter, newRewriter, true);
this.translatableRewriter = translatableRewriter;
this.mappedItemFunction = mappedItemFunction; this.mappedItemFunction = mappedItemFunction;
} }
protected ItemRewriter(T protocol, MappedItemFunction mappedItemFunction) { protected ItemRewriter(T protocol, TranslatableRewriter translatableRewriter, MappedItemFunction mappedItemFunction) {
super(protocol, true); super(protocol, true);
this.translatableRewriter = translatableRewriter;
this.mappedItemFunction = mappedItemFunction; this.mappedItemFunction = mappedItemFunction;
} }
@ -25,32 +30,62 @@ public abstract class ItemRewriter<T extends BackwardsProtocol> extends ItemRewr
public Item handleItemToClient(Item item) { public Item handleItemToClient(Item item) {
if (item == null) return null; if (item == null) return null;
CompoundTag tag = null;
boolean textChanged = false;
if (translatableRewriter != null
&& item.getTag() != null && (tag = item.getTag().get("display")) != null) {
// Handle name and lore components
StringTag name = tag.get("Name");
if (name != null) {
String newValue = translatableRewriter.processText(name.getValue());
if (name.getValue().equals(newValue)) {
textChanged = true;
}
name.setValue(newValue);
}
ListTag lore = tag.get("Lore");
if (lore != null) {
for (Tag loreEntry : lore) {
StringTag stringTag = (StringTag) loreEntry;
String newValue = translatableRewriter.processText(stringTag.getValue());
if (stringTag.getValue().equals(newValue)) {
textChanged = true;
}
stringTag.setValue(newValue);
}
}
}
if (textChanged) {
// Backup data for toServer
item.getTag().put(createViaNBT(item));
}
MappedItem data = mappedItemFunction.get(item.getIdentifier()); MappedItem data = mappedItemFunction.get(item.getIdentifier());
if (data == null) { if (data == null) {
// Just rewrite the id // Just rewrite the id
return super.handleItemToClient(item); return super.handleItemToClient(item);
} }
// Backup data for toServer if not already done above
if (!textChanged) {
if (item.getTag() == null) { if (item.getTag() == null) {
item.setTag(new CompoundTag("")); item.setTag(new CompoundTag(""));
} }
// Backup data for toServer
item.getTag().put(createViaNBT(item)); item.getTag().put(createViaNBT(item));
}
// Also includes the already mapped id // Set remapped id
item.setIdentifier(data.getId()); item.setIdentifier(data.getId());
// Set custom name // Set custom name - only done if there is no original one
CompoundTag tag = item.getTag().get("display");
if (tag == null) { if (tag == null) {
item.getTag().put(tag = new CompoundTag("display")); item.getTag().put(tag = new CompoundTag("display"));
} }
// Only set name if there is no original one
if (!tag.contains("Name")) { if (!tag.contains("Name")) {
tag.put(new StringTag("Name", data.getJsonName())); tag.put(new StringTag("Name", data.getJsonName()));
} }
return item; return item;
} }

View File

@ -45,11 +45,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
Via.getManager().getProviders().register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider()); Via.getManager().getProviders().register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider());
}); });
(blockItemPackets = new BlockItemPackets1_13(this)).register();
new EntityPackets1_13(this).register();
new PlayerPacket1_13(this).register();
new SoundPackets1_13(this).register();
TranslatableRewriter translatableRewriter = new TranslatableRewriter(this) { TranslatableRewriter translatableRewriter = new TranslatableRewriter(this) {
@Override @Override
protected void handleTranslate(JsonObject root, String translate) { protected void handleTranslate(JsonObject root, String translate) {
@ -68,6 +63,11 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
translatableRewriter.registerTitle(0x4B, 0x48); translatableRewriter.registerTitle(0x4B, 0x48);
translatableRewriter.registerPlayerList(0x4E, 0x4A); translatableRewriter.registerPlayerList(0x4E, 0x4A);
(blockItemPackets = new BlockItemPackets1_13(this, translatableRewriter)).register();
new EntityPackets1_13(this).register();
new PlayerPacket1_13(this).register();
new SoundPackets1_13(this).register();
// Thanks to https://wiki.vg/index.php?title=Pre-release_protocol&oldid=14150 // Thanks to https://wiki.vg/index.php?title=Pre-release_protocol&oldid=14150
out(State.PLAY, 0x11, -1, cancel()); // Declare Commands TODO NEW out(State.PLAY, 0x11, -1, cancel()); // Declare Commands TODO NEW

View File

@ -13,6 +13,7 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.block_entity_handlers.FlowerPotHandler; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.block_entity_handlers.FlowerPotHandler;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings;
@ -48,8 +49,8 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
private final Map<String, String> enchantmentMappings = new HashMap<>(); private final Map<String, String> enchantmentMappings = new HashMap<>();
private final String NBT_TAG_NAME; private final String NBT_TAG_NAME;
public BlockItemPackets1_13(Protocol1_12_2To1_13 protocol) { public BlockItemPackets1_13(Protocol1_12_2To1_13 protocol, TranslatableRewriter translatableRewriter) {
super(protocol, id -> BackwardsMappings.itemMappings.getMappedItem(id)); super(protocol, translatableRewriter, id -> BackwardsMappings.itemMappings.getMappedItem(id));
NBT_TAG_NAME = "ViaBackwards|" + protocol.getClass().getSimpleName() + "|Part2"; NBT_TAG_NAME = "ViaBackwards|" + protocol.getClass().getSimpleName() + "|Part2";
} }

View File

@ -29,13 +29,6 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol {
protected void registerPackets() { protected void registerPackets() {
executeAsyncAfterLoaded(Protocol1_14To1_13_2.class, BackwardsMappings::init); executeAsyncAfterLoaded(Protocol1_14To1_13_2.class, BackwardsMappings::init);
blockItemPackets = new BlockItemPackets1_14(this);
blockItemPackets.register();
entityPackets = new EntityPackets1_14(this);
entityPackets.register();
new PlayerPackets1_14(this).register();
new SoundPackets1_14(this).register();
TranslatableRewriter translatableRewriter = new TranslatableRewriter(this); TranslatableRewriter translatableRewriter = new TranslatableRewriter(this);
translatableRewriter.registerBossBar(0x0C, 0x0C); translatableRewriter.registerBossBar(0x0C, 0x0C);
translatableRewriter.registerChatMessage(0x0E, 0x0E); translatableRewriter.registerChatMessage(0x0E, 0x0E);
@ -45,6 +38,13 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol {
translatableRewriter.registerTitle(0x4F, 0x4B); translatableRewriter.registerTitle(0x4F, 0x4B);
translatableRewriter.registerPing(); translatableRewriter.registerPing();
blockItemPackets = new BlockItemPackets1_14(this, translatableRewriter);
blockItemPackets.register();
entityPackets = new EntityPackets1_14(this);
entityPackets.register();
new PlayerPackets1_14(this).register();
new SoundPackets1_14(this).register();
registerOutgoing(State.PLAY, 0x15, 0x16); registerOutgoing(State.PLAY, 0x15, 0x16);
registerOutgoing(State.PLAY, 0x18, 0x19); registerOutgoing(State.PLAY, 0x18, 0x19);
registerOutgoing(State.PLAY, 0x1B, 0x1C); registerOutgoing(State.PLAY, 0x1B, 0x1C);

View File

@ -5,6 +5,7 @@ import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter;
import nl.matsv.viabackwards.api.rewriters.RecipeRewriter; import nl.matsv.viabackwards.api.rewriters.RecipeRewriter;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.BackwardsMappings;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.RecipeRewriter1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.RecipeRewriter1_14;
@ -47,8 +48,8 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It
private EnchantmentRewriter enchantmentRewriter; private EnchantmentRewriter enchantmentRewriter;
public BlockItemPackets1_14(Protocol1_13_2To1_14 protocol) { public BlockItemPackets1_14(Protocol1_13_2To1_14 protocol, TranslatableRewriter translatableRewriter) {
super(protocol, BlockItemPackets1_14::getOldItemId, BlockItemPackets1_14::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); super(protocol, translatableRewriter, BlockItemPackets1_14::getOldItemId, BlockItemPackets1_14::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id));
} }
@Override @Override

View File

@ -23,7 +23,7 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter<Protocol1_14_4To1_15> { public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter<Protocol1_14_4To1_15> {
public BlockItemPackets1_15(Protocol1_14_4To1_15 protocol) { public BlockItemPackets1_15(Protocol1_14_4To1_15 protocol) {
super(protocol, BlockItemPackets1_15::getOldItemId, BlockItemPackets1_15::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); super(protocol, null, BlockItemPackets1_15::getOldItemId, BlockItemPackets1_15::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id));
} }
@Override @Override

View File

@ -23,16 +23,13 @@ import java.util.UUID;
public class Protocol1_15_2To1_16 extends BackwardsProtocol { public class Protocol1_15_2To1_16 extends BackwardsProtocol {
private BlockItemPackets1_16 blockItemPackets; private BlockItemPackets1_16 blockItemPackets;
private TranslatableRewriter translatableRewriter;
@Override @Override
protected void registerPackets() { protected void registerPackets() {
executeAsyncAfterLoaded(Protocol1_16To1_15_2.class, BackwardsMappings::init); executeAsyncAfterLoaded(Protocol1_16To1_15_2.class, BackwardsMappings::init);
(blockItemPackets = new BlockItemPackets1_16(this)).register(); translatableRewriter = new TranslatableRewriter1_16(this);
EntityPackets1_16 entityPackets = new EntityPackets1_16(this);
entityPackets.register();
TranslatableRewriter translatableRewriter = new TranslatableRewriter1_16(this);
translatableRewriter.registerBossBar(0x0D, 0x0D); translatableRewriter.registerBossBar(0x0D, 0x0D);
translatableRewriter.registerCombatEvent(0x33, 0x33); translatableRewriter.registerCombatEvent(0x33, 0x33);
translatableRewriter.registerDisconnect(0x1B, 0x1B); translatableRewriter.registerDisconnect(0x1B, 0x1B);
@ -40,6 +37,10 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
translatableRewriter.registerTitle(0x50, 0x50); translatableRewriter.registerTitle(0x50, 0x50);
translatableRewriter.registerPing(); translatableRewriter.registerPing();
(blockItemPackets = new BlockItemPackets1_16(this, translatableRewriter)).register();
EntityPackets1_16 entityPackets = new EntityPackets1_16(this);
entityPackets.register();
// Chat Message // Chat Message
registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() { registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() {
@Override @Override
@ -207,4 +208,8 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
public BlockItemPackets1_16 getBlockItemPackets() { public BlockItemPackets1_16 getBlockItemPackets() {
return blockItemPackets; return blockItemPackets;
} }
public TranslatableRewriter getTranslatableRewriter() {
return translatableRewriter;
}
} }

View File

@ -2,6 +2,7 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets;
import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter; import nl.matsv.viabackwards.api.rewriters.EnchantmentRewriter;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.RecipeRewriter1_15; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.RecipeRewriter1_15;
import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16;
import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings;
@ -31,8 +32,9 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It
private EnchantmentRewriter enchantmentRewriter; private EnchantmentRewriter enchantmentRewriter;
public BlockItemPackets1_16(Protocol1_15_2To1_16 protocol) { public BlockItemPackets1_16(Protocol1_15_2To1_16 protocol, TranslatableRewriter translatableRewriter) {
super(protocol, BlockItemPackets1_16::getOldItemId, BlockItemPackets1_16::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id)); super(protocol, translatableRewriter,
BlockItemPackets1_16::getOldItemId, BlockItemPackets1_16::getNewItemId, id -> BackwardsMappings.itemMappings.getMappedItem(id));
} }
@Override @Override

View File

@ -157,6 +157,11 @@ public class EntityPackets1_16 extends EntityRewriter<Protocol1_15_2To1_16> {
} else if (type == MetaType1_14.PARTICLE) { } else if (type == MetaType1_14.PARTICLE) {
Particle particle = (Particle) meta.getValue(); Particle particle = (Particle) meta.getValue();
particle.setId(ParticleMapping.getOldId(particle.getId())); particle.setId(ParticleMapping.getOldId(particle.getId()));
} else if (type == MetaType1_14.OptChat) {
String text = meta.getCastedValue();
if (text != null) {
meta.setValue(protocol.getTranslatableRewriter().processText(text));
}
} }
return meta; return meta;
}); });