Search in sources :

Example 6 with Ticks

use of org.spongepowered.api.util.Ticks in project SpongeCommon by SpongePowered.

the class LivingEntityMixin method impl$onGetRemainingItemDuration.

@Redirect(method = "updatingUsingItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getUseItemRemainingTicks()I", ordinal = 0))
private int impl$onGetRemainingItemDuration(final LivingEntity self) {
    if (this.level.isClientSide) {
        return self.getUseItemRemainingTicks();
    }
    final UseItemStackEvent.Tick event;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(this.useItem);
        final HandType handType = (HandType) (Object) this.shadow$getUsedItemHand();
        this.impl$addSelfToFrame(frame, snapshot, handType);
        final Ticks useItemRemainingTicks = Ticks.of(this.useItemRemaining);
        event = SpongeEventFactory.createUseItemStackEventTick(PhaseTracker.getCauseStackManager().currentCause(), useItemRemainingTicks, useItemRemainingTicks, snapshot);
        SpongeCommon.post(event);
    }
    // Because the item usage will only finish if useItemRemaining == 0 and decrements it first, it should be >= 1
    this.useItemRemaining = Math.max((int) event.remainingDuration().ticks(), 1);
    if (event.isCancelled()) {
        // condition evaluate to false, so an integer >= 25
        return 26;
    }
    return this.shadow$getUseItemRemainingTicks();
}
Also used : 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 7 with Ticks

use of org.spongepowered.api.util.Ticks in project SpongeCommon by SpongePowered.

the class LivingEntityMixin method impl$onResetActiveHand.

@Inject(method = "stopUsingItem", at = @At("HEAD"))
private void impl$onResetActiveHand(final CallbackInfo ci) {
    if (this.level.isClientSide) {
        return;
    }
    // If we finished using an item, impl$activeItemStackCopy will be non-null
    // However, if a player stopped using an item early, impl$activeItemStackCopy will not be set
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(this.impl$activeItemStackCopy != null ? this.impl$activeItemStackCopy : this.useItem);
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        this.impl$addSelfToFrame(frame, snapshot);
        final Ticks useItemRemainingTicks = Ticks.of(this.useItemRemaining);
        SpongeCommon.post(SpongeEventFactory.createUseItemStackEventReset(PhaseTracker.getCauseStackManager().currentCause(), useItemRemainingTicks, useItemRemainingTicks, snapshot));
    }
    this.impl$activeItemStackCopy = null;
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) Ticks(org.spongepowered.api.util.Ticks) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 8 with Ticks

use of org.spongepowered.api.util.Ticks in project SpongeCommon by SpongePowered.

the class SpongePotionBuilder method buildContent.

@Override
protected Optional<PotionEffect> buildContent(final DataView container) throws InvalidDataException {
    checkNotNull(container);
    if (!container.contains(Constants.Item.Potions.POTION_TYPE) || !container.contains(Constants.Item.Potions.POTION_DURATION) || !container.contains(Constants.Item.Potions.POTION_AMPLIFIER) || !container.contains(Constants.Item.Potions.POTION_AMBIANCE) || !container.contains(Constants.Item.Potions.POTION_SHOWS_PARTICLES)) {
        return Optional.empty();
    }
    final String effectName = container.getString(Constants.Item.Potions.POTION_TYPE).get();
    final Optional<PotionEffectType> optional = Sponge.game().registry(RegistryTypes.POTION_EFFECT_TYPE).findValue(ResourceKey.resolve(effectName));
    if (!optional.isPresent()) {
        throw new InvalidDataException("The container has an invalid potion type name: " + effectName);
    }
    final Ticks duration = Ticks.of(container.getInt(Constants.Item.Potions.POTION_DURATION).get());
    final int amplifier = container.getInt(Constants.Item.Potions.POTION_AMPLIFIER).get();
    final boolean ambience = container.getBoolean(Constants.Item.Potions.POTION_AMBIANCE).get();
    final boolean particles = container.getBoolean(Constants.Item.Potions.POTION_SHOWS_PARTICLES).get();
    final PotionEffect.Builder builder = new SpongePotionBuilder();
    return Optional.of(builder.potionType(optional.get()).showParticles(particles).duration(duration).amplifier(amplifier).ambient(ambience).build());
}
Also used : PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType) Ticks(org.spongepowered.api.util.Ticks) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Aggregations

Ticks (org.spongepowered.api.util.Ticks)8 CauseStackManager (org.spongepowered.api.event.CauseStackManager)6 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)6 HandType (org.spongepowered.api.data.type.HandType)5 UseItemStackEvent (org.spongepowered.api.event.item.inventory.UseItemStackEvent)4 Inject (org.spongepowered.asm.mixin.injection.Inject)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 Collectors (java.util.stream.Collectors)1 GsonComponentSerializer (net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)1 Entity (net.minecraft.world.entity.Entity)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 Player (net.minecraft.world.entity.player.Player)1 ResourceKey (org.spongepowered.api.ResourceKey)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 Keys (org.spongepowered.api.data.Keys)1 Transaction (org.spongepowered.api.data.Transaction)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 Value (org.spongepowered.api.data.value.Value)1 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)1 PotionEffectType (org.spongepowered.api.effect.potion.PotionEffectType)1