diff --git a/plugin.yml b/plugin.yml deleted file mode 100644 index 544b6c1..0000000 --- a/plugin.yml +++ /dev/null @@ -1,9 +0,0 @@ - name: Wauh - version: 1.0 - main: org.gecko.wauh.Main - description: Bo o o wa uh - - commands: - stopwauh: - description: Stop removing the wauh - usage: /stopwauhremoval \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2075f6c..fed6cb1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.gecko Wauh - 1.0-closed + 2.0.0 jar Wauh diff --git a/src/main/java/org/gecko/wauh/Main.java b/src/main/java/org/gecko/wauh/Main.java index b0e7686..a8c4739 100644 --- a/src/main/java/org/gecko/wauh/Main.java +++ b/src/main/java/org/gecko/wauh/Main.java @@ -4,10 +4,19 @@ import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; import org.gecko.wauh.barriuh.BarrierListener; +import org.gecko.wauh.commands.StopWauh; import org.gecko.wauh.wauhbuck.BucketListener; +import org.gecko.wauh.commands.SetRadiusLimitCommand; public final class Main extends JavaPlugin { - int radiusLimit = 500; + private int radiusLimit = 20; + public int getRadiusLimit() { + return radiusLimit + 2; + } + + public void setRadiusLimit(int newLimit) { + radiusLimit = newLimit; + } @Override public void onEnable() { @@ -15,14 +24,18 @@ public void onEnable() { Bukkit.getConsoleSender().sendMessage(""); Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Yay"); Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "TEST"); - getServer().getPluginManager().registerEvents(new BucketListener(), this); - getServer().getPluginManager().registerEvents(new BarrierListener(), this); - try { - getCommand("stopwauh").setExecutor(new BucketListener()); - } catch (NullPointerException e) { - Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "I am in eternal suffering."); - } + // Create instances of the listeners + BucketListener bucketListener = new BucketListener(); + BarrierListener barrierListener = new BarrierListener(); + + // Register the listeners + getServer().getPluginManager().registerEvents(bucketListener, this); + getServer().getPluginManager().registerEvents(barrierListener, this); + + // Register the StopWauh command with the listeners as arguments + this.getCommand("stopwauh").setExecutor(new StopWauh(bucketListener, barrierListener)); + this.getCommand("setradiuslimit").setExecutor(new SetRadiusLimitCommand(this)); } @Override @@ -31,8 +44,4 @@ public void onDisable() { Bukkit.getConsoleSender().sendMessage(""); Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "kys"); } - - public int getRadiusLimit() { - return radiusLimit + 2; - } -} +} \ No newline at end of file diff --git a/src/main/java/org/gecko/wauh/barriuh/BarrierListener.java b/src/main/java/org/gecko/wauh/barriuh/BarrierListener.java index 903ed30..0bbc85c 100644 --- a/src/main/java/org/gecko/wauh/barriuh/BarrierListener.java +++ b/src/main/java/org/gecko/wauh/barriuh/BarrierListener.java @@ -5,10 +5,6 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -18,33 +14,26 @@ import java.util.HashSet; import java.util.Set; -public class BarrierListener implements Listener, CommandExecutor { +public class BarrierListener implements Listener { private int grassRemovedCount = 0; private int dirtRemovedCount = 0; + private int barrierRemovedCount = 0; private Set blocksToProcess = new HashSet<>(); private Player currentRemovingPlayer; - private boolean stopBlockRemoval = false; + public boolean stopBlockRemoval = false; private Location clickedLocation; private boolean limitReached = false; - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ - if (sender instanceof Player) { - Player player = (Player) sender; - stopBlockRemoval = true; - player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "Water removal" + ChatColor.RED + "stopped."); - displaySummary(); - } - return true; - } + private int highestDist = 0; + public boolean blockRemovalActive = false; @EventHandler public void BarrierClick(BlockBreakEvent event) { Player player = event.getPlayer(); - if (event.getBlock().getType() == Material.GRASS || event.getBlock().getType() == Material.DIRT) { + if (event.getBlock().getType() == Material.GRASS || event.getBlock().getType() == Material.DIRT || event.getBlock().getType() == Material.BARRIER) { // Check if the bucket is filling with water if (player.getInventory().getItemInMainHand().getType() == Material.BARRIER) { + blockRemovalActive = true; limitReached = false; event.getBlock().setType(Material.BEDROCK); clickedLocation = event.getBlock().getLocation(); @@ -52,6 +41,8 @@ public void BarrierClick(BlockBreakEvent event) { // Reset the water removal counts and initialize the set of blocks to process grassRemovedCount = 0; dirtRemovedCount = 0; + barrierRemovedCount = 0; + highestDist = 0; blocksToProcess.clear(); currentRemovingPlayer = player; @@ -66,6 +57,7 @@ public void BarrierClick(BlockBreakEvent event) { private void processBlockRemoval() { int radiusLimit = Main.getPlugin(Main.class).getRadiusLimit(); + int realRadiusLimit = radiusLimit - 2; if (stopBlockRemoval) { stopBlockRemoval = false; displaySummary(); @@ -74,25 +66,52 @@ private void processBlockRemoval() { Set nextSet = new HashSet<>(); boolean limitReachedThisIteration = false; // Variable to track whether the limit was reached this iteration for (Block block : blocksToProcess) { - if (clickedLocation.distance(block.getLocation()) > (radiusLimit + 2)) { + int dist = (int) clickedLocation.distance(block.getLocation()); + if (dist > radiusLimit) { limitReached = true; limitReachedThisIteration = true; } + if (dist > highestDist) { + if (highestDist <= (realRadiusLimit - 1)) { + highestDist = dist; + // Send a message to the player only when the dist value rises + if (highestDist < realRadiusLimit) { + currentRemovingPlayer.sendMessage(ChatColor.GREEN + "Block removal: " + ChatColor.RED + dist + ChatColor.WHITE + "/" + ChatColor.GREEN + realRadiusLimit); + } else if (highestDist == realRadiusLimit) { + currentRemovingPlayer.sendMessage(ChatColor.GREEN + "Block removal: " + ChatColor.GREEN + dist + ChatColor.WHITE + "/" + ChatColor.GREEN + realRadiusLimit); + } + } else { + limitReached = true; + limitReachedThisIteration = true; + } + } // Check if the block is grass or dirt if (block.getType() == Material.GRASS) { grassRemovedCount++; } else if (block.getType() == Material.DIRT) { dirtRemovedCount++; + } else if (block.getType() == Material.BARRIER) { + barrierRemovedCount++; } block.setType(Material.AIR); // Iterate through neighboring blocks and add them to the next set - for (BlockFace face : BlockFace.values()) { - Block neighboringBlock = block.getRelative(face); - if ((neighboringBlock.getType() == Material.GRASS || neighboringBlock.getType() == Material.DIRT)) { - nextSet.add(neighboringBlock); + for (int i = -1; i <= 1; i++) { + if (i == 0) continue; // Skip the current block + Block neighboringBlockX = block.getRelative(i, 0, 0); + Block neighboringBlockY = block.getRelative(0, i, 0); + Block neighboringBlockZ = block.getRelative(0, 0, i); + + if ((neighboringBlockX.getType() == Material.GRASS || neighboringBlockX.getType() == Material.DIRT || neighboringBlockX.getType() == Material.BARRIER)) { + nextSet.add(neighboringBlockX); + } + if ((neighboringBlockY.getType() == Material.GRASS || neighboringBlockY.getType() == Material.DIRT || neighboringBlockY.getType() == Material.BARRIER)) { + nextSet.add(neighboringBlockY); + } + if ((neighboringBlockZ.getType() == Material.GRASS || neighboringBlockZ.getType() == Material.DIRT || neighboringBlockZ.getType() == Material.BARRIER)) { + nextSet.add(neighboringBlockZ); } } } @@ -119,12 +138,13 @@ private void barriuhFin() { } } - private void displaySummary() { + public void displaySummary() { // Display the block removal summary to the player Player player = currentRemovingPlayer; player.sendMessage(ChatColor.GREEN + "Removed " + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks and " + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + " dirt blocks."); // Display the block removal summary in the console - Bukkit.getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.GREEN + " removed " + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks and " + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + " dirt blocks."); + Bukkit.getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.GREEN + " removed " + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks, " + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + " dirt blocks and " + ChatColor.RED + barrierRemovedCount + ChatColor.GREEN + " barriers"); + blockRemovalActive = false; } } diff --git a/src/main/java/org/gecko/wauh/commands/SetRadiusLimitCommand.java b/src/main/java/org/gecko/wauh/commands/SetRadiusLimitCommand.java new file mode 100644 index 0000000..971c0e8 --- /dev/null +++ b/src/main/java/org/gecko/wauh/commands/SetRadiusLimitCommand.java @@ -0,0 +1,38 @@ +package org.gecko.wauh.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.gecko.wauh.Main; + +public class SetRadiusLimitCommand implements CommandExecutor { + private final Main plugin; // Reference to the Main class + + public SetRadiusLimitCommand(Main plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + + if (args.length == 1) { + // Check if the argument is an integer + try { + int newLimit = Integer.parseInt(args[0]); + plugin.setRadiusLimit(newLimit); // Use the setter method + player.sendMessage("Radius limit set to " + newLimit); + } catch (NumberFormatException e) { + player.sendMessage("Please specify a valid integer."); + } + } else { + player.sendMessage("Usage: /setradiuslimit "); + } + } else { + sender.sendMessage("Only players can use this command."); + } + return true; + } +} diff --git a/src/main/java/org/gecko/wauh/commands/StopWauh.java b/src/main/java/org/gecko/wauh/commands/StopWauh.java new file mode 100644 index 0000000..21daf12 --- /dev/null +++ b/src/main/java/org/gecko/wauh/commands/StopWauh.java @@ -0,0 +1,43 @@ +package org.gecko.wauh.commands; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.gecko.wauh.barriuh.BarrierListener; +import org.gecko.wauh.wauhbuck.BucketListener; + +public class StopWauh implements CommandExecutor { + private final BucketListener bucketListener; + private final BarrierListener barrierListener; + + public StopWauh(BucketListener bucketListener, BarrierListener barrierListener) { + this.bucketListener = bucketListener; + this.barrierListener = barrierListener; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "Only players can use this command."); + return true; + } + + Player player = (Player) sender; + + if (bucketListener.wauhRemovalActive) { + bucketListener.stopWaterRemoval = true; + player.sendMessage(ChatColor.GREEN + "Wauh removal " + ChatColor.RED + ChatColor.BOLD + "stopped."); + } + if (barrierListener.blockRemovalActive) { + barrierListener.stopBlockRemoval = true; + player.sendMessage(ChatColor.GREEN + "Block removal " + ChatColor.RED + ChatColor.BOLD + "stopped."); + } + if (!bucketListener.wauhRemovalActive && !barrierListener.blockRemovalActive) { + player.sendMessage(ChatColor.RED + "There are no block removals running"); + } + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/org/gecko/wauh/wauhbuck/BucketListener.java b/src/main/java/org/gecko/wauh/wauhbuck/BucketListener.java index 5efa971..d496f1c 100644 --- a/src/main/java/org/gecko/wauh/wauhbuck/BucketListener.java +++ b/src/main/java/org/gecko/wauh/wauhbuck/BucketListener.java @@ -5,10 +5,6 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -18,34 +14,25 @@ import java.util.HashSet; import java.util.Set; -public class BucketListener implements Listener, CommandExecutor { +public class BucketListener implements Listener { private int waterRemovedCount = 0; private int stationaryWaterRemovedCount = 0; private Set blocksToProcess = new HashSet<>(); private Player currentRemovingPlayer; private final Set replacedBlocks = new HashSet<>(); - private boolean stopWaterRemoval = false; + public boolean stopWaterRemoval = false; private Location clickedLocation; private boolean limitReached = false; - private int dist = 0; - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - stopWaterRemoval = true; - player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "Water removal" + ChatColor.RED + " stopped."); - displaySummary(); - } - return true; - } + private int highestDist = 0; + public boolean wauhRemovalActive = false; @EventHandler public void onBucketFill(PlayerBucketFillEvent event) { // Check if the bucket is filling with water if (event.getBlockClicked().getType() == Material.WATER || event.getBlockClicked().getType() == Material.STATIONARY_WATER) { if (event.getBucket() == Material.BUCKET) { + wauhRemovalActive = true; limitReached = false; event.getBlockClicked().setType(Material.BEDROCK); Player player = event.getPlayer(); @@ -54,6 +41,7 @@ public void onBucketFill(PlayerBucketFillEvent event) { // Reset the water removal counts and initialize the set of blocks to process waterRemovedCount = 0; stationaryWaterRemovedCount = 0; + highestDist = 0; blocksToProcess.clear(); currentRemovingPlayer = player; replacedBlocks.clear(); @@ -71,7 +59,7 @@ public void onBucketFill(PlayerBucketFillEvent event) { private void processWaterRemoval() { int radiusLimit = Main.getPlugin(Main.class).getRadiusLimit(); - String radiusLimitToString = String.valueOf(radiusLimit); + int realRadiusLimit = radiusLimit - 2; if (stopWaterRemoval) { stopWaterRemoval = false; displaySummary(); @@ -82,13 +70,25 @@ private void processWaterRemoval() { Set nextSet = new HashSet<>(); boolean limitReachedThisIteration = false; // Variable to track whether the limit was reached this iteration for (Block block : blocksToProcess) { - dist = (int) clickedLocation.distance(block.getLocation()); - if (dist > (radiusLimit + 2)) { + int dist = (int) clickedLocation.distance(block.getLocation()); + if (dist > radiusLimit) { limitReached = true; limitReachedThisIteration = true; } - - currentRemovingPlayer.sendMessage(ChatColor.RED + dist + ChatColor.WHITE + "/" + ChatColor.GREEN + radiusLimitToString); + if (dist > highestDist) { + if (highestDist <= (realRadiusLimit - 1)) { + highestDist = dist; + // Send a message to the player only when the dist value rises + if (highestDist < realRadiusLimit) { + currentRemovingPlayer.sendMessage(ChatColor.GREEN + "Wauh removal: " + ChatColor.RED + dist + ChatColor.WHITE+ "/" + ChatColor.GREEN + realRadiusLimit); + } else { + currentRemovingPlayer.sendMessage(ChatColor.GREEN + "Wauh removal: " + ChatColor.GREEN + dist + ChatColor.WHITE+ "/" + ChatColor.GREEN + realRadiusLimit); + } + } else { + limitReached = true; + limitReachedThisIteration = true; + } + } // Check if the block is water or stationary water if (block.getType() == Material.WATER) { @@ -104,10 +104,20 @@ private void processWaterRemoval() { replacedBlocks.add(block); // Iterate through neighboring blocks and add them to the next set - for (BlockFace face : BlockFace.values()) { - Block neighboringBlock = block.getRelative(face); - if ((neighboringBlock.getType() == Material.WATER || neighboringBlock.getType() == Material.STATIONARY_WATER)) { - nextSet.add(neighboringBlock); + for (int i = -1; i <= 1; i++) { + if (i == 0) continue; // Skip the current block + Block neighboringBlockX = block.getRelative(i, 0, 0); + Block neighboringBlockY = block.getRelative(0, i, 0); + Block neighboringBlockZ = block.getRelative(0, 0, i); + + if ((neighboringBlockX.getType() == Material.WATER || neighboringBlockX.getType() == Material.STATIONARY_WATER)) { + nextSet.add(neighboringBlockX); + } + if ((neighboringBlockY.getType() == Material.WATER || neighboringBlockY.getType() == Material.STATIONARY_WATER)) { + nextSet.add(neighboringBlockY); + } + if ((neighboringBlockZ.getType() == Material.WATER || neighboringBlockZ.getType() == Material.STATIONARY_WATER)) { + nextSet.add(neighboringBlockZ); } } } @@ -132,16 +142,18 @@ private void wauhFin() { Bukkit.getScheduler().runTaskLater(Main.getPlugin(Main.class), this::processWaterRemoval, 2L); } else { displaySummary(); + Bukkit.getScheduler().runTaskLater(Main.getPlugin(Main.class), this::removeReplacedBlocks, 20L); } } - private void displaySummary() { + public void displaySummary() { // Display the water removal summary to the player Player player = currentRemovingPlayer; player.sendMessage(ChatColor.GREEN + "Removed " + ChatColor.RED + waterRemovedCount + ChatColor.GREEN + " wauh blocks."); // Display the water removal summary in the console Bukkit.getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.GREEN + " removed " + ChatColor.RED + waterRemovedCount + ChatColor.GREEN + " flowing water blocks and " + ChatColor.RED + stationaryWaterRemovedCount + ChatColor.GREEN + " stationary water blocks."); + wauhRemovalActive = false; } private void removeReplacedBlocks() { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0d5d939..21753a1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,3 +1,11 @@ name: Wauh version: '${project.version}' main: org.gecko.wauh.Main +description: Bo o o wa uh +commands: + stopwauh: + description: Stop removing the wauh + usage: /stopwauhremoval + setradiuslimit: + description: Set the radius limit + usage: /setradiuslimit \ No newline at end of file