Merge pull request #1617 from KennyTV/channelnames

Make plugin channel mappings modifiable
This commit is contained in:
Myles 2020-01-06 14:55:38 +00:00 committed by GitHub
commit a6932bb71f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 37 deletions

View File

@ -1,20 +1,33 @@
package us.myles.ViaVersion.api.data;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.*;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.util.GsonUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.util.Map;
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) {
InputStream stream = MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
InputStream stream = getResource(name);
InputStreamReader reader = new InputStreamReader(stream);
try {
return GsonUtil.getGson().fromJson(reader, JsonObject.class);
@ -107,4 +120,8 @@ public class MappingDataLoader {
}
return null;
}
private static InputStream getResource(String name) {
return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
}
}

View File

@ -6,6 +6,7 @@ import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import java.io.File;
import java.util.UUID;
import java.util.logging.Logger;
@ -139,6 +140,13 @@ public interface ViaPlatform<T> {
*/
ConfigurationProvider getConfigurationProvider();
/**
* Get ViaVersions's data folder.
*
* @return data folder
*/
File getDataFolder();
/**
* Called when a reload happens
*/

View File

@ -27,6 +27,7 @@ public class MappingData {
public static final BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
public static final Map<String, String> translateMapping = 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 soundMappings;
public static Mappings blockMappings;
@ -48,6 +49,21 @@ public class MappingData {
enchantmentMappings = new Mappings(72, mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments"));
Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 sound mapping...");
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");
Map<String, String> translateData = GsonUtil.getGson().fromJson(
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) {
for (Map.Entry<String, JsonElement> entry : newTags.entrySet()) {
JsonArray ids = entry.getValue().getAsJsonArray();

View File

@ -443,6 +443,7 @@ public class InventoryPackets {
}
public static String getNewPluginChannelId(String old) {
// Default channels that should not be modifiable
switch (old) {
case "MC|TrList":
return "minecraft:trader_list";
@ -460,21 +461,12 @@ public class InventoryPackets {
return "minecraft:unregister";
case "BungeeCord":
return "bungeecord:main";
case "WDL|INIT":
return "wdl:init";
case "WDL|CONTROL":
return "wdl:control";
case "WDL|REQUEST":
return "wdl:request";
case "bungeecord:main":
return null;
case "FML|MP":
return "fml:mp";
case "FML|HS":
return "fml:hs";
default:
return old.matches("([0-9a-z_.-]+):([0-9a-z_/.-]+)") // Identifier regex
? old : null;
String mappedChannel = MappingData.channelMappings.get(old);
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) {
if (!newId.matches("([0-9a-z_.-]+):([0-9a-z_/.-]+)")) {
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;
}
newId = MappingData.validateNewChannel(newId);
if (newId == null) return null;
// Default channels that should not be modifiable
switch (newId) {
case "minecraft:trader_list":
return "MC|TrList";
@ -707,17 +695,9 @@ public class InventoryPackets {
return "MC|Brand";
case "bungeecord:main":
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:
String mappedChannel = MappingData.channelMappings.inverse().get(newId);
if (mappedChannel != null) return mappedChannel;
return newId.length() > 20 ? newId.substring(0, 20) : newId;
}
}

View File

@ -0,0 +1,7 @@
{
"WDL|INIT": "wdl:init",
"WDL|CONTROL": "wdl:control",
"WDL|REQUEST": "wdl:request",
"FML|MP": "fml:mp",
"FML|HS": "fml:hs"
}

View File

@ -195,6 +195,11 @@ public class SpongePlugin implements ViaPlatform {
return conf;
}
@Override
public File getDataFolder() {
return defaultConfig.getParentFile();
}
@Override
public void onReload() {
getLogger().severe("ViaVersion is already loaded, this should work fine. If you get any console errors, try rebooting.");

View File

@ -30,6 +30,7 @@ import us.myles.ViaVersion.velocity.platform.*;
import us.myles.ViaVersion.velocity.service.ProtocolDetectorService;
import us.myles.ViaVersion.velocity.util.LoggerWrapper;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@ -176,6 +177,11 @@ public class VelocityPlugin implements ViaPlatform<Player> {
return conf;
}
@Override
public File getDataFolder() {
return configDir.toFile();
}
@Override
public void onReload() {