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();
}
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;
}
}
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()]);
}
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;
}
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();
}
Aggregations