Search in sources :

Example 21 with IMixinWorldServer

use of org.spongepowered.common.interfaces.world.IMixinWorldServer in project SpongeCommon by SpongePowered.

the class PlaceBlockPacketState method unwind.

@Override
public void unwind(BasicPacketContext context) {
    final Packet<?> packet = context.getPacket();
    final EntityPlayerMP player = context.getPacketPlayer();
    final IMixinWorldServer mixinWorld = (IMixinWorldServer) player.world;
    // Note - CPacketPlayerTryUseItem is swapped with
    // CPacketPlayerBlockPlacement
    final ItemStack itemStack = context.getItemUsed();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
    context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
        try (@SuppressWarnings("unused") CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(player);
            Sponge.getCauseStackManager().pushCause(snapshot);
            Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.SPAWN_EGG);
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            if (!spawnEntityEvent.isCancelled()) {
                processSpawnedEntities(player, spawnEntityEvent);
            }
        }
    });
    context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(originalBlocks -> {
        Sponge.getCauseStackManager().pushCause(player);
        boolean success = TrackingUtil.processBlockCaptures(originalBlocks, this, context);
        if (!success && snapshot != ItemTypeRegistryModule.NONE_SNAPSHOT) {
            Sponge.getCauseStackManager().pushCause(player);
            EnumHand hand = ((CPacketPlayerTryUseItemOnBlock) packet).getHand();
            PacketPhaseUtil.handlePlayerSlotRestore(player, (net.minecraft.item.ItemStack) itemStack, hand);
        }
        Sponge.getCauseStackManager().popCause();
    });
    context.getCapturedItemStackSupplier().acceptAndClearIfNotEmpty(drops -> {
        final List<Entity> entities = drops.stream().map(drop -> drop.create(player.getServerWorld())).map(EntityUtil::fromNative).collect(Collectors.toList());
        if (!entities.isEmpty()) {
            try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
                Sponge.getCauseStackManager().pushCause(player);
                DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(Sponge.getCauseStackManager().getCurrentCause(), entities);
                SpongeImpl.postEvent(event);
                if (!event.isCancelled()) {
                    for (Entity droppedItem : event.getEntities()) {
                        droppedItem.setCreator(player.getUniqueID());
                        mixinWorld.forceSpawnEntity(droppedItem);
                    }
                }
            }
        }
    });
    final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
    mixinContainer.setCaptureInventory(false);
    mixinContainer.getCapturedTransactions().clear();
}
Also used : Entity(org.spongepowered.api.entity.Entity) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EnumHand(net.minecraft.util.EnumHand) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 22 with IMixinWorldServer

use of org.spongepowered.common.interfaces.world.IMixinWorldServer in project SpongeCommon by SpongePowered.

the class MixinEntityMob method isValidLightLevel.

/**
 * @author aikar - February 20th, 2017 - Optimizes light level check.
 * @author blood - February 20th, 2017 - Avoids checking unloaded chunks and chunks with pending light updates.
 *
 * @reason Avoids checking unloaded chunks and chunks with pending light updates.
 *
 * @return Whether current position has a valid light level for spawning
 */
@Overwrite
protected boolean isValidLightLevel() {
    final BlockPos blockpos = new BlockPos(this.posX, this.getEntityBoundingBox().minY, this.posZ);
    final Chunk chunk = ((IMixinChunkProviderServer) this.world.getChunkProvider()).getLoadedChunkWithoutMarkingActive(blockpos.getX() >> 4, blockpos.getZ() >> 4);
    if (chunk == null || chunk.unloadQueued) {
        return false;
    }
    if (this.world.getLightFor(EnumSkyBlock.SKY, blockpos) > this.rand.nextInt(32)) {
        return false;
    } else {
        // int i = this.worldObj.getLightFromNeighbors(blockpos);
        // Sponge
        boolean passes;
        if (this.world.isThundering()) {
            int j = this.world.getSkylightSubtracted();
            ;
            this.world.setSkylightSubtracted(10);
            passes = !((IMixinWorldServer) this.world).isLightLevel(chunk, blockpos, this.rand.nextInt(9));
            this.world.setSkylightSubtracted(j);
        } else {
            passes = !((IMixinWorldServer) this.world).isLightLevel(chunk, blockpos, this.rand.nextInt(9));
        }
        return passes;
    }
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) Chunk(net.minecraft.world.chunk.Chunk) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 23 with IMixinWorldServer

