use of cn.nukkit.command.ConsoleCommandSender in project Nukkit by Nukkit.
the class TimingsExport method run.
@Override
public void run() {
this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.uploadStart"));
this.out.add("data", JsonUtil.mapToArray(this.history, TimingsHistory::export));
String response = null;
try {
HttpURLConnection con = (HttpURLConnection) new URL("http://timings.aikar.co/post").openConnection();
con.setDoOutput(true);
con.setRequestProperty("User-Agent", "Nukkit/" + Server.getInstance().getName() + "/" + InetAddress.getLocalHost().getHostName());
con.setRequestMethod("POST");
con.setInstanceFollowRedirects(false);
PGZIPOutputStream request = new PGZIPOutputStream(con.getOutputStream());
request.setLevel(Deflater.BEST_COMPRESSION);
request.write(new Gson().toJson(this.out).getBytes("UTF-8"));
request.close();
response = getResponse(con);
if (con.getResponseCode() != 302) {
this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.uploadError", new String[] { String.valueOf(con.getResponseCode()), con.getResponseMessage() }));
if (response != null) {
Server.getInstance().getLogger().alert(response);
}
return;
}
String location = con.getHeaderField("Location");
this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.timingsLocation", location));
if (!(this.sender instanceof ConsoleCommandSender)) {
Server.getInstance().getLogger().info(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsLocation", location));
}
if (response != null && !response.isEmpty()) {
Server.getInstance().getLogger().info(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsResponse", response));
}
File timingFolder = new File(Server.getInstance().getDataPath() + File.separator + "timings");
timingFolder.mkdirs();
String fileName = timingFolder + File.separator + new SimpleDateFormat("'timings-'yyyy-MM-dd-hh-mm'.txt'").format(new Date());
FileWriter writer = new FileWriter(fileName);
writer.write(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsLocation", location) + "\n\n");
writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(this.out));
writer.close();
Server.getInstance().getLogger().info(Server.getInstance().getLanguage().translateString("nukkit.command.timings.timingsWrite", fileName));
} catch (IOException exception) {
this.sender.sendMessage(TextFormat.RED + "" + new TranslationContainer("nukkit.command.timings.reportError"));
if (response != null) {
Server.getInstance().getLogger().alert(response);
}
Server.getInstance().getLogger().logException(exception);
}
}
use of cn.nukkit.command.ConsoleCommandSender in project LuckPerms by lucko.
the class PermissibleMonitoringInjector method injectConsole.
private void injectConsole() throws Exception {
ConsoleCommandSender consoleSender = this.plugin.getBootstrap().getServer().getConsoleSender();
// get the perm field
Field permField = ConsoleCommandSender.class.getDeclaredField("perm");
permField.setAccessible(true);
// get the PermissibleBase instance
PermissibleBase permBase = (PermissibleBase) permField.get(consoleSender);
// create a monitored instance which delegates to the previous PermissibleBase
MonitoredPermissibleBase newPermBase = wrap(permBase, "internal/console");
// inject the monitored instance
permField.set(consoleSender, newPermBase);
}
use of cn.nukkit.command.ConsoleCommandSender in project Nukkit by Nukkit.
the class SayCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
if (args.length == 0) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return false;
}
String senderString;
if (sender instanceof Player) {
senderString = ((Player) sender).getDisplayName();
} else if (sender instanceof ConsoleCommandSender) {
senderString = "Server";
} else {
senderString = sender.getName();
}
String msg = "";
for (String arg : args) {
msg += arg + " ";
}
if (msg.length() > 0) {
msg = msg.substring(0, msg.length() - 1);
}
sender.getServer().broadcastMessage(new TranslationContainer(TextFormat.LIGHT_PURPLE + "%chat.type.announcement", new String[] { senderString, TextFormat.LIGHT_PURPLE + msg }));
return true;
}
use of cn.nukkit.command.ConsoleCommandSender in project Nukkit by Nukkit.
the class HelpCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
String command = "";
int pageNumber = 1;
int pageHeight = 5;
if (args.length != 0) {
try {
pageNumber = Integer.valueOf(args[args.length - 1]);
if (pageNumber <= 0) {
pageNumber = 1;
}
String[] newargs = new String[args.length - 1];
System.arraycopy(args, 0, newargs, 0, newargs.length);
args = newargs;
/*if (args.length > 1) {
args = Arrays.copyOfRange(args, 0, args.length - 2);
} else {
args = new String[0];
}*/
for (String arg : args) {
if (!command.equals("")) {
command += " ";
}
command += arg;
}
} catch (NumberFormatException e) {
pageNumber = 1;
for (String arg : args) {
if (!command.equals("")) {
command += " ";
}
command += arg;
}
}
}
if (sender instanceof ConsoleCommandSender) {
pageHeight = Integer.MAX_VALUE;
}
if (command.equals("")) {
Map<String, Command> commands = new TreeMap<>();
for (Command cmd : sender.getServer().getCommandMap().getCommands().values()) {
if (cmd.testPermissionSilent(sender)) {
commands.put(cmd.getName(), cmd);
}
}
int totalPage = commands.size() % pageHeight == 0 ? commands.size() / pageHeight : commands.size() / pageHeight + 1;
pageNumber = Math.min(pageNumber, totalPage);
if (pageNumber < 1) {
pageNumber = 1;
}
sender.sendMessage(new TranslationContainer("commands.help.header", new String[] { String.valueOf(pageNumber), String.valueOf(totalPage) }));
int i = 1;
for (Command command1 : commands.values()) {
if (i >= (pageNumber - 1) * pageHeight + 1 && i <= Math.min(commands.size(), pageNumber * pageHeight)) {
sender.sendMessage(TextFormat.DARK_GREEN + "/" + command1.getName() + ": " + TextFormat.WHITE + command1.getDescription());
}
i++;
}
return true;
} else {
Command cmd = sender.getServer().getCommandMap().getCommand(command.toLowerCase());
if (cmd != null) {
if (cmd.testPermissionSilent(sender)) {
String message = TextFormat.YELLOW + "--------- " + TextFormat.WHITE + " Help: /" + cmd.getName() + TextFormat.YELLOW + " ---------\n";
message += TextFormat.GOLD + "Description: " + TextFormat.WHITE + cmd.getDescription() + "\n";
String usage = "";
String[] usages = cmd.getUsage().split("\n");
for (String u : usages) {
if (!usage.equals("")) {
usage += "\n" + TextFormat.WHITE;
}
usage += u;
}
message += TextFormat.GOLD + "Usage: " + TextFormat.WHITE + usage + "\n";
sender.sendMessage(message);
return true;
}
}
sender.sendMessage(TextFormat.RED + "No help for " + command.toLowerCase());
return true;
}
}
Aggregations