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