2021-10-24 18:49:48 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.crafting;
|
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
import com.google.gson.JsonArray;
|
2021-10-24 18:49:48 +02:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2024-03-03 01:20:53 +01:00
|
|
|
import net.minecraft.core.RegistryAccess;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.data.recipes.FinishedRecipe;
|
|
|
|
import net.minecraft.network.FriendlyByteBuf;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.util.GsonHelper;
|
|
|
|
import net.minecraft.world.Container;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.crafting.Ingredient;
|
|
|
|
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.ItemLike;
|
|
|
|
import net.minecraft.world.level.Level;
|
2024-03-03 01:20:53 +01:00
|
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
2021-10-24 18:49:48 +02:00
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2021-10-24 18:49:48 +02:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class CrushingRecipe implements Recipe<Container> {
|
2021-10-24 18:49:48 +02:00
|
|
|
public static String NAME = "crushing";
|
2021-11-13 17:30:53 +01:00
|
|
|
private final ResourceLocation id;
|
2021-10-24 18:49:48 +02:00
|
|
|
protected Ingredient input;
|
|
|
|
protected ItemStack outputOne;
|
|
|
|
protected ItemStack outputTwo;
|
2021-11-13 17:30:53 +01:00
|
|
|
protected float chance1;
|
|
|
|
protected float chance2;
|
2021-10-24 18:49:48 +02:00
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
public CrushingRecipe(ResourceLocation id, Ingredient input, ItemStack outputOne, float chance1, ItemStack outputTwo, float chance2) {
|
2021-10-24 18:49:48 +02:00
|
|
|
this.id = id;
|
|
|
|
this.input = input;
|
|
|
|
this.outputOne = outputOne;
|
|
|
|
this.outputTwo = outputTwo;
|
2021-11-13 17:30:53 +01:00
|
|
|
this.chance1 = chance1;
|
|
|
|
this.chance2 = chance2;
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
public CrushingRecipe(Ingredient input, ItemStack outputOne, float chance1, ItemStack outputTwo, float chance2) {
|
2024-03-03 01:20:53 +01:00
|
|
|
this.id = new ResourceLocation(ActuallyAdditions.MODID, ForgeRegistries.ITEMS.getKey(input.getItems()[0].getItem()).getPath() + "_crushing");
|
2021-10-24 18:49:48 +02:00
|
|
|
this.input = input;
|
|
|
|
this.outputOne = outputOne;
|
|
|
|
this.outputTwo = outputTwo;
|
2021-11-13 17:30:53 +01:00
|
|
|
this.chance1 = chance1;
|
|
|
|
this.chance2 = chance2;
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public boolean matches(Container pInv, Level pLevel) {
|
2021-10-24 18:49:48 +02:00
|
|
|
return input.test(pInv.getItem(0));
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:30:01 +01:00
|
|
|
public boolean matches (ItemStack stack) {
|
|
|
|
return input.test(stack);
|
|
|
|
}
|
|
|
|
|
2023-01-15 22:46:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean isSpecial() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-24 18:49:48 +02:00
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack assemble(Container pInv, RegistryAccess pRegistryAccess) {
|
2021-10-24 18:49:48 +02:00
|
|
|
return ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canCraftInDimensions(int pWidth, int pHeight) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack getResultItem(RegistryAccess pRegistryAccess) {
|
2021-10-24 18:49:48 +02:00
|
|
|
return outputOne;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2021-10-24 18:49:48 +02:00
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeSerializer<?> getSerializer() {
|
2021-10-24 18:49:48 +02:00
|
|
|
return ActuallyRecipes.CRUSHING_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeType<?> getType() {
|
2021-10-24 18:49:48 +02:00
|
|
|
return ActuallyRecipes.Types.CRUSHING;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getOutputOne() {
|
|
|
|
return this.outputOne;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getOutputTwo() {
|
|
|
|
return this.outputTwo;
|
|
|
|
}
|
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
public float getFirstChance() {
|
|
|
|
return this.chance1;
|
|
|
|
}
|
|
|
|
public float getSecondChance() {
|
|
|
|
return this.chance2;
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Ingredient getInput() {
|
|
|
|
return this.input;
|
|
|
|
}
|
|
|
|
|
2024-03-03 01:20:53 +01:00
|
|
|
public static class Serializer implements RecipeSerializer<CrushingRecipe> {
|
2021-10-24 18:49:48 +02:00
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
|
|
|
public CrushingRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
|
2024-03-02 21:23:08 +01:00
|
|
|
Ingredient ingredient = Ingredient.fromJson(GsonHelper.getAsJsonObject(pJson, "ingredient"));
|
2021-11-13 17:30:53 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
JsonArray resultList = GsonHelper.getAsJsonArray(pJson, "result");
|
2021-11-13 17:30:53 +01:00
|
|
|
if (resultList.size() < 1)
|
|
|
|
throw new IllegalStateException(pRecipeId.toString() + ": Recipe must contain at least 1 result item");
|
|
|
|
|
2022-10-19 20:04:42 +02:00
|
|
|
JsonObject result1 = resultList.get(0).getAsJsonObject();
|
2024-03-02 21:23:08 +01:00
|
|
|
int count1 = GsonHelper.getAsInt(result1, "count", 0);
|
|
|
|
ItemStack output1 = new ItemStack(GsonHelper.getAsItem(result1, "item"), count1);
|
|
|
|
float chance1 = GsonHelper.getAsFloat(result1, "chance");
|
2021-10-24 18:49:48 +02:00
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
ItemStack output2 = ItemStack.EMPTY;
|
|
|
|
float chance2 = 1.0f;
|
|
|
|
if (resultList.size() > 1) {
|
2022-10-19 20:04:42 +02:00
|
|
|
JsonObject result2 = resultList.get(1).getAsJsonObject();
|
2024-03-02 21:23:08 +01:00
|
|
|
int count2 = GsonHelper.getAsInt(result2, "count", 0);
|
|
|
|
output2 = new ItemStack(GsonHelper.getAsItem(result2, "item"), count2);
|
|
|
|
chance2 = GsonHelper.getAsFloat(result2, "chance");
|
2021-11-13 17:30:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new CrushingRecipe(pRecipeId, ingredient, output1, chance1, output2, chance2);
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public CrushingRecipe fromNetwork(@Nonnull ResourceLocation pRecipeId, @Nonnull FriendlyByteBuf pBuffer) {
|
2021-10-24 18:49:48 +02:00
|
|
|
Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
|
|
|
|
ItemStack output1 = pBuffer.readItem();
|
|
|
|
ItemStack output2 = pBuffer.readItem();
|
2021-11-13 17:30:53 +01:00
|
|
|
float chance1 = pBuffer.readFloat();
|
|
|
|
float chance2 = pBuffer.readFloat();
|
2021-10-24 18:49:48 +02:00
|
|
|
|
2021-11-13 17:30:53 +01:00
|
|
|
return new CrushingRecipe(pRecipeId, ingredient, output1, chance1, output2, chance2);
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void toNetwork(@Nonnull FriendlyByteBuf pBuffer, CrushingRecipe pRecipe) {
|
2021-10-24 18:49:48 +02:00
|
|
|
pRecipe.input.toNetwork(pBuffer);
|
|
|
|
pBuffer.writeItem(pRecipe.outputOne);
|
|
|
|
pBuffer.writeItem(pRecipe.outputTwo);
|
2021-11-13 17:30:53 +01:00
|
|
|
pBuffer.writeFloat(pRecipe.chance1);
|
|
|
|
pBuffer.writeFloat(pRecipe.chance2);
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static class Result implements FinishedRecipe {
|
2021-11-13 17:30:53 +01:00
|
|
|
private final ResourceLocation id;
|
2021-10-24 18:49:48 +02:00
|
|
|
protected Ingredient input;
|
2024-03-02 21:23:08 +01:00
|
|
|
protected ItemLike outputOne;
|
2021-11-13 17:30:53 +01:00
|
|
|
protected int countOne;
|
|
|
|
protected float outputChance1;
|
2024-03-02 21:23:08 +01:00
|
|
|
protected ItemLike outputTwo;
|
2021-11-13 17:30:53 +01:00
|
|
|
protected int countTwo;
|
|
|
|
protected float outputChance2;
|
2021-10-24 18:49:48 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public Result(ResourceLocation id, Ingredient input, ItemLike outputOne, int countOne, float outputChance1, ItemLike outputTwo, int countTwo, float outputChance2) {
|
2021-10-24 18:49:48 +02:00
|
|
|
this.id = id;
|
2021-11-13 17:30:53 +01:00
|
|
|
this.countOne = countOne;
|
|
|
|
this.countTwo = countTwo;
|
2021-10-24 18:49:48 +02:00
|
|
|
this.input = input;
|
|
|
|
this.outputOne = outputOne;
|
|
|
|
this.outputTwo = outputTwo;
|
2021-11-13 17:30:53 +01:00
|
|
|
this.outputChance1 = outputChance1;
|
|
|
|
this.outputChance2 = outputChance2;
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serializeRecipeData(JsonObject pJson) {
|
2021-11-13 17:30:53 +01:00
|
|
|
pJson.add("ingredient", input.toJson());
|
|
|
|
|
|
|
|
JsonObject result1 = new JsonObject();
|
2024-03-03 01:20:53 +01:00
|
|
|
result1.addProperty("item", ForgeRegistries.ITEMS.getKey(outputOne.asItem()).toString());
|
2022-10-19 20:04:42 +02:00
|
|
|
result1.addProperty("count", countOne);
|
2021-11-13 17:30:53 +01:00
|
|
|
result1.addProperty("chance", outputChance1);
|
|
|
|
|
|
|
|
JsonObject result2 = new JsonObject();
|
2024-03-03 01:20:53 +01:00
|
|
|
result2.addProperty("item", ForgeRegistries.ITEMS.getKey(outputTwo.asItem()).toString());
|
2022-10-19 20:04:42 +02:00
|
|
|
result2.addProperty("count", countTwo);
|
2021-11-24 19:19:31 +01:00
|
|
|
result2.addProperty("chance", outputChance2);
|
2021-11-13 17:30:53 +01:00
|
|
|
|
|
|
|
JsonArray resultList = new JsonArray();
|
|
|
|
resultList.add(result1);
|
|
|
|
resultList.add(result2);
|
|
|
|
|
|
|
|
pJson.add("result", resultList);
|
2021-10-24 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2021-10-24 18:49:48 +02:00
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-13 17:30:53 +01:00
|
|
|
@Nonnull
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeSerializer<?> getType() {
|
2021-10-24 18:49:48 +02:00
|
|
|
return ActuallyRecipes.CRUSHING_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public JsonObject serializeAdvancement() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getAdvancementId() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|