Add more Bukkit utilities
This commit is contained in:
parent
766e6e1998
commit
d8e56079ad
3 changed files with 139 additions and 1 deletions
66
bukkit/src/main/java/org/zhdev/BukkitPlugin.java
Normal file
66
bukkit/src/main/java/org/zhdev/BukkitPlugin.java
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
package org.zhdev;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class BukkitPlugin extends JavaPlugin {
|
||||||
|
public void registerCommand(String label, CommandExecutor executor, TabCompleter completer) {
|
||||||
|
PluginCommand command = Bukkit.getPluginCommand(label);
|
||||||
|
if (command != null) {
|
||||||
|
command.setExecutor(executor);
|
||||||
|
command.setTabCompleter(completer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCommand(String label, CommandExecutor executor) {
|
||||||
|
registerCommand(label, executor, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerEvents(Listener listener) {
|
||||||
|
Bukkit.getPluginManager().registerEvents(listener, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancelTask(int taskId) {
|
||||||
|
Bukkit.getScheduler().cancelTask(taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancelTasks() {
|
||||||
|
Bukkit.getScheduler().cancelTasks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public BukkitTask runTask(@NotNull Runnable task) throws IllegalArgumentException {
|
||||||
|
return Bukkit.getScheduler().runTask(this, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public BukkitTask runTaskAsynchronously(@NotNull Runnable task) throws IllegalArgumentException {
|
||||||
|
return Bukkit.getScheduler().runTaskAsynchronously(this, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public BukkitTask runTaskLater(@NotNull Runnable task, long delay) throws IllegalArgumentException {
|
||||||
|
return Bukkit.getScheduler().runTaskLater(this, task, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public BukkitTask runTaskLaterAsynchronously(@NotNull Runnable task, long delay) throws IllegalArgumentException {
|
||||||
|
return Bukkit.getScheduler().runTaskLaterAsynchronously(this, task, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public BukkitTask runTaskTimer(@NotNull Runnable task, long delay, long period) throws IllegalArgumentException {
|
||||||
|
return Bukkit.getScheduler().runTaskTimer(this, task, delay, period);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public BukkitTask runTaskTimerAsynchronously(@NotNull Runnable task, long delay, long period) throws IllegalArgumentException {
|
||||||
|
return Bukkit.getScheduler().runTaskTimerAsynchronously(this, task, delay, period);
|
||||||
|
}
|
||||||
|
}
|
||||||
66
bukkit/src/main/java/org/zhdev/PreparedBukkitPlugin.java
Normal file
66
bukkit/src/main/java/org/zhdev/PreparedBukkitPlugin.java
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
package org.zhdev;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.zhdev.config.BukkitConfig;
|
||||||
|
import org.zhdev.config.BukkitYamlConfig;
|
||||||
|
import org.zhdev.language.Language;
|
||||||
|
import org.zhdev.sql.SqlAdapter;
|
||||||
|
import org.zhdev.util.ConfigUtils;
|
||||||
|
|
||||||
|
public abstract class PreparedBukkitPlugin extends BukkitPlugin implements Listener {
|
||||||
|
protected final BukkitConfig configuration = new BukkitYamlConfig();
|
||||||
|
protected final Language language = new Language();
|
||||||
|
protected final SqlAdapter sqlAdapter = new SqlAdapter();
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public FileConfiguration getConfig() {
|
||||||
|
throw new UnsupportedOperationException("You should use PreparedBukkitPlugin#getConfiguration");
|
||||||
|
}
|
||||||
|
|
||||||
|
public BukkitConfig getConfiguration() {
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Language getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onLoading() {}
|
||||||
|
|
||||||
|
protected void onEnabling() {}
|
||||||
|
|
||||||
|
protected void onDisabling() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onLoad() {
|
||||||
|
configuration.load(this);
|
||||||
|
|
||||||
|
String locale = configuration.getString("locale", "default");
|
||||||
|
BukkitConfig languageConfig = new BukkitYamlConfig("language/" + locale + ".yml");
|
||||||
|
languageConfig.load(this);
|
||||||
|
ConfigUtils.addPhrases(language, languageConfig);
|
||||||
|
|
||||||
|
BukkitConfig databaseConfig = new BukkitYamlConfig("database.yml");
|
||||||
|
databaseConfig.load(this);
|
||||||
|
sqlAdapter.setConnection(ConfigUtils.createSqlConnection(databaseConfig, getDataFolder().getPath()));
|
||||||
|
|
||||||
|
onLoading();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onEnable() {
|
||||||
|
registerEvents(this);
|
||||||
|
onEnabling();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onDisable() {
|
||||||
|
onDisabling();
|
||||||
|
sqlAdapter.close();
|
||||||
|
configuration.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ import org.yaml.snakeyaml.nodes.Node;
|
||||||
import org.zhdev.util.ResourceUtils;
|
import org.zhdev.util.ResourceUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -56,8 +57,13 @@ public class BukkitYamlConfig extends YamlConfig implements BukkitConfig {
|
||||||
File file = load(plugin.getDataFolder().getPath() + File.separatorChar + path);
|
File file = load(plugin.getDataFolder().getPath() + File.separatorChar + path);
|
||||||
if (file.length() == 0) {
|
if (file.length() == 0) {
|
||||||
ResourceUtils.saveResource(path, file, classLoader);
|
ResourceUtils.saveResource(path, file, classLoader);
|
||||||
|
try {
|
||||||
|
load(file);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ConfigException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return load(plugin.getDataFolder().getPath() + File.separatorChar + path);
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue