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

The following examples show how to use org.bukkit.event.player.PlayerCommandPreprocessEvent#setCancelled() . 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: CommandListener.java    From ChestCommands with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onCommand(PlayerCommandPreprocessEvent event) {

	if (ChestCommands.getSettings().use_only_commands_without_args && event.getMessage().contains(" ")) {
		return;
	}

	// Very fast method compared to split & substring
	String command = StringUtils.getCleanCommand(event.getMessage());

	if (command.isEmpty()) {
		return;
	}

	ExtendedIconMenu menu = ChestCommands.getCommandToMenuMap().get(command);

	if (menu != null) {
		event.setCancelled(true);

		if (event.getPlayer().hasPermission(menu.getPermission())) {
			menu.open(event.getPlayer());
		} else {
			menu.sendNoPermissionMessage(event.getPlayer());
		}
	}
}
 
Example 2
Source File: CMListener.java    From ChatMenuAPI with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onCommandPreprocess(PlayerCommandPreprocessEvent e)
{
	String cmd = e.getMessage().substring(1);
	if(cmd.length() <= 0) return;
	String[] unprocessedArgs = cmd.split(" ");
	
	String label = unprocessedArgs[0];
	String[] args = new String[unprocessedArgs.length - 1];
	System.arraycopy(unprocessedArgs, 1, args, 0, args.length);
	
	if(label.equalsIgnoreCase("cmapi"))
	{
		e.setCancelled(true);
		command.onCommand(e.getPlayer(), null, label, args);
	}
}
 
Example 3
Source File: DeathListener.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();
    Location location = player.getLocation();
    Civilian civilian = CivilianManager.getInstance().getCivilian(player.getUniqueId());

    long jailTime = ConfigManager.getInstance().getJailTime();
    if (civilian.getLastJail() + jailTime < System.currentTimeMillis()) {
        return;
    }
    long timeRemaining = civilian.getLastJail() + jailTime - System.currentTimeMillis();

    Region region = RegionManager.getInstance().getRegionAt(location);
    if (region == null || !region.getEffects().containsKey("jail")) {
        return;
    }
    event.setCancelled(true);
    player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslationWithPlaceholders(player,
            "no-commands-in-jail").replace("$1", (int) (timeRemaining / 1000) + "s"));
}
 
Example 4
Source File: ChunkEventHelper.java    From ClaimChunk with MIT License 6 votes vote down vote up
public static void handleCommandEvent(@Nonnull Player ply, @Nonnull Chunk chunk, @Nonnull PlayerCommandPreprocessEvent e) {
    if (e.isCancelled()) return;

    // If the user is an admin, they can run any command.
    if (Utils.hasAdmin(ply)) return;

    // If the user has permission to edit within this chunk, they can use
    // any command they have permissions to use.
    if (getCanEdit(chunk, ply.getUniqueId())) return;

    // Get the command from the message.
    final String[] cmdAndArgs = e.getMessage()
            .trim()
            .substring(1)
            .split(" ", 1);

    // Length should never be >1 but it could be 0.
    if (cmdAndArgs.length == 1) {
        // Cancel the event if the blocked commands list contains this
        // command.
        if (config.getList("protection", "blockedCmds").contains(cmdAndArgs[0])) e.setCancelled(true);
    }
}
 
Example 5
Source File: CommandInterceptor.java    From mcspring-boot with MIT License 5 votes vote down vote up
@EventHandler
void onPlayerCommand(PlayerCommandPreprocessEvent event) {
    if (event.isCancelled() || commandService.isRegistered()) return;
    val player = context.getPlayer();
    val result = commandExecutor.execute(event.getMessage().substring(1).split(" "));
    event.setCancelled(result.isExists());
    result.getOutput().forEach(player::sendMessage);
}
 
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: Main.java    From TAB with Apache License 2.0 5 votes vote down vote up
@EventHandler
public void a(PlayerCommandPreprocessEvent e) {
	if (Shared.disabled) return;
	if (Configs.bukkitBridgeMode) return;
	ITabPlayer sender = Shared.getPlayer(e.getPlayer().getUniqueId());
	if (sender == null) return;
	if (e.getMessage().equalsIgnoreCase("/tab") || e.getMessage().equalsIgnoreCase("/tab:tab")) {
		Shared.sendPluginInfo(sender);
		return;
	}
	for (CommandListener listener : Shared.commandListeners) {
		if (listener.onCommand(sender, e.getMessage())) e.setCancelled(true);
	}
}
 
