Search in sources :

Example 36 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callInteractInventoryOpenEvent.

public static boolean callInteractInventoryOpenEvent(EntityPlayerMP player) {
    ItemStackSnapshot newCursor = player.inventory.getItemStack().isEmpty() ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) player.inventory.getItemStack()).createSnapshot();
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, newCursor);
    InteractInventoryEvent.Open event = SpongeEventFactory.createInteractInventoryEventOpen(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer);
    SpongeImpl.postEvent(event);
    if (event.isCancelled()) {
        player.closeScreen();
        return false;
    }
    // Custom cursor
    if (event.getCursorTransaction().getCustom().isPresent()) {
        handleCustomCursor(player, event.getCursorTransaction().getFinal());
    }
    return true;
}
Also used : SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 37 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callCraftEventPost.

public static CraftItemEvent.Craft callCraftEventPost(EntityPlayer player, CraftingInventory inventory, ItemStackSnapshot result, @Nullable CraftingRecipe recipe, Container container, List<SlotTransaction> transactions) {
    // Get previous cursor if captured
    ItemStack previousCursor = ((IMixinContainer) container).getPreviousCursor();
    if (previousCursor == null) {
        // or get the current one
        previousCursor = player.inventory.getItemStack();
    }
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackUtil.snapshotOf(previousCursor), ItemStackUtil.snapshotOf(player.inventory.getItemStack()));
    CraftItemEvent.Craft event = SpongeEventFactory.createCraftItemEventCraft(Sponge.getCauseStackManager().getCurrentCause(), result, inventory, cursorTransaction, Optional.ofNullable(recipe), ((org.spongepowered.api.item.inventory.Container) container), transactions);
    SpongeImpl.postEvent(event);
    ((IMixinContainer) container).setCaptureInventory(false);
    // handle slot-transactions
    PacketPhaseUtil.handleSlotRestore(player, container, new ArrayList<>(transactions), event.isCancelled());
    if (event.isCancelled() || !event.getCursorTransaction().isValid() || event.getCursorTransaction().getCustom().isPresent()) {
        // handle cursor-transaction
        ItemStackSnapshot newCursor = event.isCancelled() || event.getCursorTransaction().isValid() ? event.getCursorTransaction().getOriginal() : event.getCursorTransaction().getFinal();
        player.inventory.setItemStack(ItemStackUtil.fromSnapshotToNative(newCursor));
        if (player instanceof EntityPlayerMP) {
            ((EntityPlayerMP) player).connection.sendPacket(new SPacketSetSlot(-1, -1, player.inventory.getItemStack()));
        }
    }
    transactions.clear();
    ((IMixinContainer) container).setCaptureInventory(true);
    return event;
}
Also used : IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) Container(net.minecraft.inventory.Container) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack)

Example 38 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callCreativeClickInventoryEvent.

public static ClickInventoryEvent.Creative callCreativeClickInventoryEvent(EntityPlayerMP player, CPacketCreativeInventoryAction packetIn) {
    Sponge.getCauseStackManager().pushCause(player);
    // Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
    // Due to this, we can only send the original item that was clicked in slot
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
    if (((IMixinContainer) player.openContainer).getCapturedTransactions().size() == 0 && packetIn.getSlotId() >= 0 && packetIn.getSlotId() < player.openContainer.inventorySlots.size()) {
        Slot slot = player.openContainer.getSlot(packetIn.getSlotId());
        if (slot != null) {
            ItemStackSnapshot clickedItem = slot.getStack() == null ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
            SlotTransaction slotTransaction = new SlotTransaction(((org.spongepowered.api.item.inventory.Slot) slot), clickedItem, ItemStackSnapshot.NONE);
            ((IMixinContainer) player.openContainer).getCapturedTransactions().add(slotTransaction);
        }
    }
    ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer, ((IMixinContainer) player.openContainer).getCapturedTransactions());
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    return event;
}
Also used : ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot)

Example 39 with Transaction

use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callChangeBlockEventModifyLiquidBreak.

public static ChangeBlockEvent.Break callChangeBlockEventModifyLiquidBreak(net.minecraft.world.World worldIn, BlockPos pos, IBlockState state, int flags) {
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData data = phaseTracker.getCurrentPhaseData();
    BlockState fromState = BlockUtil.fromNative(worldIn.getBlockState(pos));
    BlockState toState = BlockUtil.fromNative(state);
    User owner = data.context.getOwner().orElse(null);
    User notifier = data.context.getNotifier().orElse(null);
    Object source = data.context.getSource(LocatableBlock.class).orElse(null);
    if (source == null) {
        // Fallback
        source = worldIn;
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(source);
        Sponge.getCauseStackManager().addContext(EventContextKeys.LIQUID_BREAK, (World) worldIn);
        if (owner != null) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
        }
        if (notifier != null) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
        }
        WorldProperties world = ((World) worldIn).getProperties();
        Vector3i position = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
        Transaction<BlockSnapshot> transaction = new Transaction<>(BlockSnapshot.builder().blockState(fromState).world(world).position(position).build(), BlockSnapshot.builder().blockState(toState).world(world).position(position).build());
        ChangeBlockEvent.Break event = SpongeEventFactory.createChangeBlockEventBreak(Sponge.getCauseStackManager().getCurrentCause(), Collections.singletonList(transaction));
        SpongeImpl.postEvent(event);
        return event;
    }
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Vector3i(com.flowpowered.math.vector.Vector3i) IInteractionObject(net.minecraft.world.IInteractionObject) LocatableBlock(org.spongepowered.api.world.LocatableBlock) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 40 with Transaction

