Search in sources :

Example 11 with Action

use of org.cubeengine.module.vigil.report.Action in project modules-extra by CubeEngine.

the class ChangeInventoryReport method showReport.

@Override
public void showReport(List<Action> actions, Receiver receiver) {
    Text cause = Recall.cause(actions.get(0));
    LinkedList<Transaction<ItemStack>> transactions = new LinkedList<>();
    for (Action action : actions) {
        List<Map<String, Object>> changes = action.getData(INVENTORY_CHANGES);
        for (Map<String, Object> change : changes) {
            ItemStack originStack = Recall.item(((Map<String, Object>) change.get(ORIGINAL))).get().createStack();
            ItemStack finalStack = Recall.item(((Map<String, Object>) change.get(REPLACEMENT))).get().createStack();
            if (COMPARATOR.compare(originStack, finalStack) == 0) {
                if (originStack.getQuantity() > finalStack.getQuantity()) {
                    ItemStack stack = originStack;
                    stack.setQuantity(originStack.getQuantity() - finalStack.getQuantity());
                    originStack = stack;
                    finalStack = ItemStack.empty();
                } else {
                    ItemStack stack = finalStack;
                    stack.setQuantity(finalStack.getQuantity() - originStack.getQuantity());
                    finalStack = stack;
                    originStack = ItemStack.empty();
                }
            }
            boolean added = false;
            for (Transaction<ItemStack> trans : transactions) {
                if (originStack.isEmpty()) {
                    if (COMPARATOR.compare(trans.getFinal(), finalStack) == 0 && trans.getOriginal().isEmpty()) {
                        trans.getFinal().setQuantity(trans.getFinal().getQuantity() + finalStack.getQuantity());
                        added = true;
                        break;
                    } else if (trans.getFinal().isEmpty() && COMPARATOR.compare(trans.getOriginal(), finalStack) == 0) {
                        trans.getOriginal().setQuantity(trans.getOriginal().getQuantity() - finalStack.getQuantity());
                        added = true;
                        break;
                    } else if (COMPARATOR.compare(trans.getOriginal(), finalStack) == 0) {
                        break;
                    }
                }
                if (finalStack.isEmpty()) {
                    if (COMPARATOR.compare(trans.getOriginal(), originStack) == 0) {
                        trans.getOriginal().setQuantity(trans.getOriginal().getQuantity() + originStack.getQuantity());
                        added = true;
                        break;
                    } else if (trans.getOriginal().isEmpty() && COMPARATOR.compare(trans.getFinal(), originStack) == 0) {
                        trans.getFinal().setQuantity(trans.getFinal().getQuantity() - originStack.getQuantity());
                        added = true;
                        break;
                    } else if (COMPARATOR.compare(trans.getFinal(), originStack) == 0) {
                        break;
                    }
                }
            }
            if (!added) {
                transactions.addFirst(new Transaction<>(originStack, finalStack));
            }
        }
    }
    Collections.reverse(transactions);
    for (Transaction<ItemStack> trans : transactions) {
        ItemStack stack1 = trans.getOriginal();
        ItemStack stack2 = trans.getFinal();
        if (stack1.isEmpty() && stack2.isEmpty()) {
            continue;
        }
        if (stack1.getType() == ItemTypes.AIR) {
            receiver.sendReport(this, actions, "{txt} inserted {txt}", cause, ReportUtil.name(stack2.createSnapshot()));
        } else if (stack2.getType() == ItemTypes.AIR) {
            receiver.sendReport(this, actions, "{txt} took {txt}", cause, ReportUtil.name(stack1.createSnapshot()));
        } else {
            receiver.sendReport(this, actions, "{txt} swapped {txt} with {txt}", cause, ReportUtil.name(stack1.createSnapshot()), ReportUtil.name(stack2.createSnapshot()));
        }
    }
}
Also used : Action(org.cubeengine.module.vigil.report.Action) Text(org.spongepowered.api.text.Text) LinkedList(java.util.LinkedList) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map)

Example 12 with Action

use of org.cubeengine.module.vigil.report.Action in project modules-extra by CubeEngine.

the class ChangeInventoryReport method listen.

