Search in sources :

Example 1 with PlayerCommandPreprocessEvent

use of org.bukkit.event.player.PlayerCommandPreprocessEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method mockCommandEvent.

private static PlayerCommandPreprocessEvent mockCommandEvent(String message) {
    PlayerCommandPreprocessEvent commandEvent = mock(PlayerCommandPreprocessEvent.class);
    given(commandEvent.getMessage()).willReturn(message);
    return commandEvent;
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent)

Example 2 with PlayerCommandPreprocessEvent

use of org.bukkit.event.player.PlayerCommandPreprocessEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldAllowEssentialsMotd.

@Test
public void shouldAllowEssentialsMotd() {
    // given
    given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(true);
    PlayerCommandPreprocessEvent event = mockCommandEvent("/MOTD");
    // when
    listener.onPlayerCommandPreprocess(event);
    // then
    verify(event, only()).getMessage();
    verifyZeroInteractions(listenerService, messages);
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Test(org.junit.Test)

Example 3 with PlayerCommandPreprocessEvent

use of org.bukkit.event.player.PlayerCommandPreprocessEvent in project Denizen-For-Bukkit by DenizenScript.

the class ExecuteCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element cmd = scriptEntry.getElement("command");
    Element type = scriptEntry.getElement("type");
    Element silent = scriptEntry.getElement("silent");
    // Report to dB
    dB.report(scriptEntry, getName(), type.debug() + cmd.debug() + silent.debug());
    String command = cmd.asString();
    switch(Type.valueOf(type.asString())) {
        case AS_PLAYER:
            try {
                PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity(), "/" + command);
                Bukkit.getPluginManager().callEvent(pcpe);
                if (!pcpe.isCancelled()) {
                    boolean silentBool = silent.asBoolean();
                    Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
                    if (silentBool) {
                        silencedPlayers.add(player.getUniqueId());
                    }
                    player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
                    if (silentBool) {
                        silencedPlayers.remove(player.getUniqueId());
                    }
                }
            } catch (Throwable e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as player.");
                dB.echoError(scriptEntry.getResidingQueue(), e);
            }
            break;
        case AS_OP:
            if (CoreUtilities.toLowerCase(command).equals("stop")) {
                dB.echoError("Please use as_server to execute 'stop'.");
                return;
            }
            Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
            PlayerHelper playerHelper = NMSHandler.getInstance().getPlayerHelper();
            boolean isOp = player.isOp();
            if (!isOp) {
                playerHelper.setTemporaryOp(player, true);
            }
            try {
                PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(player, "/" + command);
                Bukkit.getPluginManager().callEvent(pcpe);
                if (!pcpe.isCancelled()) {
                    boolean silentBool = silent.asBoolean();
                    if (silentBool) {
                        silencedPlayers.add(player.getUniqueId());
                    }
                    player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
                    if (silentBool) {
                        silencedPlayers.remove(player.getUniqueId());
                    }
                }
            } catch (Throwable e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as OP.");
                dB.echoError(scriptEntry.getResidingQueue(), e);
            }
            if (!isOp) {
                playerHelper.setTemporaryOp(player, false);
            }
            break;
        case AS_NPC:
            if (!((BukkitScriptEntryData) scriptEntry.entryData).getNPC().isSpawned()) {
                dB.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Spawned.");
                return;
            }
            if (((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity().getType() != EntityType.PLAYER) {
                dB.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
                return;
            }
            ((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).setOp(true);
            try {
                ((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).performCommand(command);
            } catch (Throwable e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as NPC-OP.");
                dB.echoError(scriptEntry.getResidingQueue(), e);
            }
            ((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).setOp(false);
            break;
        case AS_SERVER:
            dcs.clearOutput();
            dcs.silent = silent.asBoolean();
            ServerCommandEvent sce = new ServerCommandEvent(dcs, command);
            Bukkit.getPluginManager().callEvent(sce);
            DenizenAPI.getCurrentInstance().getServer().dispatchCommand(dcs, sce.getCommand());
            scriptEntry.addObject("output", new dList(dcs.getOutput()));
            break;
    }
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Player(org.bukkit.entity.Player) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) PlayerHelper(net.aufdemrand.denizen.nms.interfaces.PlayerHelper) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent)

Example 4 with PlayerCommandPreprocessEvent

use of org.bukkit.event.player.PlayerCommandPreprocessEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldNotStopAllowedCommand.

@Test
public void shouldNotStopAllowedCommand() {
    // given
    given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(true);
    given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/plugins", "/mail", "/msg"));
    PlayerCommandPreprocessEvent event = mockCommandEvent("/Mail send test Test");
    // when
    listener.onPlayerCommandPreprocess(event);
    // then
    verify(event, only()).getMessage();
    verifyZeroInteractions(listenerService, messages);
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Test(org.junit.Test)

Example 5 with PlayerCommandPreprocessEvent

use of org.bukkit.event.player.PlayerCommandPreprocessEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldCancelCommandEvent.

@Test
public void shouldCancelCommandEvent() {
    // given
    given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
    given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/spawn", "/help"));
    Player player = playerWithMockedServer();
    PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    // when
    listener.onPlayerCommandPreprocess(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(event).setCancelled(true);
    verify(messages).send(player, MessageKey.DENIED_COMMAND);
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Player(org.bukkit.entity.Player) Test(org.junit.Test)

Aggregations

PlayerCommandPreprocessEvent (org.bukkit.event.player.PlayerCommandPreprocessEvent)6 Test (org.junit.Test)4 Player (org.bukkit.entity.Player)3 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)1 PlayerHelper (net.aufdemrand.denizen.nms.interfaces.PlayerHelper)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 ServerCommandEvent (org.bukkit.event.server.ServerCommandEvent)1