Skip to content

Commit

Permalink
Fixed the plugin creating new chunks into disk for no reason (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jun 9, 2022
1 parent 41a5755 commit 73763ac
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 48 deletions.
@@ -1,12 +1,21 @@
package com.bgsoftware.superiorskyblock.listeners;

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.world.chunks.ChunkPosition;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public final class ChunksListener implements Listener {

Expand All @@ -18,11 +27,22 @@ public ChunksListener(SuperiorSkyblockPlugin plugin) {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnloadMonitor(ChunkUnloadEvent e) {
// noinspection deprecation
plugin.getWorldEventsManager().unloadChunk(e.getChunk());
}

@EventHandler
public void onChunkLoad(ChunkLoadEvent e) {
if (e.isNewChunk() && plugin.getGrid().isIslandsWorld(e.getWorld()) && e.getWorld().getEnvironment() == World.Environment.NORMAL) {
// We want to update the biome for new island chunks.
Island island = plugin.getGrid().getIslandAt(e.getChunk());
if (island != null && !island.getBiome().name().equals(plugin.getSettings().getWorlds().getNormal().getBiome())) {
List<Player> playersToUpdate = island.getAllPlayersInside().stream().map(SuperiorPlayer::asPlayer).collect(Collectors.toList());
plugin.getNMSChunks().setBiome(Collections.singletonList(ChunkPosition.of(e.getChunk())), island.getBiome(), playersToUpdate);
}
}

// noinspection deprecation
plugin.getWorldEventsManager().loadChunk(e.getChunk());
}

Expand Down
Expand Up @@ -40,10 +40,6 @@ public void loadChunk(Chunk chunk) {
if (island == null || island.isSpawn())
return;

if (chunk.getWorld().getEnvironment() == plugin.getSettings().getWorlds().getDefaultWorld()) {
island.setBiome(firstBlock.getWorld().getBiome(firstBlock.getBlockX(), firstBlock.getBlockZ()), false);
}

plugin.getNMSChunks().injectChunkSections(chunk);

boolean cropGrowthEnabled = BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeCropGrowth.class);
Expand All @@ -66,8 +62,12 @@ public void loadChunk(Chunk chunk) {

Location islandCenter = island.getCenter(chunk.getWorld().getEnvironment());

if (BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeEntityLimits.class)) {
if (chunk.getX() == (islandCenter.getBlockX() >> 4) && chunk.getZ() == (islandCenter.getBlockZ() >> 4)) {
if (chunk.getX() == (islandCenter.getBlockX() >> 4) && chunk.getZ() == (islandCenter.getBlockZ() >> 4)) {
if (chunk.getWorld().getEnvironment() == plugin.getSettings().getWorlds().getDefaultWorld()) {
island.setBiome(firstBlock.getWorld().getBiome(firstBlock.getBlockX(), firstBlock.getBlockZ()), false);
}

if (BuiltinModules.UPGRADES.isUpgradeTypeEnabled(UpgradeTypeEntityLimits.class)) {
Executor.sync(() -> {
if (chunk.isLoaded())
island.getEntitiesTracker().recalculateEntityCounts();
Expand Down
Expand Up @@ -86,6 +86,9 @@ public static void runActionOnUnloadedChunks(WorldServer worldServer, Collection

Executor.createTask().runAsync(v -> {
chunks.forEach(chunkCoords -> {
if (!chunkLoader.chunkExists(chunkCoords.x, chunkCoords.z))
return;

try {
Chunk loadedChunk = chunkLoader.a(worldServer, chunkCoords.x, chunkCoords.z);

Expand Down
Expand Up @@ -18,7 +18,6 @@
import net.minecraft.server.v1_16_R3.Chunk;
import net.minecraft.server.v1_16_R3.ChunkConverter;
import net.minecraft.server.v1_16_R3.ChunkCoordIntPair;
import net.minecraft.server.v1_16_R3.ChunkRegionLoader;
import net.minecraft.server.v1_16_R3.ChunkSection;
import net.minecraft.server.v1_16_R3.HeightMap;
import net.minecraft.server.v1_16_R3.IBlockData;
Expand Down Expand Up @@ -103,18 +102,16 @@ public static void runActionOnUnloadedChunks(WorldServer worldServer,
try {
NBTTagCompound chunkCompound = playerChunkMap.read(chunkCoords);

if (chunkCompound == null) {
ProtoChunk protoChunk = createProtoChunk(chunkCoords, worldServer);
chunkCompound = ChunkRegionLoader.saveChunk(worldServer, protoChunk);
} else {
chunkCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound, chunkCoords, worldServer);
}
if (chunkCompound == null)
return;

NBTTagCompound chunkDataCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound, chunkCoords, worldServer);

if (chunkCompound.hasKeyOfType("Level", 10)) {
chunkConsumer.accept(chunkCoords, chunkCompound.getCompound("Level"));
if (chunkDataCompound.hasKeyOfType("Level", 10)) {
chunkConsumer.accept(chunkCoords, chunkDataCompound.getCompound("Level"));
if (saveChunks)
playerChunkMap.a(chunkCoords, chunkCompound);
playerChunkMap.a(chunkCoords, chunkDataCompound);
}
} catch (Exception ex) {
ex.printStackTrace();
Expand Down
Expand Up @@ -30,7 +30,6 @@
import net.minecraft.world.level.chunk.ChunkSection;
import net.minecraft.world.level.chunk.IChunkAccess;
import net.minecraft.world.level.chunk.ProtoChunk;
import net.minecraft.world.level.chunk.storage.ChunkRegionLoader;
import net.minecraft.world.level.levelgen.HeightMap;

import java.util.ArrayList;
Expand Down Expand Up @@ -103,18 +102,16 @@ public static void runActionOnUnloadedChunks(WorldServer worldServer,
try {
NBTTagCompound chunkCompound = playerChunkMap.read(chunkCoords);

if (chunkCompound == null) {
ProtoChunk protoChunk = createProtoChunk(chunkCoords, worldServer);
chunkCompound = ChunkRegionLoader.saveChunk(worldServer, protoChunk);
} else {
chunkCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound, chunkCoords, worldServer);
}
if (chunkCompound == null)
return;

NBTTagCompound chunkDataCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound, chunkCoords, worldServer);

if (chunkCompound.hasKeyOfType("Level", 10)) {
chunkConsumer.accept(chunkCoords, chunkCompound.getCompound("Level"));
if (chunkDataCompound.hasKeyOfType("Level", 10)) {
chunkConsumer.accept(chunkCoords, chunkDataCompound.getCompound("Level"));
if (saveChunks)
playerChunkMap.a(chunkCoords, chunkCompound);
playerChunkMap.a(chunkCoords, chunkDataCompound);
}
} catch (Exception ex) {
ex.printStackTrace();
Expand Down
Expand Up @@ -120,20 +120,18 @@ public static void runActionOnUnloadedChunks(WorldServer worldServer,
try {
NBTTagCompound chunkCompound = playerChunkMap.read(chunkCoords);

if (chunkCompound == null) {
ChunkAccess protoChunk = createProtoChunk(chunkCoords, worldServer);
chunkCompound = worldServer.saveChunk(protoChunk);
} else {
chunkCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound,
chunkCoords, worldServer.getHandle());
}
if (chunkCompound == null)
return;

NBTTagCompound chunkDataCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound,
chunkCoords, worldServer.getHandle());

UnloadedChunkCompound unloadedChunkCompound = new UnloadedChunkCompound(chunkCompound, chunkCoords);
UnloadedChunkCompound unloadedChunkCompound = new UnloadedChunkCompound(chunkDataCompound, chunkCoords);
chunkConsumer.accept(unloadedChunkCompound);

if (saveChunks)
chunkCompounds.add(new Pair<>(chunkCoords, chunkCompound));
chunkCompounds.add(new Pair<>(chunkCoords, chunkDataCompound));
} catch (Exception ex) {
ex.printStackTrace();
PluginDebugger.debug(ex);
Expand Down
Expand Up @@ -120,20 +120,18 @@ public static void runActionOnUnloadedChunks(WorldServer worldServer,
try {
NBTTagCompound chunkCompound = playerChunkMap.read(chunkCoords);

if (chunkCompound == null) {
ChunkAccess protoChunk = createProtoChunk(chunkCoords, worldServer);
chunkCompound = worldServer.saveChunk(protoChunk);
} else {
chunkCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound,
chunkCoords, worldServer.getHandle());
}
if (chunkCompound == null)
return;

NBTTagCompound chunkDataCompound = playerChunkMap.getChunkData(worldServer.getTypeKey(),
Suppliers.ofInstance(worldServer.getWorldPersistentData()), chunkCompound,
chunkCoords, worldServer.getHandle());

UnloadedChunkCompound unloadedChunkCompound = new UnloadedChunkCompound(chunkCompound, chunkCoords);
UnloadedChunkCompound unloadedChunkCompound = new UnloadedChunkCompound(chunkDataCompound, chunkCoords);
chunkConsumer.accept(unloadedChunkCompound);

if (saveChunks)
chunkCompounds.add(new Pair<>(chunkCoords, chunkCompound));
chunkCompounds.add(new Pair<>(chunkCoords, chunkDataCompound));
} catch (Exception ex) {
ex.printStackTrace();
PluginDebugger.debug(ex);
Expand Down
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.server.v1_8_R3.Chunk;
import net.minecraft.server.v1_8_R3.ChunkCoordIntPair;
import net.minecraft.server.v1_8_R3.ChunkProviderServer;
import net.minecraft.server.v1_8_R3.ChunkRegionLoader;
import net.minecraft.server.v1_8_R3.ChunkSection;
import net.minecraft.server.v1_8_R3.EntityHuman;
import net.minecraft.server.v1_8_R3.EntityPlayer;
Expand Down Expand Up @@ -79,6 +80,10 @@ public static void runActionOnUnloadedChunks(WorldServer worldServer, Collection
uuid -> CHUNK_LOADER.get(worldServer.chunkProviderServer));

chunks.forEach(chunkCoords -> {
if (chunkLoader instanceof ChunkRegionLoader &&
!((ChunkRegionLoader) chunkLoader).chunkExists(worldServer, chunkCoords.x, chunkCoords.z))
return;

try {
Chunk loadedChunk = chunkLoader.a(worldServer, chunkCoords.x, chunkCoords.z);

Expand Down

0 comments on commit 73763ac

Please sign in to comment.