@Override @Deprecated public Optional<ItemStackSnapshot> getOriginalItemStack() { return Optional.ofNullable(this.getTransaction().getOriginal()); }
/** * 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 the location of the {@link Transaction}, the {@link Transaction} is * marked as "invalid" and will not apply post event. * * <p>{@link Transaction#getOriginal()} is used to get the {@link Location}</p> * * @param predicate The predicate to use for filtering * @return The transactions for which the predicate returned * <code>false</code> */ default List<Transaction<BlockSnapshot>> filter(Predicate<Location<World>> predicate) { List<Transaction<BlockSnapshot>> invalidatedTransactions = Lists.newArrayList(); for (Transaction<BlockSnapshot> transaction: this.getTransactions()) { if (!predicate.test(transaction.getOriginal().getLocation().get())) { transaction.setValid(false); invalidatedTransactions.add(transaction); } } return invalidatedTransactions; }
@Test public void testChangeBlockEvent_filter() { Transaction<BlockSnapshot> transaction = new Transaction<>(mockParam(BlockSnapshot.class), mockParam(BlockSnapshot.class)); when(transaction.getOriginal().getLocation()).thenReturn(Optional.of(new Location<>(mockParam(World.class), Vector3d.ZERO))); ChangeBlockEvent.Break event = SpongeEventFactory.createChangeBlockEventBreak( Cause.of(EventContext.empty(), "none"), Lists.newArrayList(transaction)); event.filter(location -> false); assertThat(transaction.isValid(), is(false)); }
@Listener public void onBlockBreak(ChangeBlockEvent.Break event) { event.getTransactions().stream().map(transaction -> transaction.getOriginal().getLocation().get()).forEach(location -> { if (loadedICs.containsKey(location)) { IC ic = loadedICs.remove(location); ic.unload(); if (ic instanceof SelfTriggeringIC) { ((SpongeSelfTriggerManager) CraftBookPlugin.inst().getSelfTriggerManager().get()).unregister(this, location); } } }); }
/** * 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 the location of the {@link Transaction}, the {@link Transaction} is * marked as "invalid" and will not apply post event. * * <p>{@link Transaction#getOriginal()} is used to get the {@link Location}</p> * * @param predicate The predicate to use for filtering * @return The transactions for which the predicate returned <code>false</code> */ default List<Transaction<BlockSnapshot>> filter(Predicate<Location<World>> predicate) { List<Transaction<BlockSnapshot>> invalidatedTransactions = Lists.newArrayList(); for (Transaction<BlockSnapshot> transaction: this.getTransactions()) { if (!predicate.test(transaction.getOriginal().getLocation().get())) { transaction.setValid(false); invalidatedTransactions.add(transaction); } } return invalidatedTransactions; }
@Listener public void onBlockBreak(ChangeBlockEvent.Break event) { event.getTransactions().stream().forEach((trans) -> { if ((trans.getOriginal().getState().getType().equals(BlockTypes.WALL_SIGN) || trans.getOriginal().getState().getType().equals(BlockTypes.STANDING_SIGN))) { Optional<Location<World>> locationOptional = trans.getOriginal().getLocation(); Optional<Player> playerOptional = event.getCause().first(Player.class); if (!removeSign(playerOptional.get(), SpongeSerializedBlockLocation.create(locationOptional.get()))) { event.setCancelled(true); } } }); }
@Listener public void onBlockBreak(ChangeBlockEvent.Break event) { event.getTransactions().stream().forEach((trans) -> { if ((trans.getOriginal().getState().getType().equals(BlockTypes.WALL_SIGN) || trans.getOriginal().getState().getType().equals(BlockTypes.STANDING_SIGN))) { Optional<Location<World>> locationOptional = trans.getOriginal().getLocation(); Optional<Player> playerOptional = event.getCause().first(Player.class); if (!removeSign(playerOptional.get(), SpongeSerializedBlockLocation.create(locationOptional.get()))) { event.setCancelled(true); } } }); } }
@Listener public void onBlockBreak(ChangeBlockEvent.Break event) { event.getTransactions().forEach((transaction) -> transaction.getOriginal().getLocation().ifPresent((location) -> { Chair<?> chair = getChair(location); if (chair != null) removeChair(chair, true); })); }
@Listener public void onBlockBreak(ChangeBlockEvent.Break event, @First Player player) { if (event.getCause().containsType(PluginContainer.class)) { return; //Ignore plugin caused breaks. } if(ItemUtil.doesStackPassFilters(allowedItems.getValue(), player.getItemInHand(HandTypes.MAIN_HAND).orElse(null)) && !disabledPlayers.getValue().contains(player.getUniqueId())) { event.getTransactions().stream().filter((t) -> BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), t.getOriginal().getState())).forEach(transaction -> { Optional<TreeType> treeType = transaction.getOriginal().get(Keys.TREE_TYPE); treeType.ifPresent(treeType1 -> transaction.getOriginal().getLocation().ifPresent(location -> { List<Location> traversed = Lists.newArrayList(location); for(Direction dir : BlockUtil.getDirectFaces()) { checkBlocks(location.getRelative(dir), player, treeType1, traversed); } })); }); } }
/** * 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 onBlockBreak(ChangeBlockEvent.Break event) { event.getTransactions().stream() .filter(transaction -> transaction.getOriginal().getState().getType() == BlockTypes.SKULL) .map(Transaction::getOriginal) .forEach(snapshot -> {
/** * 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("_", " ")); } }
event.getTransactions().forEach(blockSnapshotTransaction -> { Location<World> baseLocation = blockSnapshotTransaction.getFinal().getLocation().get(); BlockState originalState = blockSnapshotTransaction.getOriginal().getExtendedState(); List<Location<World>> icCheckSpots = new ArrayList<>(); boolean wasPowered = false;
public void onChangeBlock(ChangeBlockEvent event) { List<Transaction<BlockSnapshot>> transactions = event.getTransactions(); Transaction<BlockSnapshot> first = transactions.get(0); BlockSnapshot original = first.getOriginal(); Optional<World> world = SpongeMain.THIS.getServer().getWorld(original.getWorldUniqueId()); String worldName = world.get().getName(); Location loc = SpongeUtil.getLocation(worldName, original.getPosition()); PlotArea area = loc.getPlotArea(); if (area == null) { return; } Plot plot = area.getPlot(loc); if (plot == null) { if (!loc.isPlotArea()) { return; } event.setCancelled(true); return; } event.filter(loc1 -> !SpongeUtil.getLocation(worldName, loc1).isPlotRoad()); }
Object rawOriginal = result.getTransaction().get().getOriginal(); Object rawFinal = result.getTransaction().get().getFinal();
List<Transaction<BlockSnapshot>> transactions = event.getTransactions(); Transaction<BlockSnapshot> first = transactions.get(0); BlockSnapshot pos = first.getOriginal(); Optional<World> world = SpongeMain.THIS.getServer().getWorld(pos.getWorldUniqueId()); String worldName = world.get().getName();
List<Transaction<BlockSnapshot>> transactions = event.getTransactions(); Transaction<BlockSnapshot> first = transactions.get(0); BlockSnapshot original = first.getOriginal(); Optional<World> world = SpongeMain.THIS.getServer().getWorld(original.getWorldUniqueId()); String worldName = world.get().getName();
BlockType original = transaction.getOriginal().getState().getType(); BlockType finalBlock = transaction.getFinal().getState().getType();