3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-12-26 00:00:33 +01:00

Some minor optimizations

Dieser Commit ist enthalten in:
KennyTV 2020-06-03 11:20:17 +02:00
Ursprung e25abe57e0
Commit 5988ab4c19
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
11 geänderte Dateien mit 100 neuen und 77 gelöschten Zeilen

Datei anzeigen

@ -106,7 +106,11 @@ public class EnchantmentRewriter {
public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant) {
String key = storedEnchant ? "StoredEnchantments" : "Enchantments";
ListTag remappedEnchantments = tag.get(nbtTagName + "|" + key);
ListTag enchantments = tag.contains(key) ? tag.get(key) : new ListTag(key, CompoundTag.class);
ListTag enchantments = tag.get(key);
if (enchantments == null) {
enchantments = new ListTag(key, CompoundTag.class);
}
if (!storedEnchant && tag.contains(nbtTagName + "|dummyEnchant")) {
tag.remove(nbtTagName + "|dummyEnchant");

Datei anzeigen

@ -7,6 +7,7 @@ import us.myles.viaversion.libs.opennbt.conversion.builtin.CompoundTagConverter;
import us.myles.viaversion.libs.opennbt.tag.builtin.ByteTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.ShortTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewriter<T> {
@ -21,7 +22,7 @@ public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewr
this.toClientRewriter = toClientRewriter;
this.toServerRewriter = toServerRewriter;
this.jsonNameFormat = jsonNameFormat;
nbtTagName = "ViaBackwards|" + protocol.getClass().getSimpleName();
nbtTagName = "VB|" + protocol.getClass().getSimpleName();
}
protected ItemRewriterBase(T protocol, boolean jsonNameFormat) {
@ -47,21 +48,23 @@ public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewr
return item;
}
CompoundTag viaTag = tag.get(nbtTagName);
CompoundTag viaTag = tag.remove(nbtTagName);
if (viaTag != null) {
short id = (short) viaTag.get("id").getValue();
short data = (short) viaTag.get("data").getValue();
byte amount = (byte) viaTag.get("amount").getValue();
CompoundTag extras = viaTag.get("extras");
item.setIdentifier(id);
Tag dataTag = viaTag.get("data");
short data = dataTag != null ? (short) dataTag.getValue() : 0;
item.setData(data);
Tag amountTag = viaTag.get("amount");
byte amount = amountTag != null ? (byte) amountTag.getValue() : 1;
item.setAmount(amount);
CompoundTag extras = viaTag.get("extras");
if (extras != null) {
item.setTag(CONVERTER.convert("", CONVERTER.convert(extras)));
}
// Remove data tag
tag.remove(nbtTagName);
} else {
// Rewrite id normally
if (toServerRewriter != null) {
@ -74,8 +77,12 @@ public abstract class ItemRewriterBase<T extends BackwardsProtocol> extends Rewr
protected CompoundTag createViaNBT(Item item) {
CompoundTag tag = new CompoundTag(nbtTagName);
tag.put(new ShortTag("id", (short) item.getIdentifier()));
tag.put(new ShortTag("data", item.getData()));
tag.put(new ByteTag("amount", item.getAmount()));
if (item.getAmount() != 1) {
tag.put(new ByteTag("amount", item.getAmount()));
}
if (item.getData() != 0) {
tag.put(new ShortTag("data", item.getData()));
}
if (item.getTag() != null) {
tag.put(CONVERTER.convert("extras", CONVERTER.convert(item.getTag())));
}

Datei anzeigen

@ -27,6 +27,7 @@ import us.myles.viaversion.libs.gson.JsonPrimitive;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.IntTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
import java.util.HashMap;
import java.util.Map;
@ -50,7 +51,7 @@ public abstract class LegacyBlockItemRewriter<T extends BackwardsProtocol> exten
JsonPrimitive blockField = object.getAsJsonPrimitive("block");
boolean block = blockField != null && blockField.getAsBoolean();
if (dataEntry.getKey().contains("-")) {
if (dataEntry.getKey().indexOf('-') != -1) {
// Range of ids
String[] split = dataEntry.getKey().split("-", 2);
int from = Integer.parseInt(split[0]);
@ -153,17 +154,23 @@ public abstract class LegacyBlockItemRewriter<T extends BackwardsProtocol> exten
// Map Block Entities
Map<Pos, CompoundTag> tags = new HashMap<>();
for (CompoundTag tag : chunk.getBlockEntities()) {
if (!(tag.contains("x") && tag.contains("y") && tag.contains("z")))
Tag xTag;
Tag yTag;
Tag zTag;
if ((xTag = tag.get("x")) == null || (yTag = tag.get("y")) == null || (zTag = tag.get("z")) == null) {
continue;
}
Pos pos = new Pos(
(int) tag.get("x").getValue() & 0xF,
(int) tag.get("y").getValue(),
(int) tag.get("z").getValue() & 0xF);
(int) xTag.getValue() & 0xF,
(int) yTag.getValue(),
(int) zTag.getValue() & 0xF);
tags.put(pos, tag);
// Handle given Block Entities
ChunkSection section = chunk.getSections()[pos.getY() >> 4];
if (section == null) continue;
int block = section.getFlatBlock(pos.getX(), pos.getY() & 0xF, pos.getZ());
int btype = block >> 4;

Datei anzeigen

@ -77,10 +77,12 @@ public class LegacyEnchantmentRewriter {
public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant) {
String key = storedEnchant ? "StoredEnchantments" : "ench";
ListTag remappedEnchantments = tag.get(nbtTagName + "|" + key);
ListTag enchantments = tag.contains(key) ? tag.get(key) : new ListTag(key, CompoundTag.class);
if (!storedEnchant && tag.contains(nbtTagName + "|dummyEnchant")) {
tag.remove(nbtTagName + "|dummyEnchant");
ListTag enchantments = tag.get(key);
if (enchantments == null) {
enchantments = new ListTag(key, CompoundTag.class);
}
if (!storedEnchant && tag.remove(nbtTagName + "|dummyEnchant") != null) {
for (Tag enchantment : enchantments.clone()) {
Short id = (Short) ((CompoundTag) enchantment).get("id").getValue();
Short level = (Short) ((CompoundTag) enchantment).get("lvl").getValue();

Datei anzeigen

@ -36,6 +36,7 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
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.Tag;
import java.util.Arrays;
import java.util.Optional;
@ -198,14 +199,15 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter<Protocol1_10To
handleChunk(chunk);
// only patch it for signs for now, TODO-> Find all the block entities old/new to replace ids and implement in ViaVersion
chunk.getBlockEntities().stream()
.filter(tag -> tag.contains("id") && tag.get("id") instanceof StringTag)
.forEach(tag -> {
String id = (String) tag.get("id").getValue();
if (id.equals("minecraft:sign")) {
((StringTag) tag.get("id")).setValue("Sign");
}
});
for (CompoundTag tag : chunk.getBlockEntities()) {
Tag idTag = tag.get("id");
if (!(idTag instanceof StringTag)) continue;
String id = (String) idTag.getValue();
if (id.equals("minecraft:sign")) {
((StringTag) idTag).setValue("Sign");
}
}
}
});
}

Datei anzeigen

@ -41,12 +41,13 @@ public class BannerHandler implements BackwardsBlockEntityHandler {
}
// Invert colors
if (tag.contains("Patterns") && tag.get("Patterns") instanceof ListTag) {
for (Tag pattern : (ListTag) tag.get("Patterns")) {
if (pattern instanceof CompoundTag) {
IntTag c = ((CompoundTag) pattern).get("Color");
c.setValue(15 - c.getValue()); // Invert color id
}
Tag patternsTag = tag.get("Patterns");
if (patternsTag instanceof ListTag) {
for (Tag pattern : (ListTag) patternsTag) {
if (!(pattern instanceof CompoundTag)) continue;
IntTag c = ((CompoundTag) pattern).get("Color");
c.setValue(15 - c.getValue()); // Invert color id
}
}

Datei anzeigen

@ -55,7 +55,7 @@ public class FlowerPotHandler implements BackwardsBlockEntityProvider.BackwardsB
}
public static boolean isFlowah(int id) {
return flowers.containsKey(id);
return id >= 5265 && id <= 5286;
}
public Pair<String, Byte> getOrDefault(int blockId) {

Datei anzeigen

@ -72,12 +72,12 @@ public class PistonHandler implements BackwardsBlockEntityProvider.BackwardsBloc
String dataFromTag = getDataFromTag(blockState);
if (dataFromTag == null) return tag;
if (!pistonIds.containsKey(dataFromTag)) {
Integer id = pistonIds.get(dataFromTag);
if (id == null) {
ViaBackwards.getPlatform().getLogger().warning("Unmapped piston id: " + dataFromTag);
return tag;
}
int id = pistonIds.get(dataFromTag);
tag.put(new IntTag("blockId", id >> 4));
tag.put(new IntTag("blockData", id & 15));
return tag;

Datei anzeigen

@ -15,18 +15,21 @@ import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBl
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag;
import us.myles.viaversion.libs.opennbt.tag.builtin.Tag;
public class SpawnerHandler implements BackwardsBlockEntityProvider.BackwardsBlockEntityHandler {
@Override
public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) {
if (tag.contains("SpawnData") && tag.get("SpawnData") instanceof CompoundTag) {
CompoundTag data = tag.get("SpawnData");
if (data.contains("id") && data.get("id") instanceof StringTag) {
StringTag s = data.get("id");
Tag dataTag = tag.get("SpawnData");
if (dataTag instanceof CompoundTag) {
CompoundTag data = (CompoundTag) dataTag;
Tag idTag = data.get("id");
if (idTag instanceof StringTag) {
StringTag s = (StringTag) idTag;
s.setValue(EntityNameRewrites.rewrite(s.getValue()));
}
}
return tag;
}
}

Datei anzeigen

@ -63,8 +63,9 @@ public class BackwardsMappings {
short hardId = -1;
if (value == null) {
JsonPrimitive replacement = mapping.getAsJsonPrimitive(key);
if (replacement == null && key.contains("[")) {
replacement = mapping.getAsJsonPrimitive(key.substring(0, key.indexOf('[')));
int propertyIndex;
if (replacement == null && (propertyIndex = key.indexOf('[')) != -1) {
replacement = mapping.getAsJsonPrimitive(key.substring(0, propertyIndex));
}
if (replacement != null) {
if (replacement.getAsString().startsWith("id:")) {

Datei anzeigen

@ -57,11 +57,11 @@ import java.util.Optional;
public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.ItemRewriter<Protocol1_12_2To1_13> {
private final Map<String, String> enchantmentMappings = new HashMap<>();
private final String NBT_TAG_NAME;
private final String extraNbtTag;
public BlockItemPackets1_13(Protocol1_12_2To1_13 protocol) {
super(protocol, null, id -> BackwardsMappings.itemMappings.getMappedItem(id));
NBT_TAG_NAME = "ViaBackwards|" + protocol.getClass().getSimpleName() + "|Part2";
extraNbtTag = "VB|" + protocol.getClass().getSimpleName() + "|2";
}
public static int toOldId(int oldId) {
@ -96,7 +96,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
// Set Cooldown
protocol.out(State.PLAY, 0x18, 0x17, new PacketRemapper() {
@Override
public void registerMap() { //TODO is this actually the correct id conversion?
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
@ -309,14 +309,13 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
BackwardsBlockEntityProvider provider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);
BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class);
for (CompoundTag tag : chunk.getBlockEntities()) {
if (!tag.contains("id"))
continue;
Tag idTag = tag.get("id");
if (idTag == null) continue;
String id = (String) tag.get("id").getValue();
String id = (String) idTag.getValue();
// Ignore if we don't handle it
if (!provider.isHandled(id))
continue;
if (!provider.isHandled(id)) continue;
int sectionIndex = ((int) tag.get("y").getValue()) >> 4;
ChunkSection section = chunk.getSections()[sectionIndex];
@ -550,10 +549,10 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
// Use tag to get original ID and data
if (tag != null) {
// Check for valid tag
if (tag.get(NBT_TAG_NAME) instanceof IntTag) {
rawId = (Integer) tag.get(NBT_TAG_NAME).getValue();
if (tag.get(extraNbtTag) instanceof IntTag) {
rawId = (Integer) tag.get(extraNbtTag).getValue();
// Remove the tag
tag.remove(NBT_TAG_NAME);
tag.remove(extraNbtTag);
gotRawIdFromTag = true;
}
}
@ -613,7 +612,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
if (display != null) {
StringTag name = display.get("Name");
if (name instanceof StringTag) {
StringTag via = display.remove(NBT_TAG_NAME + "|Name");
StringTag via = display.remove(extraNbtTag + "|Name");
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(name.getValue()));
}
}
@ -650,7 +649,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
if (blockTag == null) return;
ListTag newCanPlaceOn = new ListTag(tagName, StringTag.class);
tag.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|" + tagName, ConverterRegistry.convertToValue(blockTag)));
tag.put(ConverterRegistry.convertToTag(extraNbtTag + "|" + tagName, ConverterRegistry.convertToValue(blockTag)));
for (Tag oldTag : blockTag) {
Object value = oldTag.getValue();
String[] newValues = value instanceof String ?
@ -671,7 +670,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
ListTag enchantments = tag.get(key);
if (enchantments == null) return;
ListTag noMapped = new ListTag(NBT_TAG_NAME + "|" + key, CompoundTag.class);
ListTag noMapped = new ListTag(extraNbtTag + "|" + key, CompoundTag.class);
ListTag newEnchantments = new ListTag(storedEnch ? key : "ench", CompoundTag.class);
List<Tag> lore = new ArrayList<>();
boolean hasValidEnchants = false;
@ -696,8 +695,9 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
// Some custom-enchant plugins write it into the lore manually, which would double its entry
if (ViaBackwards.getConfig().addCustomEnchantsToLore()) {
String name = newId;
if (name.contains(":")) {
name = name.split(":")[1];
int index = name.indexOf(':') + 1;
if (index != 0 && index != name.length()) {
name = name.substring(index);
}
name = "§7" + Character.toUpperCase(name.charAt(0)) + name.substring(1).toLowerCase(Locale.ENGLISH);
@ -729,9 +729,9 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
IntTag hideFlags = tag.get("HideFlags");
if (hideFlags == null) {
hideFlags = new IntTag("HideFlags");
tag.put(new ByteTag(NBT_TAG_NAME + "|DummyEnchant"));
tag.put(new ByteTag(extraNbtTag + "|DummyEnchant"));
} else {
tag.put(new IntTag(NBT_TAG_NAME + "|OldHideFlags", hideFlags.getValue()));
tag.put(new IntTag(extraNbtTag + "|OldHideFlags", hideFlags.getValue()));
}
if (newEnchantments.size() == 0) {
@ -758,9 +758,9 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
ListTag loreTag = display.get("Lore");
if (loreTag == null) {
display.put(loreTag = new ListTag("Lore", StringTag.class));
tag.put(new ByteTag(NBT_TAG_NAME + "|DummyLore"));
tag.put(new ByteTag(extraNbtTag + "|DummyLore"));
} else if (loreTag.size() != 0) {
ListTag oldLore = new ListTag(NBT_TAG_NAME + "|OldLore", StringTag.class);
ListTag oldLore = new ListTag(extraNbtTag + "|OldLore", StringTag.class);
for (Tag value : loreTag) {
oldLore.add(value.clone());
}
@ -808,7 +808,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
CompoundTag displayTag = (CompoundTag) display;
StringTag name = displayTag.get("Name");
if (name instanceof StringTag) {
displayTag.put(new StringTag(NBT_TAG_NAME + "|Name", name.getValue()));
displayTag.put(new StringTag(extraNbtTag + "|Name", name.getValue()));
name.setValue(ChatRewriter.legacyTextToJson(name.getValue()));
}
}
@ -858,7 +858,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
if (!MappingData.oldToNewItems.containsKey(rawId)) {
if (!isDamageable(item.getIdentifier()) && item.getIdentifier() != 358) { // Map
if (tag == null) item.setTag(tag = new CompoundTag("tag"));
tag.put(new IntTag(NBT_TAG_NAME, originalId)); // Data will be lost, saving original id
tag.put(new IntTag(extraNbtTag, originalId)); // Data will be lost, saving original id
}
if (item.getIdentifier() == 229) { // purple shulker box
@ -885,7 +885,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
}
private void rewriteCanPlaceToServer(CompoundTag tag, String tagName) {
ListTag blockTag = tag.remove(NBT_TAG_NAME + "|" + tagName);
ListTag blockTag = tag.remove(extraNbtTag + "|" + tagName);
if (blockTag != null) {
tag.put(ConverterRegistry.convertToTag(tagName, ConverterRegistry.convertToValue(blockTag)));
} else if ((blockTag = tag.get(tagName)) != null) {
@ -920,15 +920,13 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
ListTag newEnchantments = new ListTag(key, CompoundTag.class);
boolean dummyEnchant = false;
if (!storedEnch) {
IntTag hideFlags = tag.get(NBT_TAG_NAME + "|OldHideFlags");
IntTag hideFlags = tag.remove(extraNbtTag + "|OldHideFlags");
if (hideFlags != null) {
tag.put(new IntTag("HideFlags", hideFlags.getValue()));
dummyEnchant = true;
tag.remove(NBT_TAG_NAME + "|OldHideFlags");
} else if (tag.contains(NBT_TAG_NAME + "|DummyEnchant")) {
} else if (tag.remove(extraNbtTag + "|DummyEnchant") != null) {
tag.remove("HideFlags");
dummyEnchant = true;
tag.remove(NBT_TAG_NAME + "|DummyEnchant");
}
}
@ -949,12 +947,12 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
newEnchantments.add(enchantmentEntry);
}
ListTag noMapped = tag.get(NBT_TAG_NAME + "|Enchantments");
ListTag noMapped = tag.get(extraNbtTag + "|Enchantments");
if (noMapped != null) {
for (Tag value : noMapped) {
newEnchantments.add(value);
}
tag.remove(NBT_TAG_NAME + "|Enchantments");
tag.remove(extraNbtTag + "|Enchantments");
}
CompoundTag display = tag.get("display");
@ -962,7 +960,7 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
tag.put(display = new CompoundTag("display"));
}
ListTag oldLore = tag.get(NBT_TAG_NAME + "|OldLore");
ListTag oldLore = tag.get(extraNbtTag + "|OldLore");
if (oldLore != null) {
ListTag lore = display.get("Lore");
if (lore == null) {
@ -970,14 +968,12 @@ public class BlockItemPackets1_13 extends nl.matsv.viabackwards.api.rewriters.It
}
lore.setValue(oldLore.getValue());
tag.remove(NBT_TAG_NAME + "|OldLore");
} else if (tag.contains(NBT_TAG_NAME + "|DummyLore")) {
tag.remove(extraNbtTag + "|OldLore");
} else if (tag.remove(extraNbtTag + "|DummyLore") != null) {
display.remove("Lore");
if (display.isEmpty()) {
tag.remove("display");
}
tag.remove(NBT_TAG_NAME + "|DummyLore");
}
if (!storedEnch) {