cn.nukkit.command.ConsoleCommandSender Java Examples

The following examples show how to use cn.nukkit.command.ConsoleCommandSender. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PermissibleMonitoringInjector.java    From LuckPerms with MIT License 6 votes vote down vote up
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 new instance which delegates to the previous PermissibleBase
    PermissibleBase newPermBase = transform(permBase, "internal/console");
    if (newPermBase == null) {
        return;
    }

    // inject the new instance
    permField.set(consoleSender, newPermBase);
}
 
Example #2
Source File: MuteCommand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {

    if (!this.testPermission(sender)) {
        return true;
    }

    if(sender instanceof ConsoleCommandSender){
    	sender.sendMessage(TextFormat.RED + "コンソールはミュートできません。");
    	return true;
    }

    Player p = (Player)sender;

    if(p.mute){
    	p.setMute(false);
    	p.sendImportantMessage(TextFormat.GREEN + "ミュートを解除しました。");
    	return true;
    }else{
    	p.setMute(true);
    	p.sendImportantMessage(TextFormat.RED + "ミュートを有効にしました。");
    	return true;
    }
}
 
Example #3
Source File: PlanNukkitMocker.java    From Plan with GNU Lesser General Public License v3.0 6 votes vote down vote up
PlanNukkitMocker withServer() {
    Server serverMock = Mockito.mock(Server.class);
    doReturn("").when(serverMock).getIp();
    doReturn("Nukkit").when(serverMock).getName();
    doReturn(25565).when(serverMock).getPort();
    doReturn("1.12.2").when(serverMock).getVersion();
    doReturn("32423").when(serverMock).getNukkitVersion();
    doReturn(TestConstants.SERVER_MAX_PLAYERS).when(serverMock).getMaxPlayers();
    ConsoleCommandSender sender = Mockito.mock(ConsoleCommandSender.class);
    doReturn(sender).when(serverMock).getConsoleSender();

    ServerScheduler scheduler = Mockito.mock(ServerScheduler.class);
    doReturn(scheduler).when(serverMock).getScheduler();

    doReturn(serverMock).when(planMock).getServer();
    return this;
}
 
Example #4
Source File: SayCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@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",
            senderString, TextFormat.LIGHT_PURPLE + msg));
    return true;
}
 
Example #5
Source File: SimpleCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (this.forbidConsole && sender instanceof ConsoleCommandSender) {
        this.sendInGameMessage(sender);
        return false;
    } else if (!this.testPermission(sender)) {
        return false;
    } else if (this.maxArgs != 0 && args.length > this.maxArgs) {
        this.sendUsageMessage(sender);
        return false;
    } else if (this.minArgs != 0 && args.length < this.minArgs) {
        this.sendUsageMessage(sender);
        return false;
    }

    boolean success = false;

    try {
        success = (Boolean) this.method.invoke(this.object, sender, commandLabel, args);
    } catch (Exception exception) {
        Server.getInstance().getLogger().logException(exception);
    }

    if (!success) {
        this.sendUsageMessage(sender);
    }

    return success;
}
 
Example #6
Source File: SayCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #7
Source File: NukkitCommandExecutor.java    From LuckPerms with MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onConsoleCommand(ServerCommandEvent e) {
    if (!(e.getSender() instanceof ConsoleCommandSender)) {
        return;
    }

    String buffer = e.getCommand();
    if (buffer.isEmpty() || buffer.charAt(0) != '/') {
        return;
    }

    buffer = buffer.substring(1);

    String commandLabel;
    int firstSpace = buffer.indexOf(' ');
    if (firstSpace == -1) {
        commandLabel = buffer;
    } else {
        commandLabel = buffer.substring(0, firstSpace);
    }

    Command command = this.plugin.getBootstrap().getServer().getCommandMap().getCommand(commandLabel);
    if (command != this.command) {
        return;
    }

    e.setCommand(buffer);
}
 
Example #8
Source File: NukkitSenderFactory.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
protected void sendMessage(CommandSender sender, String s) {
    // we can safely send async for players and the console
    if (sender instanceof Player || sender instanceof ConsoleCommandSender) {
        sender.sendMessage(s);
        return;
    }

    // otherwise, send the message sync
    getPlugin().getBootstrap().getScheduler().executeSync(new SyncMessengerAgent(sender, s));
}
 
