-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Hutch79/feature/restructure
Feature/restructure
- Loading branch information
Showing
7 changed files
with
226 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/ch/hutch79/commandExecutor/EventCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.