start updating to new bukkit world generation API

This commit is contained in:
dfsek 2021-11-27 09:36:54 -07:00
parent 50da6d9d9b
commit 7aaa94dedc
6 changed files with 61 additions and 152 deletions

View File

@ -24,7 +24,7 @@ dependencies {
because("Minecraft 1.17+ includes slf4j 1.8.0-beta4, so we need to shade it for other versions.")
}
compileOnly("io.papermc.paper:paper-api:1.17-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT")
shadedImplementation("io.papermc:paperlib:1.0.5")
shadedImplementation("org.bstats:bstats-bukkit:1.7")

View File

@ -36,8 +36,6 @@ import com.dfsek.terra.api.command.exception.MalformedCommandException;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
import com.dfsek.terra.bukkit.command.BukkitCommandAdapter;
import com.dfsek.terra.bukkit.command.FixChunkCommand;
import com.dfsek.terra.bukkit.command.SaveDataCommand;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.bukkit.listeners.CommonListener;
import com.dfsek.terra.bukkit.listeners.PaperListener;
@ -53,12 +51,6 @@ public class TerraBukkitPlugin extends JavaPlugin {
private final PlatformImpl terraPlugin = new PlatformImpl(this);
private final Map<String, com.dfsek.terra.api.world.generator.ChunkGenerator> generatorMap = new HashMap<>();
private final Map<String, ConfigPack> worlds = new HashMap<>();
@Override
public void onDisable() {
BukkitChunkGeneratorWrapper.saveAll();
}
@Override
public void onEnable() {
@ -77,8 +69,6 @@ public class TerraBukkitPlugin extends JavaPlugin {
try {
CommandUtil.registerAll(manager);
manager.register("save-data", SaveDataCommand.class);
manager.register("fix-chunk", FixChunkCommand.class);
} catch(MalformedCommandException e) { // This should never happen.
logger.error("""
TERRA HAS BEEN DISABLED
@ -96,9 +86,6 @@ public class TerraBukkitPlugin extends JavaPlugin {
cmd.setTabCompleter(command);
long save = terraPlugin.getTerraConfig().getDataSaveInterval();
Bukkit.getScheduler().runTaskTimerAsynchronously(this, BukkitChunkGeneratorWrapper::saveAll, save,
save); // Schedule population data saving
Bukkit.getPluginManager().registerEvents(new CommonListener(terraPlugin), this); // Register master event listener
PaperUtil.checkPaper(this);
@ -222,8 +209,7 @@ public class TerraBukkitPlugin extends JavaPlugin {
return new BukkitChunkGeneratorWrapper(generatorMap.computeIfAbsent(worldName, name -> {
if(!terraPlugin.getConfigRegistry().contains(id)) throw new IllegalArgumentException("No such config pack \"" + id + "\"");
ConfigPack pack = terraPlugin.getConfigRegistry().get(id);
worlds.put(worldName, pack);
return pack.getGeneratorProvider().newInstance(pack);
}));
}), terraPlugin.getRawConfigRegistry().get(id));
}
}

View File

@ -1,38 +0,0 @@
/*
* This file is part of Terra.
*
* Terra is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Terra is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
import com.dfsek.terra.api.entity.CommandSender;
import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
@Command
@WorldCommand
@PlayerCommand
public class FixChunkCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
BukkitChunkGeneratorWrapper.fixChunk(player.world().getChunkAt(player.position()));
}
}

View File

@ -1,34 +0,0 @@
/*
* This file is part of Terra.
*
* Terra is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Terra is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dfsek.terra.bukkit.command;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.entity.CommandSender;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.config.lang.LangUtil;
@Command
public class SaveDataCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
BukkitChunkGeneratorWrapper.saveAll();
LangUtil.send("debug.data-save", sender);
}
}

View File

@ -0,0 +1,36 @@
package com.dfsek.terra.bukkit.generator;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.world.biome.TerraBiome;
import org.bukkit.block.Biome;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.WorldInfo;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
public class BukkitBiomeProvider extends BiomeProvider implements Handle {
private final com.dfsek.terra.api.world.biome.generation.BiomeProvider delegate;
public BukkitBiomeProvider(com.dfsek.terra.api.world.biome.generation.BiomeProvider delegate) { this.delegate = delegate; }
@Override
public @NotNull Biome getBiome(@NotNull WorldInfo worldInfo, int x, int y, int z) {
TerraBiome terraBiome = delegate.getBiome(x, z, worldInfo.getSeed());
return (Biome) terraBiome.getVanillaBiomes().get(terraBiome.getGenerator().getBiomeNoise(), x, y, z).getHandle();
}
@Override
public @NotNull List<Biome> getBiomes(@NotNull WorldInfo worldInfo) {
return Arrays.stream(Biome.values()).toList();
}
@Override
public Object getHandle() {
return delegate;
}
}

View File

@ -17,95 +17,54 @@
package com.dfsek.terra.bukkit.generator;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dfsek.terra.api.config.ConfigPack;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.WorldInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.config.WorldConfig;
import com.dfsek.terra.api.world.Chunk;
import com.dfsek.terra.api.world.generator.ChunkGenerator;
import com.dfsek.terra.api.world.generator.GeneratorWrapper;
import com.dfsek.terra.bukkit.population.PopulationManager;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGenerator implements GeneratorWrapper {
private static final Logger logger = LoggerFactory.getLogger(BukkitChunkGeneratorWrapper.class);
private static final Map<com.dfsek.terra.api.world.World, PopulationManager> popMap = new HashMap<>();
private final PopulationManager popMan;
private final ChunkGenerator delegate;
private boolean needsLoad = true;
private WorldConfig worldConfig;
public BukkitChunkGeneratorWrapper(ChunkGenerator delegate) {
private final ConfigPack pack;
public BukkitChunkGeneratorWrapper(ChunkGenerator delegate, ConfigPack pack) {
this.delegate = delegate;
Platform platform = delegate.getPlatform();
this.popMan = new PopulationManager(delegate, platform);
}
public static synchronized void saveAll() {
for(Map.Entry<com.dfsek.terra.api.world.World, PopulationManager> entry : popMap.entrySet()) {
try {
entry.getValue().saveBlocks(entry.getKey());
} catch(IOException e) {
logger.error("Error occurred while saving population manager", e);
}
}
}
public static synchronized void fixChunk(Chunk chunk) {
popMap.get(chunk.getWorld()).checkNeighbors(chunk.getX(), chunk.getZ(), chunk.getWorld());
}
private void load(com.dfsek.terra.api.world.World w) {
try {
popMan.loadBlocks(w);
} catch(FileNotFoundException ignore) {
} catch(IOException | ClassNotFoundException e) {
logger.error("Error occurred while loading terra world", e);
}
popMap.put(w, popMan);
needsLoad = false;
this.pack = pack;
}
@Override
public @NotNull ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome) {
public @Nullable BiomeProvider getDefaultBiomeProvider(@NotNull WorldInfo worldInfo) {
return new BukkitBiomeProvider(pack.getBiomeProviderBuilder());
}
@Override
public void generateNoise(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
if(this.worldConfig == null) {
this.worldConfig = delegate.getConfigPack().toWorldConfig(BukkitAdapter.adapt(world));
this.worldConfig = delegate.getConfigPack().toWorldConfig(BukkitAdapter.adapt(Bukkit.getWorld(worldInfo.getUID())));
}
com.dfsek.terra.api.world.World bukkitWorld = BukkitAdapter.adapt(world);
if(needsLoad) load(bukkitWorld); // Load population data for world.
ChunkData data = createChunkData(world);
delegate.generateChunkData(bukkitWorld, random, x, z, new BukkitProtoChunk(data));
return data;
delegate.generateChunkData(worldConfig.getWorld(), random, x, z, new BukkitProtoChunk(chunkData));
}
@Override
public @NotNull List<BlockPopulator> getDefaultPopulators(@NotNull World world) {
return Arrays.asList(popMan, new BukkitPopulatorWrapper(delegate));
}
@Override
public boolean isParallelCapable() {
return true;
return Collections.singletonList(new BukkitPopulatorWrapper(delegate));
}
@Override