3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-20 06:50:08 +01:00

Make plugin channel mappings modifiable

Dieser Commit ist enthalten in:
KennyTV 2020-01-05 21:21:58 +01:00
Ursprung 280b6fae2a
Commit 5ad62a03ed
7 geänderte Dateien mit 100 neuen und 43 gelöschten Zeilen

Datei anzeigen

@ -1,20 +1,41 @@
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.nio.file.Files;
import java.util.Map;
public class MappingDataLoader {
public static JsonObject loadFromDataDir(String name) {
File file = new File(Via.getPlatform().getDataFolder(), name);
if (!file.exists()) {
try (InputStream in = getResource(name)) {
Files.copy(in, file.toPath());
} catch (IOException e) {
Via.getPlatform().getLogger().warning("Error loading " + name + " from the config directory!");
e.printStackTrace();
return null;
}
}
try (FileReader reader = new FileReader(file)) {
return GsonUtil.getGson().fromJson(reader, JsonObject.class);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JsonSyntaxException | JsonIOException e) {
Via.getPlatform().getLogger().warning(name + " is badly formatted!");
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 +128,8 @@ public class MappingDataLoader {
}
return null;
}
private static InputStream getResource(String name) {
return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
}
}

Datei anzeigen

@ -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
*/

Datei anzeigen

@ -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();

Datei anzeigen

@ -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,31 +674,11 @@ 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";
case "minecraft:book_open":
return "MC|BOpen";
case "minecraft:debug/paths":
return "MC|DebugPath";
case "minecraft:debug/neighbors_update":
return "MC|DebugNeighborsUpdate";
case "minecraft:register":
return "REGISTER";
case "minecraft:unregister":
return "UNREGISTER";
case "minecraft:brand":
return "MC|Brand";
case "bungeecord:main":
return "BungeeCord";
case "wdl:init":
return "WDL|INIT";
case "wdl:control":
@ -718,6 +690,8 @@ public class InventoryPackets {
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;
}
}

Datei anzeigen

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

Datei anzeigen

@ -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.");

Datei anzeigen

@ -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() {