Skip to content

Commit

Permalink
- remove redundant thing
Browse files Browse the repository at this point in the history
- tweaks to feature injectors
  • Loading branch information
dags- committed Jun 26, 2020
1 parent a3943cc commit 6e6eb97
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/terraforged/fm/FeatureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.terraforged.fm.modifier.FeatureModifiers;
import com.terraforged.fm.modifier.ModifierSet;
import com.terraforged.fm.template.TemplateManager;
import net.minecraft.world.IWorld;
import com.terraforged.fm.transformer.InjectionPosition;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.feature.ConfiguredFeature;
Expand Down Expand Up @@ -84,7 +84,7 @@ public static void clearData() {
TemplateManager.getInstance().clear();
}

public static FeatureManager create(IWorld world, FeatureModifiers modifiers) {
public static FeatureManager create(FeatureModifiers modifiers) {
LOG.debug(INIT, "Initializing FeatureManager");
int predicates = modifiers.getPredicates().size();
int replacers = modifiers.getReplacers().size();
Expand All @@ -108,7 +108,8 @@ public static FeatureManager create(IWorld world, FeatureModifiers modifiers) {
private static BiomeFeatures compute(Biome biome, FeatureModifiers modifiers) {
BiomeFeatures.Builder builder = BiomeFeatures.builder();
for (GenerationStage.Decoration stage : GenerationStage.Decoration.values()) {
builder.add(stage, modifiers.getPrependers(stage, biome));
// add 'prepend' injectors to the head of the feature list
builder.add(stage, modifiers.getAppenders(stage, biome, InjectionPosition.HEAD));

for (ConfiguredFeature<?, ?> feature : biome.getFeatures(stage)) {
ModifierSet modifierSet = modifiers.getFeature(stage, biome, feature);
Expand All @@ -117,7 +118,8 @@ private static BiomeFeatures compute(Biome biome, FeatureModifiers modifiers) {
builder.add(stage, modifierSet.after);
}

builder.add(stage, modifiers.getPrependers(stage, biome));
// add 'append' injectors to the tail of the feature list
builder.add(stage, modifiers.getAppenders(stage, biome, InjectionPosition.TAIL));
}
return builder.build();
}
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/com/terraforged/fm/modifier/FeatureModifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.terraforged.fm.transformer.FeatureInjector;
import com.terraforged.fm.transformer.FeatureReplacer;
import com.terraforged.fm.transformer.FeatureTransformer;
import com.terraforged.fm.transformer.InjectionPosition;
import com.terraforged.fm.util.FeatureDebugger;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.GenerationStage;
Expand Down Expand Up @@ -86,12 +87,8 @@ public void sort() {
transformers.sort();
}

public List<BiomeFeature> getPrependers(GenerationStage.Decoration stage, Biome biome) {
return getAppenders(stage, FeatureInjector.Type.BEFORE, biome);
}

public List<BiomeFeature> getAppenders(GenerationStage.Decoration stage, Biome biome) {
return getAppenders(stage, FeatureInjector.Type.AFTER, biome);
public List<BiomeFeature> getAppenders(GenerationStage.Decoration stage, Biome biome, InjectionPosition position) {
return getAppenders(stage, position, biome);
}

public ModifierSet getFeature(GenerationStage.Decoration stage, Biome biome, ConfiguredFeature<?, ?> feature) {
Expand All @@ -108,8 +105,8 @@ public ModifierSet getFeature(GenerationStage.Decoration stage, Biome biome, Con
predicate = getPredicate(biome, element);
}

List<BiomeFeature> before = getInjectors(biome, predicate, element, FeatureInjector.Type.BEFORE);
List<BiomeFeature> after = getInjectors(biome, predicate, element, FeatureInjector.Type.AFTER);
List<BiomeFeature> before = getInjectors(biome, predicate, element, InjectionPosition.BEFORE);
List<BiomeFeature> after = getInjectors(biome, predicate, element, InjectionPosition.AFTER);

return new ModifierSet(new BiomeFeature(predicate, result), before, after);
} catch (Throwable t) {
Expand Down Expand Up @@ -155,10 +152,10 @@ public ModifierSet getFeature(GenerationStage.Decoration stage, Biome biome, Con
}
}

private List<BiomeFeature> getInjectors(Biome biome, FeaturePredicate predicate, JsonElement element, FeatureInjector.Type type) {
private List<BiomeFeature> getInjectors(Biome biome, FeaturePredicate predicate, JsonElement element, InjectionPosition type) {
List<BiomeFeature> result = Collections.emptyList();
for (Modifier<FeatureInjector> modifier : getInjectors()) {
if (modifier.getModifier().getType() != type) {
if (modifier.getModifier().getPosition() != type) {
continue;
}
if (modifier.getMatcher().test(biome, element)) {
Expand All @@ -171,10 +168,10 @@ private List<BiomeFeature> getInjectors(Biome biome, FeaturePredicate predicate,
return result;
}

private List<BiomeFeature> getAppenders(GenerationStage.Decoration stage, FeatureInjector.Type type, Biome biome) {
private List<BiomeFeature> getAppenders(GenerationStage.Decoration stage, InjectionPosition position, Biome biome) {
List<BiomeFeature> result = Collections.emptyList();
for (Modifier<FeatureAppender> modifier : getAppenders()) {
if (modifier.getModifier().getType() != type) {
if (modifier.getModifier().getPosition() != position) {
continue;
}
if (modifier.getModifier().getStage() != stage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class FeatureAppender extends FeatureInjector {

private final GenerationStage.Decoration stage;

public FeatureAppender(ConfiguredFeature<?, ?> feature, Type type, GenerationStage.Decoration stage) {
public FeatureAppender(ConfiguredFeature<?, ?> feature, InjectionPosition type, GenerationStage.Decoration stage) {
super(feature, type);
this.stage = stage;
}
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/terraforged/fm/transformer/FeatureInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@

public class FeatureInjector {

private final Type type;
private final InjectionPosition position;
private final ConfiguredFeature<?, ?> feature;

public FeatureInjector(ConfiguredFeature<?, ?> feature, Type type) {
public FeatureInjector(ConfiguredFeature<?, ?> feature, InjectionPosition position) {
this.feature = feature;
this.type = type;
this.position = position;
}

public ConfiguredFeature<?, ?> getFeature() {
return feature;
}

public Type getType() {
return type;
}

public enum Type {
BEFORE,
AFTER,
public InjectionPosition getPosition() {
return position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public static Optional<FeatureReplacer> parseReplacer(JsonObject root) {
public static Optional<FeatureInjector> parseInjector(JsonObject root) {
if (root.has("before")) {
return FeatureSerializer.deserialize(root.get("before"))
.map(feature -> new FeatureInjector(feature, FeatureInjector.Type.BEFORE));
.map(feature -> new FeatureInjector(feature, InjectionPosition.BEFORE));
}
if (root.has("after")) {
return FeatureSerializer.deserialize(root.get("after"))
.map(feature -> new FeatureInjector(feature, FeatureInjector.Type.AFTER));
.map(feature -> new FeatureInjector(feature, InjectionPosition.AFTER));
}
return Optional.empty();
}
Expand All @@ -60,11 +60,11 @@ public static Optional<FeatureAppender> parseAppender(JsonObject root) {
GenerationStage.Decoration stage = GenerationStage.Decoration.valueOf(root.get("stage").getAsString());
if (root.has("prepend")) {
return FeatureSerializer.deserialize(root.get("prepend"))
.map(feature -> new FeatureAppender(feature, FeatureInjector.Type.BEFORE, stage));
.map(feature -> new FeatureAppender(feature, InjectionPosition.HEAD, stage));
}
if (root.has("append")) {
return FeatureSerializer.deserialize(root.get("append"))
.map(feature -> new FeatureAppender(feature, FeatureInjector.Type.AFTER, stage));
.map(feature -> new FeatureAppender(feature, InjectionPosition.TAIL, stage));
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.terraforged.fm.transformer;

public enum InjectionPosition {
BEFORE,
AFTER,
HEAD,
TAIL
;

public static InjectionPosition parse(String name) {
return InjectionPosition.valueOf(name.toUpperCase());
}
}

0 comments on commit 6e6eb97

Please sign in to comment.