Search in sources :

Example 11 with LevelBridge

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

the class SpongePacketHandler method init.

public static void init(final SpongeChannelManager registry) {
    SpongePacketHandler.channel = registry.createChannel(ResourceKey.sponge("default"), PacketChannel.class);
    SpongePacketHandler.channel.registerTransactional(RequestBlockTrackerDataPacket.class, TrackerDataResponsePacket.class, 0).setRequestHandler(EngineConnectionTypes.SERVER_PLAYER, (requestPacket, connection, response) -> {
        final ServerPlayer player = connection.player();
        if (!player.hasPermission("sponge.debug.block-tracking")) {
            return;
        }
        final net.minecraft.server.level.ServerPlayer sender = (net.minecraft.server.level.ServerPlayer) player;
        final BlockPos pos = new BlockPos(requestPacket.x, requestPacket.y, requestPacket.z);
        if (!sender.level.hasChunkAt(pos)) {
            return;
        }
        final LevelChunkBridge levelChunkBridge = (LevelChunkBridge) sender.level.getChunkAt(pos);
        final Optional<UUID> owner = levelChunkBridge.bridge$getBlockCreatorUUID(pos);
        final Optional<UUID> notifier = levelChunkBridge.bridge$getBlockNotifierUUID(pos);
        response.success(SpongePacketHandler.createTrackerDataResponse(owner, notifier));
    });
    SpongePacketHandler.channel.registerTransactional(RequestEntityTrackerDataPacket.class, TrackerDataResponsePacket.class, 1).setRequestHandler(EngineConnectionTypes.SERVER_PLAYER, (requestPacket, connection, response) -> {
        final ServerPlayer player = connection.player();
        if (!player.hasPermission("sponge.debug.entity-tracking")) {
            return;
        }
        final net.minecraft.server.level.ServerPlayer sender = (net.minecraft.server.level.ServerPlayer) player;
        final Entity entity = sender.level.getEntity(requestPacket.entityId);
        if (!(entity instanceof CreatorTrackedBridge)) {
            return;
        }
        final CreatorTrackedBridge creatorTrackedBridge = (CreatorTrackedBridge) entity;
        final Optional<UUID> owner = creatorTrackedBridge.tracker$getCreatorUUID();
        final Optional<UUID> notifier = creatorTrackedBridge.tracker$getNotifierUUID();
        response.success(SpongePacketHandler.createTrackerDataResponse(owner, notifier));
    });
    SpongePacketHandler.channel.register(ChangeViewerEnvironmentPacket.class, 3).addHandler(ClientSideConnection.class, (packet, connection) -> {
        final ClientLevel world = Minecraft.getInstance().level;
        if (world == null) {
            return;
        }
        final DimensionType dimensionType = SpongeCommon.server().registryAccess().dimensionTypes().get(packet.dimensionLogic);
        ((LevelBridge) world).bridge$adjustDimensionLogic(dimensionType);
    });
}
Also used : Entity(net.minecraft.world.entity.Entity) DimensionType(net.minecraft.world.level.dimension.DimensionType) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) PacketChannel(org.spongepowered.api.network.channel.packet.PacketChannel) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) UUID(java.util.UUID)

Example 12 with LevelBridge

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

the class EntityMixin method impl$throwDropItemConstructEvent.

/**
 * @return
 * @author gabizou - January 30th, 2016
 * @author blood - May 12th, 2016
 * @author gabizou - June 2nd, 2016
 *     <p>
 *     TODO from i509VCB: gabizou's remider to refactor this code here
 * @reason Rewrites the method entirely for several reasons:
 *     1) If we are in a forge environment, we do NOT want forge to be capturing the item entities, because we handle them ourselves
 *     2) If we are in a client environment, we should not perform any sort of processing whatsoever.
 *     3) This method is entirely managed from the standpoint where our events have final say, as per usual.
 */
