Skip to content

Commit

Permalink
v1.1.1 Update
Browse files Browse the repository at this point in the history
- Minor fixes in Message#addPlaceHolder method
- Major changes in ConfigManager.java class
  • Loading branch information
AlbeMiglio committed Aug 27, 2020
1 parent 79aeafd commit fd16697
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>PowerLibAPI</artifactId>
<groupId>it.mycraft</groupId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PowerLibExample</artifactId>
Expand Down Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>it.mycraft</groupId>
<artifactId>PowerLib</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
8 changes: 7 additions & 1 deletion Java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>PowerLibAPI</artifactId>
<groupId>it.mycraft</groupId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -59,6 +59,12 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions Java/src/main/java/it/mycraft/powerlib/chat/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public Message(List<String> messages) {
}

public Message addPlaceHolder(String placeholder, Object value) {
message = message.replaceAll(placeholder, value.toString());
message = message.replace(placeholder, value.toString());

List<String> 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;

Expand Down
84 changes: 62 additions & 22 deletions Java/src/main/java/it/mycraft/powerlib/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
*
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Java/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PowerLib
authors: [AlbeMiglio, pompiere1]
main: it.mycraft.powerlib.PowerLib
version: 1.1.0
version: 1.1.1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>it.albemiglio</groupId>
<artifactId>PowerLib</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</dependency>
</dependencies>
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>it.mycraft</groupId>
<artifactId>PowerLibAPI</artifactId>
<packaging>pom</packaging>
<version>1.1.0</version>
<version>1.1.1</version>
<modules>
<module>Example</module>
<module>Java</module>
Expand Down

0 comments on commit fd16697

Please sign in to comment.