Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate getters and setters to their own class (SetAndGet) #67

Merged
merged 6 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: SonarCloud analysis
on:
push:
branches:
- 'branch/*'
- 'short-term/*'
- 'long-term/*'
- 'release/*'
- 'master'
pull_request:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Warning: This plugin has not yet been tested with a server with multiple people on it!

[![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-black.svg)](https://sonarcloud.io/summary/new_code?id=SmartGecko44_Spigot-Admin-Toys)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=SmartGecko44_Spigot-Admin-Toys&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=SmartGecko44_Spigot-Admin-Toys)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=SmartGecko44_Spigot-Admin-Toys&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=SmartGecko44_Spigot-Admin-Toys)
Expand Down
137 changes: 26 additions & 111 deletions src/main/java/org/gecko/wauh/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.java.JavaPlugin;
import org.gecko.wauh.commands.*;
import org.gecko.wauh.data.ConfigurationManager;
import org.gecko.wauh.enchantments.enchants.weapons.bows.*;
import org.gecko.wauh.enchantments.enchants.weapons.swords.Disarm;
import org.gecko.wauh.enchantments.logic.EnchantmentHandler;
import org.gecko.wauh.enchantments.tools.pickaxes.Drill;
import org.gecko.wauh.enchantments.tools.pickaxes.Smelt;
import org.gecko.wauh.gui.ConfigGUI;
import org.gecko.wauh.items.weapons.Shortbow;
import org.gecko.wauh.listeners.*;
import org.gecko.wauh.logic.IterateBlocks;
import org.gecko.wauh.logic.SetAndGet;

import java.lang.reflect.Field;

Expand All @@ -36,20 +34,8 @@ public final class Main extends JavaPlugin {
public static final Enchantment glow = new Glow(); // Id: 105
public static final Enchantment endanger = new Endanger(); // Id: 106
public static final Enchantment explosive = new Explosive(); // Id: 107
private final EnchantmentHandler enchantmentHandler = new EnchantmentHandler();
ConfigurationManager configManager;
FileConfiguration config;
private int playerRadiusLimit;
private int tntRadiusLimit;
private int creeperRadiusLimit;
private boolean showRemoval = true;
private BucketListener bucketListener;
private BarrierListener barrierListener;
private BedrockListener bedrockListener;
private WaterBucketListener waterBucketListener;
private TNTListener tntListener;
private CreeperListener creeperListener;
private IterateBlocks iterateBlocks;
private SetAndGet setAndGet;

public static void registerEnchantment(Enchantment enchantment) throws RegisterError {
try {
Expand All @@ -75,23 +61,25 @@ public static void registerEnchantment(Enchantment enchantment) throws RegisterE

@Override
public void onEnable() {
Shortbow shortbow;
// Plugin startup logic
Bukkit.getConsoleSender().sendMessage("");
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Yay");
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Spigot-Admin-Toys has been enabled!");

// Create instances of the listeners
bucketListener = new BucketListener(this);
barrierListener = new BarrierListener(this);
bedrockListener = new BedrockListener(this);
waterBucketListener = new WaterBucketListener(this);
tntListener = new TNTListener(this);
creeperListener = new CreeperListener(this);
configManager = new ConfigurationManager(this);
config = configManager.getConfig();
iterateBlocks = new IterateBlocks();
ConfigGUI configGUI = new ConfigGUI(this);
shortbow = new Shortbow();

TNTListener tntListener = new TNTListener(configManager);
CreeperListener creeperListener = new CreeperListener(configManager, this);

// Create instances of some misc classes
setAndGet = new SetAndGet(configManager, tntListener, creeperListener);

// Create instances of the listeners
BucketListener bucketListener = setAndGet.getBucketListener();
BarrierListener barrierListener = setAndGet.getBarrierListener();
BedrockListener bedrockListener = setAndGet.getBedrockListener();
WaterBucketListener waterBucketListener = setAndGet.getWaterBucketListener();
ConfigGUI configGUI = new ConfigGUI(setAndGet);
Shortbow shortbow = new Shortbow();


// Register the listeners
Expand Down Expand Up @@ -132,16 +120,16 @@ public void onEnable() {

// Register commands
this.getCommand("stopwauh").setExecutor(new StopWauh(bucketListener, barrierListener, bedrockListener, waterBucketListener));
this.getCommand("setradiuslimit").setExecutor(new SetRadiusLimitCommand(this));
this.getCommand("toggleremovalview").setExecutor(new ToggleRemovalView(this));
this.getCommand("setradiuslimit").setExecutor(new SetRadiusLimitCommand(setAndGet));
this.getCommand("toggleremovalview").setExecutor(new ToggleRemovalView(setAndGet));
this.getCommand("Test").setExecutor(new Test(configGUI));
this.getCommand("givecustomitems").setExecutor(new GiveCustomItems(this));
this.getCommand("ench").setExecutor(new Ench(this));
this.getCommand("givecustomitems").setExecutor(new GiveCustomItems());
this.getCommand("ench").setExecutor(new Ench(setAndGet));
this.getCommand("spawn").setExecutor(new Spawn());
// Register TabCompleters
this.getCommand("setradiuslimit").setTabCompleter(new SetRadiusLimitCommand(this));
this.getCommand("givecustomitems").setTabCompleter(new GiveCustomItems(this));
this.getCommand("ench").setTabCompleter(new Ench(this));
this.getCommand("setradiuslimit").setTabCompleter(new SetRadiusLimitCommand(setAndGet));
this.getCommand("givecustomitems").setTabCompleter(new GiveCustomItems());
this.getCommand("ench").setTabCompleter(new Ench(setAndGet));
this.getCommand("spawn").setTabCompleter(new Spawn());
}

Expand All @@ -151,80 +139,7 @@ public void onDisable() {
Enchantment.stopAcceptingRegistrations();
}

public int getRadiusLimit() {
config = configManager.getConfig();
playerRadiusLimit = config.getInt("playerRadiusLimit", playerRadiusLimit);
return playerRadiusLimit + 2;
}

public void setRadiusLimit(int newLimit) {
playerRadiusLimit = newLimit;
config.set("playerRadiusLimit", playerRadiusLimit);
configManager.saveConfig();
}

public int getTntRadiusLimit() {
config = configManager.getConfig();
tntRadiusLimit = config.getInt("tntRadiusLimit", tntRadiusLimit);
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "TNT Radius Limit: " + tntRadiusLimit);
return tntRadiusLimit + 2;
}

public void setTntRadiusLimit(int newLimit) {
tntRadiusLimit = newLimit;
config.set("tntRadiusLimit", tntRadiusLimit);
configManager.saveConfig();
}

public int getCreeperRadiusLimit() {
config = configManager.getConfig();
creeperRadiusLimit = config.getInt("creeperRadiusLimit", creeperRadiusLimit);
return creeperRadiusLimit + 2;
}

public void setCreeperLimit(int newLimit) {
creeperRadiusLimit = newLimit;
config.set("creeperRadiusLimit", creeperRadiusLimit);
configManager.saveConfig();
}

public boolean getShowRemoval() {
return showRemoval;
}

public void setRemovalView(boolean newShowRemoval) {
showRemoval = newShowRemoval;
}

public BucketListener getBucketListener() {
return bucketListener;
}

public BarrierListener getBarrierListener() {
return barrierListener;
}

public BedrockListener getBedrockListener() {
return bedrockListener;
}

public WaterBucketListener getWaterBucketListener() {
return waterBucketListener;
}

public TNTListener getTntListener() {
return tntListener;
}

public CreeperListener getCreeperListener() {
return creeperListener;
}

public EnchantmentHandler getEnchantmentHandler() {
return enchantmentHandler;
}

public IterateBlocks getIterateBlocks() {
return iterateBlocks;
public SetAndGet getSetAndGet() {
return setAndGet;
}
}
10 changes: 5 additions & 5 deletions src/main/java/org/gecko/wauh/commands/Ench.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.gecko.wauh.Main;
import org.gecko.wauh.enchantments.logic.EnchantmentHandler;
import org.gecko.wauh.logic.SetAndGet;

import java.util.ArrayList;
import java.util.List;

public class Ench implements CommandExecutor, TabCompleter {

private final Main plugin;
private final SetAndGet setAndGet;

public Ench(Main plugin) {
this.plugin = plugin;
public Ench(SetAndGet setAndGet) {
this.setAndGet = setAndGet;
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Expand All @@ -36,7 +36,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
String operation = args[0].toLowerCase();
ItemStack enchItem = senderPlayer.getInventory().getItemInMainHand();
String enchantmentNameFinal = operation.substring(0, 1).toUpperCase() + operation.substring(1);
EnchantmentHandler enchantmentHandler = plugin.getEnchantmentHandler();
EnchantmentHandler enchantmentHandler = setAndGet.getEnchantmentHandler();

if (!enchantmentHandler.getEnchantmentExists(enchantmentNameFinal)) {
sender.sendMessage("This enchantment does not exist");
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/gecko/wauh/commands/GiveCustomItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.gecko.wauh.Main;
import org.gecko.wauh.items.TriggerItems;
import org.gecko.wauh.items.weapons.Shortbow;

Expand All @@ -21,11 +20,6 @@ public class GiveCustomItems implements CommandExecutor, TabCompleter {
public static final String BEDROCK = "bedrock";
public static final String TSUNAMI = "tsunami";
public static final String SHORTBOW = "shortbow";
private final Main plugin;

public GiveCustomItems(Main plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/gecko/wauh/commands/SetRadiusLimitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.gecko.wauh.Main;
import org.gecko.wauh.logic.SetAndGet;

import java.util.ArrayList;
import java.util.List;

public class SetRadiusLimitCommand implements CommandExecutor, TabCompleter {
public static final String PLAYER = "player";
public static final String CREEPER = "creeper";
private final Main plugin; // Reference to the Main class
private final SetAndGet setAndGet; // Reference to the Main class

public SetRadiusLimitCommand(Main plugin) {
this.plugin = plugin;
public SetRadiusLimitCommand(SetAndGet setAndGet) {
this.setAndGet = setAndGet;
}

@Override
Expand All @@ -42,13 +42,13 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}

if (operation.equals("tnt")) {
plugin.setTntRadiusLimit(newLimit); // Use the setter method for TNT operations
setAndGet.setTntRadiusLimit(newLimit); // Use the setter method for TNT operations
player.sendMessage("TNT radius set to " + newLimit);
} else if (operation.equals(PLAYER)) {
plugin.setRadiusLimit(newLimit); // Use the setter method for player operations
setAndGet.setRadiusLimit(newLimit); // Use the setter method for player operations
player.sendMessage("Player operation limit set to " + newLimit);
} else {
plugin.setCreeperLimit(newLimit);
setAndGet.setCreeperLimit(newLimit);
player.sendMessage("Creeper radius limit set to " + newLimit);
}
} catch (NumberFormatException e) {
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/org/gecko/wauh/commands/Spawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,10 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
String entity = args[0].toUpperCase();
try {
int amount = Integer.parseInt(args[1]);
int radius = 0; // Default radius if not specified

if (args.length == 3) {
// If radius is specified, use it
radius = Integer.parseInt(args[2]);
} else {
// Automatically adjust radius to avoid entity cramming limit
int maxEntities = 24;
radius = Math.max(radius, (int) Math.ceil(Math.sqrt(amount / Math.PI)));
radius = Math.max(radius, (int) Math.ceil(Math.sqrt(maxEntities / Math.PI)));
}
int radius = getRadius(args, amount);

if (amount < 1 || radius < 0) {
sender.sendMessage("The amount must be positive, and the radius must be a non-negative value.");
return true;
} else {
EntityType entityType = EntityType.valueOf(entity);

Expand All @@ -47,12 +36,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
player.getWorld().spawnEntity(player.getLocation().add(x, 0, z), entityType);
}
sender.sendMessage("Spawned " + amount + " " + entity.toLowerCase() + "s with a radius of " + radius + ".");
return true;
} else {
sender.sendMessage("This entity cannot be spawned.");
return true;
}
}
return true;
} catch (NumberFormatException e) {
sender.sendMessage("Please specify valid integers for amount and radius.");
return true;
Expand All @@ -69,6 +57,21 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
}

private static int getRadius(String[] args, int amount) {
int radius = 0; // Default radius if not specified

if (args.length == 3) {
// If radius is specified, use it
radius = Integer.parseInt(args[2]);
} else {
// Automatically adjust radius to avoid entity cramming limit
int maxEntities = 24;
radius = Math.max(radius, (int) Math.ceil(Math.sqrt(amount / Math.PI)));
radius = Math.max(radius, (int) Math.ceil(Math.sqrt(maxEntities / Math.PI)));
}
return radius;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
List<String> completions = new ArrayList<>();
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/gecko/wauh/commands/ToggleRemovalView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.gecko.wauh.Main;
import org.gecko.wauh.logic.SetAndGet;

public class ToggleRemovalView implements CommandExecutor {
private final Main plugin;
private final SetAndGet setAndGet;

public ToggleRemovalView(Main plugin) {
this.plugin = plugin;
public ToggleRemovalView(SetAndGet setAndGet) {
this.setAndGet = setAndGet;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player player) {
boolean showRemoval = plugin.getShowRemoval();
plugin.setRemovalView(!showRemoval);
if (!plugin.getShowRemoval()) {
player.sendMessage("Removal visibility set to " + ChatColor.RED + "false");
} else {
player.sendMessage("Removal visibility set to " + ChatColor.GREEN + "true");
}
setAndGet.toggleRemovalView();
player.sendMessage("Removal visibility set to " + (!setAndGet.getShowRemoval() ? ChatColor.RED + "false" : ChatColor.GREEN + "true"));
}
return true;
}
Expand Down
Loading
Loading