Skip to content

Commit

Permalink
Merge pull request #23 from TheNextLvl-net/reload
Browse files Browse the repository at this point in the history
Add /command reload and /command save
  • Loading branch information
NonSwag authored Aug 10, 2024
2 parents 9266361 + 630f590 commit 9372453
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.thenextlvl.commander;

import net.kyori.adventure.audience.Audience;

import java.util.Set;

public interface CommandRegistry {
Expand All @@ -20,4 +22,6 @@ public interface CommandRegistry {
boolean unregister(String command);

void unregisterCommands();

boolean reload(Audience audience);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.thenextlvl.commander;

import net.kyori.adventure.audience.Audience;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
Expand All @@ -22,4 +23,6 @@ public interface PermissionOverride {
boolean reset(String command);

void overridePermissions();

boolean reload(Audience audience);
}
4 changes: 4 additions & 0 deletions api/src/main/resources/commander.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ command.registered=<prefix> <white>All commands matching <green><command> <white
command.hidden=<prefix> <white>All commands matching <green><command> <white>are now hidden
command.revealed=<prefix> <white>All commands matching <green><command> <white>can be seen again
command.reset=<prefix> <white>The command <green><command> <white>has been reset
command.reload.failed=<prefix> <hover:show_text:'<error>'><red>Failed to reloaded command files</hover>
command.reload.success=<prefix> <white>Successfully reloaded command files
command.reload.changes=<prefix> <green><additions> <white>additions and <red><deletions> <white>deletions <gray>(<file>)
command.saved=<prefix> <white>Successfully saved command files
command.unknown=<prefix> <red>The command <dark_gray>(<dark_red><command><dark_gray>) <red>does not exist
nothing.changed=<prefix> <red>Nothing could be changed
4 changes: 4 additions & 0 deletions api/src/main/resources/commander_german.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ command.registered=<prefix> <white>Alle Befehle die <green><command> <white>glei
command.hidden=<prefix> <white>Alle Befehle die <green><command> <white>gleichen, wurden versteckt
command.revealed=<prefix> <white>Alle Befehle die <green><command> <white>gleichen, können wieder gesehen werden
command.reset=<prefix> <white>Der Befehl <green><command> <white>wurde zurück gesetzt
command.reload.failed=<prefix> <hover:show_text:'<error>'><red>Befehlsdateien konnten nicht neu geladen werden</hover>
command.reload.success=<prefix> <white>Befehlsdateien wurden erfolgreich neu geladen
command.reload.changes=<prefix> <green><additions> <white>Hinzugefügt und <red><deletions> <white>entfernt <gray>(<file>)
command.saved=<prefix> <white>Befehlsdateien wurden erfolgreich gespeichert
command.unknown=<prefix> <red>Der Befehl <dark_gray>(<dark_red><command><dark_gray>) <red>existiert nicht
nothing.changed=<prefix> <red>Es konnte nichts geändert werden
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public void register(CommanderPlugin plugin) {
.then(new HideCommand(plugin).create())
.then(new PermissionCommand(plugin).create())
.then(new RegisterCommand(plugin).create())
.then(new ReloadCommand(plugin).create())
.then(new ResetCommand(plugin).create())
.then(new RevealCommand(plugin).create())
.then(new SaveCommand(plugin).create())
.then(new UnregisterCommand(plugin).create())
.build();
plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS.newHandler(event ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.entity.Player;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class ReloadCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("reload").executes(this::reload);
}

private int reload(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
try {
var commands = plugin.commandRegistry().reload(sender);
var permissions = plugin.permissionOverride().reload(sender);
var success = commands || permissions;
if (success && sender instanceof Player player) player.updateCommands();
var message = success ? "command.reload.success" : "nothing.changed";
plugin.bundle().sendMessage(sender, message);
return success ? Command.SINGLE_SUCCESS : 0;
} catch (Exception e) {
plugin.bundle().sendMessage(sender, "command.reload.failed",
Placeholder.parsed("error", e.getMessage()));
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.thenextlvl.commander.paper.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import lombok.RequiredArgsConstructor;
import net.thenextlvl.commander.paper.CommanderPlugin;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
class SaveCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSourceStack, ?> create() {
return Commands.literal("save").executes(this::save);
}

private int save(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
plugin.commandRegistry().getHiddenFile().save();
plugin.commandRegistry().getUnregisteredFile().save();
plugin.permissionOverride().getOverridesFile().save();
plugin.bundle().sendMessage(sender, "command.saved");
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import core.file.format.GsonFile;
import core.io.IO;
import lombok.Getter;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.CommandRegistry;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
Expand All @@ -14,6 +16,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Getter
public class PaperCommandRegistry implements CommandRegistry {
Expand Down Expand Up @@ -63,7 +66,7 @@ public boolean isUnregistered(String command) {

@Override
public boolean register(String command) {
return !plugin.commandFinder().findCommands(new HashSet<>(commands.keySet()).stream(), command).stream()
return !plugin.commandFinder().findCommands(new HashSet<>(unregisteredFile.getRoot()).stream(), command).stream()
.filter(unregisteredFile.getRoot()::remove)
.filter(this::internalRegister)
.toList().isEmpty();
Expand Down Expand Up @@ -92,6 +95,56 @@ public void unregisterCommands() {
.forEach(this::internalUnregister);
}

@Override
public boolean reload(Audience audience) {
var hidden = reloadHidden(audience);
var unregistered = reloadUnregistered(audience);
return hidden || unregistered;
}

private boolean reloadUnregistered(Audience audience) {
var previous = getUnregisteredFile().getRoot();
var current = getUnregisteredFile().reload();
if (previous.equals(current.getRoot())) return false;
var difference = difference(previous, current.getRoot());
var additions = difference.entrySet().stream()
.filter(Map.Entry::getValue).count();
plugin.bundle().sendMessage(audience, "command.reload.changes",
Placeholder.parsed("additions", String.valueOf(additions)),
Placeholder.parsed("deletions", String.valueOf(difference.size() - additions)),
Placeholder.parsed("file", "unregistered-commands.json"));
difference.forEach((command, added) -> {
if (added) internalUnregister(command);
else internalRegister(command);
});
return true;
}

private boolean reloadHidden(Audience audience) {
var previous = getHiddenFile().getRoot();
var current = getHiddenFile().reload();
if (previous.equals(current.getRoot())) return false;
var difference = difference(previous, current.getRoot());
var additions = difference.entrySet().stream()
.filter(Map.Entry::getValue).count();
plugin.bundle().sendMessage(audience, "command.reload.changes",
Placeholder.parsed("additions", String.valueOf(additions)),
Placeholder.parsed("deletions", String.valueOf(difference.size() - additions)),
Placeholder.parsed("file", "hidden-commands.json"));
return true;
}

private Map<String, Boolean> difference(Set<String> previous, Set<String> current) {
var differences = new HashMap<String, Boolean>();
differences.putAll(current.stream()
.filter(s -> !previous.contains(s))
.collect(Collectors.toMap(s -> s, s -> true)));
differences.putAll(previous.stream()
.filter(s -> !current.contains(s))
.collect(Collectors.toMap(s -> s, s -> false)));
return differences;
}

private boolean internalRegister(String command) {
var register = commands.remove(command);
if (register == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import core.io.IO;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.PermissionOverride;
import net.thenextlvl.commander.paper.CommanderPlugin;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -79,6 +81,39 @@ public void overridePermissions() {
overridesFile.getRoot().forEach(this::internalOverride);
}

@Override
public boolean reload(Audience audience) {
var previous = getOverridesFile().getRoot();
var current = getOverridesFile().reload();
if (previous.equals(current.getRoot())) return false;
var difference = difference(previous, current.getRoot());
var additions = difference.entrySet().stream()
.filter(Map.Entry::getValue).count();
plugin.bundle().sendMessage(audience, "command.reload.changes",
Placeholder.parsed("additions", String.valueOf(additions)),
Placeholder.parsed("deletions", String.valueOf(difference.size() - additions)),
Placeholder.parsed("file", "permission-overrides.json"));
difference.forEach((command, added) -> {
if (added) override(command.command(), command.permission());
else reset(command.command());
});
return true;
}

private Map<PermissionOverride, Boolean> difference(Map<String, @Nullable String> previous, Map<String, @Nullable String> current) {
var differences = new HashMap<PermissionOverride, Boolean>();
current.entrySet().stream()
.filter(entry -> !Objects.equals(previous.get(entry.getKey()), entry.getValue()))
.forEach(entry -> differences.put(new PermissionOverride(entry.getKey(), entry.getValue()), true));
previous.entrySet().stream()
.filter(entry -> !current.containsKey(entry.getKey()))
.forEach(entry -> differences.put(new PermissionOverride(entry.getKey(), entry.getValue()), false));
return differences;
}

private record PermissionOverride(String command, @Nullable String permission) {
}

private boolean internalOverride(String command, @Nullable String permission) {
var registered = Bukkit.getCommandMap().getKnownCommands().get(command);
if (registered == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public BrigadierCommand create(CommanderPlugin plugin) {
.then(new HideCommand(plugin).create())
.then(new PermissionCommand(plugin).create())
.then(new RegisterCommand(plugin).create())
.then(new ReloadCommand(plugin).create())
.then(new ResetCommand(plugin).create())
.then(new RevealCommand(plugin).create())
.then(new SaveCommand(plugin).create())
.then(new UnregisterCommand(plugin).create())
.build();
return new BrigadierCommand(command);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.thenextlvl.commander.velocity.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.commander.velocity.CommanderPlugin;

@RequiredArgsConstructor
class ReloadCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSource, ?> create() {
return BrigadierCommand.literalArgumentBuilder("reload").executes(this::reload);
}

private int reload(CommandContext<CommandSource> context) {
var sender = context.getSource();
try {
var commands = plugin.commandRegistry().reload(sender);
var permissions = plugin.permissionOverride().reload(sender);
var success = commands || permissions;
var message = success ? "command.reload.success" : "nothing.changed";
plugin.bundle().sendMessage(sender, message);
return success ? Command.SINGLE_SUCCESS : 0;
} catch (Exception e) {
plugin.bundle().sendMessage(sender, "command.reload.failed",
Placeholder.parsed("error", e.getMessage()));
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.thenextlvl.commander.velocity.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource;
import lombok.RequiredArgsConstructor;
import net.thenextlvl.commander.velocity.CommanderPlugin;

@RequiredArgsConstructor
class SaveCommand {
private final CommanderPlugin plugin;

public ArgumentBuilder<CommandSource, ?> create() {
return BrigadierCommand.literalArgumentBuilder("save").executes(this::save);
}

private int save(CommandContext<CommandSource> context) {
var sender = context.getSource();
plugin.commandRegistry().getHiddenFile().save();
plugin.commandRegistry().getUnregisteredFile().save();
plugin.permissionOverride().getOverridesFile().save();
plugin.bundle().sendMessage(sender, "command.saved");
return Command.SINGLE_SUCCESS;
}
}
Loading

0 comments on commit 9372453

Please sign in to comment.