Merge pull request #841 from creeper123123321/dev2

Enchantment and display name rewriting, use a map for new to old items
This commit is contained in:
Myles 2018-06-13 22:38:14 +01:00 committed by GitHub
commit a41cbdde7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 145 additions and 29 deletions

View File

@ -41,6 +41,10 @@ public class ProtocolSnapshotTo1_12_2 extends Protocol {
MappingData.init();
}
public static String jsonTextToLegacy(String value) {
return TextComponent.toLegacyText(ComponentSerializer.parse(value));
}
@Override
protected void registerPackets() {
// Register grouped packet changes

View File

@ -1,5 +1,7 @@
package us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -14,9 +16,11 @@ import java.util.Map;
public class MappingData {
public static Map<Integer, Integer> oldToNewBlocks = new HashMap<>();
public static Map<Integer, Integer> oldToNewItems = new HashMap<>();
public static Map<Integer, Integer> newToOldItems = new HashMap<>();
public static Map<String, int[]> blockTags = new HashMap<>();
public static Map<String, int[]> itemTags = new HashMap<>();
public static Map<String, int[]> fluidTags = new HashMap<>();
public static BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
public static void init() {
JsonObject mapping1_12 = loadData("mapping-1.12.json");
@ -27,9 +31,14 @@ public class MappingData {
mapIdentifiers(oldToNewBlocks, mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks"));
System.out.println("Loading item mapping...");
mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items"));
System.out.println("Loading new to old item mapping...");
mapIdentifiers(newToOldItems, mapping1_13.getAsJsonObject("items"), mapping1_12.getAsJsonObject("items"));
System.out.println("Loading new tags...");
loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags"));
loadTags(itemTags, mapping1_13.getAsJsonObject("item_tags"));
loadTags(fluidTags, mapping1_13.getAsJsonObject("fluid_tags"));
System.out.println("Loading enchantments...");
loadEnchantments(oldEnchantmentsIds, mapping1_12.getAsJsonObject("enchantments"));
}
private static void mapIdentifiers(Map<Integer, Integer> output, JsonObject oldIdentifiers, JsonObject newIdentifiers) {
@ -64,6 +73,12 @@ public class MappingData {
}
}
public static void loadEnchantments(Map<Short, String> output, JsonObject enchantments) {
for (Map.Entry<String, JsonElement> enchantment : enchantments.entrySet()) {
output.put(Short.parseShort(enchantment.getKey()), enchantment.getValue().getAsString());
}
}
public static JsonObject loadData(String name) {
InputStream stream = MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
InputStreamReader reader = new InputStreamReader(stream);

View File

@ -1,8 +1,6 @@
package us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.packets;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.*;
import com.google.common.base.Optional;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.item.Item;
@ -11,6 +9,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.ProtocolSnapshotTo1_12_2;
import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data.SoundSource;
import us.myles.ViaVersion.protocols.protocolsnapshotto1_12_2.data.SpawnEggRewriter;
@ -212,6 +211,7 @@ public class InventoryPackets {
int originalId = (item.getId() << 16 | item.getData() & 0xFFFF);
tag.put(new IntTag(NBT_TAG_NAME, originalId));
// NBT changes
if (isDamageable(item.getId())) {
tag.put(new IntTag("Damage", item.getData()));
}
@ -230,13 +230,45 @@ public class InventoryPackets {
}
}
// Display Name now uses JSON
if (tag.get("display") instanceof CompoundTag) {
if (((CompoundTag) tag.get("display")).get("Name") instanceof StringTag) {
StringTag name = ((CompoundTag) tag.get("display")).get("Name");
name.setValue(
ProtocolSnapshotTo1_12_2.legacyTextToJson(
name.getValue()
)
);
}
}
// ench is now Enchantments and now uses identifiers
if (tag.get("ench") instanceof ListTag) {
ListTag ench = tag.get("ench");
ListTag enchantments = new ListTag("Enchantments", CompoundTag.class);
for (Tag enchEntry : ench) {
if (enchEntry instanceof CompoundTag) {
CompoundTag enchantmentEntry = new CompoundTag("");
enchantmentEntry.put(new StringTag("id",
MappingData.oldEnchantmentsIds.get(
(Short) ((CompoundTag) enchEntry).get("id").getValue()
)
));
enchantmentEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchEntry).get("lvl").getValue()));
enchantments.add(enchantmentEntry);
}
}
tag.remove("ench");
tag.put(enchantments);
}
int rawId = (item.getId() << 4 | item.getData() & 0xF);
// Handle SpawnEggs
if (item.getId() == 383) {
if (tag.contains("EntityTag")) {
if (tag.get("EntityTag") instanceof CompoundTag) {
CompoundTag entityTag = tag.get("EntityTag");
if (entityTag.contains("id") && entityTag.get("id") instanceof StringTag) {
if (entityTag.get("id") instanceof StringTag) {
StringTag identifier = entityTag.get("id");
rawId = SpawnEggRewriter.getSpawnEggId(identifier.getValue());
} else {
@ -271,9 +303,9 @@ public class InventoryPackets {
CompoundTag tag = item.getTag();
// Use tag to get original ID and data
if (tag != null) {
// Check for valid tag
if (tag.contains(NBT_TAG_NAME)) {
if (tag.get(NBT_TAG_NAME) instanceof IntTag) {
rawId = (Integer) tag.get(NBT_TAG_NAME).getValue();
// Remove the tag
@ -281,11 +313,10 @@ public class InventoryPackets {
gotRawIdFromTag = true;
}
}
}
if (rawId == null) {
for (Map.Entry<Integer, Integer> entry : MappingData.oldToNewItems.entrySet()) {
if (entry.getValue() == item.getId()) {
int oldId = entry.getKey();
Integer oldId = MappingData.newToOldItems.get((int) item.getId());
if (oldId != null) {
// Handle spawn eggs
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
if (eggEntityId.isPresent()) {
@ -300,8 +331,6 @@ public class InventoryPackets {
} else {
rawId = (oldId >> 4) << 16 | oldId & 0xF;
}
break;
}
}
}
@ -313,6 +342,7 @@ public class InventoryPackets {
item.setId((short) (rawId >> 16));
item.setData((short) (rawId & 0xFFFF));
// NBT changes
if (tag != null) {
if (isDamageable(item.getId())) {
if (tag.get("Damage") instanceof IntTag) {
@ -339,6 +369,41 @@ public class InventoryPackets {
}
}
}
// Display Name now uses JSON
if (tag.get("display") instanceof CompoundTag) {
if (((CompoundTag) tag.get("display")).get("Name") instanceof StringTag) {
StringTag name = ((CompoundTag) tag.get("display")).get("Name");
name.setValue(
ProtocolSnapshotTo1_12_2.jsonTextToLegacy(
name.getValue()
)
);
}
}
// ench is now Enchantments and now uses identifiers
if (tag.get("Enchantments") instanceof ListTag) {
ListTag enchantments = tag.get("Enchantments");
ListTag ench = new ListTag("ench", CompoundTag.class);
for (Tag enchantmentEntry : enchantments) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag("");
enchEntry.put(
new ShortTag(
"id",
MappingData.oldEnchantmentsIds.inverse().get(
(String) ((CompoundTag) enchantmentEntry).get("id").getValue()
)
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
ench.add(enchEntry);
}
}
tag.remove("Enchantment");
tag.put(ench);
}
}
}

View File

@ -2377,5 +2377,37 @@
"25100328": "minecraft:zombie_horse_spawn_egg",
"25100329": "minecraft:zombie_pigman_spawn_egg",
"25100330": "minecraft:zombie_villager_spawn_egg"
},
"enchantments": {
"0": "minecraft:protection",
"1": "minecraft:fire_protection",
"2": "minecraft:feather_falling",
"3": "minecraft:blast_protection",
"4": "minecraft:projectile_protection",
"5": "minecraft:respiration",
"6": "minecraft:aqua_affinity",
"7": "minecraft:thorns",
"8": "minecraft:depth_strider",
"9": "minecraft:frost_walker",
"10": "minecraft:binding_curse",
"16": "minecraft:sharpness",
"17": "minecraft:smite",
"18": "minecraft:bane_of_arthropods",
"19": "minecraft:knockback",
"20": "minecraft:fire_aspect",
"21": "minecraft:looting",
"22": "minecraft:sweeping",
"32": "minecraft:efficiency",
"33": "minecraft:silk_touch",
"34": "minecraft:unbreaking",
"35": "minecraft:fortune",
"48": "minecraft:power",
"49": "minecraft:punch",
"50": "minecraft:flame",
"51": "minecraft:infinity",
"61": "minecraft:luck_of_the_sea",
"62": "minecraft:lure",
"70": "minecraft:mending",
"71": "minecraft:vanishing_curse"
}
}