Skip to content

Commit

Permalink
Some optimalizations and code quality boosts
Browse files Browse the repository at this point in the history
  • Loading branch information
ceskyDJ committed Apr 27, 2020
1 parent f0e0095 commit 583294a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 62 deletions.
46 changes: 46 additions & 0 deletions src/com/leetzilantonis/netherwater/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}
24 changes: 11 additions & 13 deletions src/com/leetzilantonis/netherwater/NWReloadCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.leetzilantonis.netherwater;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -18,18 +17,16 @@

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;

public class Main extends JavaPlugin {
private List<String> dWorlds = new ArrayList<String>();
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!");
Expand Down Expand Up @@ -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();
Expand All @@ -73,7 +70,7 @@ public boolean canBuild(Player p, Block b) {
return canBypass || regionQuery.testState(location, wgPlayer, Flags.BUILD);
}

public List<String> getWorlds() {
return dWorlds;
public ConfigManager getConfigManager() {
return configManager;
}
}
62 changes: 31 additions & 31 deletions src/com/leetzilantonis/netherwater/WaterPlaceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
5 changes: 4 additions & 1 deletion src/config.yml
Original file line number Diff line number Diff line change
@@ -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!"
8 changes: 4 additions & 4 deletions src/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 583294a

Please sign in to comment.