-
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #29 from CyR1en/console-delegate
Console delegate
- Loading branch information
Showing
23 changed files
with
674 additions
and
309 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
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
81 changes: 81 additions & 0 deletions
81
src/main/java/com/cyr1en/commandprompter/commands/ConsoleDelegate.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,81 @@ | ||
package com.cyr1en.commandprompter.commands; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.ConsoleCommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import com.cyr1en.commandprompter.CommandPrompter; | ||
import com.cyr1en.commandprompter.PluginLogger; | ||
import com.cyr1en.commandprompter.PluginMessenger; | ||
import com.cyr1en.commandprompter.prompt.ContextProcessor; | ||
import com.cyr1en.commandprompter.prompt.PromptContext; | ||
import com.cyr1en.kiso.mc.I18N; | ||
|
||
public class ConsoleDelegate extends ContextProcessor implements CommandExecutor { | ||
|
||
private final CommandPrompter commandPrompter; | ||
private final PluginLogger logger; | ||
private final PluginMessenger messenger; | ||
private final I18N i18n; | ||
private final String usage; | ||
|
||
public ConsoleDelegate(JavaPlugin plugin) { | ||
super((CommandPrompter) plugin, ((CommandPrompter) plugin).getPromptManager()); | ||
this.commandPrompter = (CommandPrompter) plugin; | ||
this.logger = commandPrompter.getPluginLogger(); | ||
this.messenger = commandPrompter.getMessenger(); | ||
this.i18n = commandPrompter.getI18N(); | ||
this.usage = "consoledelegate <target player> <command...>"; | ||
} | ||
|
||
private void doCommand(Player targetPlayer, String command) { | ||
var context = new PromptContext(null, targetPlayer, command); | ||
context.setIsConsoleDelegate(true); | ||
this.process(context); | ||
} | ||
|
||
@Override | ||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | ||
logger.debug("Command: " + command); | ||
logger.debug("Label: " + label); | ||
logger.debug("Args: " + Arrays.toString(args)); | ||
|
||
if (!(sender instanceof ConsoleCommandSender)) { | ||
messenger.sendMessage(sender, i18n.getProperty("DelegateConsoleOnly")); | ||
return true; | ||
} | ||
|
||
if (args.length < 2) { | ||
messenger.sendMessage(sender, i18n.getProperty("DelegateInvalidArgs")); | ||
messenger.sendMessage(sender, i18n.getFormattedProperty("DelegateUsage", this.usage)); | ||
return true; | ||
} | ||
|
||
var arg0 = args[0]; | ||
var targetPlayer = commandPrompter.getServer().getPlayer(arg0); | ||
|
||
if (targetPlayer == null) { | ||
messenger.sendMessage(sender, i18n.getFormattedProperty("DelegateInvalidPlayer", arg0)); | ||
return true; | ||
} | ||
|
||
var delegatedCommand = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); | ||
|
||
if (delegatedCommand.isEmpty()) { | ||
messenger.sendMessage(sender, i18n.getProperty("DelegateInvalidCommand")); | ||
return true; | ||
} | ||
|
||
if (delegatedCommand.contains("%target_player%")) | ||
delegatedCommand = delegatedCommand.replace("%target_player%", targetPlayer.getName()); | ||
|
||
doCommand(targetPlayer, delegatedCommand); | ||
return true; | ||
} | ||
|
||
} |
Oops, something went wrong.