use of org.spongepowered.api.data.Transaction in project Skree by Skelril.

the class WildernessWorldWrapper method onBlockBreak.

@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @Named(NamedCause.SOURCE) Entity srcEnt) {
    List<Transaction<BlockSnapshot>> transactions = event.getTransactions();
    for (Transaction<BlockSnapshot> block : transactions) {
        BlockSnapshot original = block.getOriginal();
        Optional<Location<World>> optLoc = original.getLocation();
        if (!optLoc.isPresent()) {
            continue;
        }
        Optional<Integer> optLevel = getLevel(optLoc.get());
        if (!optLevel.isPresent()) {
            continue;
        }
        int level = optLevel.get();
        Location<World> loc = optLoc.get();
        BlockState state = original.getState();
        BlockType type = state.getType();
        // Prevent item dupe glitch by removing the position before subsequent breaks
        markedOrePoints.remove(loc);
        if (config.getDropAmplificationConfig().amplifies(state) && !original.getCreator().isPresent()) {
            markedOrePoints.add(loc);
            if (srcEnt instanceof Player) {
                Optional<HighScoreService> optHighScores = Sponge.getServiceManager().provide(HighScoreService.class);
                optHighScores.ifPresent(highScoreService -> highScoreService.update((Player) srcEnt, ScoreTypes.WILDERNESS_ORES_MINED, 1));
            }
        }
        if (srcEnt instanceof Player && type.equals(BlockTypes.STONE) && Probability.getChance(Math.max(12, 250 - level))) {
            Vector3d max = loc.getPosition().add(1, 1, 1);
            Vector3d min = loc.getPosition().sub(1, 1, 1);
            Extent world = loc.getExtent();
            if (Probability.getChance(3)) {
                Entity entity = world.createEntity(EntityTypes.SILVERFISH, loc.getPosition().add(.5, 0, .5));
                world.spawnEntity(entity, Cause.source(SpawnCause.builder().type(SpawnTypes.BLOCK_SPAWNING).build()).build());
            }
            // Do this one tick later to guarantee no collision with transaction data
            Task.builder().delayTicks(1).execute(() -> {
                for (int x = min.getFloorX(); x <= max.getFloorX(); ++x) {
                    for (int z = min.getFloorZ(); z <= max.getFloorZ(); ++z) {
                        for (int y = min.getFloorY(); y <= max.getFloorY(); ++y) {
                            if (!world.containsBlock(x, y, z)) {
                                continue;
                            }
                            if (world.getBlockType(x, y, z) == BlockTypes.STONE) {
                                world.setBlockType(x, y, z, BlockTypes.MONSTER_EGG, BlockChangeFlag.NONE, Cause.source(SkreePlugin.container()).build());
                            }
                        }
                    }
                }
            }).submit(SkreePlugin.inst());
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Extent(org.spongepowered.api.world.extent.Extent) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) HighScoreService(com.skelril.skree.service.HighScoreService) BlockState(org.spongepowered.api.block.BlockState) Transaction(org.spongepowered.api.data.Transaction) BlockType(org.spongepowered.api.block.BlockType) Vector3d(com.flowpowered.math.vector.Vector3d) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Aggregations

Transaction (org.spongepowered.api.data.Transaction)50 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)32 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)28 World (org.spongepowered.api.world.World)18 ArrayList (java.util.ArrayList)15 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)14 CauseStackManager (org.spongepowered.api.event.CauseStackManager)11 Entity (org.spongepowered.api.entity.Entity)10 ClickInventoryEvent (org.spongepowered.api.event.item.inventory.ClickInventoryEvent)10 ChangeBlockEvent (org.spongepowered.api.event.block.ChangeBlockEvent)9 List (java.util.List)8 BlockPos (net.minecraft.util.math.BlockPos)8 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)8 ItemStack (org.spongepowered.api.item.inventory.ItemStack)8 CauseStack (org.lanternpowered.server.event.CauseStack)7 Sponge (org.spongepowered.api.Sponge)7 Player (org.spongepowered.api.entity.living.player.Player)7 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)7 ImmutableList (com.google.common.collect.ImmutableList)6 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)6