Skip to content

Commit

Permalink
Better action log #32
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock authored Sep 25, 2023
2 parents f5f5ea4 + 3075501 commit 96684eb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class ActionSubCommand implements SubCommand {
private final MessageManager message;
private final PluginsManager manager;
private final List<String> actions;
private static final String ACTION_TAG = "action";

@Contract(pure = true)
public ActionSubCommand(@NotNull PluginControl plugin) {
Expand All @@ -30,22 +29,26 @@ public ActionSubCommand(@NotNull PluginControl plugin) {
@Override
public void execute(CommandSender sender, Command command, String label, String @NotNull [] args) {
if (args.length == 0 || args[0].isBlank()) {
message.send(sender, message.getActionType(), Placeholder.parsed(ACTION_TAG, config.getAction().toLowerCase()));
message.send(sender, message.getActionType(), Placeholder.parsed("action", config.getAction().toLowerCase()));
return;
}

try {
var actiontype = ConfigManager.ActionType.from(args[0].toLowerCase());
config.setAction(actiontype);
message.send(sender, message.getActionSet(), Placeholder.parsed(ACTION_TAG, actiontype.getAction()));
var actionType = ConfigManager.ActionType.from(args[0].toLowerCase());
config.setAction(actionType);
message.send(sender, message.getActionSet(), Placeholder.parsed("action", actionType.getAction()));
if (actionType != ConfigManager.ActionType.DISALLOW_PLAYER_LOGIN) {
manager.unregisterListener();
}
manager.checkPlugins();
} catch (IllegalArgumentException e) {
message.send(sender, message.getActionTypeList(), Placeholder.parsed(ACTION_TAG, String.join(", ", actions)));
message.send(sender, message.getActionTypeList(), Placeholder.parsed("actions", String.join(", ", actions)));
}
}

@Override
public List<String> tabComplete(CommandSender sender, Command command, String label, String[] args) {
if (args.length != 1) return List.of();
return actions.stream().filter(s -> s.startsWith(args[0])).toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public void send(@NotNull CommandSender sender, @NotNull List<String> message, @
}

public @NotNull Component getPluginListComponent(@NotNull Set<String> pluginList) {
var joinConfiguration = JoinConfiguration.separators(MM.deserialize(getPluginListSeparator()),
var joinConfiguration = JoinConfiguration.separators(
MM.deserialize(getPluginListSeparator()),
MM.deserialize(getPluginListSeparatorLast()));

var componentList = new ArrayList<Component>();
Expand All @@ -120,6 +121,24 @@ public void send(@NotNull CommandSender sender, @NotNull List<String> message, @
return Component.join(joinConfiguration, componentList);
}

public @NotNull Component getGroupListComponent(@NotNull Set<String> groupList) {
var joinConfiguration = JoinConfiguration.separators(
MM.deserialize(getPluginListSeparator()),
MM.deserialize(getPluginListSeparatorLast()));

var componentList = new ArrayList<Component>();
if (!groupList.isEmpty()) {
var command = "/plugincontrol group delete %s";
for (var groupName : groupList) {
componentList.add(MM.deserialize(groupName)
.hoverEvent(HoverEvent.showText(MM.deserialize(getGroupClickDelete())))
.clickEvent(ClickEvent.runCommand(command.formatted(groupName))));
}
}

return Component.join(joinConfiguration, componentList);
}

public @NotNull Component getGroupListComponent(@NotNull Map<String, Set<String>> pluginGroups) {
if (pluginGroups.isEmpty()) return MM.deserialize(getGroupListEmpty());

Expand All @@ -132,14 +151,16 @@ public void send(@NotNull CommandSender sender, @NotNull List<String> message, @
for (var groupEntry : sortedGroups) {
var groupName = groupEntry.getKey();

componentList.add(Component.newline().append(MM.deserialize(getGroupListName(), Placeholder.parsed("group", groupName))
componentList.add(Component.newline().append(MM.deserialize(getGroupListName(),
Placeholder.parsed("group", groupName))
.hoverEvent(HoverEvent.showText(MM.deserialize(getGroupClickInfo())))
.clickEvent(ClickEvent.runCommand(groupCommand.formatted(groupName)))));

var plugins = groupEntry.getValue().stream()
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toCollection(TreeSet::new));
var pluginComponents = new ArrayList<Component>();

if (!plugins.isEmpty()) {
var joinConfiguration = JoinConfiguration.builder()
.separator(MM.deserialize(getPluginListSeparator()))
Expand Down Expand Up @@ -273,8 +294,8 @@ public String getCheckFinished() {
return lang.getString("console.finished-checking");
}

public String getLogToConsole() {
return lang.getString("console.log-to-console");
public String getLogToConsolePlugin() {
return lang.getString("console.log-to-console-plugin");
}

public String getLogToConsoleGroup() {
Expand Down Expand Up @@ -377,6 +398,10 @@ public String getGroupListName() {
return lang.getString("command.group-list-name");
}

private String getGroupClickDelete() {
return lang.getString("command.group-click-delete");
}

public String getGroupClickRemovePlugin() {
return lang.getString("command.group-click-remove-plugin");
}
Expand Down
57 changes: 43 additions & 14 deletions src/main/java/com/armamc/plugincontrol/managers/PluginsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.armamc.plugincontrol.PluginControl;
import com.armamc.plugincontrol.listeners.PlayerListener;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.event.player.PlayerLoginEvent;
Expand Down Expand Up @@ -36,6 +37,7 @@ public void checkPlugins() {
}
}

var missingGroups = new HashSet<String>();
var pluginGroup = config.getPluginGroups();
for (var groups : pluginGroup.entrySet()) {
boolean groupHasEnabledPlugin = false;
Expand All @@ -48,33 +50,60 @@ public void checkPlugins() {
}

if (!groupHasEnabledPlugin) {
missingPlugins.add(message.getLogToConsoleGroup().replace("<group>", groups.getKey()));
missingGroups.add(groups.getKey());
}
}

if (!missingPlugins.isEmpty()) {
registerAction(missingPlugins);
if (!missingPlugins.isEmpty() || !missingGroups.isEmpty()) {
registerAction(missingPlugins, missingGroups);
} else {
message.send(console, message.getCheckFinished());
}
}

private void registerAction(Set<String> missingPlugins) {
var tag = Placeholder.component("plugins", message.getPluginListComponent(new HashSet<>(missingPlugins)));
if (config.getAction().equalsIgnoreCase(ConfigManager.ActionType.DISALLOW_PLAYER_LOGIN.getAction())) {
private void registerAction(@NotNull Set<String> missingPlugins, @NotNull Set<String> missingGroups) {
TagResolver.Single pluginTag = null;
if (!missingPlugins.isEmpty()) {
pluginTag = Placeholder.component("plugins", message.getPluginListComponent(missingPlugins));
}

TagResolver.Single groupTag = null;
if (!missingGroups.isEmpty()) {
groupTag = Placeholder.component("groups", message.getGroupListComponent(missingGroups));
}

switch (ConfigManager.ActionType.from(config.getAction().toLowerCase())) {
case DISALLOW_PLAYER_LOGIN -> handleDisallowPlayerLogin(pluginTag, groupTag);
case LOG_TO_CONSOLE -> logToConsole(pluginTag, groupTag);
case SHUTDOWN_SERVER -> shutdownServer(pluginTag, groupTag);
default -> throw new IllegalArgumentException("Unknown action: %s".formatted(config.getAction()));
}
}

private void handleDisallowPlayerLogin(TagResolver.Single pluginTag, TagResolver.Single groupTag) {
if (playerListener == null) {
playerListener = new PlayerListener(plugin);
playerListener.init();
message.send(console, message.getLogToConsole(), tag);
return;
}
if (config.getAction().equalsIgnoreCase(ConfigManager.ActionType.LOG_TO_CONSOLE.getAction())) {
message.send(console, message.getLogToConsole(), tag);
return;

logToConsole(pluginTag, groupTag);
}

private void shutdownServer(TagResolver.Single pluginTag, TagResolver.Single groupTag) {
logToConsole(pluginTag, groupTag);
message.send(console, message.getDisablingServer());
plugin.getServer().shutdown();
}

private void logToConsole(TagResolver.Single pluginTag, TagResolver.Single groupTag) {
if (pluginTag != null) {
message.send(console, message.getLogToConsolePlugin(), pluginTag);
}
if (config.getAction().equalsIgnoreCase(ConfigManager.ActionType.SHUTDOWN_SERVER.getAction())) {
message.send(console, message.getDisablingServer(), tag);
plugin.getServer().shutdown();
if (groupTag != null) {
message.send(console, message.getLogToConsoleGroup(), groupTag);
}

message.send(console, message.getCheckFinished());
}

public void unregisterListener() {
Expand Down
9 changes: 5 additions & 4 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ prefix: '<dark_gray>[<red>PluginControl<dark_gray>]'
kick-message: '<red>[PluginControl] You are not allowed to join the server!'
console:
checking-plugins: '<prefix> <red>Checking plugins...'
disabling-server: '<prefix> <red>Disabling server because <yellow><plugins> <red>was not found or enabled!'
disabling-server: '<prefix> <red>Disabling server because plugins or groups were not found or enabled!'
finished-checking: '<prefix> <green>Plugins verified!'
log-to-console: '<prefix> <red>Plugin <yellow><plugins> <red>not found or enabled...'
log-to-console-group: '<yellow>from the group <red><group>'
log-to-console-plugin: '<prefix> <red>Required plugins were not found: <plugins>'
log-to-console-group: '<prefix> <red>Required groups were not found: <groups>'
command:
action-list: '<prefix> <green>Actions available: <yellow><actions>'
action-set: '<prefix> <green>Action set to <yellow><action>'
Expand Down Expand Up @@ -49,10 +49,11 @@ command:
group-has-no-plugins: '<prefix> <red>Group <yellow><group> <red>has no plugins!'
group-plugin-list: '<prefix> <yellow>Plugins in group <green><group><yellow>: <yellow><plugins>'
group-list-name: '<yellow>Group <group>:'
group-click-delete: '<red>Click to delete the group'
group-click-remove-plugin: '<red>Click to remove the plugin from the group'
group-click-info: '<yellow>Click to see the group info'
group-help:
- '<gradient:aqua:green>==== Plugin Control Group Help ====</gradient>'
- '<gradient:aqua:green>=== Plugin Control Group Help ===</gradient>'
- '<aqua>/<command> group create <green><group> <yellow>- Create a group'
- '<aqua>/<command> group delete <green><group> <yellow>- Remove a group'
- '<aqua>/<command> group list <yellow>- List all required groups'
Expand Down

0 comments on commit 96684eb

Please sign in to comment.