Search in sources :

Example 1 with InventoryOpenEvent

use of org.bukkit.event.inventory.InventoryOpenEvent in project TriggerReactor by wysohn.

the class JavaPluginBridge method createInterrupterForInv.

@Override
public ProcessInterrupter createInterrupterForInv(Object e, Interpreter interpreter, Map<UUID, Long> cooldowns, Map<IInventory, InventoryTrigger> inventoryMap) {
    return new ProcessInterrupter() {

        @Override
        public boolean onNodeProcess(Node node) {
            if (interpreter.isCooldown()) {
                if (e instanceof InventoryInteractEvent) {
                    HumanEntity he = ((InventoryInteractEvent) e).getWhoClicked();
                    if (he instanceof Player) {
                        Player player = (Player) he;
                        UUID uuid = player.getUniqueId();
                        cooldowns.put(uuid, interpreter.getCooldownEnd());
                    }
                }
                return false;
            }
            // is still running after the inventory is closed.
            if (e instanceof InventoryOpenEvent || e instanceof InventoryClickEvent) {
                Inventory inv = ((InventoryEvent) e).getInventory();
                // it's not GUI so stop execution
                if (!inventoryMap.containsKey(new BukkitInventory(inv)))
                    return true;
            }
            return false;
        }

        @Override
        public boolean onCommand(Object context, String command, Object[] args) {
            if ("CALL".equals(command)) {
                if (args.length < 1)
                    throw new RuntimeException("Need parameter [String] or [String, boolean]");
                if (args[0] instanceof String) {
                    Trigger trigger = getNamedTriggerManager().getTriggerForName((String) args[0]);
                    if (trigger == null)
                        throw new RuntimeException("No trigger found for Named Trigger " + args[0]);
                    if (args.length > 1 && args[1] instanceof Boolean) {
                        trigger.setSync((boolean) args[1]);
                    } else {
                        trigger.setSync(true);
                    }
                    if (trigger.isSync()) {
                        trigger.activate(e, interpreter.getVars());
                    } else {
                        // use snapshot to avoid concurrent modification
                        trigger.activate(e, new HashMap<>(interpreter.getVars()));
                    }
                    return true;
                } else {
                    throw new RuntimeException("Parameter type not match; it should be a String." + " Make sure to put double quotes, if you provided String literal.");
                }
            } else if ("CANCELEVENT".equals(command)) {
                if (!interpreter.isSync())
                    throw new RuntimeException("CANCELEVENT is illegal in async mode!");
                if (context instanceof Cancellable) {
                    ((Cancellable) context).setCancelled(true);
                    return true;
                } else {
                    throw new RuntimeException(context + " is not a Cancellable event!");
                }
            }
            return false;
        }
    };
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) InventoryEvent(org.bukkit.event.inventory.InventoryEvent) DelegatedPlayer(io.github.wysohn.triggerreactor.bukkit.tools.DelegatedPlayer) IPlayer(io.github.wysohn.triggerreactor.core.bridge.player.IPlayer) Player(org.bukkit.entity.Player) BukkitPlayer(io.github.wysohn.triggerreactor.bukkit.bridge.player.BukkitPlayer) Cancellable(org.bukkit.event.Cancellable) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) ProcessInterrupter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter) InventoryClickEvent(org.bukkit.event.inventory.InventoryClickEvent) Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) HumanEntity(org.bukkit.entity.HumanEntity) InventoryInteractEvent(org.bukkit.event.inventory.InventoryInteractEvent) UUID(java.util.UUID) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) Inventory(org.bukkit.inventory.Inventory)

Example 2 with InventoryOpenEvent

use of org.bukkit.event.inventory.InventoryOpenEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldAllowInventoryOpen.

@Test
public void shouldAllowInventoryOpen() {
    // given
    HumanEntity player = mock(Player.class);
    InventoryView transaction = mock(InventoryView.class);
    InventoryOpenEvent event = new InventoryOpenEvent(transaction);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(false);
    // when
    listener.onPlayerInventoryOpen(event);
    // then
    assertThat(event.isCancelled(), equalTo(false));
    verifyNoInteractions(bukkitService);
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) InventoryView(org.bukkit.inventory.InventoryView) HumanEntity(org.bukkit.entity.HumanEntity) Test(org.junit.Test)

Example 3 with InventoryOpenEvent

use of org.bukkit.event.inventory.InventoryOpenEvent in project Glowstone by GlowstoneMC.

the class GlowPlayer method openInventory.

@Override
public void openInventory(InventoryView view) {
    session.send(new CloseWindowMessage(invMonitor.getId()));
    super.openInventory(view);
    invMonitor = new InventoryMonitor(getOpenInventory());
    int viewId = invMonitor.getId();
    if (viewId != 0) {
        InventoryOpenEvent event = EventFactory.getInstance().callEvent(new InventoryOpenEvent(view));
        if (event.isCancelled()) {
            // close the inventory but don't fire the InventoryCloseEvent
            resetInventoryView();
            return;
        }
        String title = view.getTitle();
        boolean defaultTitle = Objects.equals(view.getType().getDefaultTitle(), title);
        if (view.getTopInventory() instanceof PlayerInventory && defaultTitle) {
            title = ((PlayerInventory) view.getTopInventory()).getHolder().getName();
        }
        Message open = new OpenWindowMessage(viewId, invMonitor.getType(), title, ((GlowInventory) view.getTopInventory()).getRawSlots());
        session.send(open);
    }
    updateInventory();
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) CloseWindowMessage(net.glowstone.net.message.play.inv.CloseWindowMessage) OpenWindowMessage(net.glowstone.net.message.play.inv.OpenWindowMessage) TitleMessage(net.glowstone.net.message.play.game.TitleMessage) EntityMetadataMessage(net.glowstone.net.message.play.entity.EntityMetadataMessage) OpenWindowMessage(net.glowstone.net.message.play.inv.OpenWindowMessage) BlockBreakAnimationMessage(net.glowstone.net.message.play.game.BlockBreakAnimationMessage) WindowPropertyMessage(net.glowstone.net.message.play.inv.WindowPropertyMessage) UpdateBlockEntityMessage(net.glowstone.net.message.play.game.UpdateBlockEntityMessage) RespawnMessage(net.glowstone.net.message.play.game.RespawnMessage) EntityVelocityMessage(net.glowstone.net.message.play.entity.EntityVelocityMessage) PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) HealthMessage(net.glowstone.net.message.play.game.HealthMessage) PluginMessage(net.glowstone.net.message.play.game.PluginMessage) SetWindowSlotMessage(net.glowstone.net.message.play.inv.SetWindowSlotMessage) PlayEffectMessage(net.glowstone.net.message.play.game.PlayEffectMessage) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) MultiBlockChangeMessage(net.glowstone.net.message.play.game.MultiBlockChangeMessage) UnloadChunkMessage(net.glowstone.net.message.play.game.UnloadChunkMessage) StateChangeMessage(net.glowstone.net.message.play.game.StateChangeMessage) SpawnPositionMessage(net.glowstone.net.message.play.game.SpawnPositionMessage) ExperienceMessage(net.glowstone.net.message.play.game.ExperienceMessage) NamedSoundEffectMessage(net.glowstone.net.message.play.game.NamedSoundEffectMessage) UserListHeaderFooterMessage(net.glowstone.net.message.play.game.UserListHeaderFooterMessage) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) MapDataMessage(net.glowstone.net.message.play.game.MapDataMessage) StopSoundMessage(net.glowstone.net.message.play.game.StopSoundMessage) CloseWindowMessage(net.glowstone.net.message.play.inv.CloseWindowMessage) ChunkDataMessage(net.glowstone.net.message.play.game.ChunkDataMessage) TimeMessage(net.glowstone.net.message.play.game.TimeMessage) Message(com.flowpowered.network.Message) SetWindowContentsMessage(net.glowstone.net.message.play.inv.SetWindowContentsMessage) PlayParticleMessage(net.glowstone.net.message.play.game.PlayParticleMessage) ResourcePackSendMessage(net.glowstone.net.message.play.player.ResourcePackSendMessage) DestroyEntitiesMessage(net.glowstone.net.message.play.entity.DestroyEntitiesMessage) JoinGameMessage(net.glowstone.net.message.play.game.JoinGameMessage) SignEditorMessage(net.glowstone.net.message.play.game.SignEditorMessage) SetPassengerMessage(net.glowstone.net.message.play.entity.SetPassengerMessage) EntityAnimationMessage(net.glowstone.net.message.play.entity.EntityAnimationMessage) TextMessage(net.glowstone.util.TextMessage) UpdateSignMessage(net.glowstone.net.message.play.game.UpdateSignMessage) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) ChatMessage(net.glowstone.net.message.play.game.ChatMessage) InventoryMonitor(net.glowstone.inventory.InventoryMonitor) PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 4 with InventoryOpenEvent

use of org.bukkit.event.inventory.InventoryOpenEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldCancelInventoryOpen.

@Test
public void shouldCancelInventoryOpen() {
    // given
    HumanEntity player = mock(Player.class);
    InventoryView transaction = mock(InventoryView.class);
    given(transaction.getTitle()).willReturn("Spawn");
    given(settings.getProperty(RestrictionSettings.UNRESTRICTED_INVENTORIES)).willReturn(Collections.emptySet());
    InventoryOpenEvent event = new InventoryOpenEvent(transaction);
    given(event.getPlayer()).willReturn(player);
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
    // when
    listener.onPlayerInventoryOpen(event);
    // then
    assertThat(event.isCancelled(), equalTo(true));
    verify(player).closeInventory();
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) InventoryView(org.bukkit.inventory.InventoryView) HumanEntity(org.bukkit.entity.HumanEntity) Test(org.junit.Test)

Aggregations

InventoryOpenEvent (org.bukkit.event.inventory.InventoryOpenEvent)4 HumanEntity (org.bukkit.entity.HumanEntity)2 Message (com.flowpowered.network.Message)1 BukkitInventory (io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory)1 BukkitPlayer (io.github.wysohn.triggerreactor.bukkit.bridge.player.BukkitPlayer)1 DelegatedPlayer (io.github.wysohn.triggerreactor.bukkit.tools.DelegatedPlayer)1 IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)1 IPlayer (io.github.wysohn.triggerreactor.core.bridge.player.IPlayer)1 InventoryTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger)1 Trigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger)1 ProcessInterrupter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter)1 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)1 UUID (java.util.UUID)1 InventoryMonitor (net.glowstone.inventory.InventoryMonitor)1 DestroyEntitiesMessage (net.glowstone.net.message.play.entity.DestroyEntitiesMessage)1 EntityAnimationMessage (net.glowstone.net.message.play.entity.EntityAnimationMessage)1 EntityMetadataMessage (net.glowstone.net.message.play.entity.EntityMetadataMessage)1 EntityVelocityMessage (net.glowstone.net.message.play.entity.EntityVelocityMessage)1 SetPassengerMessage (net.glowstone.net.message.play.entity.SetPassengerMessage)1 BlockBreakAnimationMessage (net.glowstone.net.message.play.game.BlockBreakAnimationMessage)1