Search in sources :

Example 1 with SpongeInventory

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);
}
Also used : IPlayer(io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer) SpongePlayer(io.github.wysohn.triggerreactor.sponge.bridge.entity.SpongePlayer) Player(org.spongepowered.api.entity.living.player.Player) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.inventory.InventoryTrigger) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) UUID(java.util.UUID) Inventory(org.spongepowered.api.item.inventory.Inventory) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) Listener(org.spongepowered.api.event.Listener)

Example 2 with SpongeInventory

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;
        }
    };
}
Also used : IPlayer(io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer) Player(org.spongepowered.api.entity.living.player.Player) DelegatedPlayer(io.github.wysohn.triggerreactor.sponge.tools.DelegatedPlayer) SpongePlayer(io.github.wysohn.triggerreactor.sponge.bridge.entity.SpongePlayer) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) Cancellable(org.spongepowered.api.event.Cancellable) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) ProcessInterrupter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.Trigger) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.inventory.InventoryTrigger) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) GameReloadEvent(org.spongepowered.api.event.game.GameReloadEvent) GameStartedServerEvent(org.spongepowered.api.event.game.state.GameStartedServerEvent) GameStoppingServerEvent(org.spongepowered.api.event.game.state.GameStoppingServerEvent) TriggerReactorStopEvent(io.github.wysohn.triggerreactor.sponge.manager.event.TriggerReactorStopEvent) TabCompleteEvent(org.spongepowered.api.event.command.TabCompleteEvent) Event(org.spongepowered.api.event.Event) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) IEvent(io.github.wysohn.triggerreactor.core.bridge.event.IEvent) TriggerReactorStartEvent(io.github.wysohn.triggerreactor.sponge.manager.event.TriggerReactorStartEvent) GameAboutToStartServerEvent(org.spongepowered.api.event.game.state.GameAboutToStartServerEvent) SendCommandEvent(org.spongepowered.api.event.command.SendCommandEvent) Carrier(org.spongepowered.api.item.inventory.Carrier) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory)

Example 3 with SpongeInventory

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));
}
Also used : SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) UUID(java.util.UUID) Inventory(org.spongepowered.api.item.inventory.Inventory) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory)

Example 4 with SpongeInventory

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);
}
Also used : SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.inventory.InventoryTrigger) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Listener(org.spongepowered.api.event.Listener)

Example 5 with SpongeInventory

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);
}
Also used : SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) Text(org.spongepowered.api.text.Text) SpongeInventory(io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory)

Aggregations

IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)7 SpongeInventory (io.github.wysohn.triggerreactor.sponge.bridge.SpongeInventory)7 GridInventory (org.spongepowered.api.item.inventory.type.GridInventory)6 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)5 InventoryTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.inventory.InventoryTrigger)4 SpongePlayer (io.github.wysohn.triggerreactor.sponge.bridge.entity.SpongePlayer)4 Listener (org.spongepowered.api.event.Listener)4 Player (org.spongepowered.api.entity.living.player.Player)3 Inventory (org.spongepowered.api.item.inventory.Inventory)3 IPlayer (io.github.wysohn.triggerreactor.core.bridge.entity.IPlayer)2 UUID (java.util.UUID)2 SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)2 IEvent (io.github.wysohn.triggerreactor.core.bridge.event.IEvent)1 Trigger (io.github.wysohn.triggerreactor.core.manager.trigger.Trigger)1 ProcessInterrupter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter)1 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)1 TriggerReactorStartEvent (io.github.wysohn.triggerreactor.sponge.manager.event.TriggerReactorStartEvent)1 TriggerReactorStopEvent (io.github.wysohn.triggerreactor.sponge.manager.event.TriggerReactorStopEvent)1 DelegatedPlayer (io.github.wysohn.triggerreactor.sponge.tools.DelegatedPlayer)1 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)1