From 775363aa576a6a5a0a6a8a87ab282d60e6bdf37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Mon, 3 Sep 2018 20:01:59 +0200 Subject: [PATCH] feature test --- resources/borders.yml | 7 ++ src/cz/marwland/mc/core/Feature.java | 24 ++++++ src/cz/marwland/mc/core/MarwCore.java | 26 ++++++ src/cz/marwland/mc/core/cmd/First.java | 9 +- .../marwland/mc/core/config/ConfigFile.java | 82 +++++++++++++++++++ .../mc/core/config/ConfigManager.java | 41 ++++++++++ src/cz/marwland/mc/features/Heil.java | 32 ++++++++ 7 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 resources/borders.yml create mode 100644 src/cz/marwland/mc/core/Feature.java create mode 100644 src/cz/marwland/mc/core/config/ConfigFile.java create mode 100644 src/cz/marwland/mc/core/config/ConfigManager.java create mode 100644 src/cz/marwland/mc/features/Heil.java diff --git a/resources/borders.yml b/resources/borders.yml new file mode 100644 index 0000000..87d2c4b --- /dev/null +++ b/resources/borders.yml @@ -0,0 +1,7 @@ +# Here you can specify the world limits. +# Borders will be calculated automatically from values in the 'points' list. +# These coordinates WILL be inside your border, therefore you must specify at least two. +#world: +# points: +# - 500, 500 +# - -500, -500 diff --git a/src/cz/marwland/mc/core/Feature.java b/src/cz/marwland/mc/core/Feature.java new file mode 100644 index 0000000..0aa14dc --- /dev/null +++ b/src/cz/marwland/mc/core/Feature.java @@ -0,0 +1,24 @@ +package cz.marwland.mc.core; + +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +public abstract class Feature implements Listener { + private final Plugin plugin; + private String name = ""; + + public Feature(Plugin plugin, String name) { + this.plugin = plugin; + this.name = name; + } + + public void onEnable() { + } + + public void onDisable() { + } + + public String getName() { + return this.name; + } +} diff --git a/src/cz/marwland/mc/core/MarwCore.java b/src/cz/marwland/mc/core/MarwCore.java index 4d78093..7ccfcd3 100644 --- a/src/cz/marwland/mc/core/MarwCore.java +++ b/src/cz/marwland/mc/core/MarwCore.java @@ -1,19 +1,45 @@ package cz.marwland.mc.core; +import java.util.HashMap; + import org.bukkit.plugin.java.JavaPlugin; import cz.marwland.mc.core.cmd.First; +import cz.marwland.mc.core.config.ConfigManager; +import cz.marwland.mc.features.Heil; public class MarwCore extends JavaPlugin { + private ConfigManager configManager; + private HashMap features = new HashMap<>(); + @Override public void onEnable() { getCommand("first").setExecutor(new First(this)); getServer().getPluginManager().registerEvents(new EventListener(this), this); getLogger().info("Enabled!"); + configManager = new ConfigManager(this); + configManager.registerConfig("borders.yml"); + configManager.load(); + + this.addFeature(new Heil(this)); } @Override public void onDisable() { + configManager.save(); + } + + public ConfigManager getConfigManager() { + return this.configManager; + } + + public void addFeature(Feature f) { + f.getName(); + getServer().getPluginManager().registerEvents(f, this); + } + + public Feature getFeature(String name) { + return this.features.get(name); } } diff --git a/src/cz/marwland/mc/core/cmd/First.java b/src/cz/marwland/mc/core/cmd/First.java index dbd1a1d..419d09c 100644 --- a/src/cz/marwland/mc/core/cmd/First.java +++ b/src/cz/marwland/mc/core/cmd/First.java @@ -3,6 +3,7 @@ package cz.marwland.mc.core.cmd; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import cz.marwland.mc.core.MarwCore; @@ -17,6 +18,12 @@ public class First implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { plugin.getLogger().info("Hello?"); sender.sendMessage("henlo"); - return false; + if (!(sender instanceof Player)) + return true; + + Player p = (Player) sender; + p.getWorld().getWorldBorder(); + + return true; } } diff --git a/src/cz/marwland/mc/core/config/ConfigFile.java b/src/cz/marwland/mc/core/config/ConfigFile.java new file mode 100644 index 0000000..ce94ee9 --- /dev/null +++ b/src/cz/marwland/mc/core/config/ConfigFile.java @@ -0,0 +1,82 @@ +package cz.marwland.mc.core.config; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.logging.Level; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; + +public class ConfigFile { + + private final Plugin plugin; + private File file; + private String path; + private FileConfiguration cfg; + + public ConfigFile(Plugin plugin, String template, String path) { + this.plugin = plugin; + this.path = template; + this.file = new File(plugin.getDataFolder(), path); + } + + public ConfigFile(Plugin plugin, String path) { + this(plugin, path, path); + } + + public File getFile() { + return this.file; + } + + public FileConfiguration getConfig() { + return this.cfg; + } + + public void load() { + if (!file.exists()) { + file.getParentFile().mkdirs(); + + InputStream in = plugin.getResource(path); + if (in == null) { + plugin.getLogger().config("The embedded resource '" + path + "' cannot be found!"); + return; + } + + try { + OutputStream out = new FileOutputStream(file); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + out.close(); + in.close(); + } catch (IOException ex) { + plugin.getLogger().log(Level.CONFIG, "Could not save '" + path + "' to " + file, ex); + } + } + + cfg = new YamlConfiguration(); + try { + cfg.load(file); + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidConfigurationException e) { + plugin.getLogger().config("Invalid configuration " + path + ": " + e.getMessage()); + } + } + + public void save() { + try { + cfg.save(file); + } catch (IOException e) { + plugin.getLogger().config("Cannot save " + path + ": " + e.getMessage()); + } + } + +} diff --git a/src/cz/marwland/mc/core/config/ConfigManager.java b/src/cz/marwland/mc/core/config/ConfigManager.java new file mode 100644 index 0000000..344e000 --- /dev/null +++ b/src/cz/marwland/mc/core/config/ConfigManager.java @@ -0,0 +1,41 @@ +package cz.marwland.mc.core.config; + +import java.util.HashMap; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.Plugin; + +public class ConfigManager { + private final Plugin plugin; + + private HashMap configs = new HashMap<>(); + + public ConfigManager(Plugin pl) { + this.plugin = pl; + } + + public void registerConfig(String path) { + configs.put(path, new ConfigFile(plugin, path)); + } + + public void registerConfig(String template, String path) { + configs.put(path, new ConfigFile(plugin, template, path)); + } + + public void load() { + configs.forEach((k, v) -> v.load()); + } + + public void save() { + configs.forEach((k, v) -> v.save()); + } + + public ConfigFile getConfigFile(String path) { + return configs.get(path); + } + + public FileConfiguration getConfig(String path) { + return configs.get(path).getConfig(); + } + +} diff --git a/src/cz/marwland/mc/features/Heil.java b/src/cz/marwland/mc/features/Heil.java new file mode 100644 index 0000000..1dc3bf2 --- /dev/null +++ b/src/cz/marwland/mc/features/Heil.java @@ -0,0 +1,32 @@ +package cz.marwland.mc.features; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.plugin.Plugin; + +import cz.marwland.mc.core.Feature; + +public class Heil extends Feature { + + public Heil(Plugin plugin) { + super(plugin, "heil"); + } + + @Override + public void onEnable() { + + } + + @Override + public void onDisable() { + + } + @EventHandler + public void onPlayerChat(PlayerChatEvent event) { + if (event.getMessage().contains("sieg")) { + event.setMessage("SIEG HEIL!"); + } + + } + +}