@Inject(method = "spawnAtLocation(Lnet/minecraft/world/item/ItemStack;F)Lnet/minecraft/world/entity/item/ItemEntity;", at = @At("HEAD"), cancellable = true)
public void impl$throwDropItemConstructEvent(final ItemStack stack, final float offsetY, final CallbackInfoReturnable<ItemEntity> cir) {
    if (stack.isEmpty()) {
        cir.setReturnValue(null);
        return;
    }
    if (((LevelBridge) this.level).bridge$isFake()) {
        return;
    }
    // Now the real fun begins.
    final ItemStack item;
    final double posX = this.shadow$position().x;
    final double posY = this.shadow$position().y + offsetY;
    final double posZ = this.shadow$position().z;
    // FIRST we want to throw the DropItemEvent.PRE
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack);
    final List<ItemStackSnapshot> original = new ArrayList<>();
    original.add(snapshot);
    // We want to frame ourselves here, because of the two events we have to throw, first for the drop item event, then the constructentityevent.
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        // Perform the event throws first, if they return false, return null
        item = SpongeCommonEventFactory.throwDropItemAndConstructEvent((Entity) (Object) this, posX, posY, posZ, snapshot, original, frame);
        if (item == null || item.isEmpty()) {
            cir.setReturnValue(null);
            return;
        }
        final ItemEntity entityitem = new ItemEntity(this.level, posX, posY, posZ, item);
        entityitem.setDefaultPickUpDelay();
        this.level.addFreshEntity(entityitem);
        cir.setReturnValue(entityitem);
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Entity(net.minecraft.world.entity.Entity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) PlatformServerLevelBridge(org.spongepowered.common.bridge.world.level.PlatformServerLevelBridge) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.world.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 13 with LevelBridge

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

the class LevelChunkMixin method impl$setTrackedUUID.

private void impl$setTrackedUUID(final BlockPos pos, final UUID uuid, final PlayerTracker.Type type, final BiConsumer<PlayerTracker, Integer> consumer) {
    if (((LevelBridge) this.level).bridge$isFake()) {
        return;
    }
    final PrimaryLevelDataBridge worldInfo = (PrimaryLevelDataBridge) this.level.getLevelData();
    final int index = uuid == null ? -1 : worldInfo.bridge$getIndexForUniqueId(uuid);
    if (pos.getY() <= 255) {
        final short blockPos = Constants.Sponge.blockPosToShort(pos);
        this.impl$computePlayerTracker(this.impl$trackedShortBlockPositions, blockPos, index, type, consumer);
        return;
    }
    final int blockPos = Constants.Sponge.blockPosToInt(pos);
    this.impl$computePlayerTracker(this.impl$trackedIntBlockPositions, blockPos, index, type, consumer);
}
Also used : LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge)

Example 14 with LevelBridge

use of org.spongepowered.common.bridge.world.level.LevelBridge 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 15 with LevelBridge

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

the class AbstractArrowMixin method onProjectileHit.

/**
 * Collide impact event post for plugins to cancel impact.
 */
@Inject(method = "onHitBlock", at = @At("HEAD"), cancellable = true)
private void onProjectileHit(final BlockHitResult hitResult, final CallbackInfo ci) {
    if (!((LevelBridge) this.level).bridge$isFake() && hitResult.getType() != HitResult.Type.MISS) {
        if (SpongeCommonEventFactory.handleCollideImpactEvent((AbstractArrow) (Object) this, this.impl$getProjectileSource(), hitResult)) {
            this.shadow$playSound(SoundEvents.ARROW_HIT, 1.0F, 1.2F / (this.random.nextFloat() * 0.2F + 0.9F));
            // Make it almost look like it collided with something
            final BlockHitResult blockraytraceresult = (BlockHitResult) hitResult;
            final BlockState blockstate = this.level.getBlockState(blockraytraceresult.getBlockPos());
            this.lastState = blockstate;
            final Vec3 vec3d = blockraytraceresult.getLocation().subtract(this.shadow$getX(), this.shadow$getY(), this.shadow$getZ());
            this.shadow$setDeltaMovement(vec3d);
            final Vec3 vec3d1 = vec3d.normalize().scale(0.05F);
            this.shadow$setPos(this.shadow$getX() - vec3d1.x, this.shadow$getY() - vec3d1.y, this.shadow$getZ() - vec3d1.z);
            this.inGround = true;
            this.shakeTime = 7;
            this.shadow$setCritArrow(false);
            this.shadow$setPierceLevel((byte) 0);
            this.shadow$setShotFromCrossbow(false);
            this.resetPiercedEntities();
            ci.cancel();
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Vec3(net.minecraft.world.phys.Vec3) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Inject(org.spongepowered.asm.mixin.injection.Inject)

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