Search in sources :

Example 41 with WorldServer

use of net.minecraft.world.WorldServer in project SpongeCommon by SpongePowered.

the class DeathPhase method unwind.

@Override
public void unwind(BasicEntityContext context) {
    final Entity dyingEntity = context.getSource(Entity.class).orElseThrow(TrackingUtil.throwWithContext("Dying entity not found!", context));
    final DamageSource damageSource = context.getDamageSource();
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(damageSource);
        Sponge.getCauseStackManager().pushCause(dyingEntity);
        final boolean isPlayer = dyingEntity instanceof EntityPlayer;
        final EntityPlayer entityPlayer = isPlayer ? (EntityPlayer) dyingEntity : null;
        final Optional<User> notifier = context.getNotifier();
        final Optional<User> owner = context.getOwner();
        final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
        context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
            // Separate experience orbs from other entity drops
            final List<Entity> experience = entities.stream().filter(entity -> entity instanceof ExperienceOrb).collect(Collectors.toList());
            if (!experience.isEmpty()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.EXPERIENCE);
                final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), experience);
                SpongeImpl.postEvent(spawnEntityEvent);
                if (!spawnEntityEvent.isCancelled()) {
                    for (Entity entity : spawnEntityEvent.getEntities()) {
                        EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                    }
                }
            }
            // Now process other entities, this is separate from item drops specifically
            final List<Entity> other = entities.stream().filter(entity -> !(entity instanceof ExperienceOrb)).collect(Collectors.toList());
            if (!other.isEmpty()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.ENTITY_DEATH);
                final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), experience);
                SpongeImpl.postEvent(spawnEntityEvent);
                if (!spawnEntityEvent.isCancelled()) {
                    for (Entity entity : spawnEntityEvent.getEntities()) {
                        EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                    }
                }
            }
        });
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        // This allows mods such as Draconic Evolution to add items to the drop list
        if (context.getCapturedEntityItemDropSupplier().isEmpty() && context.getCapturedEntityDropSupplier().isEmpty()) {
            final ArrayList<Entity> entities = new ArrayList<>();
            final DropItemEvent.Destruct destruct = SpongeEventFactory.createDropItemEventDestruct(frame.getCurrentCause(), entities);
            SpongeImpl.postEvent(destruct);
            if (!destruct.isCancelled()) {
                for (Entity entity : destruct.getEntities()) {
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
            return;
        }
        context.getCapturedEntityItemDropSupplier().acceptAndRemoveIfPresent(dyingEntity.getUniqueId(), items -> {
            final ArrayList<Entity> entities = new ArrayList<>();
            for (EntityItem item : items) {
                entities.add(EntityUtil.fromNative(item));
            }
            if (isPlayer) {
                // Forge and Vanilla always clear items on player death BEFORE drops occur
                // This will also provide the highest compatibility with mods such as Tinkers Construct
                entityPlayer.inventory.clear();
            }
            final DropItemEvent.Destruct destruct = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(destruct);
            if (!destruct.isCancelled()) {
                for (Entity entity : destruct.getEntities()) {
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        // Note: If cancelled, the items do not spawn in the world and are NOT copied back to player inventory.
        // This avoids many issues with mods such as Tinkers Construct's soulbound items.
        });
        // Note that this is only used if and when item pre-merging is enabled. Which is never enabled in forge.
        context.getCapturedEntityDropSupplier().acceptAndRemoveIfPresent(dyingEntity.getUniqueId(), itemStacks -> {
            final List<ItemDropData> items = new ArrayList<>();
            items.addAll(itemStacks);
            if (!items.isEmpty()) {
                final net.minecraft.entity.Entity minecraftEntity = EntityUtil.toNative(dyingEntity);
                final List<Entity> itemEntities = items.stream().map(data -> data.create((WorldServer) minecraftEntity.world)).map(EntityUtil::fromNative).collect(Collectors.toList());
                if (isPlayer) {
                    // Forge and Vanilla always clear items on player death BEFORE drops occur
                    // This will also provide the highest compatibility with mods such as Tinkers Construct
                    entityPlayer.inventory.clear();
                }
                final DropItemEvent.Destruct destruct = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), itemEntities);
                SpongeImpl.postEvent(destruct);
                if (!destruct.isCancelled()) {
                    for (Entity entity : destruct.getEntities()) {
                        if (entityCreator != null) {
                            EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
                        }
                        EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                    }
                }
            // Note: If cancelled, the items do not spawn in the world and are NOT copied back to player inventory.
            // This avoids many issues with mods such as Tinkers Construct's soulbound items.
            }
        });
    }
}
Also used : EntityItem(net.minecraft.entity.item.EntityItem) SpongeImpl(org.spongepowered.common.SpongeImpl) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Sponge(org.spongepowered.api.Sponge) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) InternalSpawnTypes(org.spongepowered.common.registry.type.event.InternalSpawnTypes) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) EntityUtil(org.spongepowered.common.entity.EntityUtil) ArrayList(java.util.ArrayList) List(java.util.List) TrackingUtil(org.spongepowered.common.event.tracking.TrackingUtil) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ExperienceOrb(org.spongepowered.api.entity.ExperienceOrb) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) Optional(java.util.Optional) WorldServer(net.minecraft.world.WorldServer) Entity(org.spongepowered.api.entity.Entity) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) User(org.spongepowered.api.entity.living.player.User) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) ArrayList(java.util.ArrayList) ExperienceOrb(org.spongepowered.api.entity.ExperienceOrb) WorldServer(net.minecraft.world.WorldServer) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) EntityItem(net.minecraft.entity.item.EntityItem)

