mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2026-11-27 00:05:07 +00:00
Own core and addon editor schemas with BetterSync validation
This commit is contained in:
1 parent
ab9911605c
commit
1d4389188f
10 files changed
+624
-29
No files matched your search
@@ -12,7 +12,7 @@
|
||||
<!-- Upload patches to https://repo.ronanplugins.com/#/ -->
|
||||
|
||||
<properties>
|
||||
<bettersync.version>1.0.1</bettersync.version>
|
||||
<bettersync.version>1.1.0</bettersync.version>
|
||||
<maven.compiler.release>8</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<editor.website>https://www.minecentral.net</editor.website>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.SuperRonanCraft.BetterRTP;
|
||||
|
||||
import com.ronanplugins.bettersync.editor.EditorClient;
|
||||
import com.ronanplugins.bettersync.editor.EditorProfile;
|
||||
import com.ronanplugins.bettersync.editor.EditorSchema;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.SuperRonanCraft.BetterRTP.player.PlayerInfo;
|
||||
@@ -58,7 +58,7 @@ public class BetterRTP extends JavaPlugin {
|
||||
registerDependencies();
|
||||
loadAll();
|
||||
editorBridge = new EditorClient(
|
||||
this, EditorProfile.BETTERRTP,
|
||||
this, EditorSchema.load(this, "META-INF/editor-schema.json"),
|
||||
EditorClient.website(this, "META-INF/betterrtp-editor.properties"),
|
||||
me.SuperRonanCraft.BetterRTP.versions.AsyncHandler::sync, this::reload,
|
||||
me.SuperRonanCraft.BetterRTP.references.PermissionNode.EDITOR::check,
|
||||
|
||||
@@ -0,0 +1,343 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://schemas.ronanplugins.com/betterrtp/1",
|
||||
"title": "BetterRTP editor configuration",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"Settings",
|
||||
"Default"
|
||||
],
|
||||
"properties": {
|
||||
"Settings": {
|
||||
"type": "object"
|
||||
},
|
||||
"Default": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Biomes": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "\\S"
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-ranges": [
|
||||
{
|
||||
"min": "MinRadius",
|
||||
"max": "MaxRadius",
|
||||
"strict": true
|
||||
},
|
||||
{
|
||||
"min": "MinY",
|
||||
"max": "MaxY",
|
||||
"strict": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Language-File": {
|
||||
"type": "string",
|
||||
"x-immutable": true,
|
||||
"description": "Change the language file locally."
|
||||
},
|
||||
"BlacklistedBlocks": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "\\S"
|
||||
}
|
||||
},
|
||||
"DisabledWorlds": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "\\S"
|
||||
}
|
||||
},
|
||||
"Overrides": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "string",
|
||||
"x-entryName": true
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "overrides"
|
||||
}
|
||||
},
|
||||
"WorldType": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"NORMAL",
|
||||
"NETHER"
|
||||
],
|
||||
"x-caseInsensitive": true
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "types"
|
||||
}
|
||||
},
|
||||
"CustomWorlds": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"UseWorldBorder": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"MaxRadius": {
|
||||
"type": "integer",
|
||||
"default": 1000
|
||||
},
|
||||
"MinRadius": {
|
||||
"type": "integer",
|
||||
"default": 10
|
||||
},
|
||||
"CenterX": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"CenterZ": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"Price": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"Shape": {
|
||||
"type": "string",
|
||||
"default": "square"
|
||||
},
|
||||
"MaxY": {
|
||||
"type": "integer",
|
||||
"default": 320
|
||||
},
|
||||
"MinY": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"Cooldown": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"default": 0
|
||||
},
|
||||
"RTPOnDeath": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"Biomes": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "\\S"
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-editableProperties": true,
|
||||
"x-inherit": "Default",
|
||||
"x-ranges": [
|
||||
{
|
||||
"min": "MinRadius",
|
||||
"max": "MaxRadius",
|
||||
"strict": true
|
||||
},
|
||||
{
|
||||
"min": "MinY",
|
||||
"max": "MaxY",
|
||||
"strict": false
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "worlds"
|
||||
}
|
||||
},
|
||||
"PermissionGroup": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Groups": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"UseWorldBorder": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"MaxRadius": {
|
||||
"type": "integer",
|
||||
"default": 1000
|
||||
},
|
||||
"MinRadius": {
|
||||
"type": "integer",
|
||||
"default": 10
|
||||
},
|
||||
"CenterX": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"CenterZ": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"Price": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"Shape": {
|
||||
"type": "string",
|
||||
"default": "square"
|
||||
},
|
||||
"MaxY": {
|
||||
"type": "integer",
|
||||
"default": 320
|
||||
},
|
||||
"MinY": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"Cooldown": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"default": 0
|
||||
},
|
||||
"RTPOnDeath": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"Biomes": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "\\S"
|
||||
}
|
||||
},
|
||||
"Priority": {
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
}
|
||||
},
|
||||
"x-editableProperties": true,
|
||||
"x-inherit": "Default",
|
||||
"x-ranges": [
|
||||
{
|
||||
"min": "MinRadius",
|
||||
"max": "MaxRadius",
|
||||
"strict": true
|
||||
},
|
||||
{
|
||||
"min": "MinY",
|
||||
"max": "MaxY",
|
||||
"strict": false
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "worlds"
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "groups"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-bettersync": {
|
||||
"format": 1,
|
||||
"plugin": "betterrtp",
|
||||
"schemaVersion": "1",
|
||||
"displayName": "BetterRTP"
|
||||
},
|
||||
"x-rules": [
|
||||
{
|
||||
"path": ".*Radius$",
|
||||
"schema": {
|
||||
"minimum": 0,
|
||||
"maximum": 30000000
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.(Time|LockAfter)$",
|
||||
"schema": {
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.PreloadRadius$",
|
||||
"schema": {
|
||||
"minimum": 0,
|
||||
"maximum": 16
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.MaxAttempts$",
|
||||
"schema": {
|
||||
"minimum": 1,
|
||||
"maximum": 10000
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.Shape$",
|
||||
"schema": {
|
||||
"enum": [
|
||||
"square",
|
||||
"circle"
|
||||
],
|
||||
"x-caseInsensitive": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"x-objectRanges": [
|
||||
{
|
||||
"min": "MinRadius",
|
||||
"max": "MaxRadius",
|
||||
"strict": true
|
||||
},
|
||||
{
|
||||
"min": "MinY",
|
||||
"max": "MaxY",
|
||||
"strict": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
<properties>
|
||||
<editor.website>https://www.minecentral.net</editor.website>
|
||||
<bettersync.version>1.0.1</bettersync.version>
|
||||
<bettersync.version>1.1.0</bettersync.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.SuperRonanCraft.BetterRTPAddons;
|
||||
|
||||
import com.ronanplugins.bettersync.editor.EditorClient;
|
||||
import com.ronanplugins.bettersync.editor.EditorProfile;
|
||||
import com.ronanplugins.bettersync.editor.EditorSchema;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.SuperRonanCraft.BetterRTPAddons.util.Files;
|
||||
@@ -33,7 +33,7 @@ public class Main extends JavaPlugin {
|
||||
Bukkit.getPluginManager().registerEvents(reloadListener, this);
|
||||
load();
|
||||
editorBridge = new EditorClient(
|
||||
this, EditorProfile.BETTERRTP_ADDONS,
|
||||
this, EditorSchema.load(this, "META-INF/editor-schema.json"),
|
||||
EditorClient.website(this, "META-INF/betterrtp-addons-editor.properties"),
|
||||
me.SuperRonanCraft.BetterRTP.versions.AsyncHandler::sync, sender -> load(),
|
||||
this::canEdit, () -> Files.FILETYPE.CONFIG.getConfig().saveToString());
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://schemas.ronanplugins.com/betterrtp-addons/1",
|
||||
"title": "BetterRTP Addons editor configuration",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"Flashback",
|
||||
"Logger",
|
||||
"Portals",
|
||||
"ExtraEffects",
|
||||
"MagicStick",
|
||||
"Commands",
|
||||
"Parties",
|
||||
"RTPMenu"
|
||||
],
|
||||
"properties": {
|
||||
"Flashback": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Timer": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Delay": {
|
||||
"minimum": 0
|
||||
},
|
||||
"Warnings": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
},
|
||||
"propertyNames": {
|
||||
"pattern": "^[0-9]+$",
|
||||
"x-maxNumericName": 2147483647
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "warnings"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Logger": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Format": {
|
||||
"type": "string",
|
||||
"x-serverCheck": "date"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Portals": {
|
||||
"type": "object"
|
||||
},
|
||||
"ExtraEffects": {
|
||||
"type": "object"
|
||||
},
|
||||
"MagicStick": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"GiveFirstJoin": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Amount": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 64
|
||||
}
|
||||
}
|
||||
},
|
||||
"Item": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Material": {
|
||||
"type": "string",
|
||||
"x-serverCheck": "material"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Commands": {
|
||||
"type": "object"
|
||||
},
|
||||
"Parties": {
|
||||
"type": "object"
|
||||
},
|
||||
"RTPMenu": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Lines": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 6
|
||||
},
|
||||
"AutoRefresh": {
|
||||
"minimum": 0
|
||||
},
|
||||
"WorldList": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"minProperties": 1,
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Name": {
|
||||
"type": "string",
|
||||
"default": ""
|
||||
},
|
||||
"Item": {
|
||||
"type": "string",
|
||||
"default": "MAP",
|
||||
"x-serverCheck": "material"
|
||||
},
|
||||
"Slot": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"default": 0,
|
||||
"x-lessThan": {
|
||||
"path": "RTPMenu.Lines",
|
||||
"multiply": 9
|
||||
}
|
||||
},
|
||||
"Lore": {
|
||||
"type": "array",
|
||||
"maxItems": 1000,
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
}
|
||||
},
|
||||
"x-editableProperties": true,
|
||||
"x-defaultChecks": {
|
||||
"Slot": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-named": true,
|
||||
"x-editor": {
|
||||
"collection": "menu"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Language-File": {
|
||||
"type": "string",
|
||||
"x-immutable": true,
|
||||
"description": "Change the language file locally."
|
||||
}
|
||||
},
|
||||
"x-bettersync": {
|
||||
"format": 1,
|
||||
"plugin": "betterrtp-addons",
|
||||
"schemaVersion": "1",
|
||||
"displayName": "BetterRTP Addons"
|
||||
},
|
||||
"x-rules": [
|
||||
{
|
||||
"path": ".*Radius$",
|
||||
"schema": {
|
||||
"minimum": 0,
|
||||
"maximum": 30000000
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.(Time|LockAfter)$",
|
||||
"schema": {
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.PreloadRadius$",
|
||||
"schema": {
|
||||
"minimum": 0,
|
||||
"maximum": 16
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.MaxAttempts$",
|
||||
"schema": {
|
||||
"minimum": 1,
|
||||
"maximum": 10000
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": ".*\\.Shape$",
|
||||
"schema": {
|
||||
"enum": [
|
||||
"square",
|
||||
"circle"
|
||||
],
|
||||
"x-caseInsensitive": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"x-objectRanges": [
|
||||
{
|
||||
"min": "MinRadius",
|
||||
"max": "MaxRadius",
|
||||
"strict": true
|
||||
},
|
||||
{
|
||||
"min": "MinY",
|
||||
"max": "MaxY",
|
||||
"strict": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -56,11 +56,28 @@ The wiki is available [here](../../wiki)!
|
||||
## Shared editor library
|
||||
|
||||
The web editor uses [BetterSync](https://github.com/RonanPlugins/BetterSync),
|
||||
downloaded by Maven from JitPack as `com.github.RonanPlugins:BetterSync:1.0.1`.
|
||||
No local BetterSync project or manual Maven install is required. Release `1.0.1` is published on JitPack. Update `bettersync.version` in the plugin and editor-test
|
||||
downloaded by Maven from JitPack as `com.github.RonanPlugins:BetterSync:1.1.0`.
|
||||
No local BetterSync project or manual Maven install is required. Release `1.1.0` is published on JitPack. Update `bettersync.version` in the plugin and editor-test
|
||||
POMs together when upgrading. Shade bundles and relocates the library into the
|
||||
plugin JAR; server owners do not install a separate BetterSync plugin.
|
||||
|
||||
The plugin retains its scheduler, permissions, reload callbacks, and filtered
|
||||
`editor.website` setting. Editor transport and configuration validation are
|
||||
maintained in BetterSync.
|
||||
|
||||
### Editor schema ownership
|
||||
|
||||
Each plugin module owns `src/main/resources/META-INF/editor-schema.json`. Edit rules here,
|
||||
not in the website or BetterSync's deprecated legacy profiles. `EditorSchema.load` reads
|
||||
this resource from the plugin JAR; the client sends its independent `schemaVersion` and
|
||||
checks the version returned by the website before applying changes.
|
||||
|
||||
Publish the same schema bytes as a public BetterSync GitHub release asset, then add the
|
||||
asset URL and SHA-256 to the website's `editor-schemas.lock.json`. Keep old versions in the
|
||||
lock for older servers. A changed rule requires a new schema version; plugin bug fixes
|
||||
that do not affect configuration can retain the version. Schema publication does not
|
||||
publish the rest of a private plugin repository.
|
||||
|
||||
Deploy the matching website and its schema-version database migrations before installing
|
||||
the new plugin JARs. Schema resources are bundled automatically by Maven; no sibling
|
||||
plugin/library checkout is needed to compile this project.
|
||||
@@ -1,7 +1,7 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>me.SuperRonanCraft</groupId><artifactId>betterrtp-editor-tests</artifactId><version>1.0</version>
|
||||
<properties><bettersync.version>1.0.1</bettersync.version><maven.compiler.release>8</maven.compiler.release><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
|
||||
<properties><bettersync.version>1.1.0</bettersync.version><maven.compiler.release>8</maven.compiler.release><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
|
||||
<repositories><repository><id>spigot</id><url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url></repository><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository></repositories>
|
||||
<dependencies>
|
||||
<dependency><groupId>com.github.RonanPlugins</groupId><artifactId>BetterSync</artifactId><version>${bettersync.version}</version></dependency>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import com.ronanplugins.bettersync.editor.betterrtp.EditorConfig;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import java.nio.file.*;
|
||||
@@ -7,7 +6,7 @@ import java.nio.charset.StandardCharsets;
|
||||
public class EditorConfigTest {
|
||||
private final String original = "Language-File: en.yml\nSettings:\n MaxAttempts: 32\n PreloadRadius: 5\nDefault:\n MinRadius: 10\n MaxRadius: 1000\n Shape: square\n Biomes: []\n";
|
||||
private void rejected(String edited) throws Exception {
|
||||
try { EditorConfig.validate(original, edited); fail("Expected rejection"); }
|
||||
try { SchemaConfig.validate(original, edited); fail("Expected rejection"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
private String addons() throws Exception {
|
||||
@@ -15,15 +14,15 @@ public class EditorConfigTest {
|
||||
}
|
||||
@Test public void acceptsAddonDefaultsAndCollectionEdits() throws Exception {
|
||||
String before = addons();
|
||||
EditorConfig.validate(before, before);
|
||||
SchemaConfig.validate(before, before);
|
||||
String after = before.replace("600: ", "300: ").replace("world_nether:", "survival:").replace("Lines: 3", "Lines: 4");
|
||||
EditorConfig.validate(before, after);
|
||||
SchemaConfig.validate(before, after);
|
||||
Path dir = Files.createTempDirectory("addons-editor");
|
||||
Path file = dir.resolve("config.yml");
|
||||
Files.write(file, before.getBytes(StandardCharsets.UTF_8));
|
||||
Path backup = EditorConfig.apply(file, before.getBytes(StandardCharsets.UTF_8), after, () -> {});
|
||||
Path backup = SchemaConfig.apply(file, before.getBytes(StandardCharsets.UTF_8), after, () -> {});
|
||||
assertEquals(before, new String(Files.readAllBytes(backup), StandardCharsets.UTF_8));
|
||||
assertTrue(EditorConfig.equivalent(after, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
assertTrue(SchemaConfig.equivalent(after, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
}
|
||||
@Test public void rejectsInvalidAddonSettingsAndUnsafeYaml() throws Exception {
|
||||
String before = addons();
|
||||
@@ -32,13 +31,13 @@ public class EditorConfigTest {
|
||||
before.replace("AutoRefresh: 0", "AutoRefresh: -1"), before.replace("600:", "bad:"),
|
||||
before.replace("en.yml", "other.yml"), before + "Bad: &anchor value\n",
|
||||
before + "Bad: !!java/object something\n", before + "Bad:\n ==: org.bukkit.inventory.ItemStack\n" }) {
|
||||
try { EditorConfig.validate(before, after); fail("Expected addon validation failure"); }
|
||||
try { SchemaConfig.validate(before, after); fail("Expected addon validation failure"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
try { EditorConfig.validate(original, before); fail("Expected cross-plugin failure"); }
|
||||
try { SchemaConfig.validate(original, before); fail("Expected cross-plugin failure"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
@Test public void acceptsValidChange() throws Exception { EditorConfig.validate(original, original.replace("1000", "2000")); }
|
||||
@Test public void acceptsValidChange() throws Exception { SchemaConfig.validate(original, original.replace("1000", "2000")); }
|
||||
@Test public void rejectsWrongTypeAndRange() throws Exception {
|
||||
rejected(original.replace("1000", "false"));
|
||||
rejected(original.replace("1000", "5"));
|
||||
@@ -57,7 +56,7 @@ public class EditorConfigTest {
|
||||
}
|
||||
@Test public void validatesBundledConfig() throws Exception {
|
||||
String bundled = new String(Files.readAllBytes(Paths.get("../../BetterRTP-Core/src/main/resources/config.yml")), StandardCharsets.UTF_8);
|
||||
EditorConfig.validate(bundled, bundled.replace("MaxRadius: 1000\n", "MaxRadius: 2000\n"));
|
||||
SchemaConfig.validate(bundled, bundled.replace("MaxRadius: 1000\n", "MaxRadius: 2000\n"));
|
||||
}
|
||||
@Test public void allowsStaleWritesAndKeepsNewFileAfterReloadFailure() throws Exception {
|
||||
Path dir = Files.createTempDirectory("betterrtp-editor-apply"); Path file = dir.resolve("config.yml");
|
||||
@@ -65,22 +64,22 @@ public class EditorConfigTest {
|
||||
try {
|
||||
Files.write(file, snapshot);
|
||||
String edited = original.replace("1000", "2000");
|
||||
Path backup = EditorConfig.apply(file, snapshot, edited, () -> {});
|
||||
Path backup = SchemaConfig.apply(file, snapshot, edited, () -> {});
|
||||
assertArrayEquals(snapshot, Files.readAllBytes(backup));
|
||||
assertTrue(EditorConfig.equivalent(edited, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
assertTrue(SchemaConfig.equivalent(edited, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
// The original snapshot is now stale; applying it is intentionally allowed.
|
||||
EditorConfig.apply(file, snapshot, original, () -> {});
|
||||
SchemaConfig.apply(file, snapshot, original, () -> {});
|
||||
assertArrayEquals(snapshot, Files.readAllBytes(file));
|
||||
byte[] current = Files.readAllBytes(file);
|
||||
final int[] reloads = { 0 };
|
||||
try {
|
||||
EditorConfig.apply(file, current, edited, () -> { reloads[0]++; throw new java.io.IOException("Simulated reload failure"); });
|
||||
SchemaConfig.apply(file, current, edited, () -> { reloads[0]++; throw new java.io.IOException("Simulated reload failure"); });
|
||||
fail("Reload should fail");
|
||||
} catch (java.io.IOException expected) { }
|
||||
assertEquals(1, reloads[0]);
|
||||
assertArrayEquals(edited.getBytes(StandardCharsets.UTF_8), Files.readAllBytes(file));
|
||||
byte[] unchanged = Files.readAllBytes(file);
|
||||
try { EditorConfig.apply(file, snapshot, "broken: [", () -> fail("Must not reload")); fail("Must reject invalid config"); }
|
||||
try { SchemaConfig.apply(file, snapshot, "broken: [", () -> fail("Must not reload")); fail("Must reject invalid config"); }
|
||||
catch (Exception expected) { }
|
||||
assertArrayEquals(unchanged, Files.readAllBytes(file));
|
||||
} finally {
|
||||
@@ -96,9 +95,9 @@ public class EditorConfigTest {
|
||||
for (Path file : (Iterable<Path>) files::iterator) {
|
||||
String value = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
|
||||
if (file.getFileName().toString().startsWith("invalid-")) {
|
||||
try { EditorConfig.validate(baseline, value); fail("Expected rejection: " + file); }
|
||||
try { SchemaConfig.validate(baseline, value); fail("Expected rejection: " + file); }
|
||||
catch (java.io.IOException expected) { }
|
||||
} else EditorConfig.validate(baseline, value);
|
||||
} else SchemaConfig.validate(baseline, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,9 +107,9 @@ public class EditorConfigTest {
|
||||
try {
|
||||
Files.write(file, original.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] replacement = original.replace("1000", "2000").getBytes(StandardCharsets.UTF_8);
|
||||
EditorConfig.replace(file, replacement); assertArrayEquals(replacement, EditorConfig.read(file));
|
||||
Files.write(file, new byte[EditorConfig.MAX_BYTES + 1]);
|
||||
try { EditorConfig.read(file); fail("Oversized read"); } catch (java.io.IOException expected) { }
|
||||
SchemaConfig.replace(file, replacement); assertArrayEquals(replacement, SchemaConfig.read(file));
|
||||
Files.write(file, new byte[SchemaConfig.MAX_BYTES + 1]);
|
||||
try { SchemaConfig.read(file); fail("Oversized read"); } catch (java.io.IOException expected) { }
|
||||
} finally { Files.deleteIfExists(file); Files.deleteIfExists(dir); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import com.ronanplugins.bettersync.editor.EditorSchema;
|
||||
import com.ronanplugins.bettersync.editor.EditorFiles;
|
||||
import java.nio.file.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/** Tests the actual plugin-owned resource, not BetterSync's legacy profile. */
|
||||
final class SchemaConfig {
|
||||
static final int MAX_BYTES = EditorFiles.MAX_BYTES;
|
||||
private static EditorSchema schema(String text) throws Exception {
|
||||
return new EditorSchema(new String(Files.readAllBytes(Paths.get(text.contains("Flashback:") ? "../../BetterRTPAddons/src/main/resources/META-INF/editor-schema.json" : "../../BetterRTP-Core/src/main/resources/META-INF/editor-schema.json")), StandardCharsets.UTF_8));
|
||||
}
|
||||
static void validate(String before, String after) throws Exception { schema(before).validate(before, after); }
|
||||
static boolean equivalent(String before, String after) throws Exception { return schema(before).equivalent(before, after); }
|
||||
static Path apply(Path file, byte[] before, String after, EditorFiles.Reload reload) throws Exception {
|
||||
return EditorFiles.apply(schema(new String(before, StandardCharsets.UTF_8)), file, before, after, reload);
|
||||
}
|
||||
static byte[] read(Path file) throws Exception { return EditorFiles.read(file); }
|
||||
static void replace(Path file, byte[] bytes) throws Exception { EditorFiles.replace(file, bytes); }
|
||||
}
|
||||
Reference in new issue
Block a user