Skip to content

Commit

Permalink
Overhaul of main functions. (#5)
Browse files Browse the repository at this point in the history
* Update incorrect spelling and grammar
* Change first while loop to for loop. (This improves speed and heat on the server)
* Change version to beta.2
* updated `getInfo()` method to be private
* Changed around the second loop breaking to just use `continue;`
* Removed useless if check
* Change Debugging to be a class and method instead of an endless if check

Huge thanks to @n-tdi for giving me his feedback!
It was a huge help to improve my coding quality!
  • Loading branch information
n-tdi authored Mar 14, 2023
1 parent f06f4b5 commit ecb6c01
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

<groupId>ch.hutch79</groupId>
<artifactId>F-Command</artifactId>
<version>2.2</version>
<version>beta.2</version>
<packaging>jar</packaging>

<name>F-Command</name>

<description>F-Command executes a command if the Player swiches his/here offhand</description>
<description>F-Command executes a command when a Player switches his/her offhand</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/ch/hutch79/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm

if (args.length != 1) return false;

if (sender instanceof Player) {
if (!sender.hasPermission("fcommand.admin")) {
sender.sendMessage("§dF-Command §8> §cYou don't have Permission to execute this command");
return false;
}
if (!sender.hasPermission("fcommand.admin")) {
sender.sendMessage("§dF-Command §8> §cYou don't have Permission to execute this command");
return false;
}


if (args[0].equalsIgnoreCase("reload")) {
FCommand.getListener().EventListenerInit();
sender.sendMessage("§dF-Command §8> §7Config has been reloaded");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ch/hutch79/Debugger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.hutch79;

import org.bukkit.Bukkit;

public final class Debugger {
public static void debug(String message) {
if (FCommand.isDebug()) Bukkit.getConsoleSender().sendMessage(message);
}
}
73 changes: 32 additions & 41 deletions src/main/java/ch/hutch79/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@
public class EventListener implements Listener {

private final FCommand mainInstance = FCommand.getInstance();
private boolean debug = false;
public List<String> commandOptions;

public void EventListenerInit() {
mainInstance.reloadConfig();
Set<String> commandOptions2 = Objects.requireNonNull(FCommand.getInstance().getConfig().getConfigurationSection("command")).getKeys(false);
commandOptions = new ArrayList<>(commandOptions2.size());
commandOptions.addAll(commandOptions2);
debug = mainInstance.getConfig().getBoolean("debug");
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7commandOptions list: §e" + commandOptions);}
Debugger.debug("§cFcmd-debug §8> §7commandOptions list: §e" + commandOptions);

Bukkit.getConsoleSender().sendMessage("§dF-Command §8> §7Loaded Commands: " + commandOptions);
}

public String getInfo(int count, String value){
private String getInfo(int count, String value){

String result = mainInstance.getConfig().getString("command." + commandOptions.get(count-1) + "." + value);
String result = mainInstance.getConfig().getString("command." + commandOptions.get(count) + "." + value);

if(result == null) {
mainInstance.getLogger().warning("The Value " + value + " for the Command " + commandOptions.get(count) + " is not set!");
Expand All @@ -39,55 +37,48 @@ public String getInfo(int count, String value){
@EventHandler
public void onSwapHandItemsEvent(PlayerSwapHandItemsEvent e) {

if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7PlayerSwapHandItemsEvent detected");}
Debugger.debug("§cFcmd-debug §8> §7PlayerSwapHandItemsEvent detected");

Player player = e.getPlayer();

int count = 0;
while (count < commandOptions.size()) {
count++;
for (int count = 0; count < commandOptions.size(); count++) {

int count1 = 0;
while (count1 < 1) {
count1++;
Debugger.debug("§cFcmd-debug §8> §7Event while count: §e" + (count));
Debugger.debug("§cFcmd-debug §8> §7Event while current command: §e" + commandOptions.get(count));

if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7Event while count: §e" + (count-1));}
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7Event while current command: §e" + commandOptions.get(count-1));}


if (!getInfo(count, "permission").equalsIgnoreCase("None")) { // Correct Permission?
if (!player.hasPermission(getInfo(count, "permission"))) {
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7return permission - §e" + commandOptions.get(count-1));}
break;
}
if (!getInfo(count, "permission").equalsIgnoreCase("None")) { // Correct Permission?
if (!player.hasPermission(getInfo(count, "permission"))) {
Debugger.debug("§cFcmd-debug §8> §7return permission - §e" + commandOptions.get(count));
continue;
}
}

if (getInfo(count, "requireShift").equalsIgnoreCase("true")) {
if (!player.isSneaking()) {
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7return sneaking 1 - §e" + commandOptions.get(count-1));}
break;
}
} else if (getInfo(count, "requireShift").equalsIgnoreCase("false")) {
if (player.isSneaking()) {
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7return sneaking 2 - §e" + commandOptions.get(count-1));}
break;
}
if (getInfo(count, "requireShift").equalsIgnoreCase("true")) {
if (!player.isSneaking()) {
Debugger.debug("§cFcmd-debug §8> §7return sneaking 1 - §e" + commandOptions.get(count));
continue;
}
} else if (getInfo(count, "requireShift").equalsIgnoreCase("false")) {
if (player.isSneaking()) {
Debugger.debug("§cFcmd-debug §8> §7return sneaking 2 - §e" + commandOptions.get(count));
continue;
}
}


if (getInfo(count, "cancel").equalsIgnoreCase("true")) {
e.setCancelled(true);
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7event canceled - §e" + commandOptions.get(count-1));}
}
if (getInfo(count, "cancel").equalsIgnoreCase("true")) {
e.setCancelled(true);
Debugger.debug("§cFcmd-debug §8> §7event canceled - §e" + commandOptions.get(count));
}


if (getInfo(count, "executeAsServer").equalsIgnoreCase("true")) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), mainInstance.replacePlaceholders(player,getInfo(count, "command")));
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7Executed by Server - §e" + commandOptions.get(count-1));}
} else {
player.performCommand(mainInstance.replacePlaceholders(player,getInfo(count, "command")));
if (debug) {Bukkit.getConsoleSender().sendMessage("§cFcmd-debug §8> §7Executed by Player - §e" + commandOptions.get(count-1));}
}
if (getInfo(count, "executeAsServer").equalsIgnoreCase("true")) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), mainInstance.replacePlaceholders(player,getInfo(count, "command")));
Debugger.debug("§cFcmd-debug §8> §7Executed by Server - §e" + commandOptions.get(count));
} else {
player.performCommand(mainInstance.replacePlaceholders(player,getInfo(count, "command")));
Debugger.debug("§cFcmd-debug §8> §7Executed by Player - §e" + commandOptions.get(count));
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ch/hutch79/FCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class FCommand extends JavaPlugin {
private static FCommand instance;
private static EventListener eventListener;
private boolean isPlaceholderApiInstalled = false;
private static boolean debug;

@Override
public void onEnable() {
Expand All @@ -30,6 +31,9 @@ public void onEnable() {
Objects.requireNonNull(getCommand("fcommand")).setTabCompleter(new CommandTab());

Metrics metrics = new Metrics(this, 17738); // bStats

debug = getConfig().getBoolean("debug");

final int SPIGOT_RESOURCE_ID = 108009; // Update checker

new UpdateChecker(this, UpdateCheckSource.SPIGET, "" + SPIGOT_RESOURCE_ID + "")
Expand Down Expand Up @@ -91,6 +95,10 @@ public static EventListener getListener() {
return eventListener;
}

public static boolean isDebug() {
return debug;
}

public String replacePlaceholders(Player player, String input) {
if(isPlaceholderApiInstalled) {
return me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, input);
Expand Down

0 comments on commit ecb6c01

Please sign in to comment.