diff --git a/.classpath b/.classpath index 1c7a2a9..4bdc242 100644 --- a/.classpath +++ b/.classpath @@ -17,14 +17,6 @@ - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 91a1e96..8038b51 100644 --- a/pom.xml +++ b/pom.xml @@ -1,19 +1,19 @@ 4.0.0 - cz.marwland.mc MarwCore - 0.0.1-SNAPSHOT MarwCore Core plugin for MarwLand.cz Spigot-based Minecraft servers https://git.lixko.eu/MarwLand/MarwCore - - MarwLand - https://marwland.cz - https://git.lixko.eu/MarwLand/MarwCore/issues Gitea Issue Tracker + + cz.marwland.mc + MarwStuff + 0.0.1-SNAPSHOT + ../MarwStuff + src @@ -53,62 +53,13 @@ - - org.spigotmc - spigot-api - 1.12-R0.1-SNAPSHOT - provided - - - me.lucko.luckperms - luckperms-api - 4.2 - provided - - - com.connorlinfoot - BountifulAPI - 1.6.4 - provided - - - com.sk89q.worldedit - worldedit-core - 6.0.0-SNAPSHOT - provided - - - com.sk89q.worldguard - worldguard-legacy - 6.2 - provided + + me.lucko.luckperms + luckperms-api - com.massivecraft.massivecore - MassiveCore - 2.14.1-SNAPSHOT - system - /home/erik/Dokumenty/Java/marwland/res/MassiveCore.jar - - - com.massivecraft.factions - Factions - 2.14.1-SNAPSHOT - system - /home/erik/Dokumenty/Java/marwland/res/Factions.jar + com.connorlinfoot + BountifulAPI - - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - sk89q-repo - http://maven.sk89q.com/repo/ - - \ No newline at end of file diff --git a/resources/config.yml b/resources/config.yml new file mode 100644 index 0000000..dc85c86 --- /dev/null +++ b/resources/config.yml @@ -0,0 +1,2 @@ +#modules: +# - cz.marwland.mc.features.Borders diff --git a/resources/plugin.yml b/resources/plugin.yml index a8fd570..f613184 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -6,3 +6,7 @@ author: ericek111 website: https://git.lixko.eu/MarwLand/MarwCore depend: [LuckPerms] softdepend: [WorldGuard, Factions] +commands: + marw: + description: Core plugin controls. + usage: /marw diff --git a/src/cz/marwland/mc/core/MarwCommandExecutor.java b/src/cz/marwland/mc/core/MarwCommandExecutor.java new file mode 100644 index 0000000..05da6c9 --- /dev/null +++ b/src/cz/marwland/mc/core/MarwCommandExecutor.java @@ -0,0 +1,26 @@ +package cz.marwland.mc.core; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +import net.md_5.bungee.api.ChatColor; + +public class MarwCommandExecutor implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length < 1) { + sendHelp(sender); + } else if (args[0].equalsIgnoreCase("reload")) { + MarwCore.getInstance().reload(); + sender.sendMessage(ChatColor.GREEN + "Reloaded!"); + } + return true; + } + + public void sendHelp(CommandSender sender) { + sender.sendMessage("Available commands:"); + sender.sendMessage("/marw reload - Reload configs."); + } +} diff --git a/src/cz/marwland/mc/core/MarwCore.java b/src/cz/marwland/mc/core/MarwCore.java index 526d586..2251d43 100644 --- a/src/cz/marwland/mc/core/MarwCore.java +++ b/src/cz/marwland/mc/core/MarwCore.java @@ -1,46 +1,43 @@ package cz.marwland.mc.core; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; import java.util.HashMap; +import java.util.logging.Level; import org.bukkit.plugin.java.JavaPlugin; import cz.marwland.mc.core.config.ConfigManager; -import cz.marwland.mc.core.features.Base; import cz.marwland.mc.core.features.Feature; -import cz.marwland.mc.essentials.features.AntiVoid; -import cz.marwland.mc.essentials.features.Borders; -import cz.marwland.mc.essentials.features.ChatNotifier; -import cz.marwland.mc.essentials.features.ChorusLimiter; -import cz.marwland.mc.essentials.features.RandomTeleport; +import cz.marwland.mc.core.plugins.ModuleClassLoader; public class MarwCore extends JavaPlugin { private ConfigManager configManager; private HashMap features = new HashMap<>(); private static MarwCore INSTANCE = null; + private File modulesFolder = null; + private ModuleClassLoader moduleClassLoader; @Override public void onEnable() { INSTANCE = this; - //getServer().getPluginManager().registerEvents(new EventListener(this), this); + this.getCommand("marw").setExecutor(new MarwCommandExecutor()); configManager = new ConfigManager(this); + configManager.registerConfig("config.yml"); + configManager.loadConfig("config.yml"); - this.addFeature(new AntiVoid()); - this.addFeature(new Base()); - this.addFeature(new Borders()); - this.addFeature(new ChatNotifier()); - this.addFeature(new ChorusLimiter()); - this.addFeature(new RandomTeleport()); - - configManager.load(); - this.features.forEach((k, v) -> v.onEnable()); + modulesFolder = this.getModulesFolderPath().toFile(); + modulesFolder.mkdirs(); + this.loadAndEnableModules(); } @Override public void onDisable() { this.features.forEach((k, v) -> v.onDisable()); - configManager.save(); + this.configManager.save(); } @Override @@ -50,7 +47,36 @@ public class MarwCore extends JavaPlugin { public void reload() { this.reloadConfig(); - this.features.forEach((k, v) -> v.onReload()); + this.features.forEach((k, v) -> v.onDisable()); + this.loadAndEnableModules(); + } + + public void loadModules() { + if (moduleClassLoader != null) { + try { + moduleClassLoader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + moduleClassLoader = new ModuleClassLoader(); + moduleClassLoader.addFromDirectory(modulesFolder); + + for (String modClass : configManager.getConfig("config.yml").getStringList("modules")) { + Feature f = moduleClassLoader.doConstructor(modClass, this); + if (f == null) + continue; + + this.addFeature(f); + this.getLogger().log(Level.INFO, "Loaded " + f.getName() + " @ " + modClass); + } + } + + public void loadAndEnableModules() { + this.loadModules(); + configManager.load(); + this.features.forEach((k, v) -> v.onEnable()); } public ConfigManager getConfigManager() { @@ -73,4 +99,12 @@ public class MarwCore extends JavaPlugin { return INSTANCE; } + public Path getModulesFolderPath() { + return this.getDataFolder().toPath().resolve("modules"); + } + + public ModuleClassLoader getModuleClassLoader() { + return this.moduleClassLoader; + } + } diff --git a/src/cz/marwland/mc/core/config/ConfigManager.java b/src/cz/marwland/mc/core/config/ConfigManager.java index 344e000..6d107b6 100644 --- a/src/cz/marwland/mc/core/config/ConfigManager.java +++ b/src/cz/marwland/mc/core/config/ConfigManager.java @@ -22,6 +22,10 @@ public class ConfigManager { configs.put(path, new ConfigFile(plugin, template, path)); } + public void loadConfig(String path) { + configs.get(path).load(); + } + public void load() { configs.forEach((k, v) -> v.load()); } diff --git a/src/cz/marwland/mc/core/features/Base.java b/src/cz/marwland/mc/core/features/Base.java deleted file mode 100644 index 72dc7bc..0000000 --- a/src/cz/marwland/mc/core/features/Base.java +++ /dev/null @@ -1,38 +0,0 @@ -package cz.marwland.mc.core.features; - -import java.util.Arrays; - -import org.bukkit.command.CommandSender; -import org.bukkit.command.defaults.BukkitCommand; -import org.bukkit.configuration.file.FileConfiguration; - -import cz.marwland.mc.core.features.Feature; - - -public class Base extends Feature { - - FileConfiguration cfg; - - public Base() { - super("marw"); - // String name, String description, String usageMessage, List aliases) - this.addCommand(new BukkitCommand("marw", "Core plugin controls.", "/marw", Arrays.asList()) { - @Override - public boolean execute(CommandSender sender, String commandLabel, String[] args) { - if (args.length < 1) { - sendHelp(sender); - } else if (args[0].equalsIgnoreCase("reload")) { - plugin.reload(); - sender.sendMessage("Reloaded!"); - } - return true; - } - - public void sendHelp(CommandSender sender) { - sender.sendMessage("Available commands:"); - sender.sendMessage("/marw reload - Reload configs."); - } - }); - } - -} diff --git a/src/cz/marwland/mc/core/features/Feature.java b/src/cz/marwland/mc/core/features/Feature.java index 6d0fd70..ca10c11 100644 --- a/src/cz/marwland/mc/core/features/Feature.java +++ b/src/cz/marwland/mc/core/features/Feature.java @@ -41,6 +41,7 @@ public abstract class Feature implements Listener { public void onEnable() { if (cmap != null) { for (BukkitCommand cmd : commands) { + System.out.println("Registering: " + cmd.getName()); cmap.register(plugin.getName().toLowerCase(), cmd); } } @@ -54,9 +55,6 @@ public abstract class Feature implements Listener { } } } - - public void onReload() { - } public String getName() { return this.name; diff --git a/src/cz/marwland/mc/core/plugins/ModuleClassLoader.java b/src/cz/marwland/mc/core/plugins/ModuleClassLoader.java new file mode 100644 index 0000000..3fb6722 --- /dev/null +++ b/src/cz/marwland/mc/core/plugins/ModuleClassLoader.java @@ -0,0 +1,87 @@ +package cz.marwland.mc.core.plugins; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.jar.JarFile; +import java.util.logging.Level; + +import org.bukkit.plugin.java.JavaPlugin; + +import cz.marwland.mc.core.features.Feature; + +public class ModuleClassLoader extends URLClassLoader { + + private ArrayList jars = new ArrayList<>(); + + public ModuleClassLoader() { + super(new URL[] { }, ModuleClassLoader.class.getClassLoader()); + } + + public void addFile(File file) throws IOException { + this.addURL(file.toURI().toURL()); + jars.add(new JarFile(file)); + } + + public void addFromDirectory(File modDir) { + if (!modDir.isDirectory()) + throw new IllegalArgumentException("Provided 'modDir' argument is not a directory: " + modDir.getPath()); + + for (File mod : modDir.listFiles()) { + if (mod.isDirectory()) + continue; + int lastdot = mod.getName().lastIndexOf('.'); + if (lastdot < 0) + continue; + + if (!mod.getName().substring(lastdot + 1).equalsIgnoreCase("jar")) + continue; + + try { + this.addFile(mod); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public Feature doConstructor(String classPath) + throws ClassNotFoundException, ClassCastException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + Class jarClass; + jarClass = Class.forName(classPath, true, this); + + Class pluginClass; + pluginClass = jarClass.asSubclass(Feature.class); + + return pluginClass.getDeclaredConstructor().newInstance(); + } + + public Feature doConstructor(String classPath, JavaPlugin plugin) { + try { + return this.doConstructor(classPath); + } catch (ClassNotFoundException e) { + plugin.getLogger().log(Level.SEVERE, "Cannot find class '" + classPath + "'!"); + } catch (ClassCastException e) { + plugin.getLogger().log(Level.SEVERE, "Class '" + classPath + "' does not extend Feature!"); + } catch (IllegalAccessException e) { + plugin.getLogger().log(Level.SEVERE, "No public constructor in '" + classPath + "'!"); + } catch (InstantiationException e) { + plugin.getLogger().log(Level.SEVERE, "Cannot instantiate '" + classPath + "'!"); + } catch (IllegalArgumentException e) { + plugin.getLogger().log(Level.SEVERE, "Illegal arguments to the constructor of '" + classPath + "'!"); + } catch (InvocationTargetException e) { + plugin.getLogger().log(Level.SEVERE, "Error occured whilst initializing '" + classPath + "'!"); + e.printStackTrace(); + } catch (NoSuchMethodException e) { + plugin.getLogger().log(Level.SEVERE, "Class '" + classPath + "' does not have a public constructor!"); + } catch (SecurityException e) { + plugin.getLogger().log(Level.SEVERE, "SecurityException occured whilst initializing '" + classPath + "'!"); + e.printStackTrace(); + } + return null; + } + +} diff --git a/src/cz/marwland/mc/essentials/features/AntiVoid.java b/src/cz/marwland/mc/essentials/features/AntiVoid.java deleted file mode 100644 index faf42df..0000000 --- a/src/cz/marwland/mc/essentials/features/AntiVoid.java +++ /dev/null @@ -1,59 +0,0 @@ -package cz.marwland.mc.essentials.features; - -import java.util.ArrayList; - -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerMoveEvent; - -import cz.marwland.mc.core.features.Feature; - -public class AntiVoid extends Feature { - - private FileConfiguration cfg; - private ArrayList allowedWorlds = new ArrayList<>(); - private double triggery = 0d; - - public AntiVoid() { - super(); - this.registerConfig("antivoid.yml"); - } - - @EventHandler - public void onPlayerMove(PlayerMoveEvent e) { - Player p = e.getPlayer(); - if (p.getLocation().getY() > triggery) - return; - - World w = p.getWorld(); - if (!allowedWorlds.contains(w)) - return; - - p.teleport(w.getSpawnLocation()); - } - - @Override - public void onEnable() { - super.onEnable(); - this.onReload(); - } - - @Override - public void onReload() { - super.onReload(); - cfg = getConfig("antivoid.yml"); - - triggery = cfg.getDouble("triggery", 0d); - for (String wname : cfg.getStringList("worlds")) { - World w = Bukkit.getWorld(wname); - - if (w == null) - continue; - allowedWorlds.add(w); - } - } - -} diff --git a/src/cz/marwland/mc/essentials/features/Borders.java b/src/cz/marwland/mc/essentials/features/Borders.java deleted file mode 100644 index 2c9e2a5..0000000 --- a/src/cz/marwland/mc/essentials/features/Borders.java +++ /dev/null @@ -1,230 +0,0 @@ -package cz.marwland.mc.essentials.features; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.command.CommandSender; -import org.bukkit.command.defaults.BukkitCommand; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.util.StringUtil; -import org.bukkit.util.Vector; - -import cz.marwland.mc.core.config.ConfigEntry; -import cz.marwland.mc.core.config.EntryManager; -import cz.marwland.mc.core.config.IValueExecuter; -import cz.marwland.mc.core.config.ValueValidator; -import cz.marwland.mc.core.features.Feature; -import net.md_5.bungee.api.ChatColor; - -public class Borders extends Feature { - - FileConfiguration cfg; - EntryManager emgr = new EntryManager(); - - public Borders() { - super(); - // String name, String description, String usageMessage, List aliases) - this.addCommand(new BukkitCommand( - "worldborder", - "Modify the world's borders", - "/border [world] (key) (value)", - Arrays.asList() ) { - @Override - public boolean execute(CommandSender sender, String commandLabel, String[] args) { - if (!permissionMissingCheck(sender, this.getPermission())) - return false; - - if (args.length < 2) { - sender.sendMessage( - ChatColor.YELLOW + "Format: " + - ChatColor.GRAY + ChatColor.ITALIC + "/" + commandLabel + - ChatColor.YELLOW + ChatColor.GRAY + " (world) (key) [value]"); - return true; - } - World w = Bukkit.getServer().getWorld(args[0]); - if (w == null) { - sender.sendMessage(ChatColor.RED + "Invalid world!"); - return true; - } - - ConfigEntry entry = emgr.getEntry(args[1]); - if (entry == null) { - sender.sendMessage( - ChatColor.RED + "Invalid key " + - ChatColor.YELLOW + args[1] + - ChatColor.RED + "! Available: " + - ChatColor.GRAY + String.join(", ", emgr.getKeys())); - return true; - } - - if (args.length == 2) { // get - if (!cfg.contains(w.getName())) { - sender.sendMessage( - ChatColor.RED + "No border specified for " + - ChatColor.YELLOW + args[0] + - ChatColor.RED + "!"); - return true; - } - Object val = entry.getFrom(cfg, w.getName()); - - sender.sendMessage( - ChatColor.YELLOW + args[1] + - ChatColor.GRAY + " @ " + - ChatColor.AQUA + w.getName() + - ChatColor.GRAY + ": " + (val == null ? ChatColor.ITALIC + "[" + entry.getDefaultValue() + "]": val)); - } else { // set - String oldval = "" + entry.getFrom(cfg, w.getName()); - String newval = String.join(" ", args).substring(args[0].length() + args[1].length() + 2); - if (entry.setIn(cfg, w.getName(), newval)) { - plugin.getConfigManager().save(); - cfg = getConfig("borders.yml"); - loadWorlds(); - sender.sendMessage( - ChatColor.GREEN + "Changed " + - ChatColor.YELLOW + entry.getSubpath() + - ChatColor.GRAY + " @ " + - ChatColor.AQUA + w.getName() + - ChatColor.GRAY + ": " + oldval + - ChatColor.GOLD + " => " + - ChatColor.GRAY + newval); - } else { - sender.sendMessage( - ChatColor.RED + "Invalid value for " + - ChatColor.YELLOW + args[1] + - ChatColor.RED + ": " + - ChatColor.GRAY + newval); - } - } - return true; - } - - @Override - public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { - ArrayList hints = new ArrayList(); - if (!sender.hasPermission(this.getPermission()) || args.length < 1) - return hints; - - String lastWord = args[args.length - 1]; - if (args.length == 1) { - for (World w : Bukkit.getServer().getWorlds()) { - if (StringUtil.startsWithIgnoreCase(w.getName(), lastWord)) - hints.add(w.getName()); - } - } else if(args.length == 2) { - for (String key : emgr.getKeys()) { - if (StringUtil.startsWithIgnoreCase(key, lastWord)) - hints.add(key); - } - } - - Collections.sort(hints, String.CASE_INSENSITIVE_ORDER); - return hints; - } - - @Override - public String getPermission() { - return getPermissionPath() + "." + this.getName().toLowerCase(); - } - }); - emgr.addEntry(new ConfigEntry<>("center", ValueValidator.VECTOR, new Vector(0, 0, 0))); -// emgr.addEntry(new ConfigEntry<>("size", ValueValidator.DOUBLE)); - emgr.addEntry(new ConfigEntry<>("radius", ValueValidator.INTEGER, 60000000)); - emgr.addEntry(new ConfigEntry<>("warningdist", ValueValidator.INTEGER, 5)); - emgr.addEntry(new ConfigEntry<>("warningtime", ValueValidator.INTEGER, 15)); - emgr.addEntry(new ConfigEntry<>("damage", ValueValidator.DOUBLE, 0.2d)); - emgr.addEntry(new ConfigEntry<>("buffer", ValueValidator.DOUBLE, 5d)); - this.registerConfig("borders.yml"); - } - - @Override - public void onEnable() { - super.onEnable(); - cfg = getConfig("borders.yml"); - this.loadWorlds(); - } - - public void loadWorlds() { - for(World w : Bukkit.getServer().getWorlds()) { - final String wname = w.getName(); - if (!cfg.contains(wname)) { - w.getWorldBorder().reset(); - continue; - } - - emgr.getEntry("center").doStuff(cfg, wname, new IValueExecuter() { - public void doStuff(Object val) { - Vector v = (Vector) val; - w.getWorldBorder().setCenter(v.getX(), v.getZ()); - } - }); - emgr.getEntry("radius").doStuff(cfg, wname, new IValueExecuter() { - public void doStuff(Object val) { - w.getWorldBorder().setSize((int) val); - } - }); - emgr.getEntry("warningdist").doStuff(cfg, wname, new IValueExecuter() { - public void doStuff(Object val) { - w.getWorldBorder().setWarningDistance((int) val); - } - }); - emgr.getEntry("warningtime").doStuff(cfg, wname, new IValueExecuter() { - public void doStuff(Object val) { - w.getWorldBorder().setWarningTime((int) val); - } - }); - emgr.getEntry("damage").doStuff(cfg, wname, new IValueExecuter() { - public void doStuff(Object val) { - w.getWorldBorder().setDamageAmount((double) val); - } - }); - emgr.getEntry("buffer").doStuff(cfg, wname, new IValueExecuter() { - public void doStuff(Object val) { - w.getWorldBorder().setDamageBuffer((double) val); - } - }); - /* - Region r = new Region(); - if (cfg.contains(wname + ".center") && cfg.contains(wname + ".size")) { - plugin.getLogger().info("should i?"); - String centerstr = cfg.getString(wname + ".center"); - Vector center = LocationUtil.deserializeVector(centerstr); - String sizestr = cfg.getString(wname + ".size"); - if (sizestr.indexOf(' ') == -1) { - w.getWorldBorder().setCenter(center.getX(), center.getZ()); - w.getWorldBorder().setSize(cfg.getDouble(wname + ".size", 0)); - plugin.getLogger().info("set the ghetto way"); - } else { - Vector size = LocationUtil.deserializeVector(sizestr); - if (center == null) { - plugin.getLogger().config("[Borders] Invalid center coordinates for " + wname + ": " + centerstr); - } else if (size == null) { - plugin.getLogger().config("[Borders] Invalid size for " + wname + ": " + sizestr); - } else { - size.divide(new Vector(2f, 2f, 2f)); - r.setOne(center.add(size)); - r.setTwo(center.subtract(size)); - } - } - } - if (cfg.contains(wname + ".points")) { - List points = cfg.getStringList(wname + ".points"); - for (String pstr : points) { - Vector p = LocationUtil.deserializeVector(pstr); - if (p == null) - continue; - r.ensureInside(p); - } - } - if (r.isValid()) { - Vector c = r.getCenter(); - w.getWorldBorder().setCenter(c.getX(), c.getZ()); - } - */ - } - } - -} diff --git a/src/cz/marwland/mc/essentials/features/ChatNotifier.java b/src/cz/marwland/mc/essentials/features/ChatNotifier.java deleted file mode 100644 index 27b6066..0000000 --- a/src/cz/marwland/mc/essentials/features/ChatNotifier.java +++ /dev/null @@ -1,31 +0,0 @@ -package cz.marwland.mc.essentials.features; - -import org.bukkit.Bukkit; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.AsyncPlayerChatEvent; - -import com.connorlinfoot.bountifulapi.BountifulAPI; - -import cz.marwland.mc.core.features.Feature; -import net.md_5.bungee.api.ChatColor; - -public class ChatNotifier extends Feature { - - // TODO: Per-player highlighting - @EventHandler - public void onPlayerChat(AsyncPlayerChatEvent e) { - for (Player p : Bukkit.getServer().getOnlinePlayers()) { - String msg = e.getMessage(); - if (msg.toLowerCase().contains(p.getName().toLowerCase()) && p.getPlayer() != e.getPlayer()) { - /*e.getRecipients().remove(p); - msg = ChatColor.RED + msg;*/ - BountifulAPI.sendActionBar(p, ChatColor.RED + "Byl jsi zminen v chatu!"); - p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 3.0F); - } - //p.sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), msg)); - } - } - -} diff --git a/src/cz/marwland/mc/essentials/features/ChorusLimiter.java b/src/cz/marwland/mc/essentials/features/ChorusLimiter.java deleted file mode 100644 index b66dd88..0000000 --- a/src/cz/marwland/mc/essentials/features/ChorusLimiter.java +++ /dev/null @@ -1,31 +0,0 @@ -package cz.marwland.mc.essentials.features; - -import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerItemConsumeEvent; - -import com.connorlinfoot.bountifulapi.BountifulAPI; - -import cz.marwland.mc.core.features.Feature; -import cz.marwland.mc.core.util.Cooldown; -import net.md_5.bungee.api.ChatColor; - -public class ChorusLimiter extends Feature { - - private Cooldown chorusCooldown = new Cooldown(this); - - @EventHandler - public void onPlayerItemConsume(PlayerItemConsumeEvent e) { - if (e.getItem().getType() != Material.CHORUS_FRUIT) - return; - - long left = chorusCooldown.timeLeftSeconds(e.getPlayer()); - if (left > 0) { - BountifulAPI.sendActionBar(e.getPlayer(), ChatColor.RED + "Musis pockat jeste " + left + " sekund pro pouziti Chorus Fruitu!"); - e.setCancelled(true); - return; - } - chorusCooldown.resetPlayer(e.getPlayer()); - } - -} diff --git a/src/cz/marwland/mc/essentials/features/RandomTeleport.java b/src/cz/marwland/mc/essentials/features/RandomTeleport.java deleted file mode 100644 index 418b7f5..0000000 --- a/src/cz/marwland/mc/essentials/features/RandomTeleport.java +++ /dev/null @@ -1,169 +0,0 @@ -package cz.marwland.mc.essentials.features; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.command.CommandSender; -import org.bukkit.command.defaults.BukkitCommand; -import org.bukkit.entity.Player; -import org.bukkit.event.block.BlockCanBuildEvent; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -import cz.marwland.mc.core.features.Feature; -import cz.marwland.mc.core.util.Cooldown; -import net.md_5.bungee.api.ChatColor; - -public class RandomTeleport extends Feature { - - private final int MAX_TRIES = 100; - private Cooldown telCooldown = new Cooldown(this); - - private static final Set INVALID_MATERIALS = new HashSet<>(Arrays.asList( - Material.AIR, - Material.WATER, - Material.STATIONARY_WATER, - Material.LAVA, - Material.STATIONARY_LAVA, - Material.MAGMA, - Material.MAGMA_CREAM, - Material.FIRE, - Material.WEB, - Material.CACTUS, - Material.ENDER_PORTAL, - Material.PORTAL - )); - - public RandomTeleport() { - super(); - this.addCommand(new BukkitCommand( - "rtp", - "Teleport player to random location in the current world.", - "/rtp [player]", - Arrays.asList("randomtp", "randomteleport") ) { - @Override - public boolean execute(CommandSender sender, String commandLabel, String[] args) { - if (!permissionMissingCheck(sender, this.getPermission())) - return true; - - if (!(sender instanceof Player) && args.length < 1) { - sender.sendMessage(ChatColor.RED + "You need to specify the player! " + ChatColor.YELLOW + "/rtp [player] [world]"); - return true; - } - - Player p = (Player) sender; - if (args.length >= 1) { - p = Bukkit.getServer().getPlayer(args[0]); - if (p != sender && !permissionMissingCheck(sender, this.getPermission() + ".others")) { - return true; - } else if (p == null) { - sender.sendMessage(ChatColor.RED + "Player not found!"); - return true; - } - } - - World w = p.getWorld(); - if (args.length >= 2) { - w = Bukkit.getServer().getWorld(args[1]); - if (w == null) { - sender.sendMessage(ChatColor.RED + "World not found!"); - return true; - } - } - - if (!sender.hasPermission(this.getPermission() + ".world." + w.getName())) { - sender.sendMessage(ChatColor.RED + "V tomhle svete se nemuzes nahodne teleportovat!"); - return true; - } - - long left = telCooldown.timeLeftSeconds(p); - if (left > 0) { - sender.sendMessage(ChatColor.RED + "Nemuzes se nahodne teleportovat jeste " + ChatColor.YELLOW + left + ChatColor.RED + " sekund" + (left == 1 ? "u!" : (left > 4 ? "!" : "y!"))); - return true; - } - - telCooldown.resetPlayer(p); - randomTeleport(w, p); - - return true; - } - - @Override - public String getPermission() { - return getPermissionPath(); - } - }); - } - - public void randomTeleport(World w, Player p) { - Location loc = null; - for (int i = 0; i < MAX_TRIES && loc == null; i++) { - loc = generateLoc(w); - if (!canTeleportHere(loc, p, false)) - loc = null; - else { - loc.add(0.5d, 1d, 0.5d); - if (!canTeleportHere(loc, p, true)) - loc = null; - } - } - if (loc != null) { - loc.setYaw((float) (Math.random() * 360f)); - p.teleport(loc, TeleportCause.COMMAND); - } else { - p.sendMessage(ChatColor.RED + "V tomhle svete se pro tebe nenaslo bezpecne misto!"); - } - } - - public Location generateLoc(World w) { - int wboff = 16; - Location loc = w.getWorldBorder().getCenter(); - int radius = (int) w.getWorldBorder().getSize() / 2; - int minx = loc.getBlockX() - radius; - int minz = loc.getBlockZ() - radius; - int maxx = loc.getBlockX() + radius; - int maxz = loc.getBlockZ() + radius; - if (wboff >= radius) - wboff = 0; - int rangex = (maxx - minx) - wboff * 2; - int rangez = (maxz - minz) - wboff * 2; - int finx = minx + (int) (Math.random() * rangex) + wboff; - int finz = minz + (int) (Math.random() * rangez) + wboff; - Location targetloc = w.getHighestBlockAt(finx, finz).getLocation(); - targetloc.subtract(0d, 1d, 0d); - return targetloc; - } - - public boolean canTeleportHere(Location loc, Player p, boolean allowAir) { - Block block = loc.getWorld().getBlockAt(loc); - if (INVALID_MATERIALS.contains(block.getType()) && (allowAir ? block.getType() != Material.AIR : true)) - return false; - - BlockCanBuildEvent canBuildEvent = new BlockCanBuildEvent(block, block.getData(), true); - Bukkit.getServer().getPluginManager().callEvent(canBuildEvent); - if (!canBuildEvent.isBuildable()) - return false; - - try { - if (!com.sk89q.worldguard.bukkit.WGBukkit.getPlugin().canBuild(p, block)) { - return false; - } - } catch (NoClassDefFoundError e) { - } - - try { - if (com.massivecraft.factions.entity.BoardColl.get().getFactionAt(com.massivecraft.massivecore.ps.PS.valueOf(loc)) != com.massivecraft.factions.entity.FactionColl.get().getNone()) { - return false; - } - } catch (NoClassDefFoundError e) { - } - - return true; - } - -}