Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.21.3-R0.1-SNAPSHOT</version>
<version>1.21.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.lokka30</groupId>
<artifactId>MicroLib</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.21.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/me/lokka30/microlib/files/JsonConfigFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package me.lokka30.microlib.files;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.plugin.Plugin;

public class JsonConfigFile {
private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().create();
private final Plugin plugin;
private final File file;
private final Map<String, Object> values;
private final BufferedReader reader;

public JsonConfigFile(Plugin plugin, File file) throws IOException {
this.values = new HashMap();
this.plugin = plugin;
this.file = file;
this.reader = Files.newBufferedReader(this.getPath());
}

public JsonConfigFile(Plugin plugin, String name) throws IOException {
this(plugin, new File(plugin.getDataFolder(), name));
}

public BufferedReader getReader() {
return this.reader;
}

public Map<String, Object> getValues() {
return this.values;
}

public Object get(String key) throws ClassCastException, NullPointerException {
return this.getValues().get(key);
}

public Object set(String key, Object value) throws UnsupportedOperationException, ClassCastException, NullPointerException, IllegalArgumentException {
return this.getValues().put(key, value);
}

public File getFile() {
return this.file;
}

public String getName() {
return this.getFile().getName();
}

public Path getPath() {
return this.getFile().toPath();
}

private void createIfNotExists() throws IOException {
if (!this.getFile().exists() || this.getFile().isDirectory()) {
try {
this.plugin.saveResource(this.getName(), false);
} catch (IllegalArgumentException var2) {
if(!this.getFile().createNewFile()) throw new IllegalArgumentException("Unable to create new JSON file!", var2);
}
}

}

public void load() throws IOException {
this.createIfNotExists();
this.getValues().putAll((Map)GSON.fromJson(this.getReader(), this.getValues().getClass()));
}

public void save() throws IOException {
String json = GSON.toJson(this.getValues());
Files.write(this.getPath(), Collections.singletonList(json), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
}
}
61 changes: 61 additions & 0 deletions src/main/java/me/lokka30/microlib/files/YamlConfigFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.lokka30.microlib.files;

import java.io.File;
import java.io.IOException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

public class YamlConfigFile {
private final Plugin plugin;
private final File configFile;
private YamlConfiguration config;
private boolean copyDefaults;

public YamlConfigFile(Plugin plugin, File configFile) {
this.copyDefaults = false;
this.plugin = plugin;
this.configFile = configFile;
}

public YamlConfigFile(Plugin plugin, String configName) {
this(plugin, new File(plugin.getDataFolder(), configName));
}

private void createIfNotExists() throws IOException {
if (!this.getConfigFile().exists() || this.getConfigFile().isDirectory()) {
try {
this.plugin.saveResource(this.getName(), false);
} catch (IllegalArgumentException var2) {
if(!this.getConfigFile().createNewFile()) throw new IllegalArgumentException("Unable to create new YAML file!", var2);
}
}

}

public void load() throws IOException {
this.createIfNotExists();
this.config = YamlConfiguration.loadConfiguration(this.getConfigFile());
this.getConfig().options().copyDefaults(this.copyDefaults);
}

public void save() throws IOException {
this.createIfNotExists();
this.getConfig().save(this.getConfigFile());
}

public void setCopyDefaults(boolean copyDefaults) {
this.copyDefaults = copyDefaults;
}

public File getConfigFile() {
return this.configFile;
}

public YamlConfiguration getConfig() {
return this.config;
}

public String getName() {
return this.configFile.getName();
}
}
52 changes: 52 additions & 0 deletions src/main/java/me/lokka30/microlib/folia/FoliaRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.lokka30.microlib.folia;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

/**
* A class for running tasks on both Bukkit and Folia. Without this class, you would need to check if the server is using Folia or not, and then run the task accordingly.
*/
public class FoliaRunnable extends BukkitRunnable {

private ScheduledTask foliaTask;
private BukkitTask bukkitTask;

/**
* Cancels the task if it is running.
*/
@Override
public synchronized void cancel() throws IllegalStateException {
if (this.foliaTask != null) {
this.foliaTask.cancel();
this.foliaTask = null;
} else if (this.bukkitTask != null) {
// Legacy Bukkit task
this.bukkitTask.cancel();
this.bukkitTask = null;
}
}

/**
* This method is not used. Instead, use the run() method in the subclass.
*/
@Override
public void run() {
}

/**
* Sets the scheduled task for Folia.
* @param task The {@link ScheduledTask}.
*/
public void setScheduledTask(ScheduledTask task) {
this.foliaTask = task;
}

/**
* Sets the Bukkit task for legacy Bukkit.
* @param task The {@link BukkitTask}.
*/
public void setBukkitTask(BukkitTask task) {
this.bukkitTask = task;
}
}
Loading