use of org.spongepowered.common.interfaces.world.IMixinWorldServer in project SpongeCommon by SpongePowered.

the class WorldManager method reorderWorldsVanillaFirst.

public static void reorderWorldsVanillaFirst() {
    final List<WorldServer> sorted = new LinkedList<>();
    int vanillaWorldsCount = 0;
    WorldServer worldServer = worldByDimensionId.get(0);
    if (worldServer != null) {
        sorted.add(worldServer);
        vanillaWorldsCount++;
    }
    worldServer = worldByDimensionId.get(-1);
    if (worldServer != null) {
        sorted.add(worldServer);
        vanillaWorldsCount++;
    }
    worldServer = worldByDimensionId.get(1);
    if (worldServer != null) {
        sorted.add(worldServer);
        vanillaWorldsCount++;
    }
    final List<WorldServer> worlds = new ArrayList<>(worldByDimensionId.values());
    final Iterator<WorldServer> iterator = worlds.iterator();
    while (iterator.hasNext()) {
        final IMixinWorldServer mixinWorld = (IMixinWorldServer) iterator.next();
        final Integer dimensionId = mixinWorld.getDimensionId();
        if (dimensionId < vanillaWorldsCount - 1) {
            iterator.remove();
        }
    }
    worlds.sort(WORLD_SERVER_COMPARATOR);
    sorted.addAll(worlds);
    SpongeImpl.getServer().worlds = sorted.toArray(new WorldServer[sorted.size()]);
}
Also used : ArrayList(java.util.ArrayList) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) LinkedList(java.util.LinkedList)

Example 24 with IMixinWorldServer

use of org.spongepowered.common.interfaces.world.IMixinWorldServer in project SpongeCommon by SpongePowered.

the class WorldManager method unloadWorld.