Example 42 with WorldServer

use of net.minecraft.world.WorldServer in project SpongeCommon by SpongePowered.

the class MixinWorldServer method createWorldGenerator.

@Override
public SpongeWorldGenerator createWorldGenerator(String settings) {
    final WorldServer worldServer = (WorldServer) (Object) this;
    final WorldType worldType = worldServer.getWorldType();
    final IChunkGenerator chunkGenerator;
    final BiomeProvider biomeProvider;
    if (worldType instanceof SpongeWorldType) {
        chunkGenerator = ((SpongeWorldType) worldType).getChunkGenerator(worldServer, settings);
        biomeProvider = ((SpongeWorldType) worldType).getBiomeProvider(worldServer);
    } else {
        final IChunkGenerator currentGenerator = this.getChunkProvider().chunkGenerator;
        if (currentGenerator != null) {
            chunkGenerator = currentGenerator;
        } else {
            final WorldProvider worldProvider = worldServer.provider;
            ((IMixinWorldProvider) worldProvider).setGeneratorSettings(settings);
            chunkGenerator = worldProvider.createChunkGenerator();
        }
        biomeProvider = worldServer.provider.biomeProvider;
    }
    return new SpongeWorldGenerator(worldServer, (BiomeGenerator) biomeProvider, SpongeGenerationPopulator.of(worldServer, chunkGenerator));
}
Also used : BiomeProvider(net.minecraft.world.biome.BiomeProvider) IMixinWorldProvider(org.spongepowered.common.interfaces.world.IMixinWorldProvider) SpongeWorldType(org.spongepowered.common.world.type.SpongeWorldType) WorldType(net.minecraft.world.WorldType) IChunkGenerator(net.minecraft.world.gen.IChunkGenerator) IMixinWorldProvider(org.spongepowered.common.interfaces.world.IMixinWorldProvider) WorldProvider(net.minecraft.world.WorldProvider) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpongeWorldGenerator(org.spongepowered.common.world.gen.SpongeWorldGenerator) SpongeWorldType(org.spongepowered.common.world.type.SpongeWorldType)

Example 43 with WorldServer

use of net.minecraft.world.WorldServer in project SpongeCommon by SpongePowered.

the class EntityActivationRange method activateEntities.

/**
 * Find what entities are in range of the players in the world and set
 * active if in range.
 *
 * @param world The world to perform activation checks in
 */
public static void activateEntities(World world) {
    if (((IMixinWorld) world).isFake()) {
        return;
    }
    for (EntityPlayer player : world.playerEntities) {
        int maxRange = 0;
        for (Integer range : maxActivationRanges.values()) {
            if (range > maxRange) {
                maxRange = range;
            }
        }
        maxRange = Math.min((((org.spongepowered.api.world.World) world).getViewDistance() << 4) - 8, maxRange);
        ((IModData_Activation) player).setActivatedTick(SpongeImpl.getServer().getTickCounter());
        growBb(maxBB, player.getEntityBoundingBox(), maxRange, 256, maxRange);
        int i = MathHelper.floor(maxBB.minX / 16.0D);
        int j = MathHelper.floor(maxBB.maxX / 16.0D);
        int k = MathHelper.floor(maxBB.minZ / 16.0D);
        int l = MathHelper.floor(maxBB.maxZ / 16.0D);
        for (int i1 = i; i1 <= j; ++i1) {
            for (int j1 = k; j1 <= l; ++j1) {
                WorldServer worldserver = (WorldServer) world;
                Chunk chunk = ((IMixinChunkProviderServer) worldserver.getChunkProvider()).getLoadedChunkWithoutMarkingActive(i1, j1);
                if (chunk != null) {
                    activateChunkEntities(player, chunk);
                }
            }
        }
    }
}
Also used : IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)

