Search in sources :

Example 16 with Transaction

use of org.spongepowered.api.data.Transaction 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()));
        }
    }
}
Also used : Action(org.cubeengine.module.vigil.report.Action) Text(org.spongepowered.api.text.Text) LinkedList(java.util.LinkedList) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map)

Example 17 with Transaction

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

the class LivingEntityMixin method impl$onSetHeldItem.

@Redirect(method = "completeUsingItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;setItemInHand(Lnet/minecraft/world/InteractionHand;Lnet/minecraft/world/item/ItemStack;)V"))
private void impl$onSetHeldItem(final LivingEntity self, final InteractionHand hand, final ItemStack stack) {
    if (this.level.isClientSide) {
        self.setItemInHand(hand, stack);
        return;
    }
    // Unforunately, ItemFood calls ItemStack#shrink in Item#onItemUseFinish.
    // To ensure that we provide the original ItemStack in the event,
    // we make a copy of in our onUpdateItemUse redirect
    // If the event or transaction is cancelled, we make sure to explicitly
    // set the copy back in the player's hand, since it may have been already
    // modified if an ItemFood is being used.
    final ItemStackSnapshot activeItemStackSnapshot = ItemStackUtil.snapshotOf(this.impl$activeItemStackCopy == null ? ItemStack.EMPTY : this.impl$activeItemStackCopy);
    final UseItemStackEvent.Replace event;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack == null ? ItemStack.EMPTY : stack);
        final HandType handType = (HandType) (Object) hand;
        this.impl$addSelfToFrame(frame, activeItemStackSnapshot, handType);
        final Ticks useItemRemainingTicks = Ticks.of(this.useItemRemaining);
        event = SpongeEventFactory.createUseItemStackEventReplace(PhaseTracker.getCauseStackManager().currentCause(), useItemRemainingTicks, useItemRemainingTicks, activeItemStackSnapshot, new Transaction<>(ItemStackUtil.snapshotOf(this.impl$activeItemStackCopy), snapshot));
    }
    if (SpongeCommon.post(event)) {
        this.shadow$setItemInHand(hand, this.impl$activeItemStackCopy.copy());
        return;
    }
    if (!event.itemStackResult().isValid()) {
        this.shadow$setItemInHand(hand, this.impl$activeItemStackCopy.copy());
        return;
    }
    this.shadow$setItemInHand(hand, ItemStackUtil.fromSnapshotToNative(event.itemStackResult().finalReplacement()));
}
Also used : PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Transaction(org.spongepowered.api.data.Transaction) UseItemStackEvent(org.spongepowered.api.event.item.inventory.UseItemStackEvent) CauseStackManager(org.spongepowered.api.event.CauseStackManager) HandType(org.spongepowered.api.data.type.HandType) Ticks(org.spongepowered.api.util.Ticks) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 18 with Transaction

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

the class ServerLevelMixin method impl$onSetWeatherParameters.

@Inject(method = "tick", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "FIELD", target = "Lnet/minecraft/server/level/ServerLevel;oRainLevel:F", shift = At.Shift.BEFORE, ordinal = 1))
public void impl$onSetWeatherParameters(final BooleanSupplier param0, final CallbackInfo ci, final ProfilerFiller var0, final boolean wasRaining) {
    final boolean isRaining = this.shadow$isRaining();
    if (this.oRainLevel != this.rainLevel || this.oThunderLevel != this.thunderLevel || wasRaining != isRaining) {
        Weather newWeather = ((ServerWorld) this).properties().weather();
        final Cause currentCause = Sponge.server().causeStackManager().currentCause();
        final Transaction<Weather> weatherTransaction = new Transaction<>(this.impl$prevWeather, newWeather);
        final ChangeWeatherEvent event = SpongeEventFactory.createChangeWeatherEvent(currentCause, ((ServerWorld) this), weatherTransaction);
        if (Sponge.eventManager().post(event)) {
            newWeather = event.weather().original();
        } else {
            newWeather = event.weather().finalReplacement();
        }
        // Set event results
        this.impl$prevWeather = newWeather;
        if (newWeather.type() == WeatherTypes.CLEAR.get()) {
            this.serverLevelData.setThunderTime(0);
            this.serverLevelData.setRainTime(0);
            this.serverLevelData.setClearWeatherTime((int) newWeather.remainingDuration().ticks());
            this.serverLevelData.setThundering(false);
            this.serverLevelData.setRaining(false);
        } else {
            final int newTime = (int) newWeather.remainingDuration().ticks();
            this.serverLevelData.setRaining(true);
            this.serverLevelData.setClearWeatherTime(0);
            this.serverLevelData.setRainTime(newTime);
            if (newWeather.type() == WeatherTypes.THUNDER.get()) {
                this.serverLevelData.setThunderTime(newTime);
                this.serverLevelData.setThundering(true);
            } else {
                this.serverLevelData.setThunderTime(0);
                this.serverLevelData.setThundering(false);
            }
        }
    }
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) ChangeWeatherEvent(org.spongepowered.api.event.world.ChangeWeatherEvent) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Transaction(org.spongepowered.api.data.Transaction) Cause(org.spongepowered.api.event.Cause) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 19 with Transaction

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

the class InventoryEventFactory method callChangeEntityEquipmentEvent.

public static ChangeEntityEquipmentEvent callChangeEntityEquipmentEvent(final LivingEntity entity, final ItemStackSnapshot before, final ItemStackSnapshot after, final Slot slot) {
    final ChangeEntityEquipmentEvent event;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(entity);
        final Transaction<ItemStackSnapshot> transaction = new Transaction<>(before, after);
        if (after.isEmpty()) {
            event = SpongeEventFactory.createChangeEntityEquipmentEventBreak(frame.currentCause(), (Entity) entity, slot, transaction);
        } else {
            event = SpongeEventFactory.createChangeEntityEquipmentEvent(frame.currentCause(), (Entity) entity, slot, transaction);
        }
        SpongeCommon.post(event);
        return event;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Entity(org.spongepowered.api.entity.Entity) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Transaction(org.spongepowered.api.data.Transaction) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ChangeEntityEquipmentEvent(org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent)

Example 20 with Transaction

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

the class InventoryEventFactory method callEnchantEventLevelRequirement.

public static int callEnchantEventLevelRequirement(EnchantmentMenu container, int seed, int option, int power, ItemStack itemStack, int levelRequirement) {
    org.spongepowered.api.item.inventory.Container enchantContainer = ContainerUtil.fromNative(container);
    Player viewer = (Player) enchantContainer.viewer();
    ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(viewer.inventory.getCarried());
    Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(cursor, cursor);
    EnchantItemEvent.CalculateLevelRequirement event = SpongeEventFactory.createEnchantItemEventCalculateLevelRequirement(PhaseTracker.getCauseStackManager().currentCause(), levelRequirement, levelRequirement, enchantContainer, cursorTrans, ItemStackUtil.snapshotOf(itemStack), option, power, seed);
    SpongeCommon.post(event);
    return event.levelRequirement();
}
Also used : Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Transaction(org.spongepowered.api.data.Transaction) EnchantItemEvent(org.spongepowered.api.event.item.inventory.EnchantItemEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

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