use of io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory in project TriggerReactor by wysohn.
the class InventoryEditManager method onClose.
@Listener
public void onClose(InteractInventoryEvent.Close e) {
Player p = e.getCause().first(Player.class).orElse(null);
if (p == null) {
return;
}
UUID u = p.getUniqueId();
if (!sessions.containsKey(u)) {
return;
}
if (suspended.containsKey(u)) {
return;
}
Inventory inv = e.getTargetInventory();
GridInventory gridInv = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(GridInventory.class)).first();
InventoryTrigger trigger = sessions.get(u);
int size = gridInv.capacity();
Inventory newInv = createInventory(size, trigger.getInfo().getTriggerName());
GridInventory newGrid = newInv.query(QueryOperationTypes.INVENTORY_TYPE.of(GridInventory.class));
for (int i = 0; i < size; i++) {
SlotIndex index = new SlotIndex(i);
gridInv.getSlot(index).ifPresent(slot -> newGrid.set(index, slot.peek().orElse(ItemStack.of(ItemTypes.AIR))));
}
suspended.put(u, new SpongeInventory(newGrid, null));
p.sendMessage(savePrompt);
}
use of io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory in project TriggerReactor by wysohn.
the class TriggerReactor 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) {
// is still running after the inventory is closed.
if (e instanceof InteractInventoryEvent.Open || e instanceof InteractInventoryEvent.Close) {
Inventory inv = ((InteractInventoryEvent) e).getTargetInventory();
if (!(inv instanceof CarriedInventory))
return false;
CarriedInventory inventory = (CarriedInventory) inv;
Carrier carrier = (Carrier) inventory.getCarrier().orElse(null);
if (carrier == null)
return false;
// it's not GUI so stop execution
return !inventoryMap.containsKey(new SpongeInventory(inv, carrier));
}
return false;
}
@Override
public boolean onCommand(Object context, String command, Object[] args) {
if ("CALL".equalsIgnoreCase(command)) {
if (args.length < 1)
throw new RuntimeException("Need parameter [String] or [String, boolean]");
if (args[0] instanceof String) {
Trigger trigger = getNamedTriggerManager().get((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".equalsIgnoreCase(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!");
}
} else if ("COOLDOWN".equalsIgnoreCase(command)) {
if (!(args[0] instanceof Number))
throw new RuntimeException(args[0] + " is not a number!");
long mills = (long) (((Number) args[0]).doubleValue() * 1000L);
if (e instanceof Event) {
((Event) e).getCause().first(Player.class).ifPresent((player) -> {
UUID uuid = player.getUniqueId();
cooldowns.put(uuid, System.currentTimeMillis() + mills);
});
}
return true;
}
return false;
}
@Override
public Object onPlaceholder(Object context, String placeholder, Object[] args) {
// }
return null;
}
};
}
use of io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory in project TriggerReactor by wysohn.
the class InventoryEditManager method startEdit.
@Override
public void startEdit(IPlayer player, InventoryTrigger trigger) {
UUID u = player.getUniqueId();
if (sessions.containsKey(u)) {
return;
}
sessions.put(u, trigger);
int size = trigger.getItems().length;
Inventory inv = createInventory(size, trigger.getInfo().getTriggerName());
fillInventory(trigger, size, inv);
player.openInventory(new SpongeInventory(inv, null));
}
use of io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory in project TriggerReactor by wysohn.
the class InventoryTriggerManager method onOpen.
@Listener
public void onOpen(InteractInventoryEvent.Open e) {
Inventory inv = e.getTargetInventory();
if (!(inv instanceof CarriedInventory))
return;
CarriedInventory inventory = (CarriedInventory) inv;
Carrier carrier = (Carrier) inventory.getCarrier().orElse(null);
if (carrier == null)
return;
if (!this.hasInventoryOpen(new SpongeInventory(inventory, carrier)))
return;
InventoryTrigger trigger = getTriggerForOpenInventory(new SpongeInventory(inventory, carrier));
Map<String, Object> varMap = getSharedVarsForInventory(new SpongeInventory(inventory, carrier));
varMap.put("player", e.getCause().first(Player.class).orElse(null));
varMap.put("trigger", "open");
Inventory grids = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(GridInventory.class));
varMap.put("inventory", grids.first());
trigger.activate(e, varMap);
}
use of io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory in project TriggerReactor by wysohn.
the class InventoryTriggerManager method createInventory.
@Override
protected IInventory createInventory(int size, String name) {
name = name.replaceAll("_", " ");
Text text = TextUtil.colorStringToText(name);
Carrier dummy = new DummyCarrier();
Inventory inv = Inventory.builder().of(InventoryArchetypes.CHEST).withCarrier(dummy).property(InventoryDimension.PROPERTY_NAME, InventoryDimension.of(9, size / 9)).property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(text)).build(plugin);
return new SpongeInventory(inv, dummy);
}
Aggregations