From fd16697cbb42e818b83abe847ed409faa70834bc Mon Sep 17 00:00:00 2001 From: Alberto Migliorato Date: Fri, 28 Aug 2020 00:32:53 +0200 Subject: [PATCH] v1.1.1 Update - Minor fixes in Message#addPlaceHolder method - Major changes in ConfigManager.java class --- Example/pom.xml | 4 +- .../powerlibexample/ExampleMessage.java | 3 +- Java/pom.xml | 8 +- .../it/mycraft/powerlib/chat/Message.java | 4 +- .../powerlib/config/ConfigManager.java | 84 ++++++++++++++----- .../powerlib/inventory/InventoryBuilder.java | 1 - Java/src/main/resources/plugin.yml | 2 +- README.md | 2 +- pom.xml | 2 +- 9 files changed, 78 insertions(+), 32 deletions(-) diff --git a/Example/pom.xml b/Example/pom.xml index 2af635f..7ac2f73 100644 --- a/Example/pom.xml +++ b/Example/pom.xml @@ -5,7 +5,7 @@ PowerLibAPI it.mycraft - 1.1.0 + 1.1.1 4.0.0 PowerLibExample @@ -59,7 +59,7 @@ it.mycraft PowerLib - 1.1.0 + 1.1.1 provided diff --git a/Example/src/main/java/it/mycraft/powerlibexample/ExampleMessage.java b/Example/src/main/java/it/mycraft/powerlibexample/ExampleMessage.java index 8c800a0..648d7c5 100644 --- a/Example/src/main/java/it/mycraft/powerlibexample/ExampleMessage.java +++ b/Example/src/main/java/it/mycraft/powerlibexample/ExampleMessage.java @@ -40,7 +40,8 @@ public void textList(){ } public void setText(){ - System.out.println(new Message() + System.out.println( + new Message() .set("&5Hello shrek, today is: %day") .addPlaceHolder("%day", "Somebody once told me the world is gonna roll me")); } diff --git a/Java/pom.xml b/Java/pom.xml index c6a5066..ee65ddd 100644 --- a/Java/pom.xml +++ b/Java/pom.xml @@ -5,7 +5,7 @@ PowerLibAPI it.mycraft - 1.1.0 + 1.1.1 4.0.0 @@ -59,6 +59,12 @@ + + org.spigotmc + spigot-api + 1.16.2-R0.1-SNAPSHOT + provided + org.spigotmc spigot-api diff --git a/Java/src/main/java/it/mycraft/powerlib/chat/Message.java b/Java/src/main/java/it/mycraft/powerlib/chat/Message.java index 9751893..0f966b3 100644 --- a/Java/src/main/java/it/mycraft/powerlib/chat/Message.java +++ b/Java/src/main/java/it/mycraft/powerlib/chat/Message.java @@ -35,10 +35,10 @@ public Message(List messages) { } public Message addPlaceHolder(String placeholder, Object value) { - message = message.replaceAll(placeholder, value.toString()); + message = message.replace(placeholder, value.toString()); List newMessages = new ArrayList<>(); - messages.forEach((s) -> newMessages.add(s.replaceAll(placeholder, value.toString()))); + messages.forEach((s) -> newMessages.add(s.replace(placeholder, value.toString()))); messages = newMessages; diff --git a/Java/src/main/java/it/mycraft/powerlib/config/ConfigManager.java b/Java/src/main/java/it/mycraft/powerlib/config/ConfigManager.java index 98468dd..3b01b3c 100644 --- a/Java/src/main/java/it/mycraft/powerlib/config/ConfigManager.java +++ b/Java/src/main/java/it/mycraft/powerlib/config/ConfigManager.java @@ -1,12 +1,16 @@ package it.mycraft.powerlib.config; import com.google.common.base.Charsets; +import com.google.common.io.ByteStreams; import org.bukkit.Bukkit; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginAwareness; import java.io.*; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.logging.Level; @@ -38,16 +42,26 @@ public FileConfiguration get(String file) { * Creates a file if it doesn't exist and then puts it into the local Map * * @param file The config file name - * @return The new file + * @return The new file */ - public FileConfiguration create(String file) { - File resourcePath = new File(this.plugin.getDataFolder() + file); + public FileConfiguration create(String file, String source) { + File resourcePath = new File(this.plugin.getDataFolder(), file); if (!resourcePath.exists()) { - createYAML(resourcePath.getName(), false); + createYAML(resourcePath.getName(), source, false); } return this.configs.get(file); } + /** + * Same as #create(String,String) but source name equals to the new one + * + * @param file The config file name + * @return The new file + */ + public FileConfiguration create(String file) { + return this.create(file, file); + } + /** * Saves the config file changes and updates it in the local Map * @@ -66,14 +80,40 @@ public void save(String file, FileConfiguration config) { * Reloads a config file * * @param file The config file name + * @author Original code from JavaPlugin.class */ public void reload(String file) { FileConfiguration conf = this.load(file); - InputStream stream = this.plugin.getResource(file); - if (stream != null) { - this.setDefaults(file, stream); + InputStream defStream = this.plugin.getResource(file); + if (defStream != null) { + YamlConfiguration yamlConfig; + if (!this.isStrictlyUTF8()) { + yamlConfig = new YamlConfiguration(); + byte[] contents; + try { + contents = ByteStreams.toByteArray(defStream); + } catch (IOException e) { + this.plugin.getLogger().log(Level.SEVERE, "Unexpected failure reading " + file, e); + return; + } + + String text = new String(contents, Charset.defaultCharset()); + if (!text.equals(new String(contents, Charsets.UTF_8))) { + this.plugin.getLogger().warning("Default system encoding has misread " + file + " from plugin jar"); + } + + try { + yamlConfig.loadFromString(text); + } catch (InvalidConfigurationException e) { + this.plugin.getLogger().log(Level.SEVERE, "Cannot load configuration from jar", e); + } + } else { + yamlConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defStream, Charsets.UTF_8)); + } + + conf.setDefaults(yamlConfig); + this.put(file, conf); } - this.put(file, conf); } /** @@ -102,29 +142,21 @@ private void put(String file, FileConfiguration config) { this.configs.put(file, config); } - /** - * Set a config's defaults - * - * @param file The config file name - * @param stream The input stream for the file - */ - private void setDefaults(String file, InputStream stream) { - this.get(file).setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(stream, Charsets.UTF_8))); - } - /** * Creates the file by looking for it inside the jar's resources, then loads it and puts it in the local Map * * @param resourcePath The file name + * @param source The source file name * @param replace Whether the file has to be replaced by the default one although it already exists + * @author Original code from JavaPlugin.class */ - private void createYAML(String resourcePath, boolean replace) { + private void createYAML(String resourcePath, String source, boolean replace) { if (resourcePath != null && !resourcePath.equals("")) { resourcePath = resourcePath.replace('\\', '/'); - InputStream in = this.plugin.getResource(resourcePath); + InputStream in = this.plugin.getResource(source); if (in == null) { - throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + - (new File(this.plugin.getDataFolder(), resourcePath))); + throw new IllegalArgumentException("The embedded resource '" + source + "' cannot be found in " + + (new File(this.plugin.getDataFolder(), source))); } else { File outFile = new File(this.plugin.getDataFolder(), resourcePath); int lastIndex = resourcePath.lastIndexOf(47); @@ -160,4 +192,12 @@ private void createYAML(String resourcePath, boolean replace) { } } + private void createYAML(String resourcePath, boolean replace) { + this.createYAML(resourcePath, resourcePath, replace); + } + + private boolean isStrictlyUTF8() { + return this.plugin.getDescription().getAwareness().contains(PluginAwareness.Flags.UTF8); + } + } diff --git a/Java/src/main/java/it/mycraft/powerlib/inventory/InventoryBuilder.java b/Java/src/main/java/it/mycraft/powerlib/inventory/InventoryBuilder.java index cf8da2d..628f643 100644 --- a/Java/src/main/java/it/mycraft/powerlib/inventory/InventoryBuilder.java +++ b/Java/src/main/java/it/mycraft/powerlib/inventory/InventoryBuilder.java @@ -177,7 +177,6 @@ private void fillBorder(Inventory inventory) { for (int i = 1; i <= 2; i++) { int max = i == 2 ? (inventory.getSize() - 1) : (i * 9) - 1; int min = max - 8; - System.out.println(max + " max-min " + min); for (int ignored; min <= max; min++) { inventory.setItem(min, fillBorder); } diff --git a/Java/src/main/resources/plugin.yml b/Java/src/main/resources/plugin.yml index da17eed..16af9ca 100644 --- a/Java/src/main/resources/plugin.yml +++ b/Java/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: PowerLib authors: [AlbeMiglio, pompiere1] main: it.mycraft.powerlib.PowerLib -version: 1.1.0 \ No newline at end of file +version: 1.1.1 \ No newline at end of file diff --git a/README.md b/README.md index 82cc85c..82cce91 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ it.albemiglio PowerLib - 1.1.0 + 1.1.1 ``` diff --git a/pom.xml b/pom.xml index 1f5a2b0..d9eb8bb 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ it.mycraft PowerLibAPI pom - 1.1.0 + 1.1.1 Example Java