Example #9
Source File: SimpleCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (this.forbidConsole && sender instanceof ConsoleCommandSender) {
        this.sendInGameMessage(sender);
        return false;
    } else if (!this.testPermission(sender)) {
        return false;
    } else if (this.maxArgs != 0 && args.length > this.maxArgs) {
        this.sendUsageMessage(sender);
        return false;
    } else if (this.minArgs != 0 && args.length < this.minArgs) {
        this.sendUsageMessage(sender);
        return false;
    }

    boolean success = false;

    try {
        success = (Boolean) this.method.invoke(this.object, sender, commandLabel, args);
    } catch (Exception exception) {
        Server.getInstance().getLogger().logException(exception);
    }

    if (!success) {
        this.sendUsageMessage(sender);
    }

    return success;
}
 
Example #10
Source File: SimpleCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (this.forbidConsole && sender instanceof ConsoleCommandSender) {
        this.sendInGameMessage(sender);
        return false;
    } else if (!this.testPermission(sender)) {
        return false;
    } else if (this.maxArgs != 0 && args.length > this.maxArgs) {
        this.sendUsageMessage(sender);
        return false;
    } else if (this.minArgs != 0 && args.length < this.minArgs) {
        this.sendUsageMessage(sender);
        return false;
    }

    boolean success = false;

    try {
        success = (Boolean) this.method.invoke(this.object, sender, commandLabel, args);
    } catch (Exception exception) {
        Server.getInstance().getLogger().logException(exception);
    }

    if (!success) {
        this.sendUsageMessage(sender);
    }

    return success;
}
 
Example #11
Source File: SayCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #12
Source File: SummonCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (sender instanceof ConsoleCommandSender) {
        sender.sendMessage(new TranslationContainer("commands.generic.ingame"));
        return true;
    }

    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }

    Position pos = new Position(Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]), ((Player) sender).getLevel());

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", pos.getX() + 0.5))
                    .add(new DoubleTag("", pos.getY()))
                    .add(new DoubleTag("", pos.getZ() + 0.5)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0))
                    .add(new DoubleTag("", 0)))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", new Random().nextFloat() * 360))
                    .add(new FloatTag("", 0)));

    Entity entity = Entity.createEntity(args[0], pos.getLevel().getChunk((int) pos.getX() >> 4, (int) pos.getZ() >> 4, true), nbt);

    entity.spawnToAll();
    return true;
}
 
Example #13
Source File: TimingsExport.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@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);

        OutputStream request = new GZIPOutputStream(con.getOutputStream()) {
            {
                this.def.setLevel(7);
            }
        };

        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);
    }
}
 
Example #14
Source File: TimingsExport.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@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(StandardCharsets.UTF_8));
        request.close();

        response = getResponse(con);

        if (con.getResponseCode() != 302) {
            this.sender.sendMessage(new TranslationContainer("nukkit.command.timings.uploadError", 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);
    }
}
 
Example #15
Source File: StopSoundCommand.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {

       if (!this.testPermission(sender)) {
           return true;
       }

       if (args.length < 2) {
           sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
           return true;
       }

       Player[] players = new Player[]{};

       if (args[0].equals("@e") || args[0].equals("@a")) {
           players = sender.getServer().getOnlinePlayers().values().toArray(new Player[0]);

       } else if (args[0].equals("@p") || args[0].equals("@s")) {
           if (sender instanceof ConsoleCommandSender) {
               sender.sendMessage(new TranslationContainer("commands.generic.ingame"));
               return true;
           } else {//TODO: CommandBlockCommandSender
               players = new Player[]{(Player) sender};
           }

       } else if (args[0].equals("@r")) {
           List<Player> list = Arrays.asList(sender.getServer().getOnlinePlayers().values().toArray(new Player[0]));
           if (list.size() <= 0) {
               sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
               return true;
           }
           Collections.shuffle(list);
           players = new Player[]{list.get(0)};

       } else {
           if (sender.getServer().getPlayer(args[0]) == null) {
               sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
               return true;
           }
           players = new Player[]{sender.getServer().getPlayer(args[0])};

       }

       String sound = args[1];
       
       Arrays.asList(players).forEach(p -> {
           StopSoundPacket pk = new StopSoundPacket();
           pk.name = sound;
           pk.stopAll = false;
           p.dataPacket(pk);
       });

	return false;
}
 
Example #16
Source File: TimingsExport.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@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);
    }
}
 
Example #17
Source File: Server.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public ConsoleCommandSender getConsoleSender() {
    return consoleSender;
}