Skip to content

Commit

Permalink
Add Missing Rules
Browse files Browse the repository at this point in the history
Initialize JitPack
  • Loading branch information
FirstMegaGame4 committed Dec 18, 2023
1 parent d0e9b8e commit 1ded851
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 40 deletions.
6 changes: 6 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# From https://github.com/jitpack/jitpack.io/issues/4506#issuecomment-864562270
before_install:
- source "$HOME/.sdkman/bin/sdkman-init.sh"
- sdk update
- sdk install java 17.0.1-tem
- sdk use java 17.0.1-tem
2 changes: 1 addition & 1 deletion src/main/java/fr/firstmegagame4/env/json/api/EnvJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ static EnvJson parse(Path path) {

List<EnvJsonMember> members();

Identifier apply(EnvJsonVisitor source);
Identifier apply(EnvJsonVisitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface EnvJsonMember {
Identifier result();

@Nullable
Identifier apply(EnvJsonVisitor source);
Identifier apply(EnvJsonVisitor visitor);
}
19 changes: 17 additions & 2 deletions src/main/java/fr/firstmegagame4/env/json/api/EnvJsonVisitor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fr.firstmegagame4.env.json.api;

import fr.firstmegagame4.env.json.api.rule.SkyEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.VoidEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.WaterEnvJsonRule;
import it.unimi.dsi.fastutil.ints.Int2BooleanFunction;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;

Expand All @@ -16,5 +19,17 @@ public interface EnvJsonVisitor {

boolean applyBiomeTag(TagKey<Biome> biomeTag);

Identifier apply(EnvJsonMember member);
boolean applyXCoord(Int2BooleanFunction operation);

boolean applyYCoord(Int2BooleanFunction operation);

boolean applyZCoord(Int2BooleanFunction operation);

boolean applySubmerged(boolean submerged);

boolean applySky(SkyEnvJsonRule.Localization localization);

boolean applyWater(WaterEnvJsonRule.Localization localization);

boolean applyVoid(VoidEnvJsonRule.Localization localization);
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package fr.firstmegagame4.env.json.api.rule;

import net.minecraft.util.StringIdentifiable;
import fr.firstmegagame4.env.json.impl.rule.CoordEnvJsonRuleImpl;

import java.util.function.BiFunction;

public interface CoordEnvJsonRule extends EnvJsonRule {

Coord coord();

Comparator comparator();

int value();

enum Coord {
X,
Y,
Z
}

enum Comparator implements StringIdentifiable {
INFERIOR("<"),
INFERIOR_FLAT_EQUALS("<="),
FLAT_EQUALS("=="),
SUPERIOR_FLAT_EQUALS(">="),
SUPERIOR(">");
enum Comparator {
INFERIOR((provided, constant) -> provided < constant, "<"),
INFERIOR_FLAT_EQUALS((provided, constant) -> provided <= constant, "<=", "=<"),
FLAT_EQUALS(Integer::equals, "=="),
SUPERIOR_FLAT_EQUALS((provided, constant) -> provided >= constant, ">=", "=>"),
SUPERIOR((provided, constant) -> provided > constant, ">");

private final BiFunction<Integer, Integer, Boolean> operation;
private final String[] representations;

private final String representation;
Comparator(BiFunction<Integer, Integer, Boolean> operation, String... representations) {
this.operation = operation;
this.representations = representations;
}

public static Comparator fromString(String string) {
return CoordEnvJsonRuleImpl.comparatorFromString(string);
}

Comparator(String representation) {
this.representation = representation;
public BiFunction<Integer, Integer, Boolean> operation() {
return this.operation;
}

@Override
public String asString() {
return this.representation;
public String[] representations() {
return this.representations;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static <T extends EnvJsonRule> T safeCast(EnvJsonRule rule, Class<T> type) {

Type type();

boolean apply(EnvJsonVisitor source);
boolean apply(EnvJsonVisitor visitor);

enum Type {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
public record EnvJsonImpl(List<EnvJsonMember> members) implements EnvJson {

@Override
public Identifier apply(EnvJsonVisitor source) {
public Identifier apply(EnvJsonVisitor visitor) {
for (EnvJsonMember member : this.members) {
Identifier result = member.apply(source);
Identifier result = member.apply(visitor);
if (result != null) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class EnvJsonInitializer implements ModInitializer {

@Override
public void onInitialize() {
LOGGER.info("EnvJson Loaded");
LOGGER.info("EnvJson Library Loaded");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public record EnvJsonMemberImpl(List<EnvJsonRule> rules, Identifier result) impl

@Override
@Nullable
public Identifier apply(EnvJsonVisitor source) {
public Identifier apply(EnvJsonVisitor visitor) {
for (EnvJsonRule rule : this.rules) {
if (rule.apply(source)) {
if (rule.apply(visitor)) {
return this.result;
}
}
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/fr/firstmegagame4/env/json/impl/EnvJsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ private static BiomeEnvJsonRule parseBiomeRule(String string) {
}
}

private static CoordEnvJsonRule parseCoordRule(CoordEnvJsonRule.Coord coord, JsonObject json) {
CoordEnvJsonRule.Comparator comparator = CoordEnvJsonRule.Comparator.fromString(json.get("operator").getAsString());
int value = json.get("value").getAsInt();
return new CoordEnvJsonRuleImpl(coord, comparator, value);
}

private static SubmergedEnvJsonRule parseSubmergedRule(boolean bool) {
return new SubmergedEnvJsonRuleImpl(bool);
}

private static SkyEnvJsonRule parseSkyRule(String string) {
return new SkyEnvJsonRuleImpl(SkyEnvJsonRule.Localization.valueOf(string.toUpperCase()));
}

private static WaterEnvJsonRule parseWaterRule(String string) {
return new WaterEnvJsonRuleImpl(WaterEnvJsonRule.Localization.valueOf(string.toUpperCase()));
}

private static VoidEnvJsonRule parseVoidRule(String string) {
return new VoidEnvJsonRuleImpl(VoidEnvJsonRule.Localization.valueOf(string.toUpperCase()));
}

private static List<EnvJsonRule> parseRules(JsonObject json) {
List<EnvJsonRule> rules = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
Expand All @@ -74,13 +96,13 @@ private static List<EnvJsonRule> parseRules(JsonObject json) {
case ANY -> EnvJsonParser.parseAnyRule(entry.getValue().getAsJsonObject());
case DIMENSION -> EnvJsonParser.parseDimensionRule(entry.getValue().getAsString());
case BIOME -> EnvJsonParser.parseBiomeRule(entry.getValue().getAsString());
case X_COORD -> null;
case Y_COORD -> null;
case Z_COORD -> null;
case SUBMERGED -> null;
case SKY -> null;
case WATER -> null;
case VOID -> null;
case X_COORD -> EnvJsonParser.parseCoordRule(CoordEnvJsonRule.Coord.X, entry.getValue().getAsJsonObject());
case Y_COORD -> EnvJsonParser.parseCoordRule(CoordEnvJsonRule.Coord.Y, entry.getValue().getAsJsonObject());
case Z_COORD -> EnvJsonParser.parseCoordRule(CoordEnvJsonRule.Coord.Z, entry.getValue().getAsJsonObject());
case SUBMERGED -> EnvJsonParser.parseSubmergedRule(entry.getValue().getAsBoolean());
case SKY -> EnvJsonParser.parseSkyRule(entry.getValue().getAsString());
case WATER -> EnvJsonParser.parseWaterRule(entry.getValue().getAsString());
case VOID -> EnvJsonParser.parseVoidRule(entry.getValue().getAsString());
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.AnyEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.EnvJsonRule;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;

@ApiStatus.Internal
public class AnyEnvJsonRuleImpl extends EnvJsonRuleImpl implements AnyEnvJsonRule {

private final List<EnvJsonRule> rules;
Expand All @@ -21,9 +23,9 @@ public List<EnvJsonRule> rules() {
}

@Override
public boolean apply(EnvJsonVisitor source) {
public boolean apply(EnvJsonVisitor visitor) {
for (EnvJsonRule rule : this.rules) {
if (rule.apply(source)) {
if (rule.apply(visitor)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.world.biome.Biome;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class BiomeEnvJsonRuleImpl extends EnvJsonRuleImpl implements BiomeEnvJsonRule {

private final RegistryKey<Biome> biome;
Expand Down Expand Up @@ -34,12 +36,12 @@ public TagKey<Biome> tag() {
}

@Override
public boolean apply(EnvJsonVisitor source) {
public boolean apply(EnvJsonVisitor visitor) {
if (this.biome != null) {
return source.applyBiomeKey(this.biome);
return visitor.applyBiomeKey(this.biome);
}
else if (this.tag != null) {
return source.applyBiomeTag(this.tag);
return visitor.applyBiomeTag(this.tag);
}
else {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fr.firstmegagame4.env.json.impl.rule;

import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.CoordEnvJsonRule;
import org.jetbrains.annotations.ApiStatus;

import java.util.Arrays;

@ApiStatus.Internal
public class CoordEnvJsonRuleImpl extends EnvJsonRuleImpl implements CoordEnvJsonRule {

private final Coord coord;
private final Comparator comparator;
private final int value;

public CoordEnvJsonRuleImpl(Coord coord, Comparator comparator, int value) {
super(
switch (coord) {
case X -> Type.X_COORD;
case Y -> Type.Y_COORD;
case Z -> Type.Z_COORD;
}
);
this.coord = coord;
this.comparator = comparator;
this.value = value;
}

public static Comparator comparatorFromString(String string) {
for (Comparator comparator : Comparator.values()) {
if (Arrays.stream(comparator.representations()).toList().contains(string)) {
return comparator;
}
}
return null;
}

@Override
public Coord coord() {
return this.coord;
}

@Override
public Comparator comparator() {
return this.comparator;
}

@Override
public int value() {
return this.value;
}

@Override
public boolean apply(EnvJsonVisitor visitor) {
return switch (this.coord) {
case X -> visitor.applyXCoord(i -> this.comparator.operation().apply(i, this.value));
case Y -> visitor.applyYCoord(i -> this.comparator.operation().apply(i, this.value));
case Z -> visitor.applyZCoord(i -> this.comparator.operation().apply(i, this.value));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.world.World;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class DimensionEnvJsonRuleImpl extends EnvJsonRuleImpl implements DimensionEnvJsonRule {

private final RegistryKey<World> dimension;
Expand Down Expand Up @@ -34,12 +36,12 @@ public TagKey<World> tag() {
}

@Override
public boolean apply(EnvJsonVisitor source) {
public boolean apply(EnvJsonVisitor visitor) {
if (this.dimension != null) {
return source.applyDimensionKey(this.dimension);
return visitor.applyDimensionKey(this.dimension);
}
else if (this.tag != null) {
return source.applyDimensionTag(this.tag);
return visitor.applyDimensionTag(this.tag);
}
else {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.firstmegagame4.env.json.impl.rule;

import fr.firstmegagame4.env.json.api.rule.EnvJsonRule;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public abstract class EnvJsonRuleImpl implements EnvJsonRule {

private final Type type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.EnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.SequenceEnvJsonRule;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;

@ApiStatus.Internal
public class SequenceEnvJsonRuleImpl extends EnvJsonRuleImpl implements SequenceEnvJsonRule {

private final List<EnvJsonRule> rules;
Expand All @@ -21,9 +23,9 @@ public List<EnvJsonRule> rules() {
}

@Override
public boolean apply(EnvJsonVisitor source) {
public boolean apply(EnvJsonVisitor visitor) {
for (EnvJsonRule rule : this.rules) {
if (!rule.apply(source)) {
if (!rule.apply(visitor)) {
return false;
}
}
Expand Down
Loading

0 comments on commit 1ded851

Please sign in to comment.