mirror of
https://gitlab.com/phoenix-dvpmt/mmoitems.git
synced 2024-12-22 04:37:42 +01:00
Optimization concept
This commit is contained in:
parent
5f1e32d581
commit
9be8d9eb18
@ -4,6 +4,7 @@ import net.Indyuce.mmoitems.MMOItems;
|
||||
import net.Indyuce.mmoitems.api.ConfigFile;
|
||||
import net.Indyuce.mmoitems.api.block.CustomBlock;
|
||||
import net.Indyuce.mmoitems.api.block.WorldGenTemplate;
|
||||
import net.Indyuce.mmoitems.util.Pair;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
@ -13,10 +14,13 @@ import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class WorldGenManager implements Listener, Reloadable {
|
||||
@ -28,12 +32,12 @@ public class WorldGenManager implements Listener, Reloadable {
|
||||
* world.
|
||||
*/
|
||||
private final Map<CustomBlock, WorldGenTemplate> assigned = new HashMap<>();
|
||||
private final Queue<Pair<Location, CustomBlock>> modificationsQueue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
private static final BlockFace[] faces = { BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST, BlockFace.DOWN, BlockFace.UP };
|
||||
private static final BlockFace[] faces = {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST, BlockFace.DOWN, BlockFace.UP};
|
||||
private static final Random random = new Random();
|
||||
|
||||
public WorldGenManager() {
|
||||
|
||||
/*
|
||||
* load the worldGenManager even if world gen is not enabled so that if
|
||||
* admins temporarily disable it, there is no console error spam saying
|
||||
@ -62,36 +66,65 @@ public class WorldGenManager implements Listener, Reloadable {
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void a(ChunkLoadEvent event) {
|
||||
if(event.isNewChunk()) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(MMOItems.plugin, () -> assigned.forEach((block, template) -> {
|
||||
if(!template.canGenerateInWorld(event.getWorld())) {
|
||||
public void onChunkLoad(ChunkLoadEvent e) {
|
||||
if (e.isNewChunk())
|
||||
return;
|
||||
}
|
||||
if(random.nextDouble() < template.getChunkChance())
|
||||
for(int i = 0; i < template.getVeinCount(); i++) {
|
||||
int y = random.nextInt(template.getMaxDepth() - template.getMinDepth() + 1) + template.getMinDepth();
|
||||
Location generatePoint = event.getChunk().getBlock(random.nextInt(16), y, random.nextInt(16)).getLocation();
|
||||
|
||||
if(template.canGenerate(generatePoint)) {
|
||||
if (e.isAsynchronous())
|
||||
generate(e);
|
||||
else
|
||||
Bukkit.getScheduler().runTaskAsynchronously(MMOItems.plugin, () -> generate(e));
|
||||
}
|
||||
|
||||
private void generate(ChunkLoadEvent e) {
|
||||
assigned.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getValue().canGenerateInWorld(e.getWorld()))
|
||||
.forEach(entry -> {
|
||||
final CustomBlock block = entry.getKey();
|
||||
final WorldGenTemplate template = entry.getValue();
|
||||
if (random.nextDouble() > template.getChunkChance())
|
||||
return;
|
||||
|
||||
for (int i = 0; i < template.getVeinCount(); i++) {
|
||||
int y = random.nextInt(template.getMaxDepth() - template.getMinDepth() + 1) + template.getMinDepth();
|
||||
Location generatePoint = e.getChunk().getBlock(random.nextInt(16), y, random.nextInt(16)).getLocation();
|
||||
|
||||
if (!template.canGenerate(generatePoint) || generatePoint.getWorld() == null)
|
||||
continue;
|
||||
Block modify = generatePoint.getWorld().getBlockAt(generatePoint);
|
||||
|
||||
for(int j = 0; j < template.getVeinSize(); j++) {
|
||||
if(template.canReplace(modify.getType())) {
|
||||
final Block fModify = modify;
|
||||
Bukkit.getScheduler().runTask(MMOItems.plugin, () -> {
|
||||
fModify.setType(block.getState().getType(), false);
|
||||
fModify.setBlockData(block.getState().getBlockData(), false);
|
||||
});
|
||||
}
|
||||
|
||||
for (int j = 0; j < template.getVeinSize(); j++) {
|
||||
if (template.canReplace(modify.getType()))
|
||||
this.modificationsQueue.add(Pair.of(modify.getLocation(), block));
|
||||
BlockFace nextFace = faces[random.nextInt(faces.length)];
|
||||
modify = modify.getRelative(nextFace);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (modificationsQueue.isEmpty()) {
|
||||
this.cancel();
|
||||
return;
|
||||
}
|
||||
}));
|
||||
Pair<Location, CustomBlock> pair = modificationsQueue.poll();
|
||||
|
||||
if (Bukkit.isPrimaryThread())
|
||||
setBlockData(pair.getKey().getBlock(), pair.getValue());
|
||||
else
|
||||
Bukkit.getScheduler().runTask(MMOItems.plugin, () -> setBlockData(pair.getKey().getBlock(), pair.getValue()));
|
||||
}
|
||||
}.runTaskTimer(MMOItems.plugin, 0, 5);
|
||||
}
|
||||
|
||||
|
||||
private void setBlockData(Block fModify, CustomBlock block) {
|
||||
fModify.setType(block.getState().getType(), false);
|
||||
fModify.setBlockData(block.getState().getBlockData(), false);
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
@ -99,7 +132,7 @@ public class WorldGenManager implements Listener, Reloadable {
|
||||
templates.clear();
|
||||
|
||||
FileConfiguration config = new ConfigFile("gen-templates").getConfig();
|
||||
for(String key : config.getKeys(false)) {
|
||||
for (String key : config.getKeys(false)) {
|
||||
try {
|
||||
WorldGenTemplate template = new WorldGenTemplate(config.getConfigurationSection(key));
|
||||
templates.put(template.getId(), template);
|
||||
|
Loading…
Reference in New Issue
Block a user