From 621c02f974a85880790dd3e72a0da4f7972ecfe0 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 13 Feb 2024 18:41:05 +0100 Subject: [PATCH] Move some data out of extending MappingData classes --- .../com/viaversion/viaversion/api/ViaAPI.java | 2 +- .../Protocol1_16To1_15_2.java | 5 +- .../data/AttributeMappings.java | 43 ++++++++++ .../data/MappingData.java | 50 ----------- .../packets/EntityPackets.java | 3 +- .../packets/InventoryPackets.java | 5 +- .../Protocol1_18To1_17_1.java | 5 +- .../data/BlockEntities.java | 76 +++++++++++++++++ .../data/MappingData.java | 84 ------------------- .../packets/WorldPackets.java | 3 +- 10 files changed, 133 insertions(+), 143 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/AttributeMappings.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java create mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/BlockEntities.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/MappingData.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java index 89322428f..a3b19bc66 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java +++ b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java @@ -63,7 +63,7 @@ public interface ViaAPI { * @return API version incremented with meaningful API changes */ default int apiVersion() { - return 23; + return 24; } /** diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index 736db5ddf..1c65a8083 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -23,6 +23,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.MappingData; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_16; import com.viaversion.viaversion.api.platform.providers.ViaProviders; @@ -35,7 +37,6 @@ import com.viaversion.viaversion.api.type.types.version.Types1_16; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.MappingData; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets; @@ -56,7 +57,7 @@ import java.util.UUID; public class Protocol1_16To1_15_2 extends AbstractProtocol { private static final UUID ZERO_UUID = new UUID(0, 0); - public static final MappingData MAPPINGS = new MappingData(); + public static final MappingData MAPPINGS = new MappingDataBase("1.15", "1.16"); private final MetadataRewriter1_16To1_15_2 metadataRewriter = new MetadataRewriter1_16To1_15_2(this); private final InventoryPackets itemRewriter = new InventoryPackets(this); private final TranslationMappings componentRewriter = new TranslationMappings(this); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/AttributeMappings.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/AttributeMappings.java new file mode 100644 index 000000000..c2eeaa3f1 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/AttributeMappings.java @@ -0,0 +1,43 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; + +public final class AttributeMappings { + private static final BiMap ATTRIBUTE_MAPPINGS = HashBiMap.create(); + + static { + ATTRIBUTE_MAPPINGS.put("generic.maxHealth", "minecraft:generic.max_health"); + ATTRIBUTE_MAPPINGS.put("zombie.spawnReinforcements", "minecraft:zombie.spawn_reinforcements"); + ATTRIBUTE_MAPPINGS.put("horse.jumpStrength", "minecraft:horse.jump_strength"); + ATTRIBUTE_MAPPINGS.put("generic.followRange", "minecraft:generic.follow_range"); + ATTRIBUTE_MAPPINGS.put("generic.knockbackResistance", "minecraft:generic.knockback_resistance"); + ATTRIBUTE_MAPPINGS.put("generic.movementSpeed", "minecraft:generic.movement_speed"); + ATTRIBUTE_MAPPINGS.put("generic.flyingSpeed", "minecraft:generic.flying_speed"); + ATTRIBUTE_MAPPINGS.put("generic.attackDamage", "minecraft:generic.attack_damage"); + ATTRIBUTE_MAPPINGS.put("generic.attackKnockback", "minecraft:generic.attack_knockback"); + ATTRIBUTE_MAPPINGS.put("generic.attackSpeed", "minecraft:generic.attack_speed"); + ATTRIBUTE_MAPPINGS.put("generic.armorToughness", "minecraft:generic.armor_toughness"); + } + + public static BiMap attributeIdentifierMappings() { + return ATTRIBUTE_MAPPINGS; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java deleted file mode 100644 index 32747f739..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2024 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data; - -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; -import com.viaversion.viaversion.api.data.MappingDataBase; - -public class MappingData extends MappingDataBase { - private final BiMap attributeMappings = HashBiMap.create(); - - public MappingData() { - super("1.15", "1.16"); - } - - @Override - protected void loadExtras(final CompoundTag data) { - attributeMappings.put("generic.maxHealth", "minecraft:generic.max_health"); - attributeMappings.put("zombie.spawnReinforcements", "minecraft:zombie.spawn_reinforcements"); - attributeMappings.put("horse.jumpStrength", "minecraft:horse.jump_strength"); - attributeMappings.put("generic.followRange", "minecraft:generic.follow_range"); - attributeMappings.put("generic.knockbackResistance", "minecraft:generic.knockback_resistance"); - attributeMappings.put("generic.movementSpeed", "minecraft:generic.movement_speed"); - attributeMappings.put("generic.flyingSpeed", "minecraft:generic.flying_speed"); - attributeMappings.put("generic.attackDamage", "minecraft:generic.attack_damage"); - attributeMappings.put("generic.attackKnockback", "minecraft:generic.attack_knockback"); - attributeMappings.put("generic.attackSpeed", "minecraft:generic.attack_speed"); - attributeMappings.put("generic.armorToughness", "minecraft:generic.armor_toughness"); - } - - public BiMap attributeIdentifierMappings() { - return attributeMappings; - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java index 33ff88b42..20db4a55d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java @@ -36,6 +36,7 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.AttributeMappings; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import com.viaversion.viaversion.util.Key; @@ -241,7 +242,7 @@ public class EntityPackets { for (int i = 0; i < size; i++) { // Attributes have been renamed and are now namespaced identifiers String key = wrapper.read(Type.STRING); - String attributeIdentifier = protocol.getMappingData().attributeIdentifierMappings().get(key); + String attributeIdentifier = AttributeMappings.attributeIdentifierMappings().get(key); if (attributeIdentifier == null) { attributeIdentifier = Key.namespaced(key); if (!Key.isValid(attributeIdentifier)) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index 0e03527f8..fce2e38a7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -32,6 +32,7 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.AttributeMappings; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; @@ -246,8 +247,8 @@ public class InventoryPackets extends ItemRewriter { - public static final MappingData MAPPINGS = new MappingData(); + public static final MappingData MAPPINGS = new MappingDataBase("1.17", "1.18"); private final EntityPackets entityRewriter = new EntityPackets(this); private final InventoryPackets itemRewriter = new InventoryPackets(this); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/BlockEntities.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/BlockEntities.java new file mode 100644 index 000000000..fa49c3435 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/BlockEntities.java @@ -0,0 +1,76 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.protocols.protocol1_18to1_17_1.data; + +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; + +public final class BlockEntities { + private static final Object2IntMap BLOCK_ENTITY_IDS = new Object2IntOpenHashMap<>(); + + static { + BLOCK_ENTITY_IDS.defaultReturnValue(-1); + final String[] blockEntities = blockEntities(); + for (int id = 0; id < blockEntities.length; id++) { + BLOCK_ENTITY_IDS.put(blockEntities[id], id); + } + } + + public static Object2IntMap blockEntityIds() { + return BLOCK_ENTITY_IDS; + } + + private static String[] blockEntities() { + return new String[]{ + "furnace", + "chest", + "trapped_chest", + "ender_chest", + "jukebox", + "dispenser", + "dropper", + "sign", + "mob_spawner", + "piston", + "brewing_stand", + "enchanting_table", + "end_portal", + "beacon", + "skull", + "daylight_detector", + "hopper", + "comparator", + "banner", + "structure_block", + "end_gateway", + "command_block", + "shulker_box", + "bed", + "conduit", + "barrel", + "smoker", + "blast_furnace", + "lectern", + "bell", + "jigsaw", + "campfire", + "beehive", + "sculk_sensor" + }; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/MappingData.java deleted file mode 100644 index e28433bdd..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/data/MappingData.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2024 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_18to1_17_1.data; - -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.viaversion.viaversion.api.data.MappingDataBase; -import it.unimi.dsi.fastutil.objects.Object2IntMap; -import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; - -public final class MappingData extends MappingDataBase { - - private final Object2IntMap blockEntityIds = new Object2IntOpenHashMap<>(); - - public MappingData() { - super("1.17", "1.18"); - blockEntityIds.defaultReturnValue(-1); - } - - @Override - protected void loadExtras(final CompoundTag data) { - final String[] blockEntities = blockEntities(); - for (int id = 0; id < blockEntities.length; id++) { - blockEntityIds.put(blockEntities[id], id); - } - } - - public Object2IntMap blockEntityIds() { - return blockEntityIds; - } - - private String[] blockEntities() { - return new String[]{ - "furnace", - "chest", - "trapped_chest", - "ender_chest", - "jukebox", - "dispenser", - "dropper", - "sign", - "mob_spawner", - "piston", - "brewing_stand", - "enchanting_table", - "end_portal", - "beacon", - "skull", - "daylight_detector", - "hopper", - "comparator", - "banner", - "structure_block", - "end_gateway", - "command_block", - "shulker_box", - "bed", - "conduit", - "barrel", - "smoker", - "blast_furnace", - "lectern", - "bell", - "jigsaw", - "campfire", - "beehive", - "sculk_sensor" - }; - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java index 13241ea5c..b817d2010 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java @@ -37,6 +37,7 @@ import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18; import com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.BlockEntityIds; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.Protocol1_18To1_17_1; +import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.data.BlockEntities; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage; import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.MathUtil; @@ -115,7 +116,7 @@ public final class WorldPackets { } final String id = idTag.getValue(); - final int typeId = protocol.getMappingData().blockEntityIds().getInt(Key.stripMinecraftNamespace(id)); + final int typeId = BlockEntities.blockEntityIds().getInt(Key.stripMinecraftNamespace(id)); if (typeId == -1) { Via.getPlatform().getLogger().warning("Unknown block entity: " + id); }