Merge submodule contents for common/addons/config-palette/master

This commit is contained in:
dfsek
2021-11-21 21:13:11 -07:00
12 changed files with 455 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020-2021 Polyhedral Development
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,3 @@
# config-palette
Registers the default configuration for Terra Palettes, `PALETTE`.

View File

@@ -0,0 +1,3 @@
dependencies {
"shadedApi"(project(":common:addons:manifest-addon-loader"))
}

View File

@@ -0,0 +1,38 @@
/*
* 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.palette;
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
import com.dfsek.terra.addons.palette.palette.PaletteLayerHolder;
import com.dfsek.terra.addons.palette.palette.PaletteLayerLoader;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.inject.annotations.Inject;
public class PaletteAddon implements AddonInitializer {
@Inject
private Platform platform;
@Inject
private BaseAddon addon;
@Override
public void initialize() {
platform.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.then(event -> {
event.getPack().registerConfigType(new PaletteConfigType(platform), "PALETTE", 2);
event.getPack().applyLoader(PaletteLayerHolder.class, new PaletteLayerLoader());
})
.failThrough();
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.palette;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.TypeLoader;
import java.util.function.Supplier;
import com.dfsek.terra.addons.palette.palette.PaletteImpl;
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.util.reflection.TypeKey;
import com.dfsek.terra.api.world.generator.Palette;
public class PaletteConfigType implements ConfigType<PaletteTemplate, Palette> {
public static final TypeKey<Palette> PALETTE_TYPE_TOKEN = new TypeKey<>() {
};
private final PaletteFactory factory = new PaletteFactory();
private final Platform platform;
public PaletteConfigType(Platform platform) {
this.platform = platform;
}
@Override
public Supplier<OpenRegistry<Palette>> registrySupplier(ConfigPack pack) {
return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<Palette>) (t, c, loader) -> {
if(((String) c).startsWith("BLOCK:"))
return new PaletteImpl.Singleton(
platform.getWorldHandle().createBlockData(((String) c).substring(6))); // Return single palette for BLOCK: shortcut.
Palette obj = registry.get((String) c);
if(obj == null)
throw new LoadException("No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry.");
return obj;
});
}
@Override
public PaletteTemplate getTemplate(ConfigPack pack, Platform platform) {
return new PaletteTemplate();
}
@Override
public ConfigFactory<PaletteTemplate, Palette> getFactory() {
return factory;
}
@Override
public TypeKey<Palette> getTypeKey() {
return PALETTE_TYPE_TOKEN;
}
}

View File

@@ -0,0 +1,26 @@
/*
* 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.palette;
import com.dfsek.terra.addons.palette.palette.NoisePalette;
import com.dfsek.terra.addons.palette.palette.PaletteLayerHolder;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.world.generator.Palette;
public class PaletteFactory implements ConfigFactory<PaletteTemplate, Palette> {
@Override
public Palette build(PaletteTemplate config, Platform platform) {
NoisePalette palette = new NoisePalette(config.getNoise());
for(PaletteLayerHolder layer : config.getPalette()) {
palette.add(layer.getLayer(), layer.getSize(), layer.getSampler());
}
return palette;
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.palette;
import com.dfsek.tectonic.annotations.Default;
import com.dfsek.tectonic.annotations.Final;
import com.dfsek.tectonic.annotations.Value;
import java.util.List;
import com.dfsek.terra.addons.palette.palette.PaletteLayerHolder;
import com.dfsek.terra.api.config.AbstractableTemplate;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.noise.NoiseSampler;
@SuppressWarnings({ "FieldMayBeFinal", "unused" })
public class PaletteTemplate implements AbstractableTemplate {
@Value("noise")
@Default
private @Meta NoiseSampler noise = NoiseSampler.zero();
@Value("id")
@Final
private String id;
@Value("layers")
private @Meta List<@Meta PaletteLayerHolder> palette;
public String getID() {
return id;
}
public List<PaletteLayerHolder> getPalette() {
return palette;
}
public NoiseSampler getNoise() {
return noise;
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.palette.palette;
import java.util.List;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.noise.NoiseSampler;
public class NoisePalette extends PaletteImpl {
private final NoiseSampler sampler;
public NoisePalette(NoiseSampler sampler) {
this.sampler = sampler;
}
@Override
public BlockState get(int layer, double x, double y, double z, long seed) {
PaletteLayer paletteLayer;
if(layer > this.getSize()) paletteLayer = this.getLayers().get(this.getLayers().size() - 1);
else {
List<PaletteLayer> pl = getLayers();
if(layer >= pl.size()) paletteLayer = pl.get(pl.size() - 1);
else paletteLayer = pl.get(layer);
}
NoiseSampler paletteSampler = paletteLayer.getSampler();
return paletteLayer.get(paletteSampler == null ? sampler : paletteSampler, x, y, z, seed);
}
}

View File

@@ -0,0 +1,119 @@
/*
* 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.palette.palette;
import java.util.ArrayList;
import java.util.List;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
import com.dfsek.terra.api.world.generator.Palette;
/**
* A class representation of a "slice" of the world.
* Used to get a section of blocks, based on the depth at which they are found.
*/
public abstract class PaletteImpl implements Palette {
private final List<PaletteLayer> pallet = new ArrayList<>();
/**
* Constructs a blank palette.
*/
public PaletteImpl() {
}
@Override
public Palette add(BlockState m, int layers, NoiseSampler sampler) {
for(int i = 0; i < layers; i++) {
pallet.add(new PaletteLayer(m, sampler));
}
return this;
}
@Override
public Palette add(ProbabilityCollection<BlockState> m, int layers, NoiseSampler sampler) {
for(int i = 0; i < layers; i++) {
pallet.add(new PaletteLayer(m, sampler));
}
return this;
}
@Override
public int getSize() {
return pallet.size();
}
public List<PaletteLayer> getLayers() {
return pallet;
}
/**
* Class representation of a layer of a BlockPalette.
*/
public static class PaletteLayer {
private final boolean col; // Is layer using a collection?
private final NoiseSampler sampler;
private ProbabilityCollection<BlockState> collection;
private BlockState m;
/**
* Constructs a PaletteLayerHolder with a ProbabilityCollection of materials and a number of layers.
*
* @param type The collection of materials to choose from.
* @param sampler Noise sampler to use
*/
public PaletteLayer(ProbabilityCollection<BlockState> type, NoiseSampler sampler) {
this.sampler = sampler;
this.col = true;
this.collection = type;
}
/**
* Constructs a PaletteLayerHolder with a single Material and a number of layers.
*
* @param type The material to use.
* @param sampler Noise sampler to use
*/
public PaletteLayer(BlockState type, NoiseSampler sampler) {
this.sampler = sampler;
this.col = false;
this.m = type;
}
public BlockState get(NoiseSampler random, double x, double y, double z, long seed) {
if(col) return this.collection.get(random, x, y, z, seed);
return m;
}
public NoiseSampler getSampler() {
return sampler;
}
public ProbabilityCollection<BlockState> getCollection() {
return collection;
}
}
public static class Singleton extends PaletteImpl {
private final BlockState item;
public Singleton(BlockState item) {
this.item = item;
}
@Override
public BlockState get(int layer, double x, double y, double z, long seed) {
return item;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.palette.palette;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
public class PaletteLayerHolder {
private final ProbabilityCollection<BlockState> layer;
private final NoiseSampler sampler;
private final int size;
public PaletteLayerHolder(@NotNull ProbabilityCollection<BlockState> layer, NoiseSampler sampler, int size) {
this.layer = layer;
this.sampler = sampler;
this.size = size;
}
@NotNull
public ProbabilityCollection<BlockState> getLayer() {
return layer;
}
public int getSize() {
return size;
}
public NoiseSampler getSampler() {
return sampler;
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.palette.palette;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeLoader;
import java.lang.reflect.AnnotatedType;
import java.util.Map;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
@SuppressWarnings("unchecked")
public class PaletteLayerLoader implements TypeLoader<PaletteLayerHolder> {
private static final AnnotatedType BLOCK_DATA_PROBABILITY_COLLECTION_TYPE;
static {
try {
BLOCK_DATA_PROBABILITY_COLLECTION_TYPE = PaletteLayerLoader.class.getDeclaredField("blockStateProbabilityCollection")
.getAnnotatedType();
} catch(NoSuchFieldException e) {
throw new Error("this should never happen. i dont know what you did to make this happen but something is very wrong.", e);
}
}
@SuppressWarnings("unused")
private ProbabilityCollection<BlockState> blockStateProbabilityCollection;
@Override
public PaletteLayerHolder load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException {
Map<String, Object> map = (Map<String, Object>) o;
ProbabilityCollection<BlockState> collection = (ProbabilityCollection<BlockState>) configLoader.loadType(
BLOCK_DATA_PROBABILITY_COLLECTION_TYPE, map.get("materials"));
NoiseSampler sampler = null;
if(map.containsKey("noise")) {
sampler = configLoader.loadType(NoiseSampler.class, map.get("noise"));
}
if(collection == null) throw new LoadException("Collection is null: " + map.get("materials"));
return new PaletteLayerHolder(collection, sampler, (Integer) map.get("layers"));
}
}

View File

@@ -0,0 +1,12 @@
schema-version: 1
contributors:
- Terra contributors
id: config-palette
version: 0.1.0
entrypoints:
- "com.dfsek.terra.addons.palette.PaletteAddon"
website:
issues: https://github.com/PolyhedralDev/Terra-config-palette/issues
source: https://github.com/PolyhedralDev/Terra-config-palette
docs: https://github.com/PolyhedralDev/Terra/wiki
license: GNU LGPL v3.0