Search in sources :

Example 11 with CreatorTrackedBridge

use of org.spongepowered.common.bridge.CreatorTrackedBridge in project SpongeCommon by SpongePowered.

the class LevelChunkMixin method bridge$addTrackedBlockPosition.

@Override
public void bridge$addTrackedBlockPosition(final Block block, final BlockPos pos, final UUID uuid, final PlayerTracker.Type trackerType) {
    if (((LevelBridge) this.level).bridge$isFake()) {
        return;
    }
    if (!PhaseTracker.getInstance().getPhaseContext().tracksCreatorsAndNotifiers()) {
        // Don't track chunk gen
        return;
    }
    // Update TE tracking cache
    // We must always check for a TE as a mod block may not implement ITileEntityProvider if a TE exists
    // Note: We do not check SpongeImplHooks.hasBlockTileEntity(block, state) as neighbor notifications do not include blockstate.
    final BlockEntity blockEntity = this.blockEntities.get(pos);
    if (blockEntity != null) {
        if (blockEntity instanceof CreatorTrackedBridge) {
            final CreatorTrackedBridge trackedBlockEntity = (CreatorTrackedBridge) blockEntity;
            if (trackerType == PlayerTracker.Type.NOTIFIER) {
                if (Objects.equals(trackedBlockEntity.tracker$getNotifierUUID().orElse(null), uuid)) {
                    return;
                }
                trackedBlockEntity.tracker$setTrackedUUID(PlayerTracker.Type.NOTIFIER, uuid);
            } else {
                if (Objects.equals(trackedBlockEntity.tracker$getCreatorUUID().orElse(null), uuid)) {
                    return;
                }
                trackedBlockEntity.tracker$setTrackedUUID(PlayerTracker.Type.CREATOR, uuid);
            }
        }
    }
    if (trackerType == PlayerTracker.Type.CREATOR) {
        this.impl$setTrackedUUID(pos, uuid, trackerType, (pt, idx) -> {
            pt.creatorindex = idx;
            pt.notifierIndex = idx;
        });
    } else {
        this.impl$setTrackedUUID(pos, uuid, trackerType, (pt, idx) -> pt.notifierIndex = idx);
    }
}
Also used : CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 12 with CreatorTrackedBridge

use of org.spongepowered.common.bridge.CreatorTrackedBridge in project SpongeCommon by SpongePowered.

the class EntityUtil method processEntitySpawn.

public static boolean processEntitySpawn(final org.spongepowered.api.entity.Entity entity, final Supplier<Optional<UUID>> supplier, final Consumer<Entity> spawner) {
    final Entity minecraftEntity = (Entity) entity;
    if (minecraftEntity instanceof ItemEntity) {
        final ItemStack item = ((ItemEntity) minecraftEntity).getItem();
        if (!item.isEmpty()) {
            final Optional<Entity> customEntityItem = Optional.ofNullable(PlatformHooks.INSTANCE.getWorldHooks().getCustomEntityIfItem(minecraftEntity));
            if (customEntityItem.isPresent()) {
                // Bypass spawning the entity item, since it is established that the custom entity is spawned.
                final Entity entityToSpawn = customEntityItem.get();
                supplier.get().ifPresent(spawned -> {
                    if (entityToSpawn instanceof CreatorTrackedBridge) {
                        ((CreatorTrackedBridge) entityToSpawn).tracker$setTrackedUUID(PlayerTracker.Type.CREATOR, spawned);
                    }
                });
                if (entityToSpawn.removed) {
                    entityToSpawn.removed = false;
                }
                // Since forge already has a new event thrown for the entity, we don't need to throw
                // the event anymore as sponge plugins getting the event after forge mods will
                // have the modified entity list for entities, so no need to re-capture the entities.
                entityToSpawn.level.addFreshEntity(entityToSpawn);
                return true;
            }
        }
    }
    // Allowed to call force spawn directly since we've applied creator and custom item logic already
    spawner.accept((Entity) entity);
    return true;
}
Also used : Entity(net.minecraft.world.entity.Entity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(net.minecraft.world.item.ItemStack)

Example 13 with CreatorTrackedBridge

use of org.spongepowered.common.bridge.CreatorTrackedBridge in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickEntity.

public static void tickEntity(final Consumer<net.minecraft.world.entity.Entity> consumer, final net.minecraft.world.entity.Entity entity) {
    checkArgument(entity instanceof Entity, "Entity %s is not an instance of SpongeAPI's Entity!", entity);
    checkNotNull(entity, "Cannot capture on a null ticking entity!");
    if (!((TrackableBridge) entity).bridge$shouldTick()) {
        return;
    }
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext(PhaseTracker.SERVER).source(entity);
    try (final EntityTickContext context = tickContext;
        final Timing entityTiming = ((TimingBridge) entity.getType()).bridge$timings()) {
        if (entity instanceof CreatorTrackedBridge) {
            ((CreatorTrackedBridge) entity).tracker$getNotifierUUID().ifPresent(context::notifier);
            ((CreatorTrackedBridge) entity).tracker$getCreatorUUID().ifPresent(context::creator);
        }
        context.buildAndSwitch();
        entityTiming.startTiming();
        consumer.accept(entity);
        if (ShouldFire.MOVE_ENTITY_EVENT) {
            SpongeCommonEventFactory.callNaturalMoveEntityEvent(entity);
        }
        if (ShouldFire.ROTATE_ENTITY_EVENT) {
            SpongeCommonEventFactory.callNaturalRotateEntityEvent(entity);
        }
    } catch (final Exception e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, tickContext);
    }
}
Also used : TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge)

