mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
feat: more works
This commit is contained in:
@@ -4,6 +4,9 @@ import com.dfsek.terra.AbstractPlatform;
|
||||
import com.dfsek.terra.api.handle.ItemHandle;
|
||||
import com.dfsek.terra.api.handle.WorldHandle;
|
||||
|
||||
import org.allaymc.api.server.Server;
|
||||
import org.allaymc.terra.allay.handle.AllayItemHandle;
|
||||
import org.allaymc.terra.allay.handle.AllayWorldHandle;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -16,8 +19,12 @@ import java.io.File;
|
||||
*/
|
||||
public class AllayPlatform extends AbstractPlatform {
|
||||
|
||||
protected static final AllayWorldHandle ALLAY_WORLD_HANDLE = new AllayWorldHandle();
|
||||
protected static final AllayItemHandle ALLAY_ITEM_HANDLE = new AllayItemHandle();
|
||||
|
||||
@Override
|
||||
public boolean reload() {
|
||||
// TODO: Implement reload
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,8 +35,12 @@ public class AllayPlatform extends AbstractPlatform {
|
||||
|
||||
@Override
|
||||
public @NotNull WorldHandle getWorldHandle() {
|
||||
// TODO
|
||||
return null;
|
||||
return ALLAY_WORLD_HANDLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemHandle getItemHandle() {
|
||||
return ALLAY_ITEM_HANDLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,8 +49,7 @@ public class AllayPlatform extends AbstractPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemHandle getItemHandle() {
|
||||
// TODO
|
||||
return null;
|
||||
public void runPossiblyUnsafeTask(@NotNull Runnable task) {
|
||||
Server.getInstance().getScheduler().runLater(Server.getInstance(), task);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package org.allaymc.terra.allay;
|
||||
|
||||
import org.allaymc.api.utils.Identifier;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
|
||||
/**
|
||||
@@ -11,30 +13,42 @@ import java.util.Map;
|
||||
* @author daoge_cmd
|
||||
*/
|
||||
public class JeBlockState {
|
||||
protected final Identifier identifier;
|
||||
protected final Map<String, String> properties;
|
||||
protected final String identifier;
|
||||
protected final TreeMap<String, String> properties;
|
||||
|
||||
public static JeBlockState fromString(String data) {
|
||||
// TODO
|
||||
return null;
|
||||
return new JeBlockState(data);
|
||||
}
|
||||
|
||||
public JeBlockState(Identifier identifier, Map<String, String> properties) {
|
||||
public static JeBlockState create(String identifier, TreeMap<String, String> properties) {
|
||||
return new JeBlockState(identifier, properties);
|
||||
}
|
||||
|
||||
private JeBlockState(String data) {
|
||||
var strings = data.replace("[", ",").replace("]", ",").replace(" ", "").split(",");
|
||||
this.identifier = strings[0];
|
||||
this.properties = new TreeMap<>();
|
||||
if (strings.length > 1) {
|
||||
for (int i = 1; i < strings.length; i++) {
|
||||
final var tmp = strings[i];
|
||||
final var index = tmp.indexOf("=");
|
||||
properties.put(tmp.substring(0, index), tmp.substring(index + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JeBlockState(String identifier, TreeMap<String, String> properties) {
|
||||
this.identifier = identifier;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String toString(boolean includeProperties) {
|
||||
if(!includeProperties) return identifier.toString();
|
||||
StringBuilder builder = new StringBuilder(identifier.toString()).append(";");
|
||||
if(!includeProperties) return identifier;
|
||||
StringBuilder builder = new StringBuilder(identifier).append(";");
|
||||
properties.forEach((k, v) -> builder.append(k).append("=").append(v).append(";"));
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public boolean hasProperty(String name) {
|
||||
return properties.containsKey(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(true);
|
||||
|
||||
@@ -14,7 +14,7 @@ public final class Mapping {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public static JeBlockState blockStateBeToJe(BlockState blockState) {
|
||||
public static JeBlockState blockStateBeToJe(BlockState beBlockState) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
@@ -28,4 +28,14 @@ public final class Mapping {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String enchantmentIdJeToBe(String jeEnchantmentId) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String itemIdJeToBe(String jeItemId) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.allaymc.api.plugin.Plugin;
|
||||
import org.allaymc.api.world.generator.WorldGeneratorFactory;
|
||||
import org.allaymc.terra.allay.generator.AllayGeneratorWrapper;
|
||||
|
||||
|
||||
/**
|
||||
@@ -15,20 +17,26 @@ import org.allaymc.api.plugin.Plugin;
|
||||
public class TerraAllayPlugin extends Plugin {
|
||||
|
||||
public static TerraAllayPlugin INSTANCE;
|
||||
public static AllayPlatform PLATFORM;
|
||||
|
||||
{
|
||||
INSTANCE = this;
|
||||
}
|
||||
|
||||
// TODO: Adapt command manager
|
||||
@Override
|
||||
public void onEnable() {
|
||||
log.info("Starting Terra...");
|
||||
|
||||
var platform = new AllayPlatform();
|
||||
platform.getEventManager().callEvent(new PlatformInitializationEvent());
|
||||
|
||||
// TODO: Adapt command manager
|
||||
PLATFORM = new AllayPlatform();
|
||||
PLATFORM.getEventManager().callEvent(new PlatformInitializationEvent());
|
||||
|
||||
log.info("Loading mapping...");
|
||||
Mapping.init();
|
||||
|
||||
log.info("Registering generator...");
|
||||
WorldGeneratorFactory.getFactory().register("TERRA", AllayGeneratorWrapper::new);
|
||||
|
||||
log.info("Terra started");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;
|
||||
|
||||
import org.allaymc.api.world.chunk.Chunk;
|
||||
import org.allaymc.api.world.chunk.UnsafeChunk;
|
||||
import org.allaymc.terra.allay.Mapping;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -13,7 +14,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
*
|
||||
* @author daoge_cmd
|
||||
*/
|
||||
public record AllayProtoChunk(Chunk allayChunk) implements ProtoChunk {
|
||||
public record AllayProtoChunk(UnsafeChunk allayChunk) implements ProtoChunk {
|
||||
@Override
|
||||
public int getMaxHeight() {
|
||||
return allayChunk.getDimensionInfo().maxHeight();
|
||||
@@ -31,7 +32,7 @@ public record AllayProtoChunk(Chunk allayChunk) implements ProtoChunk {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Chunk getHandle() {
|
||||
public UnsafeChunk getHandle() {
|
||||
return allayChunk;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
package org.allaymc.terra.allay.delegate;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import com.dfsek.terra.api.block.entity.BlockEntity;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.EntityType;
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
|
||||
import org.allaymc.api.world.Dimension;
|
||||
import org.allaymc.terra.allay.Mapping;
|
||||
import org.allaymc.terra.allay.generator.AllayGeneratorWrapper;
|
||||
|
||||
|
||||
/**
|
||||
@@ -8,5 +20,68 @@ import java.awt.Dimension;
|
||||
*
|
||||
* @author daoge_cmd
|
||||
*/
|
||||
public record AllayServerWorld(Dimension allayDimension) {
|
||||
public record AllayServerWorld(AllayGeneratorWrapper allayGeneratorWrapper, Dimension allayDimension) implements ServerWorld {
|
||||
@Override
|
||||
public Chunk getChunkAt(int x, int z) {
|
||||
return new AllayChunk(this, allayDimension.getChunkService().getChunk(x ,z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
|
||||
var allayBlockState = ((AllayBlockState)data).allayBlockState();
|
||||
allayDimension.setBlockState(x, y, z, allayBlockState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getBlockState(int x, int y, int z) {
|
||||
var allayBlockState = allayDimension.getBlockState(x, y, z);
|
||||
return new AllayBlockState(allayBlockState, Mapping.blockStateBeToJe(allayBlockState));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockEntity getBlockEntity(int x, int y, int z) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChunkGenerator getGenerator() {
|
||||
return allayGeneratorWrapper.getHandle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeProvider getBiomeProvider() {
|
||||
return allayGeneratorWrapper.getBiomeProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigPack getPack() {
|
||||
return allayGeneratorWrapper.getConfigPack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSeed() {
|
||||
return allayGeneratorWrapper.getSeed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxHeight() {
|
||||
return allayDimension.getDimensionInfo().maxHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinHeight() {
|
||||
return allayDimension.getDimensionInfo().minHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getHandle() {
|
||||
return allayDimension;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
package org.allaymc.terra.allay.generator;
|
||||
|
||||
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.util.GeneratorWrapper;
|
||||
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.allaymc.api.world.Dimension;
|
||||
import org.allaymc.api.world.biome.BiomeType;
|
||||
import org.allaymc.api.world.generator.ChunkGenerateContext;
|
||||
import org.allaymc.api.world.generator.WorldGenerator;
|
||||
import org.allaymc.api.world.generator.WorldGeneratorType;
|
||||
import org.allaymc.terra.allay.TerraAllayPlugin;
|
||||
import org.allaymc.terra.allay.delegate.AllayProtoChunk;
|
||||
import org.allaymc.terra.allay.delegate.AllayProtoWorld;
|
||||
import org.allaymc.terra.allay.delegate.AllayServerWorld;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
/**
|
||||
* Terra Project 2024/6/16
|
||||
*
|
||||
* @author daoge_cmd
|
||||
*/
|
||||
@Slf4j
|
||||
public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWrapper {
|
||||
protected static final String DEFAULT_PACK_NAME = "default";
|
||||
protected static final String OPTION_PACK_NAME = "pack";
|
||||
protected static final String OPTION_SEED = "pack";
|
||||
|
||||
@Getter
|
||||
protected final BiomeProvider biomeProvider;
|
||||
@Getter
|
||||
protected final ConfigPack configPack;
|
||||
protected final ChunkGenerator chunkGenerator;
|
||||
protected WorldProperties worldProperties;
|
||||
@Getter
|
||||
protected long seed;
|
||||
|
||||
public AllayGeneratorWrapper(String preset) {
|
||||
super(preset);
|
||||
var options = parseOptions(preset);
|
||||
var packName = options.getOrDefault(OPTION_PACK_NAME, DEFAULT_PACK_NAME);
|
||||
this.seed = Long.parseLong(options.getOrDefault(OPTION_SEED, "0"));
|
||||
this.configPack = createConfigPack(packName);
|
||||
this.chunkGenerator = createGenerator(this.configPack);
|
||||
this.biomeProvider = this.configPack.getBiomeProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(ChunkGenerateContext context) {
|
||||
var chunk = context.chunk();
|
||||
var chunkX = chunk.getX();
|
||||
var chunkZ = chunk.getZ();
|
||||
chunkGenerator.generateChunkData(
|
||||
new AllayProtoChunk(chunk),
|
||||
worldProperties, biomeProvider,
|
||||
chunkX, chunkZ
|
||||
);
|
||||
var minHeight = dimension.getDimensionInfo().minHeight();
|
||||
var maxHeight = dimension.getDimensionInfo().maxHeight();
|
||||
for (int x = 0; x < 16; x++) {
|
||||
for (int y = minHeight; y < maxHeight; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
chunk.setBiome(
|
||||
x, y, z,
|
||||
(BiomeType) biomeProvider.getBiome(chunkX * 16 + x, y, chunkZ * 16 + z, seed).getPlatformBiome().getHandle()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
var tmp = new AllayProtoWorld(new AllayServerWorld(this, dimension), chunkX, chunkZ);
|
||||
try {
|
||||
for (var generationStage : configPack.getStages()) {
|
||||
generationStage.populate(tmp);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error while populating chunk", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDimension(Dimension dimension) {
|
||||
this.worldProperties = new WorldProperties() {
|
||||
@Override
|
||||
public long getSeed() {
|
||||
return seed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxHeight() {
|
||||
return dimension.getDimensionInfo().maxHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinHeight() {
|
||||
return dimension.getDimensionInfo().minHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getHandle() {
|
||||
// 这里留null就行,没啥用
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected static ConfigPack createConfigPack(String packName) {
|
||||
var byId = TerraAllayPlugin.PLATFORM.getConfigRegistry().getByID(packName);
|
||||
return byId.orElseGet(
|
||||
() -> TerraAllayPlugin.PLATFORM.getConfigRegistry().getByID(packName.toUpperCase(Locale.ENGLISH))
|
||||
.orElseThrow(() -> new IllegalArgumentException("Cant find terra config pack named " + packName))
|
||||
);
|
||||
}
|
||||
|
||||
protected static ChunkGenerator createGenerator(ConfigPack configPack) {
|
||||
return configPack.getGeneratorProvider().newInstance(configPack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChunkGenerator getHandle() {
|
||||
return chunkGenerator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGeneratorName() {
|
||||
return "TERRA";
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorldGeneratorType getType() {
|
||||
return WorldGeneratorType.INFINITE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.allaymc.terra.allay.handle;
|
||||
|
||||
import com.dfsek.terra.api.handle.ItemHandle;
|
||||
import com.dfsek.terra.api.inventory.Item;
|
||||
import com.dfsek.terra.api.inventory.item.Enchantment;
|
||||
|
||||
import org.allaymc.api.item.enchantment.EnchantmentRegistry;
|
||||
import org.allaymc.api.item.enchantment.type.EnchantmentLuckOfTheSeaType;
|
||||
import org.allaymc.api.item.registry.ItemTypeRegistry;
|
||||
import org.allaymc.api.utils.Identifier;
|
||||
import org.allaymc.terra.allay.Mapping;
|
||||
import org.allaymc.terra.allay.delegate.AllayEnchantment;
|
||||
import org.allaymc.terra.allay.delegate.AllayItemType;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* Terra Project 2024/6/16
|
||||
*
|
||||
* @author daoge_cmd
|
||||
*/
|
||||
public class AllayItemHandle implements ItemHandle {
|
||||
@Override
|
||||
public Item createItem(String data) {
|
||||
return new AllayItemType(ItemTypeRegistry.getRegistry().get(new Identifier(Mapping.itemIdJeToBe(data))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enchantment getEnchantment(String id) {
|
||||
return new AllayEnchantment(EnchantmentRegistry.getRegistry().getByK2(new Identifier(Mapping.enchantmentIdJeToBe(id))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Enchantment> getEnchantments() {
|
||||
return EnchantmentRegistry.getRegistry().getContent().m1().values().stream()
|
||||
.map(AllayEnchantment::new)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user