--- a/net/minecraft/server/WorldServer.java +++ b/net/minecraft/server/WorldServer.java @@ -34,6 +34,15 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +// CraftBukkit start +import org.bukkit.Bukkit; +import org.bukkit.WeatherType; +import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.server.MapInitializeEvent; +import org.bukkit.event.weather.LightningStrikeEvent; +// CraftBukkit end + public class WorldServer extends World { private static final Logger LOGGER = LogManager.getLogger(); @@ -58,12 +67,29 @@ @Nullable private final MobSpawnerTrader mobSpawnerTrader; - public WorldServer(MinecraftServer minecraftserver, Executor executor, WorldNBTStorage worldnbtstorage, WorldData worlddata, DimensionManager dimensionmanager, GameProfilerFiller gameprofilerfiller, WorldLoadListener worldloadlistener) { + // CraftBukkit start + private int tickPosition; + + // Add env and gen to constructor + public WorldServer(MinecraftServer minecraftserver, Executor executor, WorldNBTStorage worldnbtstorage, WorldData worlddata, DimensionManager dimensionmanager, GameProfilerFiller gameprofilerfiller, WorldLoadListener worldloadlistener, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen) { super(worlddata, dimensionmanager, (world, worldprovider) -> { - return new ChunkProviderServer((WorldServer) world, worldnbtstorage.getDirectory(), worldnbtstorage.getDataFixer(), worldnbtstorage.f(), executor, worldprovider.getChunkGenerator(), minecraftserver.getPlayerList().getViewDistance(), worldloadlistener, () -> { + // CraftBukkit start + ChunkGenerator chunkGenerator; + + if (gen != null) { + chunkGenerator = new org.bukkit.craftbukkit.generator.CustomChunkGenerator(world, world.getSeed(), gen); + } else { + chunkGenerator = worldprovider.getChunkGenerator(); + } + + return new ChunkProviderServer((WorldServer) world, worldnbtstorage.getDirectory(), worldnbtstorage.getDataFixer(), worldnbtstorage.f(), executor, chunkGenerator, minecraftserver.getPlayerList().getViewDistance(), worldloadlistener, () -> { return minecraftserver.getWorldServer(DimensionManager.OVERWORLD).getWorldPersistentData(); }); - }, gameprofilerfiller, false); + // CraftBukkit end + }, gameprofilerfiller, false, gen, env); + this.pvpMode = minecraftserver.getPVP(); + worlddata.world = this; + // CraftBukkit end this.nextTickListBlock = new TickListServer<>(this, (block) -> { return block == null || block.getBlockData().isAir(); }, IRegistry.BLOCK::getKey, IRegistry.BLOCK::get, this::b); @@ -85,7 +111,8 @@ this.getWorldData().setGameType(minecraftserver.getGamemode()); } - this.mobSpawnerTrader = this.worldProvider.getDimensionManager() == DimensionManager.OVERWORLD ? new MobSpawnerTrader(this) : null; + this.mobSpawnerTrader = this.worldProvider.getDimensionManager().getType() == DimensionManager.OVERWORLD ? new MobSpawnerTrader(this) : null; // CraftBukkit - getType() + this.getServer().addWorld(this.getWorld()); // CraftBukkit } public void doTick(BooleanSupplier booleansupplier) { @@ -162,6 +189,7 @@ this.rainLevel = MathHelper.a(this.rainLevel, 0.0F, 1.0F); } + /* CraftBukkit start if (this.lastRainLevel != this.rainLevel) { this.server.getPlayerList().a((Packet) (new PacketPlayOutGameStateChange(7, this.rainLevel)), this.worldProvider.getDimensionManager()); } @@ -180,13 +208,34 @@ this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(7, this.rainLevel)); this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(8, this.thunderLevel)); } + // */ + for (int idx = 0; idx < this.players.size(); ++idx) { + if (((EntityPlayer) this.players.get(idx)).world == this) { + ((EntityPlayer) this.players.get(idx)).tickWeather(); + } + } + + if (flag != this.isRaining()) { + // Only send weather packets to those affected + for (int idx = 0; idx < this.players.size(); ++idx) { + if (((EntityPlayer) this.players.get(idx)).world == this) { + ((EntityPlayer) this.players.get(idx)).setPlayerWeather((!flag ? WeatherType.DOWNFALL : WeatherType.CLEAR), false); + } + } + } + for (int idx = 0; idx < this.players.size(); ++idx) { + if (((EntityPlayer) this.players.get(idx)).world == this) { + ((EntityPlayer) this.players.get(idx)).updateWeather(this.lastRainLevel, this.rainLevel, this.lastThunderLevel, this.thunderLevel); + } + } + // CraftBukkit end if (this.getWorldData().isHardcore() && this.getDifficulty() != EnumDifficulty.HARD) { this.getWorldData().setDifficulty(EnumDifficulty.HARD); } if (this.C && this.players.stream().noneMatch((entityplayer) -> { - return !entityplayer.isSpectator() && !entityplayer.isDeeplySleeping(); + return !entityplayer.isSpectator() && !entityplayer.isDeeplySleeping() && !entityplayer.fauxSleeping; // CraftBukkit })) { this.C = false; if (this.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE)) { @@ -225,7 +274,7 @@ this.ae(); this.ticking = false; gameprofilerfiller.exitEnter("entities"); - boolean flag3 = !this.players.isEmpty() || !this.getForceLoadedChunks().isEmpty(); + boolean flag3 = true || !this.players.isEmpty() || !this.getForceLoadedChunks().isEmpty(); // CraftBukkit - this prevents entity cleanup, other issues on servers with no players if (flag3) { this.resetEmptyTime(); @@ -239,6 +288,11 @@ for (i = 0; i < this.globalEntityList.size(); ++i) { entity = (Entity) this.globalEntityList.get(i); + // CraftBukkit start - Fixed an NPE + if (entity == null) { + continue; + } + // CraftBukkit end this.a((entity1) -> { ++entity1.ticksLived; entity1.tick(); @@ -257,6 +311,7 @@ Entity entity1 = (Entity) entry.getValue(); Entity entity2 = entity1.getVehicle(); + /* CraftBukkit start - We prevent spawning in general, so this butchering is not needed if (!this.server.getSpawnAnimals() && (entity1 instanceof EntityAnimal || entity1 instanceof EntityWaterAnimal)) { entity1.die(); } @@ -264,6 +319,7 @@ if (!this.server.getSpawnNPCs() && entity1 instanceof NPC) { entity1.die(); } + // CraftBukkit end */ if (entity2 != null) { if (!entity2.dead && entity2.w(entity1)) { @@ -324,10 +380,10 @@ entityhorseskeleton.r(true); entityhorseskeleton.setAgeRaw(0); entityhorseskeleton.setPosition((double) blockposition.getX(), (double) blockposition.getY(), (double) blockposition.getZ()); - this.addEntity(entityhorseskeleton); + this.addEntity(entityhorseskeleton, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.LIGHTNING); // CraftBukkit } - this.strikeLightning(new EntityLightning(this, (double) blockposition.getX() + 0.5D, (double) blockposition.getY(), (double) blockposition.getZ() + 0.5D, flag1)); + this.strikeLightning(new EntityLightning(this, (double) blockposition.getX() + 0.5D, (double) blockposition.getY(), (double) blockposition.getZ() + 0.5D, flag1), org.bukkit.event.weather.LightningStrikeEvent.Cause.WEATHER); // CraftBukkit } } @@ -338,11 +394,11 @@ BiomeBase biomebase = this.getBiome(blockposition); if (biomebase.a((IWorldReader) this, blockposition1)) { - this.setTypeUpdate(blockposition1, Blocks.ICE.getBlockData()); + org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(this, blockposition1, Blocks.ICE.getBlockData(), null); // CraftBukkit } if (flag && biomebase.b(this, blockposition)) { - this.setTypeUpdate(blockposition, Blocks.SNOW.getBlockData()); + org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(this, blockposition, Blocks.SNOW.getBlockData(), null); // CraftBukkit } if (flag && this.getBiome(blockposition1).b() == BiomeBase.Precipitation.RAIN) { @@ -389,7 +445,7 @@ protected BlockPosition a(BlockPosition blockposition) { BlockPosition blockposition1 = this.getHighestBlockYAt(HeightMap.Type.MOTION_BLOCKING, blockposition); AxisAlignedBB axisalignedbb = (new AxisAlignedBB(blockposition1, new BlockPosition(blockposition1.getX(), this.getBuildHeight(), blockposition1.getZ()))).g(3.0D); - List list = this.a(EntityLiving.class, axisalignedbb, (entityliving) -> { + List list = this.a(EntityLiving.class, axisalignedbb, (java.util.function.Predicate) (entityliving) -> { // CraftBukkit - decompile error return entityliving != null && entityliving.isAlive() && this.f(entityliving.getChunkCoordinates()); }); @@ -418,7 +474,7 @@ while (iterator.hasNext()) { EntityPlayer entityplayer = (EntityPlayer) iterator.next(); - if (entityplayer.isSpectator()) { + if (entityplayer.isSpectator() || (entityplayer.fauxSleeping && !entityplayer.isSleeping())) { // CraftBukkit ++i; } else if (entityplayer.isSleeping()) { ++j; @@ -436,10 +492,22 @@ } private void clearWeather() { - this.worldData.setWeatherDuration(0); + // CraftBukkit start this.worldData.setStorm(false); - this.worldData.setThunderDuration(0); + // If we stop due to everyone sleeping we should reset the weather duration to some other random value. + // Not that everyone ever manages to get the whole server to sleep at the same time.... + if (!this.worldData.hasStorm()) { + this.worldData.setWeatherDuration(0); + } + // CraftBukkit end this.worldData.setThundering(false); + // CraftBukkit start + // If we stop due to everyone sleeping we should reset the weather duration to some other random value. + // Not that everyone ever manages to get the whole server to sleep at the same time.... + if (!this.worldData.isThundering()) { + this.worldData.setThunderDuration(0); + } + // CraftBukkit end } public void resetEmptyTime() { @@ -477,6 +545,7 @@ return IRegistry.ENTITY_TYPE.getKey(entity.getEntityType()).toString(); }); entity.tick(); + entity.postTick(); // CraftBukkit this.getMethodProfiler().exit(); } @@ -562,6 +631,22 @@ BlockPosition blockposition = worldchunkmanager.a(0, 0, 256, list, random); ChunkCoordIntPair chunkcoordintpair = blockposition == null ? new ChunkCoordIntPair(0, 0) : new ChunkCoordIntPair(blockposition); + // CraftBukkit start + if (this.generator != null) { + Random rand = new Random(this.getSeed()); + org.bukkit.Location spawn = this.generator.getFixedSpawnLocation(((WorldServer) this).getWorld(), rand); + + if (spawn != null) { + if (spawn.getWorld() != ((WorldServer) this).getWorld()) { + throw new IllegalStateException("Cannot set spawn point for " + this.worldData.getName() + " to be in another world (" + spawn.getWorld().getName() + ")"); + } else { + this.worldData.setSpawn(new BlockPosition(spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ())); + return; + } + } + } + // CraftBukkit end + if (blockposition == null) { WorldServer.LOGGER.warn("Unable to find spawn biome"); } @@ -637,6 +722,7 @@ ChunkProviderServer chunkproviderserver = this.getChunkProvider(); if (!flag1) { + org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit if (iprogressupdate != null) { iprogressupdate.a(new ChatMessage("menu.savingLevel", new Object[0])); } @@ -648,6 +734,16 @@ chunkproviderserver.save(flag); } + + // CraftBukkit start - moved from MinecraftServer.saveChunks + // PAIL - rename + WorldServer worldserver1 = this; + WorldData worlddata = worldserver1.getWorldData(); + + worldserver1.getWorldBorder().a(worlddata); + worlddata.c(this.server.getBossBattleCustomData().c()); + worldserver1.getDataManager().saveWorldData(worlddata, this.server.getPlayerList().r()); + // CraftBukkit end } protected void k_() throws ExceptionWorldConflict { @@ -719,7 +815,8 @@ if (entity instanceof EntityInsentient) { EntityInsentient entityinsentient = (EntityInsentient) entity; - if (entityinsentient.isPersistent() || entityinsentient.I()) { + // CraftBukkit - Split out persistent check, don't apply it to special persistent mobs + if (entityinsentient.isTypeNotPersistent(0) && entityinsentient.isPersistent()) { continue; } } @@ -736,11 +833,24 @@ @Override public boolean addEntity(Entity entity) { - return this.addEntity0(entity); + // CraftBukkit start + return this.addEntity0(entity, CreatureSpawnEvent.SpawnReason.DEFAULT); + } + + @Override + public boolean addEntity(Entity entity, CreatureSpawnEvent.SpawnReason reason) { + return this.addEntity0(entity, reason); + // CraftBukkit end } public boolean addEntitySerialized(Entity entity) { - return this.addEntity0(entity); + // CraftBukkit start + return this.addEntitySerialized(entity, CreatureSpawnEvent.SpawnReason.DEFAULT); + } + + public boolean addEntitySerialized(Entity entity, CreatureSpawnEvent.SpawnReason reason) { + return this.addEntity0(entity, reason); + // CraftBukkit end } public void addEntityTeleport(Entity entity) { @@ -790,13 +900,18 @@ this.registerEntity(entityplayer); } - private boolean addEntity0(Entity entity) { + // CraftBukkit start + private boolean addEntity0(Entity entity, CreatureSpawnEvent.SpawnReason spawnReason) { if (entity.dead) { - WorldServer.LOGGER.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getName(entity.getEntityType())); + // WorldServer.LOGGER.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getName(entity.getEntityType())); // CraftBukkit return false; } else if (this.isUUIDTaken(entity)) { return false; } else { + if (!CraftEventFactory.doEntityAddEventCalling(this, entity, spawnReason)) { + return false; + } + // CraftBukkit end IChunkAccess ichunkaccess = this.getChunkAt(MathHelper.floor(entity.locX / 16.0D), MathHelper.floor(entity.locZ / 16.0D), ChunkStatus.FULL, entity.attachedToPlayer); if (!(ichunkaccess instanceof Chunk)) { @@ -824,7 +939,7 @@ if (entity1 == null) { return false; } else { - WorldServer.LOGGER.warn("Keeping entity {} that already exists with UUID {}", EntityTypes.getName(entity1.getEntityType()), entity.getUniqueID().toString()); + // WorldServer.LOGGER.warn("Keeping entity {} that already exists with UUID {}", EntityTypes.getName(entity1.getEntityType()), entity.getUniqueID().toString()); // CraftBukkit return true; } } @@ -879,6 +994,7 @@ this.H.remove(((EntityInsentient) entity).getNavigation()); } + entity.valid = false; // CraftBukkit } private void registerEntity(Entity entity) { @@ -902,6 +1018,7 @@ if (entity instanceof EntityInsentient) { this.H.add(((EntityInsentient) entity).getNavigation()); } + entity.valid = true; // CraftBukkit } } @@ -932,6 +1049,18 @@ } public void strikeLightning(EntityLightning entitylightning) { + // CraftBukkit start + this.strikeLightning(entitylightning, LightningStrikeEvent.Cause.UNKNOWN); + } + + public void strikeLightning(EntityLightning entitylightning, LightningStrikeEvent.Cause cause) { + LightningStrikeEvent lightning = new LightningStrikeEvent(this.getWorld(), (org.bukkit.entity.LightningStrike) entitylightning.getBukkitEntity(), cause); + this.getServer().getPluginManager().callEvent(lightning); + + if (lightning.isCancelled()) { + return; + } + // CraftBukkit end this.globalEntityList.add(entitylightning); this.server.getPlayerList().sendPacketNearby((EntityHuman) null, entitylightning.locX, entitylightning.locY, entitylightning.locZ, 512.0D, this.worldProvider.getDimensionManager(), new PacketPlayOutSpawnEntityWeather(entitylightning)); } @@ -940,6 +1069,12 @@ public void a(int i, BlockPosition blockposition, int j) { Iterator iterator = this.server.getPlayerList().getPlayers().iterator(); + // CraftBukkit start + EntityHuman entityhuman = null; + Entity entity = this.getEntity(i); + if (entity instanceof EntityHuman) entityhuman = (EntityHuman) entity; + // CraftBukkit end + while (iterator.hasNext()) { EntityPlayer entityplayer = (EntityPlayer) iterator.next(); @@ -948,6 +1083,12 @@ double d1 = (double) blockposition.getY() - entityplayer.locY; double d2 = (double) blockposition.getZ() - entityplayer.locZ; + // CraftBukkit start + if (entityhuman != null && entityhuman instanceof EntityPlayer && !entityplayer.getBukkitEntity().canSee(((EntityPlayer) entityhuman).getBukkitEntity())) { + continue; + } + // CraftBukkit end + if (d0 * d0 + d1 * d1 + d2 * d2 < 1024.0D) { entityplayer.playerConnection.sendPacket(new PacketPlayOutBlockBreakAnimation(i, blockposition, j)); } @@ -1008,6 +1149,14 @@ @Override public Explosion createExplosion(@Nullable Entity entity, DamageSource damagesource, double d0, double d1, double d2, float f, boolean flag, Explosion.Effect explosion_effect) { + // CraftBukkit start + Explosion explosion = super.createExplosion(entity, damagesource, d0, d1, d2, f, flag, explosion_effect); + + if (explosion.wasCanceled) { + return explosion; + } + + /* Remove Explosion explosion = new Explosion(this, entity, d0, d1, d2, f, flag, explosion_effect); if (damagesource != null) { @@ -1016,6 +1165,8 @@ explosion.a(); explosion.a(false); + */ + // CraftBukkit end - TODO: Check if explosions are still properly implemented if (explosion_effect == Explosion.Effect.NONE) { explosion.clearBlocks(); } @@ -1080,13 +1231,20 @@ } public int a(T t0, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6) { - PacketPlayOutWorldParticles packetplayoutworldparticles = new PacketPlayOutWorldParticles(t0, false, (float) d0, (float) d1, (float) d2, (float) d3, (float) d4, (float) d5, (float) d6, i); + // CraftBukkit - visibility api support + return sendParticles(null, t0, d0, d1, d2, i, d3, d4, d5, d6, false); + } + + public int sendParticles(EntityPlayer sender, T t0, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6, boolean force) { + PacketPlayOutWorldParticles packetplayoutworldparticles = new PacketPlayOutWorldParticles(t0, force, (float) d0, (float) d1, (float) d2, (float) d3, (float) d4, (float) d5, (float) d6, i); + // CraftBukkit end int j = 0; for (int k = 0; k < this.players.size(); ++k) { EntityPlayer entityplayer = (EntityPlayer) this.players.get(k); + if (sender != null && !entityplayer.getBukkitEntity().canSee(sender.getBukkitEntity())) continue; // CraftBukkit - if (this.a(entityplayer, false, d0, d1, d2, packetplayoutworldparticles)) { + if (this.a(entityplayer, force, d0, d1, d2, packetplayoutworldparticles)) { // CraftBukkit ++j; } } @@ -1169,7 +1327,13 @@ @Override public WorldMap a(String s) { return (WorldMap) this.getMinecraftServer().getWorldServer(DimensionManager.OVERWORLD).getWorldPersistentData().b(() -> { - return new WorldMap(s); + // CraftBukkit start + // We only get here when the data file exists, but is not a valid map + WorldMap newMap = new WorldMap(s); + MapInitializeEvent event = new MapInitializeEvent(newMap.mapView); + Bukkit.getServer().getPluginManager().callEvent(event); + return newMap; + // CraftBukkit end }, s); }