// TODO Result
public static boolean unloadWorld(WorldServer worldServer, boolean checkConfig) {
    checkNotNull(worldServer);
    final MinecraftServer server = SpongeImpl.getServer();
    // Likely leaked, don't want to drop leaked world data
    if (!worldByDimensionId.containsValue(worldServer)) {
        return false;
    }
    // Vanilla sometimes doesn't remove player entities from world first
    if (server.isServerRunning()) {
        if (!worldServer.playerEntities.isEmpty()) {
            return false;
        }
        // We only check config if base game wants to unload world. If mods/plugins say unload, we unload
        if (checkConfig) {
            if (((WorldProperties) worldServer.getWorldInfo()).doesKeepSpawnLoaded()) {
                return false;
            }
        }
    }
    try (PhaseContext<?> context = GeneralPhase.State.WORLD_UNLOAD.createPhaseContext().source(worldServer).buildAndSwitch()) {
        if (SpongeImpl.postEvent(SpongeEventFactory.createUnloadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer))) {
            return false;
        }
        final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldServer;
        final int dimensionId = mixinWorldServer.getDimensionId();
        try {
            // Don't save if server is stopping to avoid duplicate saving.
            if (server.isServerRunning()) {
                saveWorld(worldServer, true);
            }
            mixinWorldServer.getActiveConfig().save();
        } catch (MinecraftException e) {
            e.printStackTrace();
        } finally {
            worldByDimensionId.remove(dimensionId);
            weakWorldByWorld.remove(worldServer);
            ((IMixinMinecraftServer) server).removeWorldTickTimes(dimensionId);
            SpongeImpl.getLogger().info("Unloading world [{}] (DIM{})", worldServer.getWorldInfo().getWorldName(), dimensionId);
            reorderWorldsVanillaFirst();
        }
        if (!server.isServerRunning()) {
            unregisterDimension(dimensionId);
        }
    }
    return true;
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftException(net.minecraft.world.MinecraftException) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) World(net.minecraft.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 25 with IMixinWorldServer

use of org.spongepowered.common.interfaces.world.IMixinWorldServer in project SpongeCommon by SpongePowered.

the class SpongeChunkGenerator method populate.

@Override
public void populate(int chunkX, int chunkZ) {
    IMixinWorldServer world = (IMixinWorldServer) this.world;
    world.getTimingsHandler().chunkPopulate.startTimingIfSync();
    this.chunkGeneratorTiming.startTimingIfSync();
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    this.rand.setSeed(this.world.getSeed());
    long i1 = this.rand.nextLong() / 2L * 2L + 1L;
    long j1 = this.rand.nextLong() / 2L * 2L + 1L;
    this.rand.setSeed(chunkX * i1 + chunkZ * j1 ^ this.world.getSeed());
    BlockFalling.fallInstantly = true;
    // Have to regeneate the biomes so that any virtual biomes can be passed
    // to the populator.
    this.cachedBiomes.reuse(new Vector3i(chunkX * 16, 0, chunkZ * 16));
    this.biomeGenerator.generateBiomes(this.cachedBiomes);
    ImmutableBiomeVolume biomeBuffer = this.cachedBiomes.getImmutableBiomeCopy();
    BlockPos blockpos = new BlockPos(chunkX * 16, 0, chunkZ * 16);
    BiomeType biome = (BiomeType) this.world.getBiome(blockpos.add(16, 0, 16));
    org.spongepowered.api.world.Chunk chunk = (org.spongepowered.api.world.Chunk) this.world.getChunkFromChunkCoords(chunkX, chunkZ);
    BiomeGenerationSettings settings = getBiomeSettings(biome);
    List<Populator> populators = new ArrayList<>(this.pop);
    Populator snowPopulator = null;
    Iterator<Populator> itr = populators.iterator();
    while (itr.hasNext()) {
        Populator populator = itr.next();
        if (populator instanceof SnowPopulator) {
            itr.remove();
            snowPopulator = populator;
            break;
        }
    }
    populators.addAll(settings.getPopulators());
    if (snowPopulator != null) {
        populators.add(snowPopulator);
    }
    Sponge.getGame().getEventManager().post(SpongeEventFactory.createPopulateChunkEventPre(Sponge.getCauseStackManager().getCurrentCause(), populators, chunk));
    List<String> flags = Lists.newArrayList();
    Vector3i min = new Vector3i(chunkX * 16 + 8, 0, chunkZ * 16 + 8);
    org.spongepowered.api.world.World spongeWorld = (org.spongepowered.api.world.World) this.world;
    Extent volume = new SoftBufferExtentViewDownsize(chunk.getWorld(), min, min.add(15, 255, 15), min.sub(8, 0, 8), min.add(23, 255, 23));
    for (Populator populator : populators) {
        final PopulatorType type = populator.getType();
        if (type == null) {
            System.err.printf("Found a populator with a null type: %s populator%n", populator);
        }
        if (Sponge.getGame().getEventManager().post(SpongeEventFactory.createPopulateChunkEventPopulate(Sponge.getCauseStackManager().getCurrentCause(), populator, chunk))) {
            continue;
        }
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Timing timing = null;
            if (Timings.isTimingsEnabled()) {
                timing = this.populatorTimings.get(populator.getType().getId());
                if (timing == null) {
                    // ,
                    timing = SpongeTimingsFactory.ofSafe("populate - " + populator.getType().getId());
                    // this.chunkGeneratorTiming);
                    this.populatorTimings.put(populator.getType().getId(), timing);
                }
                timing.startTimingIfSync();
            }
            try (PhaseContext<?> context = GenerationPhase.State.POPULATOR_RUNNING.createPhaseContext().world(world).populator(type).buildAndSwitch()) {
                if (populator instanceof IFlaggedPopulator) {
                    ((IFlaggedPopulator) populator).populate(spongeWorld, volume, this.rand, biomeBuffer, flags);
                } else {
                    populator.populate(spongeWorld, volume, this.rand, biomeBuffer);
                }
                if (Timings.isTimingsEnabled()) {
                    timing.stopTimingIfSync();
                }
            }
        }
    }
    // populate method so that its particular changes are used.
    if (this.baseGenerator instanceof SpongeGenerationPopulator) {
        Timing timing = null;
        if (Timings.isTimingsEnabled()) {
            IGenerationPopulator spongePopulator = (IGenerationPopulator) this.baseGenerator;
            timing = spongePopulator.getTimingsHandler();
            timing.startTimingIfSync();
        }
        ((SpongeGenerationPopulator) this.baseGenerator).getHandle(this.world).populate(chunkX, chunkZ);
        if (Timings.isTimingsEnabled()) {
            timing.stopTimingIfSync();
        }
    }
    PopulateChunkEvent.Post event = SpongeEventFactory.createPopulateChunkEventPost(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.copyOf(populators), chunk);
    SpongeImpl.postEvent(event);
    BlockFalling.fallInstantly = false;
    this.chunkGeneratorTiming.stopTimingIfSync();
    world.getTimingsHandler().chunkPopulate.stopTimingIfSync();
}
Also used : Extent(org.spongepowered.api.world.extent.Extent) ArrayList(java.util.ArrayList) IGenerationPopulator(org.spongepowered.common.interfaces.world.gen.IGenerationPopulator) PopulatorType(org.spongepowered.api.world.gen.PopulatorType) World(net.minecraft.world.World) BiomeType(org.spongepowered.api.world.biome.BiomeType) SoftBufferExtentViewDownsize(org.spongepowered.common.world.extent.SoftBufferExtentViewDownsize) ChunkGeneratorOverworld(net.minecraft.world.gen.ChunkGeneratorOverworld) IChunkProviderOverworld(org.spongepowered.common.interfaces.world.gen.IChunkProviderOverworld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.util.math.BlockPos) BiomeGenerationSettings(org.spongepowered.api.world.biome.BiomeGenerationSettings) SpongeBiomeGenerationSettings(org.spongepowered.common.world.biome.SpongeBiomeGenerationSettings) GenerationPopulator(org.spongepowered.api.world.gen.GenerationPopulator) Populator(org.spongepowered.api.world.gen.Populator) IFlaggedPopulator(org.spongepowered.common.interfaces.world.gen.IFlaggedPopulator) SnowPopulator(org.spongepowered.common.world.gen.populators.SnowPopulator) IGenerationPopulator(org.spongepowered.common.interfaces.world.gen.IGenerationPopulator) ImmutableBiomeVolume(org.spongepowered.api.world.extent.ImmutableBiomeVolume) IFlaggedPopulator(org.spongepowered.common.interfaces.world.gen.IFlaggedPopulator) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) PopulateChunkEvent(org.spongepowered.api.event.world.chunk.PopulateChunkEvent) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Vector3i(com.flowpowered.math.vector.Vector3i) SnowPopulator(org.spongepowered.common.world.gen.populators.SnowPopulator) Timing(co.aikar.timings.Timing)

Aggregations

IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)61 WorldServer (net.minecraft.world.WorldServer)25 BlockPos (net.minecraft.util.math.BlockPos)19 ArrayList (java.util.ArrayList)17 Entity (org.spongepowered.api.entity.Entity)15 World (org.spongepowered.api.world.World)15 IBlockState (net.minecraft.block.state.IBlockState)13 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)13 CauseStackManager (org.spongepowered.api.event.CauseStackManager)11 Overwrite (org.spongepowered.asm.mixin.Overwrite)11 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)9 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)9 EntityItem (net.minecraft.entity.item.EntityItem)8 User (org.spongepowered.api.entity.living.player.User)8 LocatableBlock (org.spongepowered.api.world.LocatableBlock)8 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)7 Entity (net.minecraft.entity.Entity)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)6 Chunk (net.minecraft.world.chunk.Chunk)6 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)6