Search in sources :

Example 16 with LevelChunkBridge

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

the class DamageEventUtil method generateCauseFor.

/**
 * This applies various contexts based on the type of {@link DamageSource}, whether
 * it's provided by sponge or vanilla. This is not stack neutral, which is why it requires
 * a {@link CauseStackManager.StackFrame} reference to push onto the stack.
 */
public static void generateCauseFor(final DamageSource damageSource, final CauseStackManager.StackFrame frame) {
    if (damageSource instanceof EntityDamageSource) {
        final net.minecraft.world.entity.Entity source = damageSource.getEntity();
        if (!(source instanceof Player) && source instanceof CreatorTrackedBridge) {
            final CreatorTrackedBridge creatorBridge = (CreatorTrackedBridge) source;
            creatorBridge.tracker$getCreatorUUID().ifPresent(creator -> frame.addContext(EventContextKeys.CREATOR, creator));
            creatorBridge.tracker$getNotifierUUID().ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
        }
    } else if (damageSource instanceof BlockDamageSource) {
        final ServerLocation location = ((BlockDamageSource) damageSource).location();
        final BlockPos blockPos = VecHelper.toBlockPos(location);
        final LevelChunkBridge chunkBridge = (LevelChunkBridge) ((net.minecraft.world.level.Level) location.world()).getChunkAt(blockPos);
        chunkBridge.bridge$getBlockCreatorUUID(blockPos).ifPresent(creator -> frame.addContext(EventContextKeys.CREATOR, creator));
        chunkBridge.bridge$getBlockNotifierUUID(blockPos).ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
    }
    frame.pushCause(damageSource);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) Player(net.minecraft.world.entity.player.Player) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ServerLocation(org.spongepowered.api.world.server.ServerLocation) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) Entity(net.minecraft.world.entity.Entity) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)

Example 17 with LevelChunkBridge

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

the class ChunkMapMixin method impl$onLoad.

@Redirect(method = "*", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/chunk/LevelChunk;setLoaded(Z)V"), slice = @Slice(from = @At(value = "INVOKE", remap = false, target = "Lit/unimi/dsi/fastutil/longs/LongSet;add(J)Z"), to = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;addAllPendingBlockEntities(Ljava/util/Collection;)V")))
private void impl$onLoad(final LevelChunk levelChunk, final boolean loaded) {
    levelChunk.setLoaded(true);
    final Vector3i chunkPos = new Vector3i(levelChunk.getPos().x, 0, levelChunk.getPos().z);
    if (ShouldFire.CHUNK_EVENT_LOAD) {
        final ChunkEvent.Load loadEvent = SpongeEventFactory.createChunkEventLoad(PhaseTracker.getInstance().currentCause(), ((WorldChunk) levelChunk), chunkPos, (ResourceKey) (Object) this.level.dimension().location());
        SpongeCommon.post(loadEvent);
    }
    for (final Direction dir : Constants.Chunk.CARDINAL_DIRECTIONS) {
        final Vector3i neighborPos = chunkPos.add(dir.asBlockOffset());
        ChunkAccess neighbor = this.level.getChunk(neighborPos.x(), neighborPos.z(), ChunkStatus.EMPTY, false);
        if (neighbor instanceof ImposterProtoChunk) {
            neighbor = ((ImposterProtoChunk) neighbor).getWrapped();
        }
        if (neighbor instanceof LevelChunk) {
            final int index = DirectionUtil.directionToIndex(dir);
            final int oppositeIndex = DirectionUtil.directionToIndex(dir.opposite());
            ((LevelChunkBridge) levelChunk).bridge$setNeighborChunk(index, (LevelChunk) neighbor);
            ((LevelChunkBridge) neighbor).bridge$setNeighborChunk(oppositeIndex, levelChunk);
        }
    }
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) ImposterProtoChunk(net.minecraft.world.level.chunk.ImposterProtoChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) ChunkEvent(org.spongepowered.api.event.world.chunk.ChunkEvent) Direction(org.spongepowered.api.util.Direction) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 18 with LevelChunkBridge

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

the class ChunkSerializerMixin_Tracker method impl$writeSpongeLevelData.

