diff --git a/src/com/leetzilantonis/netherwater/ConfigManager.java b/src/com/leetzilantonis/netherwater/ConfigManager.java new file mode 100644 index 0000000..8368b83 --- /dev/null +++ b/src/com/leetzilantonis/netherwater/ConfigManager.java @@ -0,0 +1,46 @@ +package com.leetzilantonis.netherwater; + +import java.io.File; +import java.util.List; + +public class ConfigManager { + private final NetherWater plugin; + + public ConfigManager(NetherWater plugin) { + this.plugin = plugin; + + this.loadConfig(); + } + + private void loadConfig() { + if (!this.plugin.getDataFolder().exists()) { + this.plugin.getDataFolder().mkdir(); + } + + if (!new File(this.plugin.getDataFolder(), "config.yml").exists()) { + this.plugin.saveDefaultConfig(); + } else { + this.reloadConfig(); + } + } + + public void reloadConfig() { + this.plugin.reloadConfig(); + } + + public List getDisabledWorlds() { + return this.plugin.getConfig().getStringList("disabledWorlds"); + } + + public String getMessage(String name) { + return this.plugin.getConfig().getString("messages." + name); + } + + public int getMinHeight() { + return plugin.getConfig().getInt("minHeight"); + } + + public int getMaxHeight() { + return plugin.getConfig().getInt("maxHeight"); + } +} diff --git a/src/com/leetzilantonis/netherwater/NWReloadCommand.java b/src/com/leetzilantonis/netherwater/NWReloadCommand.java index 854bf57..0676b12 100644 --- a/src/com/leetzilantonis/netherwater/NWReloadCommand.java +++ b/src/com/leetzilantonis/netherwater/NWReloadCommand.java @@ -6,25 +6,23 @@ import org.bukkit.command.CommandSender; public class NWReloadCommand implements CommandExecutor { - Main plugin; - - public NWReloadCommand(Main plugin) { + private final NetherWater plugin; + private final ConfigManager configManager; + + public NWReloadCommand(NetherWater plugin) { this.plugin = plugin; + this.configManager = plugin.getConfigManager(); } - + @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (!sender.hasPermission("netherwater.reload")) { - - sender.sendMessage(ChatColor.RED + "You do not have permission to do that!"); - return true; - + sender.sendMessage(ChatColor.RED + this.configManager.getMessage("permissions")); } else { - - plugin.reloadConfig(); - sender.sendMessage(ChatColor.GREEN + "Nether Water configuration reloaded!"); - return true; - + this.configManager.reloadConfig(); + sender.sendMessage(ChatColor.GREEN + this.configManager.getMessage("config-reload")); } + + return true; } } diff --git a/src/com/leetzilantonis/netherwater/Main.java b/src/com/leetzilantonis/netherwater/NetherWater.java similarity index 78% rename from src/com/leetzilantonis/netherwater/Main.java rename to src/com/leetzilantonis/netherwater/NetherWater.java index d3904e6..167c95a 100644 --- a/src/com/leetzilantonis/netherwater/Main.java +++ b/src/com/leetzilantonis/netherwater/NetherWater.java @@ -1,6 +1,5 @@ package com.leetzilantonis.netherwater; -import java.io.File; import java.util.ArrayList; import java.util.List; @@ -18,18 +17,16 @@ import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -public class Main extends JavaPlugin { - private List dWorlds = new ArrayList(); - private WorldGuardPlugin wg; +public class NetherWater extends JavaPlugin { + private WorldGuardPlugin worldGuard; + + private ConfigManager configManager; @Override public void onEnable() { - if(!getDataFolder().exists()) getDataFolder().mkdir(); - if(!new File(getDataFolder(), "config.yml").exists()) saveDefaultConfig(); - - this.dWorlds = this.getConfig().getStringList("disabledWorlds"); + this.configManager = new ConfigManager(this); - if ((this.wg = this.getWorldGuard()) == null) { + if ((this.worldGuard = this.getWorldGuard()) == null) { this.getLogger().warning("World Guard cannot be found!"); } else { this.getLogger().info("World Guard has been found a registered!"); @@ -58,11 +55,11 @@ private WorldGuardPlugin getWorldGuard() { } public boolean canBuild(Player p, Block b) { - if (this.wg == null) { + if (this.worldGuard == null) { return true; } - LocalPlayer wgPlayer = this.wg.wrapPlayer(p); + LocalPlayer wgPlayer = this.worldGuard.wrapPlayer(p); World weWorld = wgPlayer.getWorld(); Location location = new Location(weWorld, b.getX(), b.getY(), b.getZ()); RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer(); @@ -73,7 +70,7 @@ public boolean canBuild(Player p, Block b) { return canBypass || regionQuery.testState(location, wgPlayer, Flags.BUILD); } - public List getWorlds() { - return dWorlds; + public ConfigManager getConfigManager() { + return configManager; } } diff --git a/src/com/leetzilantonis/netherwater/WaterPlaceListener.java b/src/com/leetzilantonis/netherwater/WaterPlaceListener.java index c6c2141..18e32f3 100644 --- a/src/com/leetzilantonis/netherwater/WaterPlaceListener.java +++ b/src/com/leetzilantonis/netherwater/WaterPlaceListener.java @@ -9,14 +9,14 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.EquipmentSlot; -import org.bukkit.inventory.ItemStack; public class WaterPlaceListener implements Listener { - Main plugin; + private final NetherWater plugin; + private final ConfigManager configManager; - public WaterPlaceListener(Main plugin) { + public WaterPlaceListener(NetherWater plugin) { this.plugin = plugin; + this.configManager = plugin.getConfigManager(); } @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -30,32 +30,32 @@ public void onPlayerInteract(PlayerInteractEvent e) { World w = e.getClickedBlock().getWorld(); Player p = e.getPlayer(); - if (w.getEnvironment() == Environment.NETHER && e.getItem().getType() == Material.WATER_BUCKET) { - if (p.hasPermission("netherwater.use." + w.getName()) || p.hasPermission("netherwater.use.*")) { - if (!plugin.getWorlds().contains(w.getName()) || p.hasPermission("netherwater.world.bypass")) { - if (plugin.canBuild(p, e.getClickedBlock().getRelative(e.getBlockFace()))) { - int y = e.getClickedBlock().getRelative(e.getBlockFace()).getY(); - if (y <= plugin.getConfig().getInt("maxHeight")) { - if (y >= plugin.getConfig().getInt("minHeight")) { - // Cancel native event actions - e.setCancelled(true); - - // Add watter block - e.getClickedBlock().getRelative(e.getBlockFace()).setType(Material.WATER); - - // Replace water bucket with empty one - ItemStack emptyBucket = new ItemStack(Material.BUCKET); - - if (e.getHand() == EquipmentSlot.HAND) { - p.getInventory().setItemInMainHand(emptyBucket); - } else if (e.getHand() == EquipmentSlot.OFF_HAND) { - p.getInventory().setItemInOffHand(emptyBucket); - } - } - } - } - } - } - } + if (w.getEnvironment() == Environment.NETHER && e.getItem().getType() == Material.WATER_BUCKET) + return; + + if (p.hasPermission("netherwater.use." + w.getName()) || p.hasPermission("netherwater.use.*")) + return; + + if (!this.configManager.getDisabledWorlds().contains(w.getName()) || p.hasPermission("netherwater.world.bypass")) + return; + + if (plugin.canBuild(p, e.getClickedBlock().getRelative(e.getBlockFace()))) + return; + + int y = e.getClickedBlock().getRelative(e.getBlockFace()).getY(); + if (y <= this.configManager.getMaxHeight()) + return; + + if (y >= this.configManager.getMinHeight()) + return; + + // Cancel native event actions + e.setCancelled(true); + + // Add watter block + e.getClickedBlock().getRelative(e.getBlockFace()).setType(Material.WATER); + + // Replace water bucket with empty one + e.getItem().setType(Material.BUCKET); } } diff --git a/src/config.yml b/src/config.yml index 311a5d0..6e0ed25 100644 --- a/src/config.yml +++ b/src/config.yml @@ -1,4 +1,7 @@ disabledWorlds: - - noWaterWorld + - nether_without_water maxHeight: 999 minHeight: 0 +messages: + permissions: "You do not have permission to do that!" + config-reload: "Nether Water configuration reloaded!" diff --git a/src/plugin.yml b/src/plugin.yml index d4ff0e6..e0af06d 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,15 +1,15 @@ name: NetherWater -version: 1.0.4 +version: 1.0.5 description: Allow players to use water in the nether author: Lee Tzilantonis, ceskyDJ -main: com.leetzilantonis.netherwater.Main +main: com.leetzilantonis.netherwater.NetherWater database: false +api-version: 1.15 commands: nwreload: description: Reload the Nether Water configuration aliases: [nwr] - permission: netherwater.reload - usage: Something went wrong, You shouldnt be seeing this AT ALL + usage: /nwreload or /nwr permissions: netherwater.use.*: description: Allows players to use water in all non disabled nether worlds