Example 14 with CreatorTrackedBridge

use of org.spongepowered.common.bridge.CreatorTrackedBridge in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickRidingEntity.

public static void tickRidingEntity(final net.minecraft.world.entity.Entity entity) {
    checkArgument(entity instanceof Entity, "Entity %s is not an instance of SpongeAPI's Entity!", entity);
    checkNotNull(entity, "Cannot capture on a null ticking entity!");
    if (!((TrackableBridge) entity).bridge$shouldTick()) {
        return;
    }
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext(PhaseTracker.SERVER).source(entity);
    try (final EntityTickContext context = tickContext;
        final Timing entityTiming = ((TimingBridge) entity.getType()).bridge$timings()) {
        entityTiming.startTiming();
        if (entity instanceof CreatorTrackedBridge) {
            ((CreatorTrackedBridge) entity).tracker$getNotifierUUID().ifPresent(context::notifier);
            ((CreatorTrackedBridge) entity).tracker$getCreatorUUID().ifPresent(context::creator);
        }
        context.buildAndSwitch();
        entity.rideTick();
        if (ShouldFire.MOVE_ENTITY_EVENT) {
            SpongeCommonEventFactory.callNaturalMoveEntityEvent(entity);
        }
        if (ShouldFire.ROTATE_ENTITY_EVENT) {
            SpongeCommonEventFactory.callNaturalRotateEntityEvent(entity);
        }
    } catch (final Exception e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, tickContext);
    }
}
Also used : TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge)

Aggregations

CreatorTrackedBridge (org.spongepowered.common.bridge.CreatorTrackedBridge)14 Entity (net.minecraft.world.entity.Entity)5 LevelChunkBridge (org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)5 ServerPlayer (net.minecraft.server.level.ServerPlayer)4 Entity (org.spongepowered.api.entity.Entity)4 Timing (co.aikar.timings.Timing)3 BlockPos (net.minecraft.core.BlockPos)3 TickableBlockEntity (net.minecraft.world.level.block.entity.TickableBlockEntity)3 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)3 Inject (org.spongepowered.asm.mixin.injection.Inject)3 ActiveChunkReferantBridge (org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge)3 UUID (java.util.UUID)2 ServerboundInteractPacket (net.minecraft.network.protocol.game.ServerboundInteractPacket)2 DamageSource (net.minecraft.world.damagesource.DamageSource)2 EntityDamageSource (net.minecraft.world.damagesource.EntityDamageSource)2 LivingEntity (net.minecraft.world.entity.LivingEntity)2 CauseStackManager (org.spongepowered.api.event.CauseStackManager)2 TimingBridge (org.spongepowered.common.bridge.TimingBridge)2 LevelBridge (org.spongepowered.common.bridge.world.level.LevelBridge)2 EntityTickContext (org.spongepowered.common.event.tracking.phase.tick.EntityTickContext)2