Skip to content

Commit

Permalink
🐛 修复可能存在的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Dec 19, 2024
1 parent c707b6f commit 52fa33e
Show file tree
Hide file tree
Showing 17 changed files with 675 additions and 2 deletions.
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 common/src/main/java/dev/anvilcraft/lib/recipe/IRecipeOutcome.java
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);
}
}
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 common/src/main/java/dev/anvilcraft/lib/recipe/IRecipeTrigger.java
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 common/src/main/java/dev/anvilcraft/lib/recipe/InWorldRecipe.java
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
}
}
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;
}
}
}
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() {

}
}
Loading

0 comments on commit 52fa33e

Please sign in to comment.