Skip to content

Commit

Permalink
Merge pull request #8 from Hutch79/feature/restructure
Browse files Browse the repository at this point in the history
Feature/restructure
  • Loading branch information
Hutch79 authored Sep 10, 2023
2 parents ec020c4 + 47ac2c1 commit d02ef82
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 183 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ch.hutch79</groupId>
<artifactId>F-Command</artifactId>
<version>2.5.1</version>
<version>2.5.2</version>
<packaging>jar</packaging>

<name>F-Command</name>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/ch/hutch79/FCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ch.hutch79.command.Command;
import ch.hutch79.command.CommandTab;
import ch.hutch79.utility.EventListener;
import ch.hutch79.events.EventHandler;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
Expand All @@ -14,22 +14,22 @@
public final class FCommand extends JavaPlugin {
PluginDescriptionFile pdf = this.getDescription();
private static FCommand instance;
private static EventListener eventListener;
private static EventHandler eventHandler;
private boolean isPlaceholderApiInstalled = false;
private static boolean debug;

@Override
public void onEnable() {
instance = this;

eventListener = new EventListener();
eventHandler = new EventHandler();

// getConfig().options().copyDefaults();
saveDefaultConfig();
reloadConfig();

eventListener.EventListenerInit();
Bukkit.getPluginManager().registerEvents(eventListener, this);
eventHandler.EventListenerInit();
Bukkit.getPluginManager().registerEvents(eventHandler, this);

Objects.requireNonNull(getCommand("fcommand")).setExecutor(new Command());
Objects.requireNonNull(getCommand("fcommand")).setTabCompleter(new CommandTab());
Expand Down Expand Up @@ -95,8 +95,8 @@ public static FCommand getInstance() {
return instance;
}

public static EventListener getListener() {
return eventListener;
public static EventHandler getListener() {
return eventHandler;
}

public static boolean getDebug() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/ch/hutch79/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ch.hutch79.FCommand;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class Command implements CommandExecutor {
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/ch/hutch79/commandExecutor/EventCommandExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package ch.hutch79.commandExecutor;

import ch.hutch79.FCommand;
import ch.hutch79.utility.ConfigManager;
import ch.hutch79.utility.Debugger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;

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

public class EventCommandExecutor {
private final ConfigManager configManager;
private final List<String> commandOptions;
public EventCommandExecutor(List<String> commandOptions) {
this.commandOptions = commandOptions;
configManager = new ConfigManager(this.commandOptions);
}

public boolean commandExecutor(Player player, Event event) {
String eventKey = "";

if (Objects.equals(event.getEventName(), "PlayerSwapHandItemsEvent")) {
eventKey = "f";
} else if (Objects.equals(event.getEventName(), "PlayerDropItemEvent")) {
eventKey = "q";
}

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


Debugger.debug("Event while count: §e" + (count));
Debugger.debug("Event while current command: §e" + commandOptions.get(count));


if (!configManager.getInfo(count, "key").equalsIgnoreCase(eventKey)) { // Correct Key was pressed?
Debugger.debug("return key - §e" + commandOptions.get(count));
continue;
}

if (!configManager.getInfo(count, "permission").equalsIgnoreCase("none")) { // Permission is set?
if (!player.hasPermission(configManager.getInfo(count, "permission"))) { // Correct Permission?
Debugger.debug("return permission - §e" + commandOptions.get(count));
continue;
}
}

if (configManager.getInfo(count, "requireShift").equalsIgnoreCase("true")) {
if (!player.isSneaking()) {
Debugger.debug("return sneaking 1 - §e" + commandOptions.get(count));
continue;
}
} else if (configManager.getInfo(count, "requireShift").equalsIgnoreCase("false")) {
if (player.isSneaking()) {
Debugger.debug("return sneaking 2 - §e" + commandOptions.get(count));
continue;
}
}

List<String> commandsList;
String commandString = configManager.getInfo(count, "command");

if (commandString.charAt(0) == '[' && commandString.charAt(commandString.length() - 1) == ']') { // Check if first and last character are `[` and `]`
commandsList = configManager.getCfg().getStringList("command." + commandOptions.get(count) + "." + "command");
} else {
commandsList = new ArrayList<>(1);
commandsList.add(commandString);
}

if (configManager.getInfo(count, "executeAsServer").equalsIgnoreCase("true")) {
for (String i : commandsList) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), FCommand.getInstance().replacePlaceholders(player, i));
}

Debugger.debug("Executed by Server - §e" + commandOptions.get(count));
} else {
for (String i : commandsList) {
player.performCommand(FCommand.getInstance().replacePlaceholders(player, i));
}
Debugger.debug("Executed by Player - §e" + commandOptions.get(count));
}


if (configManager.getInfo(count, "cancel").equalsIgnoreCase("true")) {

if (eventKey.equalsIgnoreCase("f")) {
return true;
} else if (eventKey.equalsIgnoreCase("q")) {
return true;
}
Debugger.debug("event canceled - §e" + commandOptions.get(count));
}
}
return false;
}
}
65 changes: 65 additions & 0 deletions src/main/java/ch/hutch79/events/EventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ch.hutch79.events;

