Skip to content

Commit

Permalink
Improve speed of ocean generator recalculation (#617)
Browse files Browse the repository at this point in the history
* Improve speed of ocean generator recalculation

* Add missing files (??)

* Add missing newline

* Fix compilation

* Ignore material dynamically

* Replace return with continue

* Remove unused import

* Update src/main/java/com/iridium/iridiumskyblock/api/IridiumSkyblockAPI.java

Co-authored-by: Peaches_MLG <[email protected]>
  • Loading branch information
dlsf and PeachesMLG authored Mar 21, 2022
1 parent 7146507 commit 4241e49
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.iridium.iridiumskyblock.commands.CommandManager;
import com.iridium.iridiumskyblock.configs.*;
import com.iridium.iridiumskyblock.database.Island;
import com.iridium.iridiumskyblock.generators.IridiumChunkGenerator;
import com.iridium.iridiumskyblock.gui.GUI;
import com.iridium.iridiumskyblock.listeners.*;
import com.iridium.iridiumskyblock.managers.*;
Expand Down Expand Up @@ -82,7 +83,7 @@ public class IridiumSkyblock extends IridiumCore {
private Placeholders placeholders;
private IslandSettings islandSettings;

private ChunkGenerator chunkGenerator;
private IridiumChunkGenerator chunkGenerator;

private List<BankItem> bankItemList;
private Map<String, Permission> permissionList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.iridium.iridiumskyblock.database.IslandBooster;
import com.iridium.iridiumskyblock.database.IslandUpgrade;
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.generators.GeneratorType;
import com.iridium.iridiumskyblock.managers.IslandManager;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -319,6 +320,16 @@ public boolean isIslandEnd(@NotNull World world) {
return iridiumSkyblock.getIslandManager().isIslandEnd(world);
}

/**
* Returns which chunk generator IridiumSkyblock is using for world generation.
*
* @return the generator IridiumSkyblock uses
* @since 3.2.7
*/
public GeneratorType getGeneratorType() {
return IridiumSkyblock.getInstance().getConfiguration().generatorSettings.generatorType;
}

/**
* Returns whether the specified player can visit the provided Island.<p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public enum GeneratorType {
SKYBLOCK(SkyblockGenerator::new),
OCEAN(OceanGenerator::new);

private ChunkGenerator chunkGenerator;
private IridiumChunkGenerator chunkGenerator;

/**
* The default constructor.
*
* @param chunkGenerator The ChunkGenerator for this generator
*/
GeneratorType(Supplier<? extends ChunkGenerator> chunkGenerator) {
GeneratorType(Supplier<? extends IridiumChunkGenerator> chunkGenerator) {
this.chunkGenerator = chunkGenerator.get();
}

Expand All @@ -27,7 +27,7 @@ public enum GeneratorType {
*
* @return The ChunkGenerator
*/
public ChunkGenerator getChunkGenerator() {
public IridiumChunkGenerator getChunkGenerator() {
return chunkGenerator;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.iridium.iridiumskyblock.generators;

import com.iridium.iridiumcore.dependencies.xseries.XMaterial;
import org.bukkit.World;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.Nullable;

/**
* Base class for all IridiumSkyblock chunk generators.
*/
public abstract class IridiumChunkGenerator extends ChunkGenerator {

/**
* Returns what a made with this generator is mainly consisting of.<p>
* Used for performance improvements.
*
* @param world the world that should be checked
* @return the most used material of the chunk generator in this generator
*/
public abstract XMaterial getMainMaterial(@Nullable World world);

/**
* Returns whether the main material returned by {@link IridiumChunkGenerator#getMainMaterial(World)}
* should be ignored.
*
* @return if the main material should be ignored
*/
public boolean ignoreMainMaterial() {
return getMainMaterial(null) == XMaterial.AIR;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.util.noise.SimplexOctaveGenerator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.Random;
Expand All @@ -19,7 +19,7 @@
* Class which handles the {@link World} generation of IridiumSkyblock.
* Creates an ocean world.
*/
public class OceanGenerator extends ChunkGenerator {
public class OceanGenerator extends IridiumChunkGenerator {

/**
* Generates an ocean.
Expand Down Expand Up @@ -152,4 +152,18 @@ public boolean canSpawn(@NotNull World world, int x, int z) {
return true;
}

/**
* Returns what a made with this generator is mainly consisting of.<p>
* Used for performance improvements.
*
* @param world the world that should be checked
* @return the most used material of the chunk generator in this generator
*/
@Override
public XMaterial getMainMaterial(@Nullable World world) {
if (world == null) return XMaterial.WATER;

return world.getEnvironment() == Environment.NETHER ? XMaterial.LAVA : XMaterial.WATER;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.iridium.iridiumskyblock.generators;

import com.iridium.iridiumcore.dependencies.xseries.XMaterial;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
Expand All @@ -13,7 +14,7 @@
* Class which handles the {@link World} generation of IridiumSkyblock.
* Creates an empty void world.
*/
public class SkyblockGenerator extends ChunkGenerator {
public class SkyblockGenerator extends IridiumChunkGenerator {

public byte[][] blockSections;

Expand Down Expand Up @@ -73,4 +74,16 @@ public boolean canSpawn(@NotNull World world, int x, int z) {
return Collections.emptyList();
}

}
/**
* Returns what a made with this generator is mainly consisting of.<p>
* Used for performance improvements.
*
* @param world the world that should be checked
* @return the most used material of the chunk generator in this generator
*/
@Override
public XMaterial getMainMaterial(@Nullable World world) {
return XMaterial.AIR;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,17 @@ public void recalculateIsland(@NotNull Island island) {
private void recalculateIsland(@NotNull Island island, @NotNull List<Chunk> chunks) {
chunks.stream().map(chunk -> chunk.getChunkSnapshot(true, false, false)).forEach(chunk -> {
World world = Bukkit.getWorld(chunk.getWorldName());
boolean ignoreMainMaterial = IridiumSkyblock.getInstance().getChunkGenerator().ignoreMainMaterial();
int maxHeight = world == null ? 255 : world.getMaxHeight() - 1;

for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
if (island.isInIsland(x + (chunk.getX() * 16), z + (chunk.getZ() * 16))) {
final int maxy = Math.min(maxHeight, chunk.getHighestBlockYAt(x, z));
for (int y = LocationUtils.getMinHeight(world); y <= maxy; y++) {
XMaterial material = XMaterial.matchXMaterial(chunk.getBlockType(x, y, z));
if (material.equals(XMaterial.AIR)) continue;
if (material == XMaterial.AIR) continue;
if (!ignoreMainMaterial && material == IridiumSkyblock.getInstance().getChunkGenerator().getMainMaterial(world)) continue;

IslandBlocks islandBlock = IridiumSkyblock.getInstance().getIslandManager().getIslandBlock(island, material);
islandBlock.setAmount(islandBlock.getAmount() + 1);
Expand All @@ -873,10 +876,10 @@ private void recalculateIsland(@NotNull Island island, @NotNull List<Chunk> chun
}
});

if (!Bukkit.isPrimaryThread()) {
Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> getAllTileInIsland(island, chunks));
} else {
if (Bukkit.isPrimaryThread()) {
getAllTileInIsland(island, chunks);
} else {
Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> getAllTileInIsland(island, chunks));
}
}

Expand Down

0 comments on commit 4241e49

Please sign in to comment.