mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-10 09:46:24 +00:00
implement ore addon
This commit is contained in:
@@ -27,9 +27,7 @@ public class OreAddon implements AddonInitializer {
|
||||
platform.getEventManager()
|
||||
.getHandler(FunctionalEventHandler.class)
|
||||
.register(addon, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
event.getPack().registerConfigType(new OreConfigType(), "ORE", 1);
|
||||
})
|
||||
.then(event -> event.getPack().registerConfigType(new OreConfigType(), "ORE", 1))
|
||||
.failThrough();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,19 +7,17 @@
|
||||
|
||||
package com.dfsek.terra.addons.ore;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.dfsek.terra.addons.ore.ores.Ore;
|
||||
import com.dfsek.terra.addons.ore.ores.VanillaOre;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.config.ConfigFactory;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
public class OreConfigType implements ConfigType<OreTemplate, Ore> {
|
||||
public static final TypeKey<Ore> ORE_TYPE_TOKEN = new TypeKey<>() {
|
||||
public class OreConfigType implements ConfigType<OreTemplate, Structure> {
|
||||
public static final TypeKey<Structure> ORE_TYPE_TOKEN = new TypeKey<>() {
|
||||
};
|
||||
private final OreFactory factory = new OreFactory();
|
||||
|
||||
@@ -29,12 +27,12 @@ public class OreConfigType implements ConfigType<OreTemplate, Ore> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigFactory<OreTemplate, Ore> getFactory() {
|
||||
public ConfigFactory<OreTemplate, Structure> getFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<Ore> getTypeKey() {
|
||||
public TypeKey<Structure> getTypeKey() {
|
||||
return ORE_TYPE_TOKEN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,17 +7,17 @@
|
||||
|
||||
package com.dfsek.terra.addons.ore;
|
||||
|
||||
import com.dfsek.terra.addons.ore.ores.Ore;
|
||||
import com.dfsek.terra.addons.ore.ores.VanillaOre;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.config.ConfigFactory;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
|
||||
|
||||
public class OreFactory implements ConfigFactory<OreTemplate, Ore> {
|
||||
public class OreFactory implements ConfigFactory<OreTemplate, Structure> {
|
||||
@Override
|
||||
public Ore build(OreTemplate config, Platform platform) {
|
||||
public VanillaOre build(OreTemplate config, Platform platform) {
|
||||
BlockState m = config.getMaterial();
|
||||
return new VanillaOre(m, config.getReplaceable(), config.doPhysics(), config.getSize(), platform, config.getMaterialOverrides());
|
||||
return new VanillaOre(m, config.getSize(), config.getReplaceable(), config.doPhysics(), config.getMaterialOverrides());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class OreTemplate implements AbstractableTemplate {
|
||||
private @Meta boolean physics = false;
|
||||
|
||||
@Value("size")
|
||||
private @Meta Range size;
|
||||
private @Meta double size;
|
||||
|
||||
@Value("deform")
|
||||
@Default
|
||||
@@ -65,7 +65,7 @@ public class OreTemplate implements AbstractableTemplate {
|
||||
return deformFrequency;
|
||||
}
|
||||
|
||||
public Range getSize() {
|
||||
public double getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.ore.ores;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.block.BlockType;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.collection.MaterialSet;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
|
||||
|
||||
public abstract class Ore {
|
||||
|
||||
private final BlockState material;
|
||||
private final MaterialSet replaceable;
|
||||
private final boolean applyGravity;
|
||||
private final Map<BlockType, BlockState> materials;
|
||||
protected Platform platform;
|
||||
|
||||
public Ore(BlockState material, MaterialSet replaceable, boolean applyGravity, Platform platform,
|
||||
Map<BlockType, BlockState> materials) {
|
||||
this.material = material;
|
||||
this.replaceable = replaceable;
|
||||
this.applyGravity = applyGravity;
|
||||
this.platform = platform;
|
||||
this.materials = materials;
|
||||
}
|
||||
|
||||
public abstract void generate(Vector3 origin, Chunk c, Random r);
|
||||
|
||||
public BlockState getMaterial(BlockType replace) {
|
||||
return materials.getOrDefault(replace, material);
|
||||
}
|
||||
|
||||
public MaterialSet getReplaceable() {
|
||||
return replaceable;
|
||||
}
|
||||
|
||||
public boolean isApplyGravity() {
|
||||
return applyGravity;
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.ore.ores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
|
||||
/**
|
||||
* Holds ordered list of ores mapped to their configs.
|
||||
*/
|
||||
public class OreHolder {
|
||||
private final List<Entry> entries = new ArrayList<>();
|
||||
|
||||
public void forEach(BiConsumer<String, Pair<Ore, OreConfig>> consumer) {
|
||||
entries.forEach(entry -> consumer.accept(entry.getId(), Pair.of(entry.getOre(), entry.getConfig())));
|
||||
}
|
||||
|
||||
public OreHolder add(Ore ore, OreConfig config, String id) {
|
||||
entries.add(new Entry(ore, config, id));
|
||||
return this;
|
||||
}
|
||||
|
||||
private static final class Entry {
|
||||
private final Ore ore;
|
||||
private final OreConfig config;
|
||||
private final String id;
|
||||
|
||||
private Entry(Ore ore, OreConfig config, String id) {
|
||||
this.ore = ore;
|
||||
this.config = config;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public OreConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public Ore getOre() {
|
||||
return ore;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,80 +7,107 @@
|
||||
|
||||
package com.dfsek.terra.addons.ore.ores;
|
||||
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.block.BlockType;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.Range;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.structure.buffer.Buffer;
|
||||
import com.dfsek.terra.api.util.Rotation;
|
||||
import com.dfsek.terra.api.util.collection.MaterialSet;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.WritableWorld;
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
|
||||
public class VanillaOre extends Ore {
|
||||
private final Range sizeRange;
|
||||
public class VanillaOre implements Structure {
|
||||
|
||||
public VanillaOre(BlockState material, MaterialSet replaceable, boolean applyGravity, Range size, Platform platform,
|
||||
private final BlockState material;
|
||||
|
||||
private final double size;
|
||||
private final MaterialSet replaceable;
|
||||
private final boolean applyGravity;
|
||||
private final Map<BlockType, BlockState> materials;
|
||||
|
||||
public VanillaOre(BlockState material, double size, MaterialSet replaceable, boolean applyGravity,
|
||||
Map<BlockType, BlockState> materials) {
|
||||
super(material, replaceable, applyGravity, platform, materials);
|
||||
this.sizeRange = size;
|
||||
this.material = material;
|
||||
this.size = size;
|
||||
this.replaceable = replaceable;
|
||||
this.applyGravity = applyGravity;
|
||||
this.materials = materials;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(Vector3 location, Chunk chunk, Random random) {
|
||||
double size = sizeRange.get(random);
|
||||
|
||||
public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) {
|
||||
generate(buffer.getOrigin(), world, random);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
|
||||
generate(location, world, random);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void generate(Vector3 location, WritableWorld world, Random random) {
|
||||
int centerX = location.getBlockX();
|
||||
int centerZ = location.getBlockZ();
|
||||
int centerY = location.getBlockY();
|
||||
|
||||
|
||||
double f = random.nextFloat() * Math.PI;
|
||||
|
||||
double fS = FastMath.sin(f) * size / 8.0F;
|
||||
double fC = FastMath.cos(f) * size / 8.0F;
|
||||
|
||||
double d1 = centerX + 8 + fS;
|
||||
double d2 = centerX + 8 - fS;
|
||||
double d3 = centerZ + 8 + fC;
|
||||
double d4 = centerZ + 8 - fC;
|
||||
|
||||
|
||||
|
||||
float f = random.nextFloat() * (float) Math.PI;
|
||||
|
||||
double d1 = centerX + 8 + FastMath.sin(f) * size / 8.0F;
|
||||
double d2 = centerX + 8 - FastMath.sin(f) * size / 8.0F;
|
||||
double d3 = centerZ + 8 + FastMath.cos(f) * size / 8.0F;
|
||||
double d4 = centerZ + 8 - FastMath.cos(f) * size / 8.0F;
|
||||
|
||||
double d5 = centerY + random.nextInt(3) - 2D;
|
||||
double d6 = centerY + random.nextInt(3) - 2D;
|
||||
|
||||
|
||||
for(int i = 0; i < size; i++) {
|
||||
double iFactor = i / size;
|
||||
|
||||
float iFactor = (float) i / (float) size;
|
||||
|
||||
double d10 = random.nextDouble() * size / 16.0D;
|
||||
double d11 = (FastMath.sin(Math.PI * iFactor) + 1.0) * d10 + 1.0;
|
||||
|
||||
double d12 = (FastMath.sin(Math.PI * iFactor) + 1.0) * d10 + 1.0;
|
||||
|
||||
int xStart = FastMath.roundToInt(FastMath.floor(d1 + (d2 - d1) * iFactor - d11 / 2.0D));
|
||||
int yStart = FastMath.roundToInt(FastMath.floor(d5 + (d6 - d5) * iFactor - d11 / 2.0D));
|
||||
int yStart = FastMath.roundToInt(FastMath.floor(d5 + (d6 - d5) * iFactor - d12 / 2.0D));
|
||||
int zStart = FastMath.roundToInt(FastMath.floor(d3 + (d4 - d3) * iFactor - d11 / 2.0D));
|
||||
|
||||
|
||||
int xEnd = FastMath.roundToInt(FastMath.floor(d1 + (d2 - d1) * iFactor + d11 / 2.0D));
|
||||
int yEnd = FastMath.roundToInt(FastMath.floor(d5 + (d6 - d5) * iFactor + d11 / 2.0D));
|
||||
int yEnd = FastMath.roundToInt(FastMath.floor(d5 + (d6 - d5) * iFactor + d12 / 2.0D));
|
||||
int zEnd = FastMath.roundToInt(FastMath.floor(d3 + (d4 - d3) * iFactor + d11 / 2.0D));
|
||||
|
||||
|
||||
for(int x = xStart; x <= xEnd; x++) {
|
||||
double d13 = (x + 0.5D - (d1 + (d2 - d1) * iFactor)) / (d11 / 2.0D);
|
||||
|
||||
|
||||
if(d13 * d13 < 1.0D) {
|
||||
for(int y = yStart; y <= yEnd; y++) {
|
||||
double d14 = (y + 0.5D - (d5 + (d6 - d5) * iFactor)) / (d11 / 2.0D);
|
||||
double d14 = (y + 0.5D - (d5 + (d6 - d5) * iFactor)) / (d12 / 2.0D);
|
||||
if(d13 * d13 + d14 * d14 < 1.0D) {
|
||||
for(int z = zStart; z <= zEnd; z++) {
|
||||
double d15 = (z + 0.5D - (d3 + (d4 - d3) * iFactor)) / (d11 / 2.0D);
|
||||
if(x > 15 || z > 15 || y > 255 || x < 0 || z < 0 || y < 0) continue;
|
||||
|
||||
BlockType type = chunk.getBlock(x, y, z).getBlockType();
|
||||
if((d13 * d13 + d14 * d14 + d15 * d15 < 1.0D) && getReplaceable().contains(type)) {
|
||||
chunk.setBlock(x, y, z, getMaterial(type), isApplyGravity());
|
||||
if(y >= world.getMaxHeight() || y < world.getMinHeight()) continue;
|
||||
BlockType block = world.getBlockData(x, y, z).getBlockType();
|
||||
if((d13 * d13 + d14 * d14 + d15 * d15 < 1.0D) && getReplaceable().contains(block)) {
|
||||
world.setBlockData(x, y, z, getMaterial(block), isApplyGravity());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,4 +116,16 @@ public class VanillaOre extends Ore {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BlockState getMaterial(BlockType replace) {
|
||||
return materials.getOrDefault(replace, material);
|
||||
}
|
||||
|
||||
public MaterialSet getReplaceable() {
|
||||
return replaceable;
|
||||
}
|
||||
|
||||
public boolean isApplyGravity() {
|
||||
return applyGravity;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user