@Inject(method = "write", at = @At(value = "RETURN"))
private static void impl$writeSpongeLevelData(final ServerLevel param0, final ChunkAccess param1, final CallbackInfoReturnable<CompoundTag> cir) {
    if (!(param1 instanceof LevelChunk)) {
        return;
    }
    final LevelChunkBridge chunk = (LevelChunkBridge) param1;
    if (!chunk.bridge$getTrackedShortPlayerPositions().isEmpty() || !chunk.bridge$getTrackedIntPlayerPositions().isEmpty()) {
        final CompoundTag level = (CompoundTag) cir.getReturnValue().get("Level");
        final CompoundTag trackedNbt = new CompoundTag();
        final ListTag positions = new ListTag();
        trackedNbt.put(Constants.Sponge.SPONGE_BLOCK_POS_TABLE, positions);
        level.put(Constants.Sponge.Data.V2.SPONGE_DATA, trackedNbt);
        ChunkSerializerMixin_Tracker.impl$writeMap(positions, chunk.bridge$getTrackedShortPlayerPositions(), (nbt, pos) -> nbt.putShort("pos", pos));
        ChunkSerializerMixin_Tracker.impl$writeMap(positions, chunk.bridge$getTrackedIntPlayerPositions(), (nbt, pos) -> nbt.putInt("ipos", pos));
    }
}
Also used : LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 19 with LevelChunkBridge

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

the class ChunkSerializerMixin_Tracker method impl$readSpongeLevelData.

