Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework how boilerplate loader predicates are registered
Instead of having 3 extra fields in the class, they are all registered automatically in PredicateLoader. In addition, we now have RegistrySetLoader and FallbackLoader to handle other common predicate elements Static methods were added to the interface to construct the common type as its less clear the loader can handle that Semantic change: all boilerplate loader types are registered under mantle, this makes the fallback registry work better.
- Loading branch information
1 parent
dc252ce
commit bc76247
Showing
20 changed files
with
474 additions
and
497 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/main/java/slimeknights/mantle/data/predicate/AndJsonPredicate.java
This file was deleted.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
src/main/java/slimeknights/mantle/data/predicate/FallbackPredicateRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package slimeknights.mantle.data.predicate; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
import lombok.RequiredArgsConstructor; | ||
import net.minecraft.resources.ResourceLocation; | ||
import slimeknights.mantle.Mantle; | ||
import slimeknights.mantle.data.loadable.record.RecordLoadable; | ||
import slimeknights.mantle.util.JsonHelper; | ||
|
||
import java.util.function.Function; | ||
|
||
/** Predicate registry that upon failure to find a predicate type will fallback to the fallback type */ | ||
public class FallbackPredicateRegistry<T,F> extends PredicateRegistry<T> { | ||
private final Function<T,F> getter; | ||
private final PredicateRegistry<F> fallback; | ||
private final RecordLoadable<FallbackPredicate> fallbackLoader; | ||
|
||
/** | ||
* Creates a new instance | ||
* @param defaultInstance Default instance, typically expected to be an any predicate. | ||
*/ | ||
public FallbackPredicateRegistry(IJsonPredicate<T> defaultInstance, PredicateRegistry<F> fallback, Function<T,F> getter, String fallbackName) { | ||
super(defaultInstance); | ||
this.fallback = fallback; | ||
this.getter = getter; | ||
this.fallbackLoader = RecordLoadable.create(fallback.directField(fallbackName + "_type", p -> p.predicate), FallbackPredicate::new); | ||
this.register(Mantle.getResource(fallbackName), fallbackLoader); | ||
} | ||
|
||
/** Creates a fallback predicate instance */ | ||
public IJsonPredicate<T> fallback(IJsonPredicate<F> predicate) { | ||
return new FallbackPredicate(predicate); | ||
} | ||
|
||
@Override | ||
public IJsonPredicate<T> convert(JsonElement element, String key) throws JsonSyntaxException { | ||
if (defaultInstance != null && element.isJsonNull()) { | ||
return defaultInstance; | ||
} | ||
// identify type key, and the object we will load from | ||
JsonObject object; | ||
ResourceLocation type; | ||
if (element.isJsonObject()) { | ||
object = element.getAsJsonObject(); | ||
type = JsonHelper.getResourceLocation(object, "type"); | ||
} else if (compact && element.isJsonPrimitive()) { | ||
EMPTY_OBJECT.entrySet().clear(); | ||
object = EMPTY_OBJECT; | ||
type = JsonHelper.convertToResourceLocation(object, "type"); | ||
} else { | ||
throw new JsonSyntaxException("Invalid JSON for " + getClass().getSimpleName() + " at " + key + ", must be a JSON object" + (compact ? " or a string" : "")); | ||
} | ||
// see if we have a primary loader, if so parse that | ||
IGenericLoader<? extends IJsonPredicate<T>> loader = loaders.getValue(type); | ||
if (loader != null) { | ||
return loader.deserialize(object); | ||
} | ||
// primary loader failed, try a fallback loader | ||
return new FallbackPredicate(this.fallback.convert(element, key)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public JsonElement serialize(IJsonPredicate<T> src) { | ||
// write the fallback directly to JSON instead of as a nested type | ||
if (src instanceof NestedPredicate<?>) { | ||
return this.fallback.serialize(((NestedPredicate<F>)src).predicate()); | ||
} | ||
return super.serialize(src); | ||
} | ||
|
||
private interface NestedPredicate<F> { | ||
IJsonPredicate<F> predicate(); | ||
} | ||
|
||
/** Predicate matching another predicate type */ | ||
@RequiredArgsConstructor | ||
public class FallbackPredicate implements IJsonPredicate<T>, NestedPredicate<F> { | ||
private final IJsonPredicate<F> predicate; | ||
|
||
@Override | ||
public IJsonPredicate<F> predicate() { | ||
return predicate; | ||
} | ||
|
||
@Override | ||
public boolean matches(T input) { | ||
return predicate.matches(getter.apply(input)); | ||
} | ||
|
||
@Override | ||
public IJsonPredicate<T> inverted() { | ||
return invert(this); | ||
} | ||
|
||
@Override | ||
public IGenericLoader<? extends IJsonPredicate<T>> getLoader() { | ||
return fallbackLoader; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 0 additions & 76 deletions
76
src/main/java/slimeknights/mantle/data/predicate/InvertedJsonPredicate.java
This file was deleted.
Oops, something went wrong.
70 changes: 0 additions & 70 deletions
70
src/main/java/slimeknights/mantle/data/predicate/NestedJsonPredicateLoader.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.