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();
}
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;
}
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());
}
Aggregations