import ch.hutch79.FCommand;
import ch.hutch79.commandExecutor.EventCommandExecutor;
import ch.hutch79.utility.Debugger;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;

import java.io.File;
import java.util.*;

public class EventHandler implements Listener {

private final FCommand mainInstance = FCommand.getInstance();
private EventCommandExecutor eventCommandExecutor;

FileConfiguration cfg;

public void EventListenerInit() {
mainInstance.reloadConfig();
Set<String> commandOptions2 = Objects.requireNonNull(FCommand.getInstance().getConfig().getConfigurationSection("command")).getKeys(false);
List<String> commandOptions = new ArrayList<>(commandOptions2.size());
commandOptions.addAll(commandOptions2);
FCommand.setDebug(mainInstance.getConfig().getBoolean("debug"));
Debugger.debug("commandOptions list: §e" + commandOptions);

cfg = YamlConfiguration.loadConfiguration(new File("plugins" + File.separator + "F-Command", "config.yml"));

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

eventCommandExecutor = new EventCommandExecutor(commandOptions);
}

@org.bukkit.event.EventHandler
private void onSwapHandItemsEvent(PlayerSwapHandItemsEvent e) {
Debugger.debug("PlayerSwapHandItemsEvent detected");
if (eventCommandExecutor.commandExecutor(e.getPlayer(), e))
e.setCancelled(true);
}
private Boolean ignoreEvent = false;
@org.bukkit.event.EventHandler
private void inventoryClickEvent(InventoryClickEvent e) {
if (e.getSlotType() != InventoryType.SlotType.valueOf("OUTSIDE")) {
Debugger.debug("Event ignored, not OUTSIDE");
ignoreEvent = true;
}
}

@org.bukkit.event.EventHandler
private void dropItemEvent(PlayerDropItemEvent e) {
Debugger.debug("PlayerDropItemEvent detected: " + e.getPlayer());
if (!ignoreEvent) {
Debugger.debug("It was Q");
if (eventCommandExecutor.commandExecutor(e.getPlayer(), e))
e.setCancelled(true);
}
ignoreEvent = false;
}
}
55 changes: 55 additions & 0 deletions src/main/java/ch/hutch79/utility/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ch.hutch79.utility;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

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

public class ConfigManager {
private final List<String> commandOptions;
private FileConfiguration cfg;

public ConfigManager(List<String> commandOptions) {
this.commandOptions = commandOptions;
}

public String getInfo(int count, String value) {

cfg = YamlConfiguration.loadConfiguration(new File("plugins" + File.separator + "F-Command", "config.yml"));
String result = cfg.getString("command." + commandOptions.get(count) + "." + value);
if (result == null) {
Debugger.debug("The Value " + value + " for the Command " + commandOptions.get(count) + " is not set!");

if (value.equals("key")) {
return "f";
}

if (value.equals("permission")) {
return "none";
}

if (value.equals("requireShift")) {
return "true";
}

if (value.equals("cancel")) {
return "false";
}

if (value.equals("executeAsServer")) {
return "false";
}

if (value.equals("command")) {
return "say hi, im a default command. Please edit the config.yml to set your own command.";
}
}

return result;
}

public FileConfiguration getCfg () {
return cfg;
}
}
Loading

0 comments on commit d02ef82

Please sign in to comment.