--- a/net/minecraft/world/level/chunk/Chunk.java +++ b/net/minecraft/world/level/chunk/Chunk.java @@ -78,7 +78,7 @@ }; private final Map tickersInLevel; public boolean loaded; - public final World level; + public final WorldServer level; // CraftBukkit - type @Nullable private Supplier fullStatus; @Nullable @@ -97,7 +97,7 @@ this.tickersInLevel = Maps.newHashMap(); this.unsavedListener = (chunkcoordintpair1) -> { }; - this.level = world; + this.level = (WorldServer) world; // CraftBukkit - type this.gameEventListenerRegistrySections = new Int2ObjectOpenHashMap(); for (HeightMap.Type heightmap_type : HeightMap.Type.values()) { @@ -111,6 +111,11 @@ this.fluidTicks = levelchunkticks1; } + // CraftBukkit start + public boolean mustNotSave; + public boolean needsDecoration; + // CraftBukkit end + public Chunk(WorldServer worldserver, ProtoChunk protochunk, @Nullable Chunk.c chunk_c) { this(worldserver, protochunk.getPos(), protochunk.getUpgradeData(), protochunk.unpackBlockTicks(), protochunk.unpackFluidTicks(), protochunk.getInhabitedTime(), protochunk.getSections(), chunk_c, protochunk.getBlendingData()); if (!Collections.disjoint(protochunk.pendingBlockEntities.keySet(), protochunk.blockEntities.keySet())) { @@ -139,6 +144,10 @@ this.skyLightSources = protochunk.skyLightSources; this.setLightCorrect(protochunk.isLightCorrect()); this.markUnsaved(); + this.needsDecoration = true; // CraftBukkit + // CraftBukkit start + this.persistentDataContainer = protochunk.persistentDataContainer; // SPIGOT-6814: copy PDC to account for 1.17 to 1.18 chunk upgrading. + // CraftBukkit end } public void setUnsavedListener(Chunk.e chunk_e) { @@ -332,7 +341,8 @@ if (!chunksection.getBlockState(k, l, i1).is(block)) { return null; } else { - if (!this.level.isClientSide && (i & 512) == 0) { + // CraftBukkit - Don't place while processing the BlockPlaceEvent, unless it's a BlockContainer. Prevents blocks such as TNT from activating when cancelled. + if (!this.level.isClientSide && (i & 512) == 0 && (!this.level.captureBlockStates || block instanceof net.minecraft.world.level.block.BlockTileEntity)) { iblockdata.onPlace(this.level, blockposition, iblockdata1, flag3); } @@ -383,7 +393,12 @@ @Nullable public TileEntity getBlockEntity(BlockPosition blockposition, Chunk.EnumTileEntityState chunk_enumtileentitystate) { - TileEntity tileentity = (TileEntity) this.blockEntities.get(blockposition); + // CraftBukkit start + TileEntity tileentity = level.capturedTileEntities.get(blockposition); + if (tileentity == null) { + tileentity = (TileEntity) this.blockEntities.get(blockposition); + } + // CraftBukkit end if (tileentity == null) { NBTTagCompound nbttagcompound = (NBTTagCompound) this.pendingBlockEntities.remove(blockposition); @@ -455,6 +470,7 @@ if (!iblockdata.hasBlockEntity()) { Chunk.LOGGER.warn("Trying to set block entity {} at position {}, but state {} does not allow it", new Object[]{tileentity, blockposition, iblockdata}); + new Exception().printStackTrace(); // CraftBukkit } else { IBlockData iblockdata1 = tileentity.getBlockState(); @@ -509,6 +525,12 @@ if (this.isInLevel()) { TileEntity tileentity = (TileEntity) this.blockEntities.remove(blockposition); + // CraftBukkit start - SPIGOT-5561: Also remove from pending map + if (!pendingBlockEntities.isEmpty()) { + pendingBlockEntities.remove(blockposition); + } + // CraftBukkit end + if (tileentity != null) { World world = this.level; @@ -562,6 +584,57 @@ } + // CraftBukkit start + public void loadCallback() { + org.bukkit.Server server = this.level.getCraftServer(); + if (server != null) { + /* + * If it's a new world, the first few chunks are generated inside + * the World constructor. We can't reliably alter that, so we have + * no way of creating a CraftWorld/CraftServer at that point. + */ + org.bukkit.Chunk bukkitChunk = new org.bukkit.craftbukkit.CraftChunk(this); + server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkLoadEvent(bukkitChunk, this.needsDecoration)); + + if (this.needsDecoration) { + this.needsDecoration = false; + java.util.Random random = new java.util.Random(); + random.setSeed(level.getSeed()); + long xRand = random.nextLong() / 2L * 2L + 1L; + long zRand = random.nextLong() / 2L * 2L + 1L; + random.setSeed((long) this.chunkPos.x * xRand + (long) this.chunkPos.z * zRand ^ level.getSeed()); + + org.bukkit.World world = this.level.getWorld(); + if (world != null) { + this.level.populating = true; + try { + for (org.bukkit.generator.BlockPopulator populator : world.getPopulators()) { + populator.populate(world, random, bukkitChunk); + } + } finally { + this.level.populating = false; + } + } + server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkPopulateEvent(bukkitChunk)); + } + } + } + + public void unloadCallback() { + org.bukkit.Server server = this.level.getCraftServer(); + org.bukkit.Chunk bukkitChunk = new org.bukkit.craftbukkit.CraftChunk(this); + org.bukkit.event.world.ChunkUnloadEvent unloadEvent = new org.bukkit.event.world.ChunkUnloadEvent(bukkitChunk, this.isUnsaved()); + server.getPluginManager().callEvent(unloadEvent); + // note: saving can be prevented, but not forced if no saving is actually required + this.mustNotSave = !unloadEvent.isSaveChunk(); + } + + @Override + public boolean isUnsaved() { + return super.isUnsaved() && !this.mustNotSave; + } + // CraftBukkit end + public boolean isEmpty() { return false; } @@ -736,7 +809,7 @@ private void updateBlockEntityTicker(T t0) { IBlockData iblockdata = t0.getBlockState(); - BlockEntityTicker blockentityticker = iblockdata.getTicker(this.level, ((TileEntity) t0).getType()); + BlockEntityTicker blockentityticker = iblockdata.getTicker(this.level, (TileEntityTypes) ((TileEntity) t0).getType()); // CraftBukkit - decompile error if (blockentityticker == null) { this.removeBlockEntityTicker(t0.getBlockPos()); @@ -778,7 +851,7 @@ private boolean loggedInvalidBlockState; a(final TileEntity tileentity, final BlockEntityTicker blockentityticker) { - this.blockEntity = tileentity; + this.blockEntity = (T) tileentity; // CraftBukkit - decompile error this.ticker = blockentityticker; }