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

This commit is contained in:
dfsek
2021-11-21 21:11:04 -07:00
14 changed files with 404 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 @@
dependencies {
"shadedApi"(project(":common:addons:manifest-addon-loader"))
}

View File

@@ -0,0 +1,61 @@
/*
* 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.feature.distributor;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.util.function.Supplier;
import com.dfsek.terra.addons.feature.distributor.config.AndDistributorTemplate;
import com.dfsek.terra.addons.feature.distributor.config.NoiseDistributorTemplate;
import com.dfsek.terra.addons.feature.distributor.config.OrDistributorTemplate;
import com.dfsek.terra.addons.feature.distributor.config.PointSetDistributorTemplate;
import com.dfsek.terra.addons.feature.distributor.config.YesDistributorTemplate;
import com.dfsek.terra.addons.feature.distributor.util.Point;
import com.dfsek.terra.addons.feature.distributor.util.PointTemplate;
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
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;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.structure.feature.Distributor;
import com.dfsek.terra.api.util.reflection.TypeKey;
public class DistributorAddon implements AddonInitializer {
public static final TypeKey<Supplier<ObjectTemplate<Distributor>>> DISTRIBUTOR_TOKEN = new TypeKey<>() {
};
@Inject
private Platform platform;
@Inject
private BaseAddon addon;
@Override
public void initialize() {
platform.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.then(event -> {
CheckedRegistry<Supplier<ObjectTemplate<Distributor>>> distributorRegistry = event.getPack().getOrCreateRegistry(
DISTRIBUTOR_TOKEN);
distributorRegistry.register("NOISE", NoiseDistributorTemplate::new);
distributorRegistry.register("POINTS", PointSetDistributorTemplate::new);
distributorRegistry.register("AND", AndDistributorTemplate::new);
distributorRegistry.register("OR", OrDistributorTemplate::new);
distributorRegistry.register("YES", YesDistributorTemplate::new);
distributorRegistry.register("NO", NoiseDistributorTemplate::new);
event.getPack()
.applyLoader(Point.class, PointTemplate::new);
})
.failThrough();
}
}

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.feature.distributor.config;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.config.ValidatedConfigTemplate;
import com.dfsek.tectonic.exception.ValidationException;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.util.List;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.structure.feature.Distributor;
public class AndDistributorTemplate implements ObjectTemplate<Distributor>, ValidatedConfigTemplate {
@Value("distributors")
private @Meta List<@Meta Distributor> distributors;
@Override
public Distributor get() {
Distributor current = distributors.remove(0);
while(!distributors.isEmpty()) {
current = current.and(distributors.remove(0));
}
return current;
}
@Override
public boolean validate() throws ValidationException {
if(distributors.isEmpty()) throw new ValidationException("AND Distributor must specify at least 1 distributor.");
return true;
}
}

View File

@@ -0,0 +1,20 @@
/*
* 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.feature.distributor.config;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import com.dfsek.terra.api.structure.feature.Distributor;
public class NoDistributorTemplate implements ObjectTemplate<Distributor> {
@Override
public Distributor get() {
return Distributor.no();
}
}

View File

@@ -0,0 +1,32 @@
/*
* 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.feature.distributor.config;
import com.dfsek.tectonic.annotations.Default;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import com.dfsek.terra.addons.feature.distributor.distributors.NoiseDistributor;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.structure.feature.Distributor;
@SuppressWarnings("FieldMayBeFinal")
public class NoiseDistributorTemplate implements ObjectTemplate<Distributor> {
@Value("threshold")
@Default
private @Meta double threshold = 0;
@Value("distribution")
private @Meta NoiseSampler noise;
@Override
public Distributor get() {
return new NoiseDistributor(noise, threshold);
}
}

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.feature.distributor.config;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.config.ValidatedConfigTemplate;
import com.dfsek.tectonic.exception.ValidationException;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.util.List;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.structure.feature.Distributor;
public class OrDistributorTemplate implements ObjectTemplate<Distributor>, ValidatedConfigTemplate {
@Value("distributors")
private @Meta List<@Meta Distributor> distributors;
@Override
public Distributor get() {
Distributor current = distributors.remove(0);
while(!distributors.isEmpty()) {
current = current.or(distributors.remove(0));
}
return current;
}
@Override
public boolean validate() throws ValidationException {
if(distributors.isEmpty()) throw new ValidationException("AND Distributor must specify at least 1 distributor.");
return true;
}
}

View File

@@ -0,0 +1,29 @@
/*
* 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.feature.distributor.config;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.util.Set;
import com.dfsek.terra.addons.feature.distributor.distributors.PointSetDistributor;
import com.dfsek.terra.addons.feature.distributor.util.Point;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.structure.feature.Distributor;
public class PointSetDistributorTemplate implements ObjectTemplate<Distributor> {
@Value("points")
private @Meta Set<@Meta Point> points;
@Override
public Distributor get() {
return new PointSetDistributor(points);
}
}

View File

@@ -0,0 +1,20 @@
/*
* 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.feature.distributor.config;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import com.dfsek.terra.api.structure.feature.Distributor;
public class YesDistributorTemplate implements ObjectTemplate<Distributor> {
@Override
public Distributor get() {
return Distributor.yes();
}
}

View File

@@ -0,0 +1,28 @@
/*
* 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.feature.distributor.distributors;
import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.structure.feature.Distributor;
public class NoiseDistributor implements Distributor {
private final NoiseSampler sampler;
private final double threshold;
public NoiseDistributor(NoiseSampler sampler, double threshold) {
this.sampler = sampler;
this.threshold = threshold;
}
@Override
public boolean matches(int x, int z, long seed) {
return sampler.getNoiseSeeded(seed, x, z) > threshold;
}
}

View File

@@ -0,0 +1,27 @@
/*
* 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.feature.distributor.distributors;
import java.util.Set;
import com.dfsek.terra.addons.feature.distributor.util.Point;
import com.dfsek.terra.api.structure.feature.Distributor;
public class PointSetDistributor implements Distributor {
private final Set<Point> points;
public PointSetDistributor(Set<Point> points) {
this.points = points;
}
@Override
public boolean matches(int x, int z, long seed) {
return points.contains(new Point(x, z));
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.feature.distributor.util;
public class Point {
private final int x;
private final int z;
private final int hash;
public Point(int x, int z) {
this.x = x;
this.z = z;
this.hash = 31 * x + z;
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
@Override
public int hashCode() {
return hash;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Point)) return false;
Point that = (Point) obj;
return this.x == that.x && this.z == that.z;
}
}

View File

@@ -0,0 +1,27 @@
/*
* 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.feature.distributor.util;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import com.dfsek.terra.api.config.meta.Meta;
public class PointTemplate implements ObjectTemplate<Point> {
@Value("x")
private @Meta int x;
@Value("z")
private @Meta int z;
@Override
public Point get() {
return new Point(x, z);
}
}

View File

@@ -0,0 +1,15 @@
schema-version: 1
contributors:
- Terra contributors
id: config-distributors
version: 0.1.0
entrypoints:
- "com.dfsek.terra.addons.feature.distributor.DistributorAddon"
website:
issues: https://github.com/PolyhedralDev/Terra-config-distributors/issues
source: https://github.com/PolyhedralDev/Terra-config-distributors
docs: https://github.com/PolyhedralDev/Terra/wiki
license: GNU LGPL v3.0
depends:
config-feature: "0.1.+"
generation-stage-feature: "0.1.+"