Java Code Examples for org.bukkit.event.player.PlayerCommandPreprocessEvent#getMessage()

The following examples show how to use org.bukkit.event.player.PlayerCommandPreprocessEvent#getMessage() . 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: CommandEventHandler.java    From GriefDefender with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerCommandMonitor(PlayerCommandPreprocessEvent event) {
    String message = event.getMessage();
    String arguments = "";
    String command = "";
    if (!message.contains(" ")) {
        command = message.replace("/", "");
    } else {
        command = message.substring(0, message.indexOf(" ")).replace("/", "");
        arguments = message.substring(message.indexOf(" ") + 1, message.length());
    }
    if (command.equalsIgnoreCase("datapack") && (arguments.contains("enable") || arguments.contains("disable"))) {
        if (GriefDefenderPlugin.getInstance().getTagProvider() != null) {
            GriefDefenderPlugin.getInstance().getTagProvider().refresh();
        }
    }
}
 
Example 2
Source File: CommandCatch.java    From Survival-Games with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
    String m = event.getMessage();

    if(!GameManager.getInstance().isPlayerActive(event.getPlayer()) && !GameManager.getInstance().isPlayerInactive(event.getPlayer()) && !GameManager.getInstance().isSpectator(event.getPlayer()))
        return;
    if(m.equalsIgnoreCase("/list")){
        event.getPlayer().sendMessage(
        		GameManager.getInstance().getStringList(
        				GameManager.getInstance().getPlayerGameId(event.getPlayer())));
        return;
    }
    if(!SettingsManager.getInstance().getConfig().getBoolean("disallow-commands"))
        return;
    if(event.getPlayer().isOp() || event.getPlayer().hasPermission("sg.staff.nocmdblock"))
        return;
    else if(m.startsWith("/sg") || m.startsWith("/survivalgames")|| m.startsWith("/hg")||m.startsWith("/hungergames")||m.startsWith("/msg")){
        return;
    }
    else if (SettingsManager.getInstance().getConfig().getStringList("cmdwhitelist").contains(m)) {
    	return;
    }
    event.setCancelled(true);
}
 
Example 3
Source File: BukkitChatListener.java    From Parties with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Auto command listener
 */
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
	if (!event.isCancelled()) {
		String result = super.onPlayerCommandPreprocess(new BukkitUser(plugin, event.getPlayer()), event.getMessage());
		if (result != null) {
			event.setMessage(result);
		}
	}
}
 
Example 4
Source File: TpsPingCmdWarpper.java    From NyaaUtils with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommandPreProcess(PlayerCommandPreprocessEvent e) {
    String cmd = e.getMessage();
    if (plugin.cfg.tps_enable && plugin.cfg.tps_override && (cmd.startsWith("/tps ") || cmd.equals("/tps"))) {
        e.setMessage(cmd.replaceAll("^/tps", "/nu tps"));
    }

    if (plugin.cfg.ping_enable && plugin.cfg.ping_override && (cmd.startsWith("/ping ") || cmd.equals("/ping"))) {
        e.setMessage(cmd.replaceAll("^/ping", "/nu ping"));
    }

    if (plugin.cfg.ping_enable && plugin.cfg.ping_override && (cmd.startsWith("/pingtop ") || cmd.equals("/pingtop"))) {
        e.setMessage(cmd.replaceAll("^/pingtop", "/nu pingtop"));
    }
}
 
Example 5
Source File: CommandsPerformedListener.java    From Statz with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPerformCommand(final PlayerCommandPreprocessEvent event) {

	final PlayerStat stat = PlayerStat.COMMANDS_PERFORMED;

	// Get player
	final Player player = event.getPlayer();

	// Do general check
	if (!plugin.doGeneralCheck(player, stat))
		return;
	
	String message = event.getMessage();
	
	int subString = message.indexOf(" ");

	String command = "";
	String arguments = "";

	if (subString > 0) {
		command = message.substring(0, subString).trim();
		arguments = message.substring(subString).trim();

		// Cut off string so it's not too long
		if (arguments.length() > 100) {
			arguments = arguments.substring(0, 99);
		}
	} else {
		command = message.trim();
	}

	PlayerStatSpecification specification = new CommandsPerformedSpecification(player.getUniqueId(), 1,
			player.getWorld().getName(), command, arguments);

	// Update value to new stat.
	plugin.getDataManager().setPlayerInfo(player.getUniqueId(), stat, specification.constructQuery());

}
 
Example 6
Source File: CustomCommand.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled=true)
public void beforeCommand(PlayerCommandPreprocessEvent e) {
    Player player = e.getPlayer();
    String message = e.getMessage();
    String[] split = message.split(" ");

    String commandName = split[0];
    if(commandName.startsWith("/")) commandName = commandName.substring(1);
    if(commandName.equals(this.getName()) || this.getAliases().contains(commandName)) {
        e.setCancelled(true);

        String[] args = split.length > 1 ? Arrays.copyOfRange(split, 1, split.length) : new String[0];
        execute(player, commandName, args);
    }
}
 
Example 7
Source File: ListenerCommandBlocker.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority=EventPriority.LOWEST, ignoreCancelled=true)
public void beforeCommand(PlayerCommandPreprocessEvent e) {
    Player player = e.getPlayer();
    ICombatManager combatManager = this.plugin.getCombatManager();
    if(!combatManager.isInCombat(player) && !isInCooldown(player)) return;

    String command = e.getMessage();
    String actualCommand = convertCommand(command);
    if(!isBlocked(actualCommand) || isAllowed(actualCommand)) return;

    e.setCancelled(true);
    String message = this.plugin.getLanguageMessageColoredWithPrefix("cheat-prevention.command-blocked").replace("{command}", actualCommand);
    this.plugin.sendMessage(player, message);
}
 
Example 8
Source File: PlayerListener.java    From BedwarsRel with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onCommand(PlayerCommandPreprocessEvent pcpe) {
  Player player = pcpe.getPlayer();
  Game game = BedwarsRel.getInstance().getGameManager().getGameOfPlayer(player);

  if (game == null) {
    return;
  }

  if (game.getState() == GameState.STOPPED) {
    return;
  }

  String message = pcpe.getMessage();
  if (!message.startsWith("/bw")) {

    for (String allowed : BedwarsRel.getInstance().getAllowedCommands()) {
      if (!allowed.startsWith("/")) {
        allowed = "/" + allowed;
      }

      if (message.startsWith(allowed.trim())) {
        return;
      }
    }

    if (player.hasPermission("bw.cmd")) {
      return;
    }

    pcpe.setCancelled(true);
    return;
  }
}
 
Example 9
Source File: Conversation.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onCommand(PlayerCommandPreprocessEvent event) {
    if (!event.getPlayer().equals(player)) {
        return;
    }
    if (event.getMessage() == null)
        return;
    String cmdName = event.getMessage().split(" ")[0].substring(1);
    if (blacklist.contains(cmdName)) {
        event.setCancelled(true);

        Config.sendNotify(PlayerConverter.getID(event.getPlayer()), "command_blocked", "command_blocked,error");
    }
}
 
Example 10
Source File: WildCard.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    if (event.getPlayer().hasPermission("cardinal.wildcard")) {
        String command = event.getMessage() + " ";
        if (command.contains(" * ")) {
            event.setCancelled(true);
            for (Player player : Bukkit.getOnlinePlayers()) {
                Bukkit.dispatchCommand(event.getPlayer(), command.substring(1).replaceAll(" \\* ", " " + player.getName() + " ").trim());
            }
        }
    }
}
 
Example 11
Source File: ChatEventListener.java    From ChatItem with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.LOWEST)
public void onCommand(final PlayerCommandPreprocessEvent e){
    if(e.getMessage().indexOf(SEPARATOR)!=-1){  //If the BELL character is found, we have to remove it
        String msg = e.getMessage().replace(Character.toString(SEPARATOR), "");
        e.setMessage(msg);
    }
    String commandString = e.getMessage().split(" ")[0].replaceAll("^/+", ""); //First part of the command, without leading slashes and without arguments
    Command cmd = Bukkit.getPluginCommand(commandString);
    if(cmd==null){ //not a plugin command
        if(!c.ALLOWED_DEFAULT_COMMANDS.contains(commandString)){
            return;
        }
    }else{
        if(!c.ALLOWED_PLUGIN_COMMANDS.contains(cmd)){
            return;
        }
    }

    Player p = e.getPlayer();
    boolean found = false;

    for (String rep : c.PLACEHOLDERS) {
        if (e.getMessage().contains(rep)) {
            found = true;
            break;
        }
    }

    if (!found) {
        return;
    }

    if (!p.hasPermission("chatitem.use")) {
        if(!c.NO_PERMISSION_MESSAGE.isEmpty() && c.SHOW_NO_PERM_COMMAND){
            p.sendMessage(c.NO_PERMISSION_MESSAGE);
        }
        e.setCancelled(true);
        return;
    }

    if (e.getPlayer().getItemInHand().getType().equals(Material.AIR)) {
        if (c.DENY_IF_NO_ITEM) {
            e.setCancelled(true);
            if (!c.DENY_MESSAGE.isEmpty()) {
                e.getPlayer().sendMessage(c.DENY_MESSAGE);
            }
        }
        if(c.HAND_DISABLED) {
            return;
        }
    }

    if(c.COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown")){
        if(COOLDOWNS.containsKey(p.getName())){
            long start = COOLDOWNS.get(p.getName());
            long current = System.currentTimeMillis()/1000;
            long elapsed = current - start;
            if(elapsed >= c.COOLDOWN){
                COOLDOWNS.remove(p.getName());
            }else{
                if(!c.LET_MESSAGE_THROUGH) {
                    e.setCancelled(true);
                }
                if(!c.COOLDOWN_MESSAGE.isEmpty()){
                    long left = (start + c.COOLDOWN) - current;
                    p.sendMessage(c.COOLDOWN_MESSAGE.replace(LEFT, calculateTime(left)));
                }
                return;
            }
        }
    }

    String s = e.getMessage();
    for(String placeholder : c.PLACEHOLDERS){
        s = s.replace(placeholder, c.PLACEHOLDERS.get(0));
    }
    int occurrences = countOccurrences(c.PLACEHOLDERS.get(0), s);

    if(occurrences>c.LIMIT){
        e.setCancelled(true);
        if(c.LIMIT_MESSAGE.isEmpty()){
           return;
        }
        e.getPlayer().sendMessage(c.LIMIT_MESSAGE);
        return;
    }

    StringBuilder sb = new StringBuilder(e.getMessage());
    sb.append(SEPARATOR).append(e.getPlayer().getName());
    e.setMessage(sb.toString());
    if(!p.hasPermission("chatitem.ignore-cooldown")) {
        COOLDOWNS.put(p.getName(), System.currentTimeMillis() / 1000);
    }
}