begin work on commands

This commit is contained in:
xGinko 2023-09-10 22:51:50 +02:00
parent ea7f7ff1da
commit 0e4c801617
11 changed files with 365 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package me.xginko.villageroptimizer; package me.xginko.villageroptimizer;
import me.xginko.villageroptimizer.cache.VillagerManager; import me.xginko.villageroptimizer.cache.VillagerManager;
import me.xginko.villageroptimizer.commands.VillagerOptimizerCommand;
import me.xginko.villageroptimizer.config.Config; import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.config.LanguageCache; import me.xginko.villageroptimizer.config.LanguageCache;
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule; import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
@ -37,6 +38,8 @@ public final class VillagerOptimizer extends JavaPlugin {
reloadLang(); reloadLang();
logger.info("Loading Config"); logger.info("Loading Config");
reloadConfiguration(); reloadConfiguration();
logger.info("Registering Commands");
VillagerOptimizerCommand.reloadCommands();
logger.info("Done."); logger.info("Done.");
} }
@ -59,6 +62,7 @@ public final class VillagerOptimizer extends JavaPlugin {
public void reloadPlugin() { public void reloadPlugin() {
reloadLang(); reloadLang();
reloadConfiguration(); reloadConfiguration();
VillagerOptimizerCommand.reloadCommands();
} }
private void reloadConfiguration() { private void reloadConfiguration() {

View File

@ -0,0 +1,11 @@
package me.xginko.villageroptimizer.commands;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.command.CommandSender;
public abstract class SubCommand {
public abstract String getLabel();
public abstract TextComponent getDescription();
public abstract TextComponent getSyntax();
public abstract void perform(CommandSender sender, String[] args);
}

View File

@ -0,0 +1,37 @@
package me.xginko.villageroptimizer.commands;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.optimizevillagers.OptVillagersRadius;
import me.xginko.villageroptimizer.commands.unoptimizevillagers.UnOptVillagersRadius;
import me.xginko.villageroptimizer.commands.villageroptimizer.VillagerOptimizerCmd;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
public interface VillagerOptimizerCommand extends CommandExecutor {
String label();
HashSet<VillagerOptimizerCommand> commands = new HashSet<>();
static void reloadCommands() {
commands.clear();
commands.add(new VillagerOptimizerCmd());
commands.add(new OptVillagersRadius());
commands.add(new UnOptVillagersRadius());
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
CommandMap commandMap = plugin.getServer().getCommandMap();
for (VillagerOptimizerCommand command : commands) {
plugin.getCommand(command.label()).unregister(commandMap);
plugin.getCommand(command.label()).setExecutor(command);
}
}
@Override
boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args);
}

View File

@ -0,0 +1,86 @@
package me.xginko.villageroptimizer.commands.optimizevillagers;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.cache.VillagerManager;
import me.xginko.villageroptimizer.commands.VillagerOptimizerCommand;
import me.xginko.villageroptimizer.enums.OptimizationType;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.models.WrappedVillager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class OptVillagersRadius implements VillagerOptimizerCommand, TabCompleter {
@Override
public String label() {
return "optimizevillagers";
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return List.of("5", "10", "25", "50");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Component.text("This command can only be executed as a player.")
.color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
return true;
}
if (args.length != 1) {
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
return true;
}
try {
int specifiedRadius = Integer.parseInt(args[0]) / 2;
int successCount = 0;
int failCount = 0;
VillagerManager villagerManager = VillagerOptimizer.getVillagerManager();
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
Villager villager = (Villager) entity;
Villager.Profession profession = villager.getProfession();
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
WrappedVillager wVillager = villagerManager.getOrAdd(villager);
if (!wVillager.isOptimized()) {
wVillager.setOptimization(OptimizationType.COMMAND);
wVillager.saveOptimizeTime();
successCount++;
}
}
final String success = Integer.toString(successCount);
final String radius = Integer.toString(specifiedRadius);
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%amount%").replacement(success).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%radius%").replacement(radius).build())
));
} catch (NumberFormatException e) {
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
}
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
return true;
}
}

View File

@ -0,0 +1,41 @@
package me.xginko.villageroptimizer.commands.unoptimizevillagers;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.VillagerOptimizerCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class UnOptVillagersRadius implements VillagerOptimizerCommand {
@Override
public String label() {
return "unoptimizevillagers";
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Component.text("This command can only be executed as a player.")
.color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
return true;
}
if (args.length < 1) {
return true;
}
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
return true;
}
}

View File

@ -0,0 +1,83 @@
package me.xginko.villageroptimizer.commands.villageroptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.commands.VillagerOptimizerCommand;
import me.xginko.villageroptimizer.commands.villageroptimizer.subcommands.ReloadSubCmd;
import me.xginko.villageroptimizer.commands.villageroptimizer.subcommands.VersionSubCmd;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class VillagerOptimizerCmd implements TabCompleter, VillagerOptimizerCommand {
private final List<SubCommand> subCommands = new ArrayList<>(7);
private final List<String> tabCompleter = new ArrayList<>(7);
public VillagerOptimizerCmd() {
subCommands.add(new ReloadSubCmd());
subCommands.add(new VersionSubCmd());
for (SubCommand subcommand : subCommands) {
tabCompleter.add(subcommand.getLabel());
}
}
@Override
public String label() {
return "villageroptimizer";
}
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
return args.length == 1 ? tabCompleter : Collections.emptyList();
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (args.length > 0) {
boolean cmdExists = false;
for (SubCommand subCommand : subCommands) {
if (args[0].equalsIgnoreCase(subCommand.getLabel())) {
subCommand.perform(sender, args);
cmdExists = true;
break;
}
}
if (!cmdExists) sendCommandOverview(sender);
} else {
sendCommandOverview(sender);
}
return true;
}
private void sendCommandOverview(CommandSender sender) {
if (!sender.hasPermission("anarchyexploitfixes.cmd.*")) return;
sender.sendMessage(Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
sender.sendMessage(Component.text("VillagerOptimizer Commands").color(NamedTextColor.BLUE));
sender.sendMessage(Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
for (SubCommand subCommand : subCommands) {
sender.sendMessage(
subCommand.getSyntax()
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(subCommand.getDescription())
);
}
sender.sendMessage(
Component.text("/optimizevillagers <message>").color(NamedTextColor.BLUE)
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Optimize villagers in a radius").color(NamedTextColor.GRAY))
);
sender.sendMessage(
Component.text("/unoptmizevillagers").color(NamedTextColor.BLUE)
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Unoptimize villagers in a radius").color(NamedTextColor.GRAY))
);
sender.sendMessage(Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
}
}

View File

@ -0,0 +1,38 @@
package me.xginko.villageroptimizer.commands.villageroptimizer.subcommands;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.CommandSender;
public class ReloadSubCmd extends SubCommand {
@Override
public String getLabel() {
return "reload";
}
@Override
public TextComponent getDescription() {
return Component.text("Reload the plugin configuration.").color(NamedTextColor.GRAY);
}
@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer reload").color(NamedTextColor.GOLD);
}
@Override
public void perform(CommandSender sender, String[] args) {
if (sender.hasPermission(Permissions.Commands.RELOAD.get())) {
sender.sendMessage(Component.text("Reloading VillagerOptimizer...").color(NamedTextColor.WHITE));
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getAsyncScheduler().runNow(plugin, reloadPlugin -> {
plugin.reloadPlugin();
sender.sendMessage(Component.text("Reload complete.").color(NamedTextColor.GREEN));
});
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
}
}

View File

@ -0,0 +1,44 @@
package me.xginko.villageroptimizer.commands.villageroptimizer.subcommands;
import io.papermc.paper.plugin.configuration.PluginMeta;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.CommandSender;
public class VersionSubCmd extends SubCommand {
@Override
public String getLabel() {
return "version";
}
@Override
public TextComponent getDescription() {
return Component.text("Show the plugin version.").color(NamedTextColor.GRAY);
}
@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer version").color(NamedTextColor.GOLD);
}
@Override
public void perform(CommandSender sender, String[] args) {
if (sender.hasPermission(Permissions.Commands.VERSION.get())) {
PluginMeta pluginMeta = VillagerOptimizer.getInstance().getPluginMeta();
sender.sendMessage(
Component.newline()
.append(Component.text(pluginMeta.getName()+" "+pluginMeta.getVersion()).color(NamedTextColor.BLUE).decorate(TextDecoration.BOLD)
.append(Component.text(" by ").color(NamedTextColor.GRAY))
.append(Component.text(pluginMeta.getAuthors().get(0)).color(NamedTextColor.DARK_AQUA))
.clickEvent(ClickEvent.openUrl(pluginMeta.getWebsite())))
.append(Component.newline())
);
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
}
}

View File

@ -18,7 +18,7 @@ public class LanguageCache {
block_optimize_success, block_on_optimize_cooldown, block_unoptimize_success, block_optimize_success, block_on_optimize_cooldown, block_unoptimize_success,
workstation_optimize_success, workstation_on_optimize_cooldown, workstation_unoptimize_success, workstation_optimize_success, workstation_on_optimize_cooldown, workstation_unoptimize_success,
command_optimize_success, command_radius_limit_exceed, command_optimize_fail, command_unoptimize_success, command_optimize_success, command_radius_limit_exceed, command_optimize_fail, command_unoptimize_success,
trades_restocked, optimize_for_trading, villager_leveling_up; command_specify_radius, command_radius_invalid, trades_restocked, optimize_for_trading, villager_leveling_up;
public LanguageCache(String lang) throws Exception { public LanguageCache(String lang) throws Exception {
this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml")); this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml"));
@ -55,14 +55,18 @@ public class LanguageCache {
this.workstation_unoptimize_success = getListTranslation("messages.workstation.unoptimize-success", this.workstation_unoptimize_success = getListTranslation("messages.workstation.unoptimize-success",
List.of("<green>Successfully unoptimized %villagertype% villager by removing workstation block %workstation%.")); List.of("<green>Successfully unoptimized %villagertype% villager by removing workstation block %workstation%."));
// Command // Command
this.command_optimize_success = getListTranslation("messages.workstation.optimize-success", this.command_optimize_success = getListTranslation("messages.command.optimize-success",
List.of("<green>Successfully optimized %amount% villager(s) in a radius of %radius% blocks.")); List.of("<green>Successfully optimized %amount% villager(s) in a radius of %radius% blocks."));
this.command_radius_limit_exceed = getListTranslation("messages.workstation.radius-limit-exceed", this.command_radius_limit_exceed = getListTranslation("messages.command.radius-limit-exceed",
List.of("<red>The radius you entered exceeds the limit of %distance% blocks.")); List.of("<red>The radius you entered exceeds the limit of %distance% blocks."));
this.command_optimize_fail = getListTranslation("messages.workstation.optimize-fail", this.command_optimize_fail = getListTranslation("messages.command.optimize-fail",
List.of("<gray>%amount% villagers couldn't be optimized because they have recently been optimized.")); List.of("<gray>%amount% villagers couldn't be optimized because they have recently been optimized."));
this.command_unoptimize_success = getListTranslation("messages.workstation.unoptimize-success", this.command_unoptimize_success = getListTranslation("messages.command.unoptimize-success",
List.of("<green>Successfully unoptimized %amount% villager(s) in a radius of %radius% blocks.")); List.of("<green>Successfully unoptimized %amount% villager(s) in a radius of %radius% blocks."));
this.command_specify_radius = getListTranslation("messages.command.specify-radius",
List.of("<red>Please specify a radius."));
this.command_radius_invalid = getListTranslation("messages.command.radius-invalid",
List.of("<red>The radius you entered is not a valid number. Try again."));
saveLang(); saveLang();
} }

View File

@ -36,3 +36,7 @@ messages:
- "<red>The radius you entered exceeds the limit of %distance% blocks." - "<red>The radius you entered exceeds the limit of %distance% blocks."
unoptimize-success: unoptimize-success:
- "<green>Successfully unoptimized %amount% villager(s) in a radius of %radius% blocks." - "<green>Successfully unoptimized %amount% villager(s) in a radius of %radius% blocks."
specify-radius:
- "<red>Please specify a radius."
radius-invalid:
- "<red>The radius you entered is not a valid number. Try again."

View File

@ -10,12 +10,19 @@ commands:
villageroptimizer: villageroptimizer:
usage: /villageroptimizer [ reload, version ] usage: /villageroptimizer [ reload, version ]
description: VillagerOptimizer admin commands description: VillagerOptimizer admin commands
aliases:
- voptimizer
- vo
optimizevillagers: optimizevillagers:
usage: /optimizevillagers <blockradius> usage: /optimizevillagers <blockradius>
description: Optmize villagers in a radius around you description: Optmize villagers in a radius around you
aliases:
- optvils
unoptmizevillagers: unoptmizevillagers:
usage: /unoptimizevillagers <blockradius> usage: /unoptimizevillagers <blockradius>
description: Unoptmize villagers in a radius around you description: Unoptmize villagers in a radius around you
aliases:
- unoptvils
permissions: permissions:
villageroptimizer.ignore: villageroptimizer.ignore:
description: Players with this permission won't be able to use the plugin features description: Players with this permission won't be able to use the plugin features