Support reloading, better error handling

This commit is contained in:
Norbi Peti 2020-09-22 20:15:32 +02:00
parent 2a7a968825
commit ad3a9202b2
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 95 additions and 85 deletions

View file

@ -6,7 +6,7 @@
<groupId>com.github.TBMCPlugins</groupId> <groupId>com.github.TBMCPlugins</groupId>
<artifactId>CustomDimensions</artifactId> <artifactId>CustomDimensions</artifactId>
<version>1.2</version> <version>1.3</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View file

@ -13,12 +13,12 @@ import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.*; import java.util.*;
public class CustomDimensions extends JavaPlugin implements Listener { public class CustomDimensions extends JavaPlugin implements Listener {
@Override @Override
public void onEnable() { public void onEnable() {
//Bukkit.getPluginManager().registerEvents(this, this);
getLogger().info("Loading custom dimensions..."); getLogger().info("Loading custom dimensions...");
try { try {
load(); load();
@ -36,93 +36,103 @@ public class CustomDimensions extends JavaPlugin implements Listener {
GeneratorSettings mainGenSettings = saveData.getGeneratorSettings(); GeneratorSettings mainGenSettings = saveData.getGeneratorSettings();
RegistryMaterials<WorldDimension> dimensionRegistry = mainGenSettings.d(); RegistryMaterials<WorldDimension> dimensionRegistry = mainGenSettings.d();
var dimIterator = dimensionRegistry.d().iterator();
var mainWorld = Bukkit.getWorlds().get(0); var mainWorld = Bukkit.getWorlds().get(0);
var convertable = Convertable.a(Bukkit.getWorldContainer().toPath()); var convertable = Convertable.a(Bukkit.getWorldContainer().toPath());
while (dimIterator.hasNext()) { for (var dimEntry : dimensionRegistry.d()) {
Map.Entry<ResourceKey<WorldDimension>, WorldDimension> dimEntry = dimIterator.next(); try {
ResourceKey<WorldDimension> dimKey = dimEntry.getKey(); loadDimension(dimEntry.getKey(), dimEntry.getValue(), convertable, console, mainWorld);
} catch (Exception e) {
if (dimKey != WorldDimension.OVERWORLD //The default dimensions are already loaded getLogger().warning("Failed to load dimension " + dimEntry.getKey());
&& dimKey != WorldDimension.THE_NETHER e.printStackTrace();
&& dimKey != WorldDimension.THE_END) {
ResourceKey<World> worldKey = ResourceKey.a(IRegistry.L, dimKey.a());
DimensionManager dimensionmanager = dimEntry.getValue().b();
ChunkGenerator chunkgenerator = dimEntry.getValue().c();
var name = dimKey.a().getKey();
getLogger().info("Loading " + name);
var session = convertable.new ConversionSession(name, dimKey) { //The original session isn't prepared for custom dimensions
@Override
public File a(ResourceKey<World> resourcekey) {
return new File(this.folder.toFile(), "custom");
}
};
MinecraftServer.convertWorld(session);
//Load world settings or create default values
RegistryReadOps<NBTBase> registryreadops = RegistryReadOps.a(DynamicOpsNBT.a, console.dataPackResources.h(), console.customRegistry);
WorldDataServer worlddata = (WorldDataServer) session.a(registryreadops, console.datapackconfiguration);
if (worlddata == null) {
Properties properties = new Properties();
properties.put("level-seed", Objects.toString(mainWorld.getSeed()));
properties.put("generate-structures", Objects.toString(true));
properties.put("level-type", WorldType.NORMAL.getName());
GeneratorSettings dimGenSettings = GeneratorSettings.a(console.getCustomRegistry(), properties);
WorldSettings worldSettings = new WorldSettings(name,
EnumGamemode.getById(Bukkit.getDefaultGameMode().getValue()),
false, //Hardcore
EnumDifficulty.EASY, false, new GameRules(), console.datapackconfiguration);
worlddata = new WorldDataServer(worldSettings, dimGenSettings, Lifecycle.stable());
}
worlddata.checkName(name);
worlddata.a(console.getServerModName(), console.getModded().isPresent());
if (console.options.has("forceUpgrade")) {
net.minecraft.server.v1_16_R2.Main.convertWorld(session, DataConverterRegistry.a(),
console.options.has("eraseCache"), () -> true,
worlddata.getGeneratorSettings().d().d().stream()
.map((entry2) -> ResourceKey.a(IRegistry.K, entry2.getKey().a()))
.collect(ImmutableSet.toImmutableSet()));
}
List<MobSpawner> spawners = ImmutableList.of(new MobSpawnerPhantom(), new MobSpawnerPatrol(), new MobSpawnerCat(), new VillageSiege(), new MobSpawnerTrader(worlddata));
ResourceKey<DimensionManager> dimManResKey = ResourceKey.a(IRegistry.K, dimKey.a());
var dimRegistry = ((RegistryMaterials<DimensionManager>) console.customRegistry.a());
{
var key = dimRegistry.getKey(dimensionmanager);
if (key != null) { //The loaded manager is the same
getLogger().warning("Dimension manager already loaded with key " + key + "! Skipping");
continue;
}
}
//Replace existing dimension manager, correctly setting the ID up (which is -1 for default worlds...)
dimRegistry.a(OptionalInt.empty(), dimManResKey, dimensionmanager, Lifecycle.stable());
var worldloadlistener = console.worldLoadListenerFactory.create(11);
WorldServer worldserver = new WorldServer(console, console.executorService, session,
worlddata, worldKey, dimensionmanager, worldloadlistener, chunkgenerator,
false, //isDebugWorld
BiomeManager.a(worlddata.getGeneratorSettings().getSeed()), //Biome seed
spawners,
true, //Update world time
org.bukkit.World.Environment.NORMAL, null);
if (Bukkit.getWorld(name.toLowerCase(Locale.ENGLISH)) == null) {
getLogger().warning("Failed to load custom dimension " + name);
} else {
console.initWorld(worldserver, worlddata, worlddata, worlddata.getGeneratorSettings());
worldserver.setSpawnFlags(true, true);
console.worldServer.put(worldserver.getDimensionKey(), worldserver);
Bukkit.getPluginManager().callEvent(new WorldInitEvent(worldserver.getWorld()));
console.loadSpawn(worldserver.getChunkProvider().playerChunkMap.worldLoadListener, worldserver);
Bukkit.getPluginManager().callEvent(new WorldLoadEvent(worldserver.getWorld()));
}
} }
} }
} }
private void loadDimension(ResourceKey<WorldDimension> dimKey, WorldDimension dimension,
Convertable convertable, DedicatedServer console, org.bukkit.World mainWorld) throws IOException {
if (dimKey == WorldDimension.OVERWORLD //The default dimensions are already loaded
|| dimKey == WorldDimension.THE_NETHER
|| dimKey == WorldDimension.THE_END)
return;
ResourceKey<World> worldKey = ResourceKey.a(IRegistry.L, dimKey.a());
DimensionManager dimensionmanager = dimension.b();
ChunkGenerator chunkgenerator = dimension.c();
var name = dimKey.a().getKey();
if (Bukkit.getWorld(name) != null) {
getLogger().info(name + " already loaded");
return;
}
getLogger().info("Loading " + name);
var session = convertable.new ConversionSession(name, dimKey) { //The original session isn't prepared for custom dimensions
@Override
public File a(ResourceKey<World> resourcekey) {
return new File(this.folder.toFile(), "custom");
}
};
MinecraftServer.convertWorld(session);
//Load world settings or create default values
RegistryReadOps<NBTBase> registryreadops = RegistryReadOps.a(DynamicOpsNBT.a, console.dataPackResources.h(), console.customRegistry);
WorldDataServer worlddata = (WorldDataServer) session.a(registryreadops, console.datapackconfiguration);
if (worlddata == null) {
Properties properties = new Properties();
properties.put("level-seed", Objects.toString(mainWorld.getSeed()));
properties.put("generate-structures", Objects.toString(true));
properties.put("level-type", WorldType.NORMAL.getName());
GeneratorSettings dimGenSettings = GeneratorSettings.a(console.getCustomRegistry(), properties);
WorldSettings worldSettings = new WorldSettings(name,
EnumGamemode.getById(Bukkit.getDefaultGameMode().getValue()),
false, //Hardcore
EnumDifficulty.EASY, false, new GameRules(), console.datapackconfiguration);
worlddata = new WorldDataServer(worldSettings, dimGenSettings, Lifecycle.stable());
}
worlddata.checkName(name);
worlddata.a(console.getServerModName(), console.getModded().isPresent());
if (console.options.has("forceUpgrade")) {
net.minecraft.server.v1_16_R2.Main.convertWorld(session, DataConverterRegistry.a(),
console.options.has("eraseCache"), () -> true,
worlddata.getGeneratorSettings().d().d().stream()
.map((entry2) -> ResourceKey.a(IRegistry.K, entry2.getKey().a()))
.collect(ImmutableSet.toImmutableSet()));
}
List<MobSpawner> spawners = ImmutableList.of(new MobSpawnerPhantom(), new MobSpawnerPatrol(), new MobSpawnerCat(), new VillageSiege(), new MobSpawnerTrader(worlddata));
ResourceKey<DimensionManager> dimManResKey = ResourceKey.a(IRegistry.K, dimKey.a());
var dimRegistry = ((RegistryMaterials<DimensionManager>) console.customRegistry.a());
{
var key = dimRegistry.getKey(dimensionmanager);
if (key != null) { //The loaded manager is the same
getLogger().warning("Dimension manager already loaded with key " + key + "! Skipping");
session.close();
return;
}
}
//Replace existing dimension manager, correctly setting the ID up (which is -1 for default worlds...)
dimRegistry.a(OptionalInt.empty(), dimManResKey, dimensionmanager, Lifecycle.stable());
var worldloadlistener = console.worldLoadListenerFactory.create(11);
WorldServer worldserver = new WorldServer(console, console.executorService, session,
worlddata, worldKey, dimensionmanager, worldloadlistener, chunkgenerator,
false, //isDebugWorld
BiomeManager.a(worlddata.getGeneratorSettings().getSeed()), //Biome seed
spawners,
true, //Update world time
org.bukkit.World.Environment.NORMAL, null);
if (Bukkit.getWorld(name.toLowerCase(Locale.ENGLISH)) == null) {
getLogger().warning("Failed to load custom dimension " + name);
} else {
console.initWorld(worldserver, worlddata, worlddata, worlddata.getGeneratorSettings());
worldserver.setSpawnFlags(true, true);
console.worldServer.put(worldserver.getDimensionKey(), worldserver);
Bukkit.getPluginManager().callEvent(new WorldInitEvent(worldserver.getWorld()));
console.loadSpawn(worldserver.getChunkProvider().playerChunkMap.worldLoadListener, worldserver);
Bukkit.getPluginManager().callEvent(new WorldLoadEvent(worldserver.getWorld()));
}
}
} }

View file

@ -1,6 +1,6 @@
name: CustomDimensions name: CustomDimensions
main: buttondevteam.customdimensions.CustomDimensions main: buttondevteam.customdimensions.CustomDimensions
version: '1.2' version: '1.3'
api-version: '1.16' api-version: '1.16'
loadbefore: loadbefore:
- Multiverse-Core - Multiverse-Core