@Redirect(method = "read", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/chunk/ChunkAccess;setLightCorrect(Z)V"))
private static void impl$readSpongeLevelData(final ChunkAccess chunkAccess, final boolean var1, final ServerLevel param0, final StructureManager param1, final PoiManager param2, final ChunkPos param3, final CompoundTag param4) {
    if (!(chunkAccess instanceof LevelChunk)) {
        return;
    }
    final CompoundTag level = (CompoundTag) param4.get("Level");
    final CompoundTag spongeData = level.getCompound(Constants.Sponge.Data.V2.SPONGE_DATA);
    if (spongeData.isEmpty()) {
        return;
    }
    final Map<Integer, PlayerTracker> trackedIntPlayerPositions = new HashMap<>();
    final Map<Short, PlayerTracker> trackedShortPlayerPositions = new HashMap<>();
    final ListTag list = spongeData.getList(Constants.Sponge.SPONGE_BLOCK_POS_TABLE, 10);
    final LevelChunkBridge chunk = (LevelChunkBridge) chunkAccess;
    for (Tag tag : list) {
        final PlayerTracker tracker = new PlayerTracker();
        final CompoundTag data = (CompoundTag) tag;
        final boolean isShortPos = data.contains("pos");
        if (data.contains("owner")) {
            tracker.creatorindex = data.getInt("owner");
        }
        if (data.contains("notifier")) {
            tracker.notifierIndex = data.getInt("notifier");
        }
        if (tracker.notifierIndex != -1 || tracker.creatorindex != -1) {
            if (isShortPos) {
                trackedShortPlayerPositions.put(data.getShort("pos"), tracker);
            } else {
                trackedIntPlayerPositions.put(data.getInt("ipos"), tracker);
            }
        }
    }
    chunk.bridge$setTrackedIntPlayerPositions(trackedIntPlayerPositions);
    chunk.bridge$setTrackedShortPlayerPositions(trackedShortPlayerPositions);
}
Also used : LevelChunk(net.minecraft.world.level.chunk.LevelChunk) HashMap(java.util.HashMap) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 20 with LevelChunkBridge

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

the class TrackingUtil method associateTrackerToTarget.

public static void associateTrackerToTarget(final BlockChange blockChange, final BlockTransactionReceipt receipt, final UUID uuid) {
    final BlockSnapshot finalSnapshot = receipt.finalBlock();
    final SpongeBlockSnapshot spongeSnapshot = (SpongeBlockSnapshot) finalSnapshot;
    final BlockPos pos = spongeSnapshot.getBlockPos();
    final Block block = ((net.minecraft.world.level.block.state.BlockState) spongeSnapshot.state()).getBlock();
    spongeSnapshot.getServerWorld().map(world -> world.getChunkAt(pos)).map(chunk -> (LevelChunkBridge) chunk).ifPresent(spongeChunk -> {
        final PlayerTracker.Type trackerType = blockChange == BlockChange.PLACE ? PlayerTracker.Type.CREATOR : PlayerTracker.Type.NOTIFIER;
        spongeChunk.bridge$addTrackedBlockPosition(block, pos, uuid, trackerType);
    });
}
Also used : ViewableInventoryBridge(org.spongepowered.common.bridge.world.inventory.ViewableInventoryBridge) Level(org.apache.logging.log4j.Level) Random(java.util.Random) SpongeCommonEventFactory(org.spongepowered.common.event.SpongeCommonEventFactory) RedstoneTorchBlock(net.minecraft.world.level.block.RedstoneTorchBlock) BlockTransactionReceipt(org.spongepowered.api.block.transaction.BlockTransactionReceipt) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TrackedWorldBridge(org.spongepowered.common.bridge.world.TrackedWorldBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TickPhase(org.spongepowered.common.event.tracking.phase.tick.TickPhase) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) TimingBridge(org.spongepowered.common.bridge.TimingBridge) Transaction(org.spongepowered.api.data.Transaction) LocatableBlock(org.spongepowered.api.world.LocatableBlock) BlockEntityBridge(org.spongepowered.common.bridge.world.level.block.entity.BlockEntityBridge) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) Set(java.util.Set) UUID(java.util.UUID) BlockChange(org.spongepowered.common.world.BlockChange) TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) BlockState(org.spongepowered.api.block.BlockState) MarkerManager(org.apache.logging.log4j.MarkerManager) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) BlockPos(net.minecraft.core.BlockPos) FluidState(net.minecraft.world.level.material.FluidState) BlockEventData(net.minecraft.world.level.BlockEventData) Optional(java.util.Optional) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) TrackableBlockEventDataBridge(org.spongepowered.common.bridge.world.level.TrackableBlockEventDataBridge) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) NonNull(org.checkerframework.checker.nullness.qual.NonNull) PrettyPrinter(org.spongepowered.common.util.PrettyPrinter) ServerLevel(net.minecraft.server.level.ServerLevel) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockChangeFlag(org.spongepowered.api.world.BlockChangeFlag) TrackableBridge(org.spongepowered.common.bridge.TrackableBridge) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) TrackedLevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.TrackedLevelChunkBridge) FluidTickContext(org.spongepowered.common.event.tracking.phase.tick.FluidTickContext) Marker(org.apache.logging.log4j.Marker) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RedstoneLampBlock(net.minecraft.world.level.block.RedstoneLampBlock) Timing(co.aikar.timings.Timing) BlockTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockTickContext) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) ShouldFire(org.spongepowered.common.event.ShouldFire) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) SpongeCommon(org.spongepowered.common.SpongeCommon) Entity(org.spongepowered.api.entity.Entity) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) Consumer(java.util.function.Consumer) BlockChangeFlags(org.spongepowered.api.world.BlockChangeFlags) VecHelper(org.spongepowered.common.util.VecHelper) Block(net.minecraft.world.level.block.Block) BlockEventTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockEventTickContext) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockState(org.spongepowered.api.block.BlockState) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) RedstoneTorchBlock(net.minecraft.world.level.block.RedstoneTorchBlock) LocatableBlock(org.spongepowered.api.world.LocatableBlock) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) RedstoneLampBlock(net.minecraft.world.level.block.RedstoneLampBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) TrackedLevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.TrackedLevelChunkBridge)

Aggregations

LevelChunkBridge (org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)23 BlockPos (net.minecraft.core.BlockPos)9 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)8 CreatorTrackedBridge (org.spongepowered.common.bridge.CreatorTrackedBridge)6 UUID (java.util.UUID)5 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)5 CompoundTag (net.minecraft.nbt.CompoundTag)4 ServerLocation (org.spongepowered.api.world.server.ServerLocation)4 ActiveChunkReferantBridge (org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge)4 TrackedLevelChunkBridge (org.spongepowered.common.bridge.world.level.chunk.TrackedLevelChunkBridge)4 ListTag (net.minecraft.nbt.ListTag)3 Block (net.minecraft.world.level.block.Block)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 Direction (org.spongepowered.api.util.Direction)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 Timing (co.aikar.timings.Timing)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2