generated from Anvil-Dev/ArchitecturyExampleMod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
675 additions
and
2 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
common/src/main/java/dev/anvilcraft/lib/recipe/IRecipeCondition.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,7 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import dev.anvilcraft.lib.util.JsonSerializable; | ||
|
||
public interface IRecipeCondition extends JsonSerializable { | ||
boolean matches(InWorldRecipeContext context); | ||
} |
22 changes: 22 additions & 0 deletions
22
common/src/main/java/dev/anvilcraft/lib/recipe/IRecipeOutcome.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,22 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import dev.anvilcraft.lib.util.JsonSerializable; | ||
import org.jetbrains.annotations.Unmodifiable; | ||
|
||
import java.util.List; | ||
|
||
public interface IRecipeOutcome extends JsonSerializable { | ||
default @Unmodifiable List<IRecipeCondition> getConditions() { | ||
return List.of(); | ||
} | ||
|
||
boolean process(InWorldRecipeContext context); | ||
|
||
@SuppressWarnings("UnusedReturnValue") | ||
default boolean processWithConditions(InWorldRecipeContext context) { | ||
for (IRecipeCondition condition : this.getConditions()) { | ||
if (!condition.matches(context)) return false; | ||
} | ||
return this.process(context); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
common/src/main/java/dev/anvilcraft/lib/recipe/IRecipePredicate.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,10 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import dev.anvilcraft.lib.util.JsonSerializable; | ||
|
||
public interface IRecipePredicate extends JsonSerializable { | ||
boolean matches(InWorldRecipeContext context); | ||
|
||
@SuppressWarnings("UnusedReturnValue") | ||
boolean process(InWorldRecipeContext context); | ||
} |
15 changes: 15 additions & 0 deletions
15
common/src/main/java/dev/anvilcraft/lib/recipe/IRecipeTrigger.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,15 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import dev.anvilcraft.lib.util.JsonSerializable; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public interface IRecipeTrigger extends JsonSerializable { | ||
ResourceLocation getId(); | ||
|
||
@Override | ||
default JsonElement toJson() { | ||
return new JsonPrimitive(this.getId().toString()); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
common/src/main/java/dev/anvilcraft/lib/recipe/InWorldRecipe.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,101 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import lombok.Getter; | ||
import net.minecraft.core.RegistryAccess; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.Recipe; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
import net.minecraft.world.item.crafting.RecipeType; | ||
import net.minecraft.world.level.Level; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Unmodifiable; | ||
|
||
import java.util.List; | ||
|
||
public class InWorldRecipe implements Recipe<InWorldRecipeContext> { | ||
private final ResourceLocation id; | ||
private final ItemStack icon; | ||
private final ResourceLocation type; | ||
@Getter | ||
private final IRecipeTrigger trigger; | ||
private final @Unmodifiable List<IRecipePredicate> predicates; | ||
private final @Unmodifiable List<IRecipeOutcome> outcomes; | ||
@Getter | ||
private final int maxCraftCount; | ||
|
||
public InWorldRecipe( | ||
ResourceLocation id, | ||
ItemStack icon, | ||
ResourceLocation type, | ||
IRecipeTrigger trigger, | ||
List<IRecipePredicate> predicates, | ||
List<IRecipeOutcome> outcomes, | ||
int maxCraftCount | ||
) { | ||
this.trigger = trigger; | ||
this.id = id; | ||
this.icon = icon; | ||
this.type = type; | ||
this.predicates = predicates; | ||
this.outcomes = outcomes; | ||
this.maxCraftCount = maxCraftCount; | ||
} | ||
|
||
@Override | ||
public boolean matches(@NotNull InWorldRecipeContext context, @NotNull Level level) { | ||
for (IRecipePredicate predicate : this.predicates) { | ||
if (!predicate.matches(context)) return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean craft(@NotNull InWorldRecipeContext context) { | ||
if (!this.matches(context, context.level())) return false; | ||
for (IRecipePredicate predicate : this.predicates) { | ||
predicate.process(context); | ||
} | ||
for (IRecipeOutcome outcome : this.outcomes) { | ||
outcome.processWithConditions(context); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public @NotNull ItemStack assemble(@NotNull InWorldRecipeContext context, @NotNull RegistryAccess registryAccess) { | ||
return ItemStack.EMPTY; | ||
} | ||
|
||
@Override | ||
public boolean canCraftInDimensions(int width, int height) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public @NotNull ItemStack getResultItem(@NotNull RegistryAccess registryAccess) { | ||
return this.icon; | ||
} | ||
|
||
@Override | ||
public @NotNull ResourceLocation getId() { | ||
return this.id; | ||
} | ||
|
||
public ResourceLocation getRecipeType() { | ||
return this.type; | ||
} | ||
|
||
@Override | ||
public @NotNull RecipeSerializer<?> getSerializer() { | ||
return InWorldRecipeSerializer.INSTANCE; | ||
} | ||
|
||
@Override | ||
public @NotNull RecipeType<?> getType() { | ||
return InWorldRecipe.Type.INSTANCE; | ||
} | ||
|
||
public enum Type implements RecipeType<InWorldRecipe> { | ||
INSTANCE | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
common/src/main/java/dev/anvilcraft/lib/recipe/InWorldRecipeBuilder.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,164 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import com.google.gson.JsonObject; | ||
import dev.anvilcraft.lib.util.JsonSerializable; | ||
import lombok.Getter; | ||
import net.minecraft.advancements.Advancement; | ||
import net.minecraft.advancements.CriterionTriggerInstance; | ||
import net.minecraft.data.recipes.FinishedRecipe; | ||
import net.minecraft.data.recipes.RecipeBuilder; | ||
import net.minecraft.data.recipes.RecipeCategory; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
@SuppressWarnings("unused") | ||
public class InWorldRecipeBuilder implements RecipeBuilder { | ||
@Getter | ||
private final RecipeCategory category; | ||
@Nullable | ||
private String group = null; | ||
private ResourceLocation id; | ||
private ItemStack icon; | ||
private ResourceLocation type; | ||
private int maxCraftCount = 64; | ||
@Getter | ||
private IRecipeTrigger trigger; | ||
private final List<IRecipePredicate> predicates = new ArrayList<>(); | ||
private final List<IRecipeOutcome> outcomes = new ArrayList<>(); | ||
@Getter | ||
private final Advancement.Builder advancement = Advancement.Builder.recipeAdvancement(); | ||
|
||
private InWorldRecipeBuilder(RecipeCategory category) { | ||
this.category = category; | ||
} | ||
|
||
public static @NotNull InWorldRecipeBuilder of(RecipeCategory category) { | ||
return new InWorldRecipeBuilder(category); | ||
} | ||
|
||
public InWorldRecipeBuilder id(ResourceLocation id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public InWorldRecipeBuilder icon(ItemStack icon) { | ||
this.icon = icon; | ||
return this; | ||
} | ||
|
||
public InWorldRecipeBuilder type(ResourceLocation type) { | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
public InWorldRecipeBuilder trigger(IRecipeTrigger trigger) { | ||
this.trigger = trigger; | ||
return this; | ||
} | ||
|
||
public InWorldRecipeBuilder predicate(IRecipePredicate predicate) { | ||
this.predicates.add(predicate); | ||
return this; | ||
} | ||
|
||
public InWorldRecipeBuilder outcome(IRecipeOutcome outcome) { | ||
this.outcomes.add(outcome); | ||
return this; | ||
} | ||
|
||
public InWorldRecipeBuilder max(int maxCraftCount) { | ||
this.maxCraftCount = maxCraftCount; | ||
return this; | ||
} | ||
|
||
public InWorldRecipe build() { | ||
return new InWorldRecipe(id, icon, type, trigger, predicates, outcomes, maxCraftCount); | ||
} | ||
|
||
@Override | ||
public @NotNull InWorldRecipeBuilder unlockedBy(@NotNull String criterionName, @NotNull CriterionTriggerInstance criterionTrigger) { | ||
this.advancement.addCriterion(criterionName, criterionTrigger); | ||
return this; | ||
} | ||
|
||
@Override | ||
public @NotNull InWorldRecipeBuilder group(@Nullable String groupName) { | ||
this.group = groupName; | ||
return this; | ||
} | ||
|
||
@Override | ||
public @NotNull Item getResult() { | ||
return this.icon.getItem(); | ||
} | ||
|
||
@Override | ||
public void save(@NotNull Consumer<FinishedRecipe> finishedRecipeConsumer, @NotNull ResourceLocation recipeId) { | ||
|
||
} | ||
|
||
static class Result implements FinishedRecipe { | ||
private final String group; | ||
@Getter | ||
private final ResourceLocation id; | ||
private final ItemStack icon; | ||
private final ResourceLocation type; | ||
@Getter | ||
private final IRecipeTrigger trigger; | ||
private final List<IRecipePredicate> predicates; | ||
private final List<IRecipeOutcome> outcomes; | ||
@Getter | ||
private final Advancement.Builder advancement; | ||
@Getter | ||
private final ResourceLocation advancementId; | ||
|
||
Result( | ||
String group, | ||
ResourceLocation id, | ||
ItemStack icon, | ||
ResourceLocation type, | ||
IRecipeTrigger trigger, | ||
List<IRecipePredicate> predicates, | ||
List<IRecipeOutcome> outcomes, | ||
Advancement.Builder advancement, | ||
ResourceLocation advancementId | ||
) { | ||
this.group = group; | ||
this.id = id; | ||
this.icon = icon; | ||
this.type = type; | ||
this.trigger = trigger; | ||
this.predicates = predicates; | ||
this.outcomes = outcomes; | ||
this.advancement = advancement; | ||
this.advancementId = advancementId; | ||
} | ||
|
||
@Override | ||
public void serializeRecipeData(@NotNull JsonObject json) { | ||
json.addProperty("id", id.toString()); | ||
json.add("icon", JsonSerializable.fromItemStack(icon)); | ||
json.add("trigger", trigger.toJson()); | ||
json.add("predicates", JsonSerializable.fromList(predicates)); | ||
json.add("outcomes", JsonSerializable.fromList(outcomes)); | ||
} | ||
|
||
@Override | ||
public @NotNull RecipeSerializer<InWorldRecipe> getType() { | ||
return InWorldRecipeSerializer.INSTANCE; | ||
} | ||
|
||
@Override | ||
public @Nullable JsonObject serializeAdvancement() { | ||
return null; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
common/src/main/java/dev/anvilcraft/lib/recipe/InWorldRecipeContext.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,57 @@ | ||
package dev.anvilcraft.lib.recipe; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.Container; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.Level; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public record InWorldRecipeContext(@NotNull Level level, @NotNull BlockPos pos, @Nullable Entity triggered) implements Container { | ||
@Override | ||
public int getContainerSize() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public @NotNull ItemStack getItem(int slot) { | ||
return ItemStack.EMPTY; | ||
} | ||
|
||
@Override | ||
public @NotNull ItemStack removeItem(int slot, int amount) { | ||
return ItemStack.EMPTY; | ||
} | ||
|
||
@Override | ||
public @NotNull ItemStack removeItemNoUpdate(int slot) { | ||
return ItemStack.EMPTY; | ||
} | ||
|
||
@Override | ||
public void setItem(int slot, @NotNull ItemStack stack) { | ||
|
||
} | ||
|
||
@Override | ||
public void setChanged() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean stillValid(@NotNull Player player) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void clearContent() { | ||
|
||
} | ||
} |
Oops, something went wrong.