Example 44 with WorldServer

use of net.minecraft.world.WorldServer in project SpongeCommon by SpongePowered.

the class MixinChunk_Tracker method addTrackedBlockPosition.

@Override
public void addTrackedBlockPosition(Block block, BlockPos pos, User user, PlayerTracker.Type trackerType) {
    if (this.world.isRemote) {
        return;
    }
    if (PhaseTracker.getInstance().getCurrentState().ignoresBlockTracking()) {
        // Don't track chunk gen
        return;
    }
    // Don't track fake players
    if (user instanceof EntityPlayerMP && SpongeImplHooks.isFakePlayer((EntityPlayerMP) user)) {
        return;
    }
    if (!SpongeHooks.getActiveConfig((WorldServer) this.world).getConfig().getBlockTracking().getBlockBlacklist().contains(((BlockType) block).getId())) {
        SpongeHooks.logBlockTrack(this.world, block, pos, user, true);
    } else {
        SpongeHooks.logBlockTrack(this.world, block, pos, user, false);
    }
    final IMixinWorldInfo worldInfo = (IMixinWorldInfo) this.world.getWorldInfo();
    final int indexForUniqueId = worldInfo.getIndexForUniqueId(user.getUniqueId());
    if (pos.getY() <= 255) {
        short blockPos = blockPosToShort(pos);
        final PlayerTracker playerTracker = this.trackedShortBlockPositions.get(blockPos);
        if (playerTracker != null) {
            if (trackerType == PlayerTracker.Type.OWNER) {
                playerTracker.ownerIndex = indexForUniqueId;
                playerTracker.notifierIndex = indexForUniqueId;
            } else {
                playerTracker.notifierIndex = indexForUniqueId;
            }
        } else {
            this.trackedShortBlockPositions.put(blockPos, new PlayerTracker(indexForUniqueId, trackerType));
        }
    } else {
        int blockPos = blockPosToInt(pos);
        final PlayerTracker playerTracker = this.trackedIntBlockPositions.get(blockPos);
        if (playerTracker != null) {
            if (trackerType == PlayerTracker.Type.OWNER) {
                playerTracker.ownerIndex = indexForUniqueId;
            } else {
                playerTracker.notifierIndex = indexForUniqueId;
            }
        } else {
            this.trackedIntBlockPositions.put(blockPos, new PlayerTracker(indexForUniqueId, trackerType));
        }
    }
}
Also used : BlockType(org.spongepowered.api.block.BlockType) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) WorldServer(net.minecraft.world.WorldServer)

Example 45 with WorldServer

use of net.minecraft.world.WorldServer in project SpongeCommon by SpongePowered.

the class MixinWorldServer method createSnapshot.

@Override
public BlockSnapshot createSnapshot(int x, int y, int z) {
    BlockPos pos = new BlockPos(x, y, z);
    IBlockState currentState = this.getBlockState(pos);
    return this.createSpongeBlockSnapshot(currentState, currentState.getActualState((WorldServer) (Object) this, pos), pos, // it DOES tell the client about the block change.
    BlockChangeFlags.PHYSICS_OBSERVER);
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockPos(net.minecraft.util.math.BlockPos) IMixinBlockPos(org.spongepowered.common.interfaces.util.math.IMixinBlockPos) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer)

Aggregations

WorldServer (net.minecraft.world.WorldServer)322 BlockPos (net.minecraft.util.math.BlockPos)96 EntityPlayer (net.minecraft.entity.player.EntityPlayer)63 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)51 ItemStack (net.minecraft.item.ItemStack)48 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)48 TileEntity (net.minecraft.tileentity.TileEntity)42 Entity (net.minecraft.entity.Entity)41 IBlockState (net.minecraft.block.state.IBlockState)39 MinecraftServer (net.minecraft.server.MinecraftServer)35 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)31 World (net.minecraft.world.World)29 Block (net.minecraft.block.Block)26 EntityLivingBase (net.minecraft.entity.EntityLivingBase)23 ArrayList (java.util.ArrayList)20 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)18 Nullable (javax.annotation.Nullable)17 PotionEffect (net.minecraft.potion.PotionEffect)16 Chunk (net.minecraft.world.chunk.Chunk)16 World (org.spongepowered.api.world.World)13