/** * Applies the provided {@link Predicate} to the {@link List} of * {@link Transaction}s from {@link #getTransactions()} such that * any time that {@link Predicate#test(Object)} returns <code>false</code> * on a {@link Transaction}, the {@link Transaction} is * marked as "invalid" and will not apply post event. * * <p>{@link Transaction#getFinal()} is used to construct * the {@link ItemStack} to pass to the predicate</p> * * @param predicate The predicate to use for filtering * @return The transactions for which the predicate returned * <code>false</code> */ default List<? extends Transaction<ItemStackSnapshot>> filter(Predicate<ItemStack> predicate) { List<Transaction<ItemStackSnapshot>> invalidatedTransactions = Lists.newArrayList(); this.getTransactions().stream().filter(transaction -> !predicate.test(transaction.getFinal().createStack())).forEach(transaction -> { transaction.setValid(false); invalidatedTransactions.add(transaction); }); return invalidatedTransactions; }
/** * Applies the provided {@link Predicate} to the {@link List} of * {@link Transaction}s from {@link #getTransactions()} such that * any time that {@link Predicate#test(Object)} returns <code>false</code> * on a {@link Transaction}, the {@link Transaction} is * marked as "invalid" and will not apply post event. * * <p>{@link Transaction#getFinal()} is used to construct * the {@link ItemStack} to pass to the predicate</p> * * @param predicate The predicate to use for filtering * @return The transactions for which the predicate returned <code>false</code> */ default List<? extends Transaction<ItemStackSnapshot>> filter(Predicate<ItemStack> predicate) { List<Transaction<ItemStackSnapshot>> invalidatedTransactions = Lists.newArrayList(); this.getTransactions().stream().filter(transaction -> !predicate.test(transaction.getFinal().createStack())).forEach(transaction -> { transaction.setValid(false); invalidatedTransactions.add(transaction); }); return invalidatedTransactions; }
/** * Helper method for formatting item container data. * * @param transaction */ private void writeItemTransaction(Transaction<ItemStackSnapshot> transaction) { checkNotNull(transaction); String itemId = transaction.getFinal().getType().getId(); int itemQuantity = transaction.getFinal().getQuantity(); if (transaction.getOriginal().getType() != ItemTypes.NONE) { itemQuantity -= transaction.getOriginal().getQuantity(); } data.set(DataQueries.Target, itemId); data.set(DataQueries.Quantity, itemQuantity); }
@Listener public void onBlockChange(ChangeBlockEvent.Post event) { event.getTransactions().forEach(blockSnapshotTransaction -> { Location<World> baseLocation = blockSnapshotTransaction.getFinal().getLocation().get(); BlockState originalState = blockSnapshotTransaction.getOriginal().getExtendedState(); List<Location<World>> icCheckSpots = new ArrayList<>(); icCheckSpots.addAll(blockSnapshotTransaction.getFinal().get(Keys.WIRE_ATTACHMENTS).map(Map::keySet).orElse(EnumSet.noneOf(Direction.class)) .stream().map(baseLocation::getRelative) .collect(Collectors.toList()));
@Listener public void placeBlock(ChangeBlockEvent.Place e, @Root Player player) { if (isOnPipe(player.getUniqueId())) { Location loc = e.getTransactions().get(0).getFinal().getLocation().get(); getUserConnection(player.getUniqueId()) .get(EntityTracker.class) .addBlockInteraction(new Position((long) loc.getX(), (long) loc.getY(), (long) loc.getZ())); } } }
/** * Helper method for writing block transaction data, using only * the final replacement value. We must alter the data structure * slightly to avoid duplication, decoupling location from blocks, etc. * * @param transaction BlockTransaction representing a block change in the world. */ private void writeBlockTransaction(Transaction<BlockSnapshot> transaction) { checkNotNull(transaction); // Location DataContainer location = transaction.getOriginal().getLocation().get().toContainer(); location.remove(DataQueries.BlockType); location.remove(DataQueries.WorldName); location.remove(DataQueries.ContentVersion); data.set(DataQueries.Location, location); // Storing the state only, so we don't also get location data.set(DataQueries.OriginalBlock, formatBlockDataContainer(transaction.getOriginal())); data.set(DataQueries.ReplacementBlock, formatBlockDataContainer(transaction.getFinal())); // Use the transaction data directly, so we never worry about different data formats if (this.eventName.equals("place")) { data.set(DataQueries.Target, transaction.getFinal().getState().getType().getId().replace("_", " ")); } else { data.set(DataQueries.Target, transaction.getOriginal().getState().getType().getId().replace("_", " ")); } }
if (result.getTransaction().isPresent()) { Object rawOriginal = result.getTransaction().get().getOriginal(); Object rawFinal = result.getTransaction().get().getFinal();
@Listener public void onInventoryClick(ClickInventoryEvent e, @Root Player player) { for (SlotTransaction transaction : e.getTransactions()) { if (ArmorType.isArmor(transaction.getFinal().getType().getId()) || ArmorType.isArmor(e.getCursorTransaction().getFinal().getType().getId())) { sendDelayedArmorUpdate(player); break; } } }
@Listener public void onInventoryClick(ClickInventoryEvent e, @Root Player player) { for (SlotTransaction transaction : e.getTransactions()) { if (ArmorType.isArmor(transaction.getFinal().getType().getId()) || ArmorType.isArmor(e.getCursorTransaction().getFinal().getType().getId())) { sendDelayedArmorUpdate(player); break; } } }
BlockType finalBlock = transaction.getFinal().getState().getType();