From d67269c9fec56e16806a1958bc225b48817bbb55 Mon Sep 17 00:00:00 2001 From: Levertion <26185209+Levertion@users.noreply.github.com> Date: Wed, 15 Sep 2021 09:52:11 +0100 Subject: [PATCH] Add support for setting a custom world name (#2672) --- .../api/configuration/ViaVersionConfig.java | 10 ++++++ .../api/minecraft/WorldIdentifiers.java | 35 +++++++++++++++++++ .../configuration/AbstractViaConfig.java | 14 ++++++++ .../packets/EntityPackets.java | 13 ++++++- .../resources/assets/viaversion/config.yml | 5 +++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/com/viaversion/viaversion/api/minecraft/WorldIdentifiers.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java index 7199105b1..59266be11 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java +++ b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java @@ -24,6 +24,7 @@ package com.viaversion.viaversion.api.configuration; import com.google.gson.JsonElement; import it.unimi.dsi.fastutil.ints.IntSet; +import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; public interface ViaVersionConfig { @@ -429,4 +430,13 @@ public interface ViaVersionConfig { * @return cached serialized component */ JsonElement get1_17ResourcePackPrompt(); + + /*** + * Get the world names which should be returned for each vanilla dimension + * + * @return the global map from vanilla dimensions to world name + * Note that this can be overriden per-user by using {@link com.viaversion.viaversion.api.connection.UserConnection#put} with + * a custom instance of {@link WorldIdentifiers} for the user's {@link UserConnection} + */ + WorldIdentifiers get1_16WorldNamesMap(); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/WorldIdentifiers.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/WorldIdentifiers.java new file mode 100644 index 000000000..bd1f77e9b --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/WorldIdentifiers.java @@ -0,0 +1,35 @@ +package com.viaversion.viaversion.api.minecraft; + +import com.viaversion.viaversion.api.connection.StorableObject; + +public class WorldIdentifiers implements StorableObject { + public static final String OVERWORLD_DEFAULT = "minecraft:overworld"; + public static final String NETHER_DEFAULT = "minecraft:the_nether"; + public static final String END_DEFAULT = "minecraft:the_end"; + + private final String overworld; + private final String nether; + private final String end; + + public WorldIdentifiers(String overworld) { + this(overworld, NETHER_DEFAULT, END_DEFAULT); + } + + public WorldIdentifiers(String overworld, String nether, String end) { + this.overworld = overworld; + this.nether = nether; + this.end = end; + } + + public String overworld() { + return this.overworld; + } + + public String nether() { + return this.nether; + } + + public String end() { + return this.end; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java index 8a6415679..55b1b7376 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -18,12 +18,16 @@ package com.viaversion.viaversion.configuration; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.viaversion.viaversion.api.configuration.ViaVersionConfig; import com.viaversion.viaversion.util.Config; +import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; import java.io.File; +import java.util.HashMap; +import java.util.Map; public abstract class AbstractViaConfig extends Config implements ViaVersionConfig { @@ -78,6 +82,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf private boolean ignoreLongChannelNames; private boolean forcedUse1_17ResourcePack; private JsonElement resourcePack1_17PromptMessage; + private WorldIdentifiers map1_16WorldNames; protected AbstractViaConfig(File configFile) { super(configFile); @@ -141,6 +146,10 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf ignoreLongChannelNames = getBoolean("ignore-long-1_16-channel-names", true); forcedUse1_17ResourcePack = getBoolean("forced-use-1_17-resource-pack", false); resourcePack1_17PromptMessage = getSerializedComponent("resource-pack-1_17-prompt"); + Map worlds = get("map-1_16-world-names", Map.class, new HashMap()); + map1_16WorldNames = new WorldIdentifiers(worlds.getOrDefault("overworld", WorldIdentifiers.OVERWORLD_DEFAULT), + worlds.getOrDefault("nether", WorldIdentifiers.NETHER_DEFAULT), + worlds.getOrDefault("end", WorldIdentifiers.END_DEFAULT)); } @Override @@ -429,4 +438,9 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf public JsonElement get1_17ResourcePackPrompt() { return resourcePack1_17PromptMessage; } + + @Override + public WorldIdentifiers get1_16WorldNamesMap() { + return map1_16WorldNames; + }; } 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 8d15abe70..c533f25f6 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 @@ -25,6 +25,7 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.LongTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; import com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; @@ -42,25 +43,35 @@ import java.util.UUID; public class EntityPackets { private static final PacketHandler DIMENSION_HANDLER = wrapper -> { + WorldIdentifiers map = Via.getConfig().get1_16WorldNamesMap(); + WorldIdentifiers userMap = wrapper.user().get(WorldIdentifiers.class); + if (userMap!=null){ + map = userMap; + } int dimension = wrapper.read(Type.INT); String dimensionName; + String outputName; switch (dimension) { case -1: dimensionName = "minecraft:the_nether"; + outputName = map.nether(); break; case 0: dimensionName = "minecraft:overworld"; + outputName = map.overworld(); break; case 1: dimensionName = "minecraft:the_end"; + outputName = map.end(); break; default: Via.getPlatform().getLogger().warning("Invalid dimension id: " + dimension); dimensionName = "minecraft:overworld"; + outputName = map.overworld(); } wrapper.write(Type.STRING, dimensionName); // dimension - wrapper.write(Type.STRING, dimensionName); // world + wrapper.write(Type.STRING, outputName); // world }; public static final CompoundTag DIMENSIONS_TAG = new CompoundTag(); private static final String[] WORLD_NAMES = {"minecraft:overworld", "minecraft:the_nether", "minecraft:the_end"}; diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index 839cb646f..1801261ca 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -205,3 +205,8 @@ force-json-transform: false minimize-cooldown: true # Allows 1.9+ left-handedness (main hand) on 1.8 servers left-handed-handling: true +# Get the world names which should be returned for each vanilla dimension +map-1_16-world-names: + overworld: "minecraft:overworld" + nether: "minecraft:the_nether" + end: "minecraft:the_end"