feat: implement config pack reloading

This commit is contained in:
daoge_cmd
2024-10-14 22:13:54 +08:00
parent f0d03d4538
commit df3e623530
14 changed files with 100 additions and 74 deletions

View File

@@ -5,25 +5,29 @@ import com.dfsek.tectonic.api.depth.DepthTracker;
import com.dfsek.tectonic.api.exception.LoadException;
import org.allaymc.api.server.Server;
import org.allaymc.api.world.biome.BiomeId;
import com.dfsek.terra.allay.delegate.AllayBiome;
import com.dfsek.terra.allay.handle.AllayItemHandle;
import com.dfsek.terra.allay.handle.AllayWorldHandle;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import com.dfsek.terra.AbstractPlatform;
import com.dfsek.terra.allay.delegate.AllayBiome;
import com.dfsek.terra.allay.generator.AllayGeneratorWrapper;
import com.dfsek.terra.allay.handle.AllayItemHandle;
import com.dfsek.terra.allay.handle.AllayWorldHandle;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.handle.ItemHandle;
import com.dfsek.terra.api.handle.WorldHandle;
import com.dfsek.terra.api.world.biome.PlatformBiome;
/**
* @author daoge_cmd
*/
public class AllayPlatform extends AbstractPlatform {
public static final Set<AllayGeneratorWrapper> GENERATOR_WRAPPERS = new HashSet<>();
protected static final AllayWorldHandle ALLAY_WORLD_HANDLE = new AllayWorldHandle();
protected static final AllayItemHandle ALLAY_ITEM_HANDLE = new AllayItemHandle();
@@ -33,8 +37,21 @@ public class AllayPlatform extends AbstractPlatform {
@Override
public boolean reload() {
// TODO: implement reload
return false;
getTerraConfig().load(this);
getRawConfigRegistry().clear();
boolean succeed = getRawConfigRegistry().loadAll(this);
GENERATOR_WRAPPERS.forEach(wrapper -> {
getConfigRegistry().get(wrapper.getConfigPack().getRegistryKey()).ifPresent(pack -> {
wrapper.setConfigPack(pack);
var dimension = wrapper.getAllayWorldGenerator().getDimension();
TerraAllayPlugin.INSTANCE.getPluginLogger().info(
"Replaced pack in chunk generator for world {}",
dimension.getWorld().getWorldData().getName() + ":" + dimension.getDimensionInfo().dimensionId()
);
});
});
return succeed;
}
@Override

View File

@@ -2,8 +2,8 @@ package com.dfsek.terra.allay;
import org.allaymc.api.plugin.Plugin;
import org.allaymc.api.registry.Registries;
import com.dfsek.terra.allay.generator.AllayGeneratorWrapper;
import com.dfsek.terra.allay.generator.AllayGeneratorWrapper;
import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
/**
@@ -31,8 +31,32 @@ public class TerraAllayPlugin extends Plugin {
// TODO: adapt command manager
pluginLogger.info("Registering generator...");
Registries.WORLD_GENERATOR_FACTORIES.register("TERRA", AllayGeneratorWrapper::createWorldGenerator);
Registries.WORLD_GENERATOR_FACTORIES.register("TERRA", preset -> {
try {
AllayGeneratorWrapper wrapper = new AllayGeneratorWrapper(preset);
AllayPlatform.GENERATOR_WRAPPERS.add(wrapper);
return wrapper.getAllayWorldGenerator();
} catch (IllegalArgumentException e) {
TerraAllayPlugin.INSTANCE.getPluginLogger().error("Fail to create world generator with preset: {}", preset);
TerraAllayPlugin.INSTANCE.getPluginLogger().error("Reason: {}", e.getMessage());
return Registries.WORLD_GENERATOR_FACTORIES.get("FLAT").apply("");
}
});
pluginLogger.info("Terra started");
}
@Override
public boolean isReloadable() {
return true;
}
@Override
public void reload() {
if(PLATFORM.reload()) {
pluginLogger.info("Terra reloaded successfully.");
} else {
pluginLogger.error("Terra failed to reload.");
}
}
}

View File

@@ -2,8 +2,8 @@ package com.dfsek.terra.allay.delegate;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockTypes;
import com.dfsek.terra.allay.JeBlockState;
import com.dfsek.terra.allay.JeBlockState;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.properties.Property;

View File

@@ -2,8 +2,8 @@ package com.dfsek.terra.allay.delegate;
import org.allaymc.api.block.tag.BlockTags;
import org.allaymc.api.block.type.BlockType;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.api.block.state.BlockState;
/**

View File

@@ -4,9 +4,9 @@ import org.allaymc.api.block.property.type.BlockPropertyTypes;
import org.allaymc.api.block.tag.BlockTags;
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.world.chunk.Chunk;
import com.dfsek.terra.allay.Mapping;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.world.ServerWorld;

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.allay.delegate;
import org.allaymc.api.item.enchantment.EnchantmentType;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.api.inventory.ItemStack;
import com.dfsek.terra.api.inventory.item.Enchantment;

View File

@@ -1,6 +1,8 @@
package com.dfsek.terra.allay.delegate;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.EnchantmentInstance;
import org.allaymc.api.item.enchantment.EnchantmentType;
import java.util.HashMap;
import java.util.Map;
@@ -8,10 +10,6 @@ import java.util.Map;
import com.dfsek.terra.api.inventory.item.Enchantment;
import com.dfsek.terra.api.inventory.item.ItemMeta;
import org.allaymc.api.item.enchantment.EnchantmentInstance;
import org.allaymc.api.item.enchantment.EnchantmentType;
/**
* @author daoge_cmd
*/

View File

@@ -1,13 +1,11 @@
package com.dfsek.terra.allay.delegate;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.EnchantmentInstance;
import com.dfsek.terra.api.inventory.Item;
import com.dfsek.terra.api.inventory.item.ItemMeta;
import org.allaymc.api.item.enchantment.EnchantmentInstance;
/**
* @author daoge_cmd
*/

View File

@@ -4,9 +4,9 @@ import org.allaymc.api.block.property.type.BlockPropertyTypes;
import org.allaymc.api.block.tag.BlockTags;
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.world.chunk.UnsafeChunk;
import com.dfsek.terra.allay.Mapping;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;

View File

@@ -4,8 +4,8 @@ import org.allaymc.api.block.property.type.BlockPropertyTypes;
import org.allaymc.api.block.tag.BlockTags;
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.world.generator.context.OtherChunkAccessibleContext;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.ConfigPack;

View File

@@ -3,9 +3,9 @@ package com.dfsek.terra.allay.delegate;
import org.allaymc.api.block.property.type.BlockPropertyTypes;
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.world.Dimension;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.generator.AllayGeneratorWrapper;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.ConfigPack;

View File

@@ -1,8 +1,5 @@
package com.dfsek.terra.allay.generator;
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.utils.AllayStringUtils;
import org.allaymc.api.world.biome.BiomeType;
import org.allaymc.api.world.chunk.UnsafeChunk;
@@ -11,22 +8,22 @@ import org.allaymc.api.world.generator.context.NoiseContext;
import org.allaymc.api.world.generator.context.PopulateContext;
import org.allaymc.api.world.generator.function.Noiser;
import org.allaymc.api.world.generator.function.Populator;
import com.dfsek.terra.allay.TerraAllayPlugin;
import com.dfsek.terra.allay.delegate.AllayProtoChunk;
import com.dfsek.terra.allay.delegate.AllayProtoWorld;
import com.dfsek.terra.allay.delegate.AllayServerWorld;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import com.dfsek.terra.allay.TerraAllayPlugin;
import com.dfsek.terra.allay.delegate.AllayProtoChunk;
import com.dfsek.terra.allay.delegate.AllayProtoWorld;
import com.dfsek.terra.allay.delegate.AllayServerWorld;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
import com.dfsek.terra.api.world.chunk.generation.util.GeneratorWrapper;
import com.dfsek.terra.api.world.info.WorldProperties;
/**
* @author daoge_cmd
*/
@@ -36,32 +33,21 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
protected static final String OPTION_SEED = "seed";
protected final BiomeProvider biomeProvider;
protected final ConfigPack configPack;
protected final ChunkGenerator chunkGenerator;
protected final long seed;
protected final WorldGenerator allayWorldGenerator;
protected ConfigPack configPack;
protected WorldProperties worldProperties;
protected AllayServerWorld allayServerWorld;
public static WorldGenerator createWorldGenerator(String preset) {
try {
AllayGeneratorWrapper wrapper = new AllayGeneratorWrapper(preset);
return wrapper.getAllayWorldGenerator();
} catch (IllegalArgumentException e) {
TerraAllayPlugin.INSTANCE.getPluginLogger().error("Fail to create world generator with preset: {}", preset);
TerraAllayPlugin.INSTANCE.getPluginLogger().error("Reason: {}", e.getMessage());
return Registries.WORLD_GENERATOR_FACTORIES.get("FLAT").apply("");
}
}
protected AllayGeneratorWrapper(String preset) {
public AllayGeneratorWrapper(String preset) {
Map<String, String> options = AllayStringUtils.parseOptions(preset);
String packName = options.get(OPTION_PACK_NAME);
if(packName == null) {
throw new IllegalArgumentException("Missing config pack name");
}
this.seed = Long.parseLong(options.getOrDefault(OPTION_SEED, "0"));
this.configPack = createConfigPack(packName);
this.configPack = getConfigPack(packName);
this.chunkGenerator = createGenerator(this.configPack);
this.biomeProvider = this.configPack.getBiomeProvider();
this.allayWorldGenerator = WorldGenerator
@@ -100,7 +86,32 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
.build();
}
public class AllayNoiser implements Noiser {
@Override
public ChunkGenerator getHandle() {
return chunkGenerator;
}
public BiomeProvider getBiomeProvider() {
return this.biomeProvider;
}
public ConfigPack getConfigPack() {
return this.configPack;
}
public void setConfigPack(ConfigPack configPack) {
this.configPack = configPack;
}
public long getSeed() {
return this.seed;
}
public WorldGenerator getAllayWorldGenerator() {
return this.allayWorldGenerator;
}
protected class AllayNoiser implements Noiser {
@Override
public boolean apply(NoiseContext context) {
@@ -133,8 +144,7 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
}
}
public class AllayPopulator implements Populator {
protected class AllayPopulator implements Populator {
@Override
public boolean apply(PopulateContext context) {
@@ -155,7 +165,7 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
}
}
protected static ConfigPack createConfigPack(String packName) {
protected static ConfigPack getConfigPack(String packName) {
Optional<ConfigPack> byId = TerraAllayPlugin.PLATFORM.getConfigRegistry().getByID(packName);
return byId.orElseGet(
() -> TerraAllayPlugin.PLATFORM.getConfigRegistry().getByID(packName.toUpperCase(Locale.ENGLISH))
@@ -166,25 +176,4 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
protected static ChunkGenerator createGenerator(ConfigPack configPack) {
return configPack.getGeneratorProvider().newInstance(configPack);
}
@Override
public ChunkGenerator getHandle() {
return chunkGenerator;
}
public BiomeProvider getBiomeProvider() {
return this.biomeProvider;
}
public ConfigPack getConfigPack() {
return this.configPack;
}
public long getSeed() {
return this.seed;
}
public WorldGenerator getAllayWorldGenerator() {
return this.allayWorldGenerator;
}
}

View File

@@ -2,13 +2,13 @@ package com.dfsek.terra.allay.handle;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.utils.Identifier;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.delegate.AllayEnchantment;
import com.dfsek.terra.allay.delegate.AllayItemType;
import java.util.Set;
import java.util.stream.Collectors;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.delegate.AllayEnchantment;
import com.dfsek.terra.allay.delegate.AllayItemType;
import com.dfsek.terra.api.handle.ItemHandle;
import com.dfsek.terra.api.inventory.Item;
import com.dfsek.terra.api.inventory.item.Enchantment;

View File

@@ -1,10 +1,10 @@
package com.dfsek.terra.allay.handle;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.allay.JeBlockState;
import com.dfsek.terra.allay.Mapping;
import com.dfsek.terra.allay.delegate.AllayBlockState;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.handle.WorldHandle;