@Listener
public void listen(ClickInventoryEvent event) {
    List<SlotTransaction> upperTransactions = new ArrayList<>();
    int upperSize = event.getTargetInventory().iterator().next().capacity();
    for (SlotTransaction transaction : event.getTransactions()) {
        Integer affectedSlot = transaction.getSlot().getInventoryProperty(SlotIndex.class).map(SlotIndex::getValue).orElse(-1);
        boolean upper = affectedSlot != -1 && affectedSlot < upperSize;
        if (upper) {
            upperTransactions.add(transaction);
        }
    }
    Inventory te = event.getTargetInventory().query(QueryOperationTypes.TYPE.of(BlockCarrier.class));
    if (!(te instanceof BlockCarrier)) {
        te = te.first();
    }
    if (te instanceof BlockCarrier) {
        Action action = this.observe(event);
        action.addData(INVENTORY_CHANGES, Observe.transactions(upperTransactions));
        action.addData(Report.LOCATION, Observe.location(((BlockCarrier) te).getLocation()));
        this.report(action);
    }
}
Also used : Action(org.cubeengine.module.vigil.report.Action) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) ArrayList(java.util.ArrayList) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) Listener(org.spongepowered.api.event.Listener)

Example 13 with Action

use of org.cubeengine.module.vigil.report.Action in project modules-extra by CubeEngine.

the class InventoryOpenReport method observe.

@Override
public Action observe(InteractInventoryEvent.Open event) {
    Action action = newReport();
    action.addData(CAUSE, Observe.causes(event.getCause()));
    // TODO get TargetInventory
    return action;
}
Also used : Action(org.cubeengine.module.vigil.report.Action)

Example 14 with Action

use of org.cubeengine.module.vigil.report.Action in project modules-extra by CubeEngine.

the class QueryManager method lookup.

private List<Action> lookup(Lookup lookup, Query query) {
    lookup.time(Lookup.LookupTiming.LOOKUP);
    List<Action> actions = new ArrayList<>();
    FindIterable<Document> results = query.find(db).sort(new Document("date", -1));
    for (Document result : results) {
        actions.add(new Action(result));
    }
    lookup.time(Lookup.LookupTiming.LOOKUP);
    return actions;
}
Also used : Action(org.cubeengine.module.vigil.report.Action) ArrayList(java.util.ArrayList) Document(org.bson.Document)

Example 15 with Action

use of org.cubeengine.module.vigil.report.Action in project modules-extra by CubeEngine.

the class QueryManager method prepareReports.

private List<ReportActions> prepareReports(Lookup lookup, Player player, List<Action> results) {
    lookup.time(Lookup.LookupTiming.REPORT);
    List<ReportActions> reportActions = new ArrayList<>();
    ReportActions last = null;
    for (Action action : results) {
        Report report = reportManager.reportOf(action);
        if (last == null) {
            last = new ReportActions(report);
            reportActions.add(last);
        }
        if (!last.add(action, report, lookup)) {
            last = new ReportActions(report);
            reportActions.add(last);
            last.add(action, report, lookup);
        }
    }
    lookup.time(Lookup.LookupTiming.REPORT);
    return reportActions;
}
Also used : Action(org.cubeengine.module.vigil.report.Action) Report(org.cubeengine.module.vigil.report.Report) ReportActions(org.cubeengine.module.vigil.report.ReportActions) ArrayList(java.util.ArrayList)

Aggregations

Action (org.cubeengine.module.vigil.report.Action)24 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)5 Text (org.spongepowered.api.text.Text)4 ArrayList (java.util.ArrayList)3 Recall (org.cubeengine.module.vigil.report.Recall)3 Map (java.util.Map)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)2 TextActions.showText (org.spongepowered.api.text.action.TextActions.showText)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 UUID (java.util.UUID)1 Document (org.bson.Document)1 Report (org.cubeengine.module.vigil.report.Report)1 ReportActions (org.cubeengine.module.vigil.report.ReportActions)1 Transaction (org.spongepowered.api.data.Transaction)1 MutableBoundedValue (org.spongepowered.api.data.value.mutable.MutableBoundedValue)1 EntitySnapshot (org.spongepowered.api.entity.EntitySnapshot)1 Living (org.spongepowered.api.entity.living.Living)1