Example 8
Source File: SlimefunCommand.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent e) {
    if (e.getMessage().equalsIgnoreCase("/help slimefun")) {
        sendHelp(e.getPlayer());
        e.setCancelled(true);
    }
}
 
Example 9
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 10
Source File: PlayerCommand.java    From FunnyGuilds with Apache License 2.0 5 votes vote down vote up
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();
    if (player.hasPermission("funnyguilds.admin")) {
        return;
    }

    String[] splited = event.getMessage().split("\\s+");
    if (splited.length == 0) {
        return;
    }
    String command = splited[0];
    for (String s : FunnyGuilds.getInstance().getPluginConfiguration().regionCommands) {
        if (("/" + s).equalsIgnoreCase(command)) {
            command = null;
            break;
        }
    }
    if (command != null) {
        return;
    }

    Region region = RegionUtils.getAt(player.getLocation());
    if (region == null) {
        return;
    }

    Guild guild = region.getGuild();
    User user = User.get(player);
    if (guild.getMembers().contains(user)) {
        return;
    }

    event.setCancelled(true);
    player.sendMessage(FunnyGuilds.getInstance().getMessageConfiguration().regionCommand);
}
 
Example 11
Source File: Updater.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent e){
    if (!e.getMessage().equalsIgnoreCase("/uhccore update")){
        return;
    }
    e.setCancelled(true);

    Player player = e.getPlayer();
    GameManager gm = GameManager.getGameManager();

    if (gm.getGameState() == GameState.PLAYING || gm.getGameState() == GameState.DEATHMATCH){
        player.sendMessage(ChatColor.RED + "You can not update the plugin during games as it will restart your server.");
        return;
    }

    player.sendMessage(ChatColor.GREEN + "Updating plugin ...");

    try{
        updatePlugin(true);
    }catch (Exception ex){
        player.sendMessage(ChatColor.RED + "Failed to update plugin, check console for more info.");
        ex.printStackTrace();
    }
}
 
Example 12
Source File: BukkitPlotListener.java    From PlotMe-Core with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
    if (event.getMessage().equalsIgnoreCase("/reload") || event.getMessage().equalsIgnoreCase("reload")) {
        event.setCancelled(true);
        event.getPlayer().sendMessage("PlotMe disabled /reload to prevent errors from occuring.");
    }
}
 
Example 13
Source File: ChatModule.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
    PermissionModule module = GameHandler.getGameHandler().getMatch().getModules().getModule(PermissionModule.class);
    if (!GameHandler.getGameHandler().getGlobalMute()) {
        if (module.isMuted(event.getPlayer()) && (event.getMessage().toLowerCase().startsWith("/me ") || event.getMessage().toLowerCase().startsWith("/minecraft:me "))) {
            event.getPlayer().sendMessage(ChatColor.RED + new LocalizedChatMessage(ChatConstant.ERROR_NO_PERMISSION).getMessage(event.getPlayer().getLocale()));
            event.setCancelled(true);
        }
    } else {
        if (event.getMessage().toLowerCase().startsWith("/me ") || event.getMessage().toLowerCase().startsWith("/minecraft:me ")) {
            event.getPlayer().sendMessage(ChatColor.RED + new LocalizedChatMessage(ChatConstant.ERROR_NO_PERMISSION).getMessage(event.getPlayer().getLocale()));
            event.setCancelled(true);
        }
    }
}
 
Example 14
Source File: MainListener.java    From ArmorStandTools with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerCommand(final PlayerCommandPreprocessEvent event) {
    Player p = event.getPlayer();
    String cmd = event.getMessage().split(" ")[0].toLowerCase();
    while(cmd.length() > 0 && cmd.charAt(0) == '/') {
        cmd = cmd.substring(1);
    }
    if(cmd.length() > 0 && Config.deniedCommands.contains(cmd) && Utils.hasAnyTools(p)) {
        event.setCancelled(true);
        p.sendMessage(ChatColor.RED + Config.cmdNotAllowed);
    }
}
 
Example 15
Source File: TellrawConvIO.java    From BetonQuest with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onCommand (PlayerCommandPreprocessEvent event) {
    if (event.getMessage().toLowerCase().startsWith("/betonquestanswer ")) {
        event.setCancelled(true);
    }
}
 
Example 16
Source File: Commands.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("null")
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerCommand(final PlayerCommandPreprocessEvent e) {
	if (handleCommand(e.getPlayer(), e.getMessage().substring(1)))
		e.setCancelled(true);
}
 
Example 17
Source File: FallbackRegistrationListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    if (commandRegistration.dispatch(event.getPlayer(), event.getMessage().substring(1))) {
        event.setCancelled(true);
    }
}
 
Example 18
Source File: DPlayerListener.java    From DungeonsXL with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();
    if (isCitizensNPC(player)) {
        return;
    }

    if (DPermission.hasPermission(player, DPermission.BYPASS)) {
        return;
    }

    if (!(dPlayers.get(player) instanceof DInstancePlayer)) {
        return;
    }
    InstancePlayer dPlayer = dPlayers.getInstancePlayer(player);

    String command = event.getMessage().toLowerCase();
    ArrayList<String> commandWhitelist = new ArrayList<>();

    if (dPlayer instanceof DEditPlayer) {
        if (DPermission.hasPermission(player, DPermission.CMD_EDIT)) {
            return;

        } else {
            commandWhitelist.addAll(config.getEditCommandWhitelist());
        }

    } else {
        commandWhitelist.addAll(dPlayer.getGroup().getDungeon().getRules().getState(GameRule.GAME_COMMAND_WHITELIST));
    }

    commandWhitelist.add("dungeonsxl");
    commandWhitelist.add("dungeon");
    commandWhitelist.add("dxl");

    event.setCancelled(true);

    for (String whitelistEntry : commandWhitelist) {
        if (command.equals('/' + whitelistEntry.toLowerCase()) || command.startsWith('/' + whitelistEntry.toLowerCase() + ' ')) {
            event.setCancelled(false);
        }
    }

    if (event.isCancelled()) {
        MessageUtil.sendMessage(player, DMessage.ERROR_CMD.getMessage());
    }
}
 
Example 19
Source File: ListenerTrMenuInfo.java    From TrMenu with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onCommand(PlayerCommandPreprocessEvent e) {
    e.setCancelled(react(e.getPlayer(), e.getMessage().substring(1)));
}
 
Example 20
Source File: PlayerCommandListener.java    From ExploitFixer with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommand(final PlayerCommandPreprocessEvent event) {
	final Player player = event.getPlayer();
	final ExploitPlayer exploitPlayer = exploitPlayerManager.get(player);

	if (!exploitPlayer.isLogged()) {
		event.setCancelled(true);
	} else if (commandsModule.isEnabled()) {
		final Server server = plugin.getServer();
		final String message = event.getMessage().replaceAll("[\\w]+:", "").toLowerCase();
		final String playerName = player.getName();

		for (final String command : commandsModule.getCommands()) {
			if (message.startsWith(command + " ")) {
				for (final String punishment : commandsModule.getPunishments()) {
					if (command.equals("kick")) {
						final String locale = VersionUtil.getLocale(player);
						final String kickMessage = messagesModule.getKickMessage(commandsModule, locale);
						final HamsterPlayer hamsterPlayer = HamsterAPI.getInstance().getHamsterPlayerManager()
								.get(player);

						hamsterPlayer.disconnect(kickMessage);
						hamsterPlayer.closeChannel();
						exploitPlayerManager.addPunishment();
					} else if (command.equals("notification")) {
						final String moduleName = commandsModule.getName();

						notificationsModule.sendNotification(moduleName, player, 1);
					} else {
						if (server.isPrimaryThread()) {
							server.dispatchCommand(server.getConsoleSender(),
									punishment.replace("%player%", playerName));
						} else {
							server.getScheduler().runTask(plugin, () -> {
								server.dispatchCommand(server.getConsoleSender(),
										punishment.replace("%player%", playerName));
							});
						}
					}
				}

				event.setCancelled(true);
				break;
			}
		}
	}
}