Skip to content

Commit 3828d52

Browse files
authored
Merge pull request #29 from CyR1en/console-delegate
Console delegate
2 parents b8405f0 + 963d7e3 commit 3828d52

23 files changed

Lines changed: 674 additions & 309 deletions

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ $RECYCLE.BIN/
7373
*.iml
7474
out
7575
gen### Java template
76+
77+
# Vscode
78+
.vscode
79+
7680
# Compiled class file
7781
*.class
7882

@@ -95,4 +99,6 @@ gen### Java template
9599
*.rar
96100

97101
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
98-
hs_err_pid*
102+
hs_err_pid*
103+
104+
bin/

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ compileTestJava.options.encoding = "UTF-8"
3333

3434
java {
3535
toolchain {
36-
languageVersion = JavaLanguageVersion.of(17)
36+
languageVersion = JavaLanguageVersion.of(16)
3737
}
3838
}
3939

4040
PluginManifest pluginManifest = [
4141
name : 'CommandPrompter',
42-
version : new Version(major: 2, minor: 4, patch: 1, fix: 0, classifier: 'SNAPSHOT'),
42+
version : new Version(major: 2, minor: 5, patch: 0, fix: 0, classifier: 'SNAPSHOT'),
4343
author : 'CyR1en',
4444
description: 'Perfect companion plugin for inventory UI menu.',
4545
entry : 'com.cyr1en.commandprompter.CommandPrompter'

src/main/java/com/cyr1en/commandprompter/CommandPrompter.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.cyr1en.commandprompter.command.CommodoreRegistry;
2828
import com.cyr1en.commandprompter.commands.Cancel;
29+
import com.cyr1en.commandprompter.commands.ConsoleDelegate;
2930
import com.cyr1en.commandprompter.commands.Reload;
3031
import com.cyr1en.commandprompter.config.CommandPrompterConfig;
3132
import com.cyr1en.commandprompter.config.ConfigurationManager;
@@ -43,6 +44,7 @@
4344
import com.cyr1en.commandprompter.unsafe.ModifiedCommandMap;
4445
import com.cyr1en.commandprompter.unsafe.PvtFieldMutator;
4546
import com.cyr1en.commandprompter.util.Util;
47+
import com.cyr1en.commandprompter.util.Util.ServerType;
4648
import com.cyr1en.kiso.mc.I18N;
4749
import com.cyr1en.kiso.mc.UpdateChecker;
4850
import com.cyr1en.kiso.mc.command.CommandManager;
@@ -79,12 +81,20 @@ public void onEnable() {
7981
new Metrics(this, 5359);
8082
setupConfig();
8183
logger = new PluginLogger(this, "CommandPrompter");
82-
loadDeps();
84+
var serverType = ServerType.resolve();
85+
logger.debug("Server Name: " + serverType.name());
86+
logger.debug("Server Version: " + serverType.version());
87+
var result = loadDeps();
88+
if (!result)
89+
return;
90+
8391
i18n = new I18N(this, "CommandPrompter");
92+
messenger = new PluginMessenger(config.promptPrefix());
93+
8494
setupUpdater();
85-
setupCommands();
8695
initPromptSystem();
87-
messenger = new PluginMessenger(config.promptPrefix());
96+
setupCommands();
97+
8898
instance = this;
8999
Bukkit.getPluginManager().registerEvents(new CommandSendListener(this), this);
90100

@@ -97,8 +107,13 @@ public void onEnable() {
97107

98108
@Override
99109
public void onDisable() {
100-
promptManager.clearPromptRegistry();
101-
getPluginLogger().ansiUninstall();
110+
if (promptManager != null)
111+
promptManager.clearPromptRegistry();
112+
113+
PluginLogger logger;
114+
if ((logger = getPluginLogger()) != null)
115+
logger.ansiUninstall();
116+
102117
if (Objects.nonNull(updateChecker) && !updateChecker.isDisabled())
103118
HandlerList.unregisterAll(updateChecker);
104119
}
@@ -109,21 +124,34 @@ private void initPromptSystem() {
109124
Bukkit.getPluginManager().registerEvents(headCache = new HeadCache(this), this);
110125
}
111126

112-
private void loadDeps() {
127+
private boolean loadDeps() {
113128
if (Util.isBundledVersion(this)) {
114129
getPluginLogger().info("This is a bundled version! Skipping dependency loading");
115-
return;
130+
return true;
116131
}
117132

133+
getPluginLogger().info("Loading dependencies...");
134+
118135
var depLoader = new DependencyLoader(this);
119-
if (depLoader.isRelocatorAvailable()) {
136+
if (!depLoader.isClassLoaderAccessSupported())
137+
return depErrAndDisable("No access to URLClassloader, cannot load depedencies!", depLoader);
138+
139+
if (!depLoader.loadCoreDeps())
140+
return depErrAndDisable("Unable to load dependencies!", depLoader);
141+
142+
if (depLoader.relocatorAvailable()) {
120143
depLoader.loadDependency();
121-
return;
144+
return true;
122145
}
123146

124-
getPluginLogger().err("Unable to load dependencies!");
147+
return depErrAndDisable("Unable to load dependencies!", depLoader);
148+
}
149+
150+
private boolean depErrAndDisable(String message, DependencyLoader depLoader) {
151+
getPluginLogger().err(message);
125152
depLoader.sendBundledMessage();
126153
Bukkit.getPluginManager().disablePlugin(this);
154+
return false;
127155
}
128156

129157
/**
@@ -174,6 +202,8 @@ private void setupCommands() {
174202
commandManager.registerCommand(Reload.class);
175203
commandManager.registerCommand(Cancel.class);
176204
PluginCommand command = getCommand("commandprompter");
205+
PluginCommand delegate = getCommand("consoledelegate");
206+
delegate.setExecutor(new ConsoleDelegate(this));
177207
Objects.requireNonNull(command).setExecutor(commandManager);
178208
CommodoreRegistry.register(this, command);
179209
}
@@ -198,7 +228,8 @@ private void setupCommandManager() {
198228

199229
private void setupUpdater() {
200230
updateChecker = new UpdateChecker(this, 47772);
201-
if (updateChecker.isDisabled()) return;
231+
if (updateChecker.isDisabled())
232+
return;
202233
Bukkit.getServer().getScheduler().runTaskAsynchronously(this, () -> {
203234
if (updateChecker.newVersionAvailable())
204235
logger.info(SRegex.ANSI_GREEN + "A new update is available! (" +
@@ -270,4 +301,3 @@ public UpdateChecker getUpdateChecker() {
270301
return updateChecker;
271302
}
272303
}
273-

src/main/java/com/cyr1en/commandprompter/PluginLogger.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
public class PluginLogger {
1313

1414
private String prefix;
15-
private String plainPrefix;
1615
private String debugPrefix;
1716

1817
private final ColorGradient normalGrad;
1918
private final ColorGradient debugGrad;
2019

2120
private boolean debugMode = false;
2221
private boolean isFancy = true;
23-
private CommandPrompter plugin;
2422

2523
public PluginLogger(CommandPrompter plugin, String prefix) {
26-
this.plugin = plugin;
2724
this.isFancy = plugin.getConfiguration().fancyLogger();
2825
this.debugMode = plugin.getConfiguration().debugMode();
2926
AnsiConsole.systemInstall();
@@ -42,7 +39,6 @@ public void ansiUninstall() {
4239
}
4340

4441
public void setPrefix(String prefix) {
45-
this.plainPrefix = prefix;
4642
var sep = isFancy ? new Ansi().fgRgb(153, 214, 90).a(">>").reset().toString() : ">>";
4743
var normal = isFancy ? makeGradient(prefix, normalGrad) : prefix;
4844
var debug = isFancy ? makeGradient(prefix + "-" + "Debug", debugGrad) : prefix + "-" + "Debug";
@@ -93,8 +89,9 @@ public void debug(String msg, Object... args) {
9389
lastDebugClass = caller;
9490

9591
if (debugMode) {
96-
msg = callerAvailable ? String.format("[%s] - %s", caller.getSimpleName(), msg) :
97-
Objects.isNull(lastDebugClass) ? msg : String.format("[%s?] - %s", lastDebugClass.getSimpleName(), msg);
92+
msg = callerAvailable ? String.format("[%s] - %s", caller.getSimpleName(), msg)
93+
: Objects.isNull(lastDebugClass) ? msg
94+
: String.format("[%s?] - %s", lastDebugClass.getSimpleName(), msg);
9895
var str = new Ansi().fgRgb(255, 195, 113).a(msg).reset().toString();
9996
log(debugPrefix, Level.INFO, str, args);
10097
}

src/main/java/com/cyr1en/commandprompter/api/Dispatcher.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
/**
3636
* Player command dispatcher for Support with CommandPrompter.
3737
*
38-
* <p>Because CommandPrompter cannot catch commands that were dispatched from
38+
* <p>
39+
* Because CommandPrompter cannot catch commands that were dispatched from
3940
* {@link org.bukkit.Bukkit#dispatchCommand(CommandSender, String)}, plugins
40-
* need a special way to execute player commands.</p>
41+
* need a special way to execute player commands.
42+
* </p>
4143
*/
4244
public class Dispatcher {
4345

@@ -58,34 +60,32 @@ public void run() {
5860
}.runTask(plugin);
5961
}
6062

61-
public static void dispatchNative(CommandSender sender, String command) {
62-
final String checked = command.codePointAt(0) == 0x2F ?
63-
command.replace("/", "") : command;
64-
Bukkit.dispatchCommand(sender, checked);
65-
}
66-
6763
/**
6864
* Dispatch the command as Console.
6965
*
7066
* @param command command that would be dispatched.
7167
*/
72-
public static void dispatchOP(String command) {
73-
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
68+
public static void dispatchConsole(final String command) {
69+
final String checked = command.codePointAt(0) == 0x2F ? command.replace("/", "") : command;
70+
new BukkitRunnable() {
71+
public void run() {
72+
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), checked);
73+
}
74+
}.runTask(CommandPrompter.getInstance());
7475
}
7576

76-
7777
/**
7878
* Dispatch a command for a player with a PermissionAttachment that contains
7979
* all the whitelisted commands.
8080
*
8181
* @param plugin Instance of plugin.
8282
* @param sender command sender (in menu's, then the item clicker)
8383
* @param command command that would be dispatched.
84-
* @param ticks Number of ticks before the attachment expires
85-
* @param perms Permissions to set to the PermissionAttachment
84+
* @param ticks Number of ticks before the attachment expires
85+
* @param perms Permissions to set to the PermissionAttachment
8686
*/
87-
public static void dispatchWithAttachment
88-
(Plugin plugin, Player sender, String command, int ticks, @NotNull String[] perms) {
87+
public static void dispatchWithAttachment(Plugin plugin, Player sender, String command, int ticks,
88+
@NotNull String[] perms) {
8989
var commandPrompter = (CommandPrompter) plugin;
9090
var logger = commandPrompter.getPluginLogger();
9191

@@ -102,6 +102,4 @@ public static void dispatchOP(String command) {
102102
dispatchCommand(plugin, (Player) attachment.getPermissible(), command);
103103
}
104104

105-
106-
107105
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.cyr1en.commandprompter.commands;
2+
3+
import java.util.Arrays;
4+
5+
import org.bukkit.command.Command;
6+
import org.bukkit.command.CommandExecutor;
7+
import org.bukkit.command.CommandSender;
8+
import org.bukkit.command.ConsoleCommandSender;
9+
import org.bukkit.entity.Player;
10+
import org.bukkit.plugin.java.JavaPlugin;
11+
12+
import com.cyr1en.commandprompter.CommandPrompter;
13+
import com.cyr1en.commandprompter.PluginLogger;
14+
import com.cyr1en.commandprompter.PluginMessenger;
15+
import com.cyr1en.commandprompter.prompt.ContextProcessor;
16+
import com.cyr1en.commandprompter.prompt.PromptContext;
17+
import com.cyr1en.kiso.mc.I18N;
18+
19+
public class ConsoleDelegate extends ContextProcessor implements CommandExecutor {
20+
21+
private final CommandPrompter commandPrompter;
22+
private final PluginLogger logger;
23+
private final PluginMessenger messenger;
24+
private final I18N i18n;
25+
private final String usage;
26+
27+
public ConsoleDelegate(JavaPlugin plugin) {
28+
super((CommandPrompter) plugin, ((CommandPrompter) plugin).getPromptManager());
29+
this.commandPrompter = (CommandPrompter) plugin;
30+
this.logger = commandPrompter.getPluginLogger();
31+
this.messenger = commandPrompter.getMessenger();
32+
this.i18n = commandPrompter.getI18N();
33+
this.usage = "consoledelegate <target player> <command...>";
34+
}
35+
36+
private void doCommand(Player targetPlayer, String command) {
37+
var context = new PromptContext(null, targetPlayer, command);
38+
context.setIsConsoleDelegate(true);
39+
this.process(context);
40+
}
41+
42+
@Override
43+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
44+
logger.debug("Command: " + command);
45+
logger.debug("Label: " + label);
46+
logger.debug("Args: " + Arrays.toString(args));
47+
48+
if (!(sender instanceof ConsoleCommandSender)) {
49+
messenger.sendMessage(sender, i18n.getProperty("DelegateConsoleOnly"));
50+
return true;
51+
}
52+
53+
if (args.length < 2) {
54+
messenger.sendMessage(sender, i18n.getProperty("DelegateInvalidArgs"));
55+
messenger.sendMessage(sender, i18n.getFormattedProperty("DelegateUsage", this.usage));
56+
return true;
57+
}
58+
59+
var arg0 = args[0];
60+
var targetPlayer = commandPrompter.getServer().getPlayer(arg0);
61+
62+
if (targetPlayer == null) {
63+
messenger.sendMessage(sender, i18n.getFormattedProperty("DelegateInvalidPlayer", arg0));
64+
return true;
65+
}
66+
67+
var delegatedCommand = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
68+
69+
if (delegatedCommand.isEmpty()) {
70+
messenger.sendMessage(sender, i18n.getProperty("DelegateInvalidCommand"));
71+
return true;
72+
}
73+
74+
if (delegatedCommand.contains("%target_player%"))
75+
delegatedCommand = delegatedCommand.replace("%target_player%", targetPlayer.getName());
76+
77+
doCommand(targetPlayer, delegatedCommand);
78+
return true;
79+
}
80+
81+
}

0 commit comments

Comments
 (0)