Search in sources :

Example 16 with LevelBridge

use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.

the class FireworkRocketItemMixin method impl$throwConstructPreEvent.

/**
 * Private method for bridging the duplicate between
 * {@link #spongeImpl$ThrowPreBeforeSpawning(World, PlayerEntity, Hand, CallbackInfoReturnable, ItemStack)} and
 * {@link #spongeImpl$ThrowPrimeEventsIfCancelled(ItemUseContext, CallbackInfoReturnable)}
 * since both follow the same logic, but differ in how they are called.
 *
 * @param world The world
 * @param player The player
 * @param usedItem The used item
 * @return True if the event is cancelled and the callback needs to be cancelled
 */
private boolean impl$throwConstructPreEvent(final Level world, final net.minecraft.world.entity.player.Player player, final ItemStack usedItem) {
    if (ShouldFire.CONSTRUCT_ENTITY_EVENT_PRE && !((LevelBridge) world).bridge$isFake()) {
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(usedItem));
            frame.addContext(EventContextKeys.PROJECTILE_SOURCE, (ProjectileSource) player);
            frame.pushCause(player);
            final ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(frame.currentCause(), ServerLocation.of((ServerWorld) world, player.getX(), player.getY(), player.getZ()), new Vector3d(0, 0, 0), EntityTypes.FIREWORK_ROCKET.get());
            return SpongeCommon.post(event);
        }
    }
    return false;
}
Also used : ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) ServerWorld(org.spongepowered.api.world.server.ServerWorld) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Vector3d(org.spongepowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager)

Example 17 with LevelBridge

use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.

the class ResultSlotMixin_Inventory method impl$afterTake.

@Inject(method = "onTake", cancellable = true, at = @At("RETURN"))
private void impl$afterTake(final Player thePlayer, final ItemStack stack, final CallbackInfoReturnable<ItemStack> cir) {
    if (((LevelBridge) thePlayer.level).bridge$isFake()) {
        return;
    }
    final AbstractContainerMenu container = thePlayer.containerMenu;
    final Inventory craftInv = ((Inventory) container).query(QueryTypes.INVENTORY_TYPE.get().of(CraftingInventory.class));
    if (!(craftInv instanceof CraftingInventory)) {
        SpongeCommon.logger().warn("Detected crafting without a InventoryCrafting!? Crafting Event will not fire.");
        return;
    }
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    transactor.logCrafting(thePlayer, this.impl$craftedStack, (CraftingInventory) craftInv, this.impl$onTakeRecipe);
    this.impl$craftedStack = null;
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 18 with LevelBridge

use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.

the class EntityMixin_Tracker method tracker$ensureDropEffectCompleted.

@Inject(method = "remove()V", at = @At("RETURN"))
private void tracker$ensureDropEffectCompleted(final CallbackInfo ci) {
    final PhaseTracker instance = PhaseTracker.SERVER;
    if (!instance.onSidedThread()) {
        return;
    }
    if (((LevelBridge) this.level).bridge$isFake()) {
        return;
    }
    final PhaseContext<@NonNull ?> context = instance.getPhaseContext();
    if (!context.doesBlockEventTracking()) {
        return;
    }
    if (this.tracker$dropsTransactor != null) {
        this.tracker$dropsTransactor.close();
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 19 with LevelBridge

use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.

the class EntityMixin_Tracker method tracker$logEntityDropTransactionIfNecessary.

@Inject(method = "spawnAtLocation(Lnet/minecraft/world/item/ItemStack;F)Lnet/minecraft/world/entity/item/ItemEntity;", at = @At("HEAD"))
private void tracker$logEntityDropTransactionIfNecessary(final ItemStack stack, final float offsetY, final CallbackInfoReturnable<ItemEntity> cir) {
    final PhaseTracker instance = PhaseTracker.SERVER;
    if (!instance.onSidedThread()) {
        return;
    }
    if (((LevelBridge) this.level).bridge$isFake()) {
        return;
    }
    final PhaseContext<@NonNull ?> context = instance.getPhaseContext();
    if (!context.doesBlockEventTracking()) {
        return;
    }
    if (this.tracker$dropsTransactor == null) {
        this.tracker$dropsTransactor = context.getTransactor().ensureEntityDropTransactionEffect((Entity) (Object) this);
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 20 with LevelBridge

use of org.spongepowered.common.bridge.world.level.LevelBridge in project SpongeCommon by SpongePowered.

the class LivingEntityMixin_Tracker method tracker$wrapOnDeathWithState.

/**
 * @author gabizou
 * @reason Instead of inlining the onDeath method with the main mixin, we can "toggle"
 * the usage of the death state control in the tracker mixin.
 */
@Redirect(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;die(Lnet/minecraft/world/damagesource/DamageSource;)V"))
private void tracker$wrapOnDeathWithState(final LivingEntity thisEntity, final DamageSource cause) {
    // Sponge Start - notify the cause tracker
    final PhaseTracker instance = PhaseTracker.SERVER;
    if (!instance.onSidedThread()) {
        return;
    }
    if (((LevelBridge) this.level).bridge$isFake()) {
        return;
    }
    final PhaseContext<@NonNull ?> context = instance.getPhaseContext();
    if (!context.doesBlockEventTracking()) {
        return;
    }
    try (final EffectTransactor ignored = context.getTransactor().ensureEntityDropTransactionEffect((LivingEntity) (Object) this)) {
        // Create new EntityDeathTransaction
        // Add new EntityDeathEffect
        this.shadow$die(cause);
    }
// Sponge End
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

LevelBridge (org.spongepowered.common.bridge.world.level.LevelBridge)20 Inject (org.spongepowered.asm.mixin.injection.Inject)5 Redirect (org.spongepowered.asm.mixin.injection.Redirect)5 Entity (net.minecraft.world.entity.Entity)4 LivingEntity (net.minecraft.world.entity.LivingEntity)3 ItemEntity (net.minecraft.world.entity.item.ItemEntity)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)3 EffectTransactor (org.spongepowered.common.event.tracking.context.transaction.EffectTransactor)3 ArrayList (java.util.ArrayList)2 ServerLevel (net.minecraft.server.level.ServerLevel)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 ItemStack (net.minecraft.world.item.ItemStack)2 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 ServerWorld (org.spongepowered.api.world.server.ServerWorld)2 CreatorTrackedBridge (org.spongepowered.common.bridge.CreatorTrackedBridge)2 MoreObjects (com.google.common.base.MoreObjects)1 WeakReference (java.lang.ref.WeakReference)1