Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-28 17:10:13 +01:00
Merge pull request #1617 from KennyTV/channelnames
Make plugin channel mappings modifiable
Dieser Commit ist enthalten in:
Commit
a6932bb71f
@ -1,20 +1,33 @@
|
|||||||
package us.myles.ViaVersion.api.data;
|
package us.myles.ViaVersion.api.data;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.*;
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import us.myles.ViaVersion.api.Via;
|
import us.myles.ViaVersion.api.Via;
|
||||||
import us.myles.ViaVersion.util.GsonUtil;
|
import us.myles.ViaVersion.util.GsonUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class MappingDataLoader {
|
public class MappingDataLoader {
|
||||||
|
|
||||||
|
public static JsonObject loadFromDataDir(String name) {
|
||||||
|
File file = new File(Via.getPlatform().getDataFolder(), name);
|
||||||
|
if (!file.exists()) return loadData(name);
|
||||||
|
|
||||||
|
// Load the file from the platform's directory if present
|
||||||
|
try (FileReader reader = new FileReader(file)) {
|
||||||
|
return GsonUtil.getGson().fromJson(reader, JsonObject.class);
|
||||||
|
} catch (JsonSyntaxException e) {
|
||||||
|
// Users might mess up the format, so let's catch the syntax error
|
||||||
|
Via.getPlatform().getLogger().warning(name + " is badly formatted!");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException | JsonIOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonObject loadData(String name) {
|
public static JsonObject loadData(String name) {
|
||||||
InputStream stream = MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
|
InputStream stream = getResource(name);
|
||||||
InputStreamReader reader = new InputStreamReader(stream);
|
InputStreamReader reader = new InputStreamReader(stream);
|
||||||
try {
|
try {
|
||||||
return GsonUtil.getGson().fromJson(reader, JsonObject.class);
|
return GsonUtil.getGson().fromJson(reader, JsonObject.class);
|
||||||
@ -107,4 +120,8 @@ public class MappingDataLoader {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static InputStream getResource(String name) {
|
||||||
|
return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import us.myles.ViaVersion.api.ViaVersionConfig;
|
|||||||
import us.myles.ViaVersion.api.command.ViaCommandSender;
|
import us.myles.ViaVersion.api.command.ViaCommandSender;
|
||||||
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -139,6 +140,13 @@ public interface ViaPlatform<T> {
|
|||||||
*/
|
*/
|
||||||
ConfigurationProvider getConfigurationProvider();
|
ConfigurationProvider getConfigurationProvider();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get ViaVersions's data folder.
|
||||||
|
*
|
||||||
|
* @return data folder
|
||||||
|
*/
|
||||||
|
File getDataFolder();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a reload happens
|
* Called when a reload happens
|
||||||
*/
|
*/
|
||||||
|
@ -27,6 +27,7 @@ public class MappingData {
|
|||||||
public static final BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
|
public static final BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
|
||||||
public static final Map<String, String> translateMapping = new HashMap<>();
|
public static final Map<String, String> translateMapping = new HashMap<>();
|
||||||
public static final Map<String, String> mojangTranslation = new HashMap<>();
|
public static final Map<String, String> mojangTranslation = new HashMap<>();
|
||||||
|
public static final BiMap<String, String> channelMappings = HashBiMap.create(); // 1.12->1.13
|
||||||
public static Mappings enchantmentMappings;
|
public static Mappings enchantmentMappings;
|
||||||
public static Mappings soundMappings;
|
public static Mappings soundMappings;
|
||||||
public static Mappings blockMappings;
|
public static Mappings blockMappings;
|
||||||
@ -48,6 +49,21 @@ public class MappingData {
|
|||||||
enchantmentMappings = new Mappings(72, mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments"));
|
enchantmentMappings = new Mappings(72, mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments"));
|
||||||
Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 sound mapping...");
|
Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 sound mapping...");
|
||||||
soundMappings = new Mappings(662, mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds"));
|
soundMappings = new Mappings(662, mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds"));
|
||||||
|
Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 plugin channel mappings...");
|
||||||
|
|
||||||
|
JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json");
|
||||||
|
if (object != null) {
|
||||||
|
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
|
||||||
|
String oldChannel = entry.getKey();
|
||||||
|
String newChannel = entry.getValue().getAsString();
|
||||||
|
if (!isValid1_13Channel(newChannel)) {
|
||||||
|
Via.getPlatform().getLogger().warning("Channel '" + newChannel + "' is not a valid 1.13 plugin channel, please check your configuration!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
channelMappings.put(oldChannel, newChannel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Via.getPlatform().getLogger().info("Loading translation mappping");
|
Via.getPlatform().getLogger().info("Loading translation mappping");
|
||||||
Map<String, String> translateData = GsonUtil.getGson().fromJson(
|
Map<String, String> translateData = GsonUtil.getGson().fromJson(
|
||||||
new InputStreamReader(MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json")),
|
new InputStreamReader(MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json")),
|
||||||
@ -79,6 +95,22 @@ public class MappingData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String validateNewChannel(String newId) {
|
||||||
|
if (!isValid1_13Channel(newId)) {
|
||||||
|
return null; // Not valid
|
||||||
|
}
|
||||||
|
int separatorIndex = newId.indexOf(':');
|
||||||
|
// Vanilla parses ``:`` and ```` as ``minecraft:`` (also ensure there's enough space)
|
||||||
|
if ((separatorIndex == -1 || separatorIndex == 0) && newId.length() <= 10) {
|
||||||
|
newId = "minecraft:" + newId;
|
||||||
|
}
|
||||||
|
return newId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValid1_13Channel(String channelId) {
|
||||||
|
return channelId.matches("([0-9a-z_.-]+):([0-9a-z_/.-]+)");
|
||||||
|
}
|
||||||
|
|
||||||
private static void loadTags(Map<String, Integer[]> output, JsonObject newTags) {
|
private static void loadTags(Map<String, Integer[]> output, JsonObject newTags) {
|
||||||
for (Map.Entry<String, JsonElement> entry : newTags.entrySet()) {
|
for (Map.Entry<String, JsonElement> entry : newTags.entrySet()) {
|
||||||
JsonArray ids = entry.getValue().getAsJsonArray();
|
JsonArray ids = entry.getValue().getAsJsonArray();
|
||||||
|
@ -443,6 +443,7 @@ public class InventoryPackets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getNewPluginChannelId(String old) {
|
public static String getNewPluginChannelId(String old) {
|
||||||
|
// Default channels that should not be modifiable
|
||||||
switch (old) {
|
switch (old) {
|
||||||
case "MC|TrList":
|
case "MC|TrList":
|
||||||
return "minecraft:trader_list";
|
return "minecraft:trader_list";
|
||||||
@ -460,21 +461,12 @@ public class InventoryPackets {
|
|||||||
return "minecraft:unregister";
|
return "minecraft:unregister";
|
||||||
case "BungeeCord":
|
case "BungeeCord":
|
||||||
return "bungeecord:main";
|
return "bungeecord:main";
|
||||||
case "WDL|INIT":
|
|
||||||
return "wdl:init";
|
|
||||||
case "WDL|CONTROL":
|
|
||||||
return "wdl:control";
|
|
||||||
case "WDL|REQUEST":
|
|
||||||
return "wdl:request";
|
|
||||||
case "bungeecord:main":
|
case "bungeecord:main":
|
||||||
return null;
|
return null;
|
||||||
case "FML|MP":
|
|
||||||
return "fml:mp";
|
|
||||||
case "FML|HS":
|
|
||||||
return "fml:hs";
|
|
||||||
default:
|
default:
|
||||||
return old.matches("([0-9a-z_.-]+):([0-9a-z_/.-]+)") // Identifier regex
|
String mappedChannel = MappingData.channelMappings.get(old);
|
||||||
? old : null;
|
if (mappedChannel != null) return mappedChannel;
|
||||||
|
return MappingData.isValid1_13Channel(old) ? old : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -682,14 +674,10 @@ public class InventoryPackets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getOldPluginChannelId(String newId) {
|
public static String getOldPluginChannelId(String newId) {
|
||||||
if (!newId.matches("([0-9a-z_.-]+):([0-9a-z_/.-]+)")) {
|
newId = MappingData.validateNewChannel(newId);
|
||||||
return null; // Not valid
|
if (newId == null) return null;
|
||||||
}
|
|
||||||
int separatorIndex = newId.indexOf(':');
|
// Default channels that should not be modifiable
|
||||||
// Vanilla parses ``:`` and ```` as ``minecraft:`` (also ensure there's enough space)
|
|
||||||
if ((separatorIndex == -1 || separatorIndex == 0) && newId.length() <= 10) {
|
|
||||||
newId = "minecraft:" + newId;
|
|
||||||
}
|
|
||||||
switch (newId) {
|
switch (newId) {
|
||||||
case "minecraft:trader_list":
|
case "minecraft:trader_list":
|
||||||
return "MC|TrList";
|
return "MC|TrList";
|
||||||
@ -707,17 +695,9 @@ public class InventoryPackets {
|
|||||||
return "MC|Brand";
|
return "MC|Brand";
|
||||||
case "bungeecord:main":
|
case "bungeecord:main":
|
||||||
return "BungeeCord";
|
return "BungeeCord";
|
||||||
case "wdl:init":
|
|
||||||
return "WDL|INIT";
|
|
||||||
case "wdl:control":
|
|
||||||
return "WDL|CONTROL";
|
|
||||||
case "wdl:request":
|
|
||||||
return "WDL|REQUEST";
|
|
||||||
case "fml:hs":
|
|
||||||
return "FML|HS";
|
|
||||||
case "fml:mp":
|
|
||||||
return "FML:MP";
|
|
||||||
default:
|
default:
|
||||||
|
String mappedChannel = MappingData.channelMappings.inverse().get(newId);
|
||||||
|
if (mappedChannel != null) return mappedChannel;
|
||||||
return newId.length() > 20 ? newId.substring(0, 20) : newId;
|
return newId.length() > 20 ? newId.substring(0, 20) : newId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"WDL|INIT": "wdl:init",
|
||||||
|
"WDL|CONTROL": "wdl:control",
|
||||||
|
"WDL|REQUEST": "wdl:request",
|
||||||
|
"FML|MP": "fml:mp",
|
||||||
|
"FML|HS": "fml:hs"
|
||||||
|
}
|
@ -195,6 +195,11 @@ public class SpongePlugin implements ViaPlatform {
|
|||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getDataFolder() {
|
||||||
|
return defaultConfig.getParentFile();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReload() {
|
public void onReload() {
|
||||||
getLogger().severe("ViaVersion is already loaded, this should work fine. If you get any console errors, try rebooting.");
|
getLogger().severe("ViaVersion is already loaded, this should work fine. If you get any console errors, try rebooting.");
|
||||||
|
@ -30,6 +30,7 @@ import us.myles.ViaVersion.velocity.platform.*;
|
|||||||
import us.myles.ViaVersion.velocity.service.ProtocolDetectorService;
|
import us.myles.ViaVersion.velocity.service.ProtocolDetectorService;
|
||||||
import us.myles.ViaVersion.velocity.util.LoggerWrapper;
|
import us.myles.ViaVersion.velocity.util.LoggerWrapper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -176,6 +177,11 @@ public class VelocityPlugin implements ViaPlatform<Player> {
|
|||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getDataFolder() {
|
||||||
|
return configDir.toFile();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReload() {
|
public void onReload() {
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren