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;
}
};
}
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);
}
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();
}
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();
}
Aggregations