Fix biomes not working

This commit is contained in:
Fabrizio La Rosa 2020-07-03 14:46:07 +02:00
parent b091c065a8
commit 6a98d62f65
8 changed files with 92 additions and 40 deletions

View File

@ -10,20 +10,28 @@ import com.songoda.skyblock.utils.version.NMSUtil;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.block.Biome;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class BiomeManager {
private final SkyBlock skyblock;
private final List<Island> updatingIslands;
private final FileConfiguration language;
private final int runEveryX;
public BiomeManager(SkyBlock skyblock) {
this.skyblock = skyblock;
this.updatingIslands = new ArrayList<>();
this.language = SkyBlock.getInstance().getFileManager().getConfig(new File(SkyBlock.getInstance().getDataFolder(), "language.yml")).getFileConfiguration();
this.runEveryX = language.getInt("Command.Island.Biome.Progress.Display-Every-X-Updates");
}
public boolean isUpdating(Island island) {
@ -45,13 +53,33 @@ public class BiomeManager {
if(skyblock.isPaperAsync()){
// We keep it sequentially in order to use less RAM
int chunkAmount = (int) Math.ceil(Math.pow(island.getSize()/16d, 2d));
AtomicInteger progress = new AtomicInteger();
ChunkLoader.startChunkLoadingPerChunk(island, IslandWorld.Normal, skyblock.isPaperAsync(), (asyncChunk, syncChunk) -> {
Chunk chunk = asyncChunk.join();
if(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_16)){ // TODO Should be 1.15 but it works fine there
setChunkBiome2D(island, biome, chunk); // 2D for the moment
setChunkBiome3D(island, biome, chunk); // 2D for the moment
} else {
setChunkBiome2D(island, biome, chunk);
}
progress.getAndIncrement();
if(language.getBoolean("Command.Island.Biome.Progress.Should-Display-Message") &&
progress.get() == 1 || progress.get() == chunkAmount || progress.get() % runEveryX == 0){
final double percent = ((double) progress.get() / (double) chunkAmount) * 100;
String message = language.getString("Command.Island.Biome.Progress.Message");
message = message.replace("%current_updated_chunks%", String.valueOf(progress.get()));
message = message.replace("%max_chunks%", String.valueOf(chunkAmount));
message = message.replace("%percent_whole%", String.valueOf((int) percent));
message = message.replace("%percent%", NumberFormat.getInstance().format(percent));
for (Player player : SkyBlock.getInstance().getIslandManager().getPlayersAtIsland(island)) {
skyblock.getMessageManager().sendMessage(player, message);
}
}
}, (island1 -> {
removeUpdatingIsland(island1);
if(task != null) {
@ -61,12 +89,29 @@ public class BiomeManager {
} else {
ChunkLoader.startChunkLoading(island, IslandWorld.Normal, skyblock.isPaperAsync(), (asyncChunks, syncChunks) -> {
Bukkit.getScheduler().runTaskAsynchronously(skyblock, () -> {
int progress = 0;
for(Chunk chunk : syncChunks){
if(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_16)){ // TODO Should be 1.15 but it works fine there
setChunkBiome2D(island, biome, chunk); // 2D for the moment
setChunkBiome3D(island, biome, chunk); // 2D for the moment
} else {
setChunkBiome2D(island, biome, chunk);
}
progress++;
if(language.getBoolean("Command.Island.Biome.Progress.Should-Display-Message") &&
progress == 1 || progress == syncChunks.size() || progress % runEveryX == 0){
final double percent = ((double) progress / (double) syncChunks.size()) * 100;
String message = language.getString("Command.Island.Biome.Progress.Message");
message = message.replace("%current_updated_chunks%", String.valueOf(progress));
message = message.replace("%max_chunks%", String.valueOf(syncChunks.size()));
message = message.replace("%percent_whole%", String.valueOf((int) percent));
message = message.replace("%percent%", NumberFormat.getInstance().format(percent));
for (Player player : SkyBlock.getInstance().getIslandManager().getPlayersAtIsland(island)) {
skyblock.getMessageManager().sendMessage(player, message);
}
}
}
});
}, (island1 -> {
@ -79,11 +124,9 @@ public class BiomeManager {
}
private void setChunkBiome2D(Island island, Biome biome, Chunk chunk) {
for(int xx = 0; xx < 16; xx++){
for(int zz = 0; zz < 16; zz++){
if(!chunk.getWorld().getBiome(xx, zz).equals(biome)){
chunk.getWorld().setBiome(xx, zz, biome);
}
for(int x = chunk.getX() << 4; x < (chunk.getX()<< 4)+16; x++){
for(int z = chunk.getZ() << 4; z < (chunk.getZ()<< 4)+16; z++){
chunk.getWorld().setBiome(x, z, biome);
}
}
updateBiomePacket(island, chunk);
@ -91,12 +134,10 @@ public class BiomeManager {
// Do not use - Too laggy
private void setChunkBiome3D(Island island, Biome biome, Chunk chunk) {
for(int x = 0; x < 16; x++){
for(int z = 0; z < 16; z++){
for(int y = 0; y<256; y++){
if(!chunk.getWorld().getBiome(x, y, z).equals(biome)){
chunk.getWorld().setBiome(x, y, z, biome);
}
for(int x = chunk.getX() << 4; x < (chunk.getX()<< 4)+16; x++){
for(int z = chunk.getZ() << 4; z < (chunk.getZ()<< 4)+16; z++){
for(int y = 0; y < chunk.getWorld().getMaxHeight(); ++y) {
chunk.getWorld().setBiome(x, y, z, biome);
}
}
}

View File

@ -1,5 +1,6 @@
package com.songoda.skyblock.command.commands.admin;
import com.songoda.core.compatibility.CompatibleBiome;
import com.songoda.core.compatibility.CompatibleSound;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.skyblock.biome.BiomeManager;
@ -11,6 +12,7 @@ import com.songoda.skyblock.island.IslandManager;
import com.songoda.skyblock.message.MessageManager;
import com.songoda.skyblock.playerdata.PlayerDataManager;
import com.songoda.skyblock.sound.SoundManager;
import com.songoda.skyblock.utils.StringUtil;
import com.songoda.skyblock.utils.player.OfflinePlayer;
import com.songoda.skyblock.utils.version.SBiome;
import org.bukkit.Bukkit;
@ -55,10 +57,10 @@ public class SetBiomeCommand extends SubCommand {
if (args.length == 2) {
String biomeName = args[1].toUpperCase().trim();
SBiome biome = null;
for (SBiome sbiome : SBiome.values()) {
if (sbiome.isAvailable() && sbiome.name().equals(biomeName)) {
biome = sbiome;
CompatibleBiome biome = null;
for (CompatibleBiome cbiome : CompatibleBiome.values()) {
if (cbiome.isCompatible() && cbiome.name().equals(biomeName)) {
biome = cbiome;
break;
}
}
@ -94,15 +96,17 @@ public class SetBiomeCommand extends SubCommand {
configLoad.getString("Command.Island.Admin.SetBiome.Island.Data.Message"));
soundManager.playSound(sender, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1.0F, 1.0F);
} else {
biomeManager.setBiome(island, biome.getBiome(), null);
island.setBiome(biome.getBiome());
CompatibleBiome finalBiome = biome;
biomeManager.setBiome(island, biome.getBiome(), () -> {
island.setBiome(finalBiome.getBiome());
});
}
}
messageManager.sendMessage(sender,
configLoad.getString("Command.Island.Admin.SetBiome.Set.Message")
.replace("%player", targetPlayerName)
.replace("%biome", biome.getFormattedBiomeName()));
.replace("%biome", StringUtil.capitalizeWord(biome.getBiome().name().replaceAll("_", " "))));
soundManager.playSound(sender, CompatibleSound.BLOCK_NOTE_BLOCK_PLING.getSound(), 1.0F, 1.0F);
}
} else {

View File

@ -5,16 +5,13 @@ import com.songoda.core.compatibility.ServerVersion;
import com.songoda.skyblock.biome.BiomeManager;
import com.songoda.skyblock.command.SubCommand;
import com.songoda.skyblock.config.FileManager.Config;
import com.songoda.skyblock.gui.bank.GuiBank;
import com.songoda.skyblock.gui.wip.GuiBiome;
import com.songoda.skyblock.gui.biome.GuiBiome;
import com.songoda.skyblock.island.Island;
import com.songoda.skyblock.island.IslandManager;
import com.songoda.skyblock.island.IslandRole;
import com.songoda.skyblock.island.IslandWorld;
import com.songoda.skyblock.menus.Biome;
import com.songoda.skyblock.message.MessageManager;
import com.songoda.skyblock.sound.SoundManager;
import com.songoda.skyblock.world.WorldManager;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

View File

@ -1,4 +1,4 @@
package com.songoda.skyblock.gui.wip;
package com.songoda.skyblock.gui.biome;
import com.songoda.core.compatibility.CompatibleBiome;
import com.songoda.core.compatibility.CompatibleMaterial;

View File

@ -1,4 +1,4 @@
package com.songoda.skyblock.gui.wip;
package com.songoda.skyblock.gui.biome;
import com.songoda.core.compatibility.CompatibleBiome;
import com.songoda.core.compatibility.CompatibleMaterial;
@ -176,12 +176,14 @@ public class GuiBiome extends Gui {
cooldownManager.createPlayer(CooldownType.Biome, player);
Bukkit.getScheduler().runTask(plugin, () -> {
biomeManager.setBiome(island, icon.biome.getBiome(), () -> {
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Biome.Completed.Message"));
soundManager.playSound(player, CompatibleSound.ENTITY_VILLAGER_YES.getSound(), 1.0F, 1.0F);
if(languageLoad.getBoolean("Command.Island.Biome.Completed.Should-Display-Message")){
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Biome.Completed.Message"));
soundManager.playSound(player, CompatibleSound.ENTITY_VILLAGER_YES.getSound(), 1.0F, 1.0F);
}
});
island.setBiome(icon.biome.getBiome());
island.save();
});
island.setBiome(icon.biome.getBiome());
soundManager.playSound(island.getLocation(IslandWorld.Normal, IslandEnvironment.Island),
CompatibleSound.ENTITY_GENERIC_SPLASH.getSound(), 1.0F, 1.0F);

View File

@ -282,14 +282,14 @@ public class IslandManager {
}
Biome biome = cBiome.getBiome();
Bukkit.getServer().getScheduler().runTaskLater(skyblock, () -> {
/*Bukkit.getServer().getScheduler().runTaskLater(skyblock, () -> {
skyblock.getBiomeManager().setBiome(island, biome, null);
if (structure.getCommands() != null) {
for (String commandList : structure.getCommands()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), commandList.replace("%player", player.getName()));
}
}
}, 20L);
}, 20L);*/
// Recalculate island level after 5 seconds
if (configLoad.getBoolean("Island.Levelling.ScanAutomatically"))

View File

@ -4,7 +4,11 @@ import com.songoda.core.compatibility.CompatibleBiome;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.island.Island;
import com.songoda.skyblock.island.IslandManager;
import com.songoda.skyblock.island.IslandWorld;
import io.papermc.lib.PaperLib;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
@ -29,12 +33,13 @@ public class VoidGenerator extends ChunkGenerator {
final Configuration configLoad = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration();
final ConfigurationSection worldSection = configLoad.getConfigurationSection("Island.World");
Biome biome = CompatibleBiome.valueOf(configLoad // 2D for the moment
.getString("Island.Biome.Default.Type").toUpperCase()).getBiome();
if(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_16)) { // TODO Should be 1.15 but it works fine there
setChunkBiome2D(CompatibleBiome.valueOf(configLoad // 2D for the moment
.getString("Island.Biome.Default.Type").toUpperCase()).getBiome(), biomeGrid);
setChunkBiome3D(biome, biomeGrid, world);
} else {
setChunkBiome2D(CompatibleBiome.valueOf(configLoad
.getString("Island.Biome.Default.Type").toUpperCase()).getBiome(), biomeGrid);
setChunkBiome2D(biome, biomeGrid);
}
@ -85,13 +90,11 @@ public class VoidGenerator extends ChunkGenerator {
}
// Do not use - Too laggy
private void setChunkBiome3D(Biome biome, BiomeGrid grid) {
private void setChunkBiome3D(Biome biome, BiomeGrid grid, World world) {
for(int x = 0; x < 16; x++){
for(int z = 0; z < 16; z++){
for(int y = 0; y<256; y++){
if(!grid.getBiome(x, y, z).equals(biome)){
grid.setBiome(x, y, z, biome);
}
for(int y = 0; y < world.getMaxHeight(); ++y) {
grid.setBiome(z, y, z, biome);
}
}
}

View File

@ -834,8 +834,13 @@ Command:
Message: '&bSkyBlock &8| &cError&8: &eA biome update is still in progress for this Island.'
Completed:
Message: '&bSkyBlock &8| &aInfo&8: &eThe biome update is now complete'
Should-Display-Message: true
Owner:
Message: '&bSkyBlock &8| &cError&8: &eYou are not an Island Owner.'
Progress:
Message: '&fUpdated &a%current_updated_chunks%&f/&b%max_chunks% &fchunks ( &a%percent%&f%&f )'
Should-Display-Message: true
Display-Every-X-Updates: 20
Visitors:
Info:
Message: '&f&oOpens the Island Visitors menu.'