use of org.spongepowered.api.data.Transaction in project Skree by Skelril.
the class BuildWorldWrapper method onBlockBreak.
@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @Named(NamedCause.SOURCE) Entity srcEnt) {
if (!isApplicable(srcEnt)) {
return;
}
List<Transaction<BlockSnapshot>> transactions = event.getTransactions();
for (Transaction<BlockSnapshot> block : transactions) {
BlockSnapshot original = block.getOriginal();
if (original.getCreator().isPresent()) {
continue;
}
Optional<Location<World>> optLoc = original.getLocation();
if (!optLoc.isPresent()) {
continue;
}
Location<World> loc = optLoc.get();
BlockState state = original.getState();
// Prevent item dupe glitch by removing the position before subsequent breaks
markedOrePoints.remove(loc);
if (config.getDropModification().blocks(state)) {
markedOrePoints.add(loc);
}
}
}
use of org.spongepowered.api.data.Transaction in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method createChangeBlockEventBreak.
public static ChangeBlockEvent.Break createChangeBlockEventBreak(BlockEvent.BreakEvent forgeEvent) {
final BlockPos pos = forgeEvent.getPos();
final net.minecraft.world.World world = forgeEvent.getWorld();
if (world.isRemote) {
return null;
}
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
BlockSnapshot originalSnapshot = ((World) forgeEvent.getWorld()).createSnapshot(pos.getX(), pos.getY(), pos.getZ());
BlockSnapshot finalSnapshot = BlockTypes.AIR.getDefaultState().snapshotFor(new Location<>((World) world, VecHelper.toVector3d(pos)));
ImmutableList<Transaction<BlockSnapshot>> blockSnapshots = new ImmutableList.Builder<Transaction<BlockSnapshot>>().add(new Transaction<>(originalSnapshot, finalSnapshot)).build();
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
EntityPlayer player = forgeEvent.getPlayer();
if (SpongeImplHooks.isFakePlayer(player)) {
Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
} else if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(owner);
}
} else {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_BREAK, (World) world);
return SpongeEventFactory.createChangeBlockEventBreak(Sponge.getCauseStackManager().getCurrentCause(), blockSnapshots);
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class OpenMenuTransaction method generateEvent.
@Override
public Optional<InteractContainerEvent> generateEvent(final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, final ImmutableList<GameTransaction<InteractContainerEvent>> gameTransactions, final Cause currentCause) {
final ItemStackSnapshot resultingCursor = ItemStackUtil.snapshotOf(this.player.inventory.getCarried());
final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(this.cursor, resultingCursor);
final InteractContainerEvent.Open event = SpongeEventFactory.createInteractContainerEventOpen(currentCause, (Container) this.menu, cursorTransaction);
return Optional.of(event);
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class BlockEventBasedTransaction method generateEvent.
@Override
public final Optional<ChangeBlockEvent.All> generateEvent(final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, final ImmutableList<GameTransaction<ChangeBlockEvent.All>> transactions, final Cause currentCause) {
final Optional<ServerWorld> o = ((SpongeServer) SpongeCommon.server()).worldManager().world(this.worldKey);
if (!o.isPresent()) {
return Optional.empty();
}
final ListMultimap<BlockPos, SpongeBlockSnapshot> positions = LinkedListMultimap.create();
for (final GameTransaction<@NonNull ?> transaction : transactions) {
final BlockEventBasedTransaction blockTransaction = (BlockEventBasedTransaction) transaction;
if (!positions.containsKey(blockTransaction.affectedPosition)) {
positions.put(blockTransaction.affectedPosition, blockTransaction.getOriginalSnapshot());
}
if (blockTransaction.getResultingSnapshot() != null) {
positions.put(blockTransaction.affectedPosition, blockTransaction.getResultingSnapshot());
}
}
final ImmutableList<BlockTransaction> eventTransactions = positions.asMap().values().stream().map(spongeBlockSnapshots -> {
final List<SpongeBlockSnapshot> snapshots = new ArrayList<>(spongeBlockSnapshots);
if (snapshots.isEmpty() || snapshots.size() < 2) {
// Error case
return Optional.<BlockTransaction>empty();
}
final SpongeBlockSnapshot original = snapshots.get(0);
final SpongeBlockSnapshot result = snapshots.get(snapshots.size() - 1);
final ImmutableList<BlockSnapshot> intermediary;
if (snapshots.size() > 2) {
intermediary = ImmutableList.copyOf(snapshots.subList(1, snapshots.size() - 2));
} else {
intermediary = ImmutableList.of();
}
final Operation operation = context.getBlockOperation(original, result);
final BlockTransaction eventTransaction = new BlockTransaction(original, result, intermediary, operation);
return Optional.of(eventTransaction);
}).filter(Optional::isPresent).map(Optional::get).collect(ImmutableList.toImmutableList());
if (eventTransactions.isEmpty()) {
return Optional.empty();
}
return Optional.of(SpongeEventFactory.createChangeBlockEventAll(currentCause, eventTransactions, o.get()));
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method callEnchantEventEnchantPost.
public static EnchantItemEvent.Post callEnchantEventEnchantPost(Player playerIn, EnchantmentMenu container, SlotTransaction enchantedItem, SlotTransaction lapisItem, int option, int seed) {
org.spongepowered.api.item.inventory.Container enchantContainer = ContainerUtil.fromNative(container);
ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(playerIn.inventory.getCarried());
Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(cursor, cursor);
List<SlotTransaction> slotTrans = new ArrayList<>();
slotTrans.add(lapisItem);
slotTrans.add(enchantedItem);
EnchantItemEvent.Post event = SpongeEventFactory.createEnchantItemEventPost(PhaseTracker.getCauseStackManager().currentCause(), enchantContainer, cursorTrans, enchantedItem.slot(), Optional.empty(), slotTrans, option, seed);
SpongeCommon.post(event);
PacketPhaseUtil.handleSlotRestore(playerIn, container, event.transactions(), event.isCancelled());
PacketPhaseUtil.handleCursorRestore(playerIn, event.cursorTransaction());
return event;
}
Aggregations