Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fallenthereaper/362aaec92216db4008c6d7bd2d2269d2 to your computer and use it in GitHub Desktop.
Save fallenthereaper/362aaec92216db4008c6d7bd2d2269d2 to your computer and use it in GitHub Desktop.
package fallenreaper.fable.core.base.ritual.base.json.factories.condition;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import fallenreaper.fable.core.base.ritual.base.IRitualPattern;
import fallenreaper.fable.core.base.ritual.base.PedestalCoordinate;
import fallenreaper.fable.core.base.ritual.base.RitualComponentBuilder;
import fallenreaper.fable.core.base.ritual.base.components.CrossRitualConditionPattern;
import fallenreaper.fable.core.base.ritual.base.components.SquareRitualConditionPattern;
import fallenreaper.fable.core.base.ritual.base.components.conditions.CollectiveRitualCondition;
import fallenreaper.fable.core.base.ritual.base.components.conditions.IRitualCondition;
import fallenreaper.fable.core.base.ritual.base.json.RitualComponentFactory;
import fallenreaper.fable.core.base.ritual.base.json.RitualParsingException;
import net.minecraft.core.Direction;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class ItemPatternRitualConditionFactory implements RitualComponentFactory<IRitualCondition> {
@Override
public IRitualCondition create(JsonObject parametersJson) throws RitualParsingException {
try {
if (hasPattern(parametersJson)) {
JsonObject patternJson = GsonHelper.getAsJsonObject(parametersJson, "pattern");
IRitualPattern<IRitualCondition> pattern = parsePattern(patternJson);
RitualComponentBuilder<IRitualCondition> builder = RitualComponentBuilder.condition().addPattern(pattern);
return new CollectiveRitualCondition(builder);
} else {
throw new RitualParsingException("Missing pattern in Item Pattern Condition");
}
} catch (Exception e) {
throw new RitualParsingException("Error while creating Item Pattern Condition: " + e.getMessage());
}
}
private PedestalCoordinate parsePedestalCoordinate(JsonObject conditionJson) {
if (hasPedestalCoordinate(conditionJson)) {
int xOffset = conditionJson.get("x_offset").getAsInt();
int zOffset = conditionJson.get("z_offset").getAsInt();
return new PedestalCoordinate(xOffset, zOffset);
} else {
return new PedestalCoordinate(0, 0);
}
}
private boolean hasIngredient(JsonObject conditionJson) {
return conditionJson.has("ingredient");
}
private boolean hasPedestalCoordinate(JsonObject conditionJson) {
return conditionJson.has("x_offset") && conditionJson.has("z_offset");
}
private IRitualPattern<IRitualCondition> parsePattern(JsonObject patternJson) throws RitualParsingException {
String type = GsonHelper.getAsString(patternJson, "type");
int size = GsonHelper.getAsInt(patternJson, "size");
JsonObject ingredientsJson = GsonHelper.getAsJsonObject(patternJson, "ingredients");
final Map<Direction, Object> ingredientMap = parseIngredients(ingredientsJson);
return switch (type) {
case "cross_pattern" -> new CrossRitualConditionPattern().withSize(size).withMultipleIngredients(ingredientMap);
case "square_pattern" -> new SquareRitualConditionPattern().withSize(size).withMultipleIngredients(ingredientMap);
default -> throw new RitualParsingException("Unknown pattern type: " + type);
};
}
private Map<Direction, Object> parseIngredients(JsonObject ingredientsJson) {
Map<Direction, Object> ingredients = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : ingredientsJson.entrySet()) {
String directionName = entry.getKey();
ItemStack ingredient = ItemStack.EMPTY;
Direction direction = Direction.byName(directionName);
if (direction != null) {
JsonElement item = entry.getValue();
ingredient = new ItemStack(parseIngredient(item));
if (ingredient != null) {
ingredients.put(direction, ingredient);
}
}
}
return ingredients;
}
public static Item parseIngredient(JsonElement ingredientJson) {
return GsonHelper.convertToItem(ingredientJson, "item");
}
private boolean hasPattern(JsonObject conditionJson) {
return conditionJson.has("pattern");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment