Some more recipe work, crushing recipe might be done?

This commit is contained in:
Flanks255 2021-11-13 10:30:53 -06:00
parent 9b11bf3245
commit a0b8a83194
6 changed files with 131 additions and 57 deletions

View file

@ -134,7 +134,7 @@ public final class ActuallyAdditionsAPI {
* @param outputTwoChance The chance of the second output (0 won't occur at all, 100 will all the time)
*/
public static void addCrusherRecipe(ItemStack input, ItemStack outputOne, ItemStack outputTwo, int outputTwoChance) {
CRUSHER_RECIPES.add(new CrushingRecipe(Ingredient.of(input), outputOne, outputTwo.isEmpty()
CRUSHER_RECIPES.add(new CrushingRecipe(Ingredient.of(input), outputOne, 1.0f, outputTwo.isEmpty()
? ItemStack.EMPTY
: outputTwo, outputTwoChance));
}
@ -148,7 +148,7 @@ public final class ActuallyAdditionsAPI {
* @param outputTwoChance The chance of the second output (0 won't occur at all, 100 will all the time)
*/
public static void addCrusherRecipe(Ingredient input, ItemStack outputOne, ItemStack outputTwo, int outputTwoChance) {
CRUSHER_RECIPES.add(new CrushingRecipe(input, outputOne, outputTwo.isEmpty()
CRUSHER_RECIPES.add(new CrushingRecipe(input, outputOne, 1.0f, outputTwo.isEmpty()
? ItemStack.EMPTY
: outputTwo, outputTwoChance));
}

View file

@ -1,11 +1,17 @@
package de.ellpeck.actuallyadditions.data;
import com.google.gson.JsonObject;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.crafting.CrushingRecipe;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DirectoryCache;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.data.RecipeProvider;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
import java.nio.file.Path;
import java.util.function.Consumer;
@ -15,11 +21,12 @@ public class CrushingRecipeGenerator extends RecipeProvider {
}
@Override
protected void saveAdvancement(DirectoryCache p_208310_1_, JsonObject p_208310_2_, Path p_208310_3_) {
protected void saveAdvancement(@Nonnull DirectoryCache p_208310_1_, @Nonnull JsonObject p_208310_2_, @Nonnull Path p_208310_3_) {
}
@Override
protected void buildShapelessRecipes(Consumer<IFinishedRecipe> p_200404_1_) {
protected void buildShapelessRecipes(Consumer<IFinishedRecipe> consumer) {
consumer.accept(new CrushingRecipe.FinishedRecipe(new ResourceLocation(ActuallyAdditions.MODID, "bone_crusher"),
Ingredient.of(Items.BONE), Items.BONE_MEAL, 6, 1.0f , Items.AIR, 0, 0.0f));
}
}

View file

@ -1,5 +1,6 @@
package de.ellpeck.actuallyadditions.mod.crafting;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import net.minecraft.data.IFinishedRecipe;
@ -16,39 +17,44 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class CrushingRecipe implements IRecipe<IInventory> {
public static String NAME = "crushing";
private ResourceLocation id;
private final ResourceLocation id;
protected Ingredient input;
protected ItemStack outputOne;
protected ItemStack outputTwo;
protected int outputChance;
protected float chance1;
protected float chance2;
public CrushingRecipe(ResourceLocation id, Ingredient input, ItemStack outputOne, ItemStack outputTwo, int outputChance) {
public CrushingRecipe(ResourceLocation id, Ingredient input, ItemStack outputOne, float chance1, ItemStack outputTwo, float chance2) {
this.id = id;
this.input = input;
this.outputOne = outputOne;
this.outputTwo = outputTwo;
this.outputChance = outputChance;
this.chance1 = chance1;
this.chance2 = chance2;
}
public CrushingRecipe(Ingredient input, ItemStack outputOne, ItemStack outputTwo, int outputChance) {
public CrushingRecipe(Ingredient input, ItemStack outputOne, float chance1, ItemStack outputTwo, float chance2) {
this.id = new ResourceLocation(ActuallyAdditions.MODID, input.getItems()[0].getItem().getRegistryName().getPath() + "_crushing");
this.input = input;
this.outputOne = outputOne;
this.outputTwo = outputTwo;
this.outputChance = outputChance;
this.chance1 = chance1;
this.chance2 = chance2;
}
@Override
public boolean matches(IInventory pInv, World pLevel) {
public boolean matches(IInventory pInv, @Nonnull World pLevel) {
return input.test(pInv.getItem(0));
}
@Override
public ItemStack assemble(IInventory pInv) {
@Nonnull
public ItemStack assemble(@Nonnull IInventory pInv) {
return ItemStack.EMPTY;
}
@ -58,21 +64,25 @@ public class CrushingRecipe implements IRecipe<IInventory> {
}
@Override
@Nonnull
public ItemStack getResultItem() {
return outputOne;
}
@Override
@Nonnull
public ResourceLocation getId() {
return id;
}
@Override
@Nonnull
public IRecipeSerializer<?> getSerializer() {
return ActuallyRecipes.CRUSHING_RECIPE.get();
}
@Override
@Nonnull
public IRecipeType<?> getType() {
return ActuallyRecipes.Types.CRUSHING;
}
@ -85,8 +95,11 @@ public class CrushingRecipe implements IRecipe<IInventory> {
return this.outputTwo;
}
public int getSecondChance() {
return this.outputChance;
public float getFirstChance() {
return this.chance1;
}
public float getSecondChance() {
return this.chance2;
}
public Ingredient getInput() {
@ -96,64 +109,96 @@ public class CrushingRecipe implements IRecipe<IInventory> {
public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<CrushingRecipe> {
@Override
public CrushingRecipe fromJson(ResourceLocation pRecipeId, JsonObject pJson) {
Ingredient ingredient = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "input"));
ItemStack output1 = new ItemStack(JSONUtils.getAsItem(pJson, "output_one"));
ItemStack output2 = new ItemStack(JSONUtils.getAsItem(pJson, "output_two"));
int chance = JSONUtils.getAsInt(pJson, "second_chance");
@Nonnull
public CrushingRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
Ingredient ingredient = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "ingredient"));
return new CrushingRecipe(pRecipeId, ingredient, output1, output2, chance);
JsonArray resultList = JSONUtils.getAsJsonObject(pJson, "result").getAsJsonArray();
if (resultList.size() < 1)
throw new IllegalStateException(pRecipeId.toString() + ": Recipe must contain at least 1 result item");
ItemStack output1 = new ItemStack(JSONUtils.getAsItem(resultList.get(0).getAsJsonObject(), "item"));
float chance1 = JSONUtils.getAsFloat(resultList.get(0).getAsJsonObject(), "chance");
ItemStack output2 = ItemStack.EMPTY;
float chance2 = 1.0f;
if (resultList.size() > 1) {
output2 = new ItemStack(JSONUtils.getAsItem(resultList.get(1).getAsJsonObject(), "item"));
chance2 = JSONUtils.getAsFloat(resultList.get(1).getAsJsonObject(), "chance");
}
return new CrushingRecipe(pRecipeId, ingredient, output1, chance1, output2, chance2);
}
@Nullable
@Override
public CrushingRecipe fromNetwork(ResourceLocation pRecipeId, PacketBuffer pBuffer) {
public CrushingRecipe fromNetwork(@Nonnull ResourceLocation pRecipeId, @Nonnull PacketBuffer pBuffer) {
Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
ItemStack output1 = pBuffer.readItem();
ItemStack output2 = pBuffer.readItem();
int chance = pBuffer.readInt();
float chance1 = pBuffer.readFloat();
float chance2 = pBuffer.readFloat();
return new CrushingRecipe(pRecipeId, ingredient, output1, output2, chance);
return new CrushingRecipe(pRecipeId, ingredient, output1, chance1, output2, chance2);
}
@Override
public void toNetwork(PacketBuffer pBuffer, CrushingRecipe pRecipe) {
public void toNetwork(@Nonnull PacketBuffer pBuffer, CrushingRecipe pRecipe) {
pRecipe.input.toNetwork(pBuffer);
pBuffer.writeItem(pRecipe.outputOne);
pBuffer.writeItem(pRecipe.outputTwo);
pBuffer.writeInt(pRecipe.outputChance);
pBuffer.writeFloat(pRecipe.chance1);
pBuffer.writeFloat(pRecipe.chance2);
}
}
public static class FinishedRecipe implements IFinishedRecipe {
private ResourceLocation id;
private final ResourceLocation id;
protected Ingredient input;
protected IItemProvider outputOne;
protected int countOne;
protected float outputChance1;
protected IItemProvider outputTwo;
protected int outputChance;
protected int countTwo;
protected float outputChance2;
public FinishedRecipe(ResourceLocation id, Ingredient input, IItemProvider outputOne, IItemProvider outputTwo, int outputChance) {
public FinishedRecipe(ResourceLocation id, Ingredient input, IItemProvider outputOne, int countOne, float outputChance1, IItemProvider outputTwo, int countTwo, float outputChance2) {
this.id = id;
this.countOne = countOne;
this.countTwo = countTwo;
this.input = input;
this.outputOne = outputOne;
this.outputTwo = outputTwo;
this.outputChance = outputChance;
this.outputChance1 = outputChance1;
this.outputChance2 = outputChance2;
}
@Override
public void serializeRecipeData(JsonObject pJson) {
pJson.add("input", input.toJson());
pJson.addProperty("output_one", outputOne.asItem().getRegistryName().toString());
pJson.addProperty("output_two", outputTwo.asItem().getRegistryName().toString());
pJson.addProperty("second_chance", outputChance);
pJson.add("ingredient", input.toJson());
JsonObject result1 = new JsonObject();
result1.addProperty("item", outputOne.asItem().getRegistryName().toString());
result1.addProperty("chance", outputChance1);
JsonObject result2 = new JsonObject();
result1.addProperty("item", outputTwo.asItem().getRegistryName().toString());
result1.addProperty("chance", outputChance2);
JsonArray resultList = new JsonArray();
resultList.add(result1);
resultList.add(result2);
pJson.add("result", resultList);
}
@Override
@Nonnull
public ResourceLocation getId() {
return id;
}
@Override
@Nonnull
public IRecipeSerializer<?> getType() {
return ActuallyRecipes.CRUSHING_RECIPE.get();
}

View file

@ -15,13 +15,14 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class EmpowererRecipe implements IRecipe<IInventory> {
public static String NAME = "empowering";
private ResourceLocation id;
private final ResourceLocation id;
protected final Ingredient input;
protected final ItemStack output;
@ -73,12 +74,13 @@ public class EmpowererRecipe implements IRecipe<IInventory> {
}
@Override
public boolean matches(IInventory pInv, World pLevel) {
public boolean matches(@Nonnull IInventory pInv, @Nonnull World pLevel) {
return false;
}
@Override
public ItemStack assemble(IInventory pInv) {
@Nonnull
public ItemStack assemble(@Nonnull IInventory pInv) {
return output.copy();
}
@ -88,21 +90,25 @@ public class EmpowererRecipe implements IRecipe<IInventory> {
}
@Override
@Nonnull
public ItemStack getResultItem() {
return output;
}
@Override
@Nonnull
public ResourceLocation getId() {
return id;
}
@Override
@Nonnull
public IRecipeSerializer<?> getSerializer() {
return ActuallyRecipes.EMPOWERING_RECIPE.get();
}
@Override
@Nonnull
public IRecipeType<?> getType() {
return ActuallyRecipes.Types.EMPOWERING;
}
@ -145,8 +151,8 @@ public class EmpowererRecipe implements IRecipe<IInventory> {
public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<EmpowererRecipe> {
@Override
public EmpowererRecipe fromJson(ResourceLocation pRecipeId, JsonObject pJson) {
ItemStack result = new ItemStack(JSONUtils.getAsItem(pJson, "result"));
@Nonnull
public EmpowererRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
Ingredient base = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "base"));
Ingredient mod1 = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "modifier1"));
Ingredient mod2 = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "modifier2"));
@ -155,13 +161,15 @@ public class EmpowererRecipe implements IRecipe<IInventory> {
int energy = JSONUtils.getAsInt(pJson, "energy");
int color = JSONUtils.getAsInt(pJson, "color");
int time = JSONUtils.getAsInt(pJson, "time");
JsonObject resultObject = JSONUtils.getAsJsonObject(pJson, "result");
ItemStack result = new ItemStack(JSONUtils.getAsItem(resultObject, "item"));
return new EmpowererRecipe(pRecipeId, result, base, mod1, mod2, mod3, mod4, energy, color, time);
}
@Nullable
@Override
public EmpowererRecipe fromNetwork(ResourceLocation pRecipeId, PacketBuffer pBuffer) {
public EmpowererRecipe fromNetwork(@Nonnull ResourceLocation pRecipeId, PacketBuffer pBuffer) {
ItemStack result = pBuffer.readItem();
Ingredient input = Ingredient.fromNetwork(pBuffer);
Ingredient mod1 = Ingredient.fromNetwork(pBuffer);
@ -190,16 +198,16 @@ public class EmpowererRecipe implements IRecipe<IInventory> {
}
public static class FinishedRecipe implements IFinishedRecipe {
private ResourceLocation id;
private Ingredient base;
private Ingredient mod1;
private Ingredient mod2;
private Ingredient mod3;
private Ingredient mod4;
private int energy;
private int color;
private int time;
private IItemProvider output;
private final ResourceLocation id;
private final Ingredient base;
private final Ingredient mod1;
private final Ingredient mod2;
private final Ingredient mod3;
private final Ingredient mod4;
private final int energy;
private final int color;
private final int time;
private final IItemProvider output;
public FinishedRecipe(ResourceLocation id, IItemProvider output, Ingredient input, Ingredient modifier1, Ingredient modifier2, Ingredient modifier3, Ingredient modifier4, int energyPerStand, int particleColor, int time) {
this.id = id;
@ -224,15 +232,21 @@ public class EmpowererRecipe implements IRecipe<IInventory> {
pJson.addProperty("energy", energy);
pJson.addProperty("time", time);
pJson.addProperty("color", color);
pJson.addProperty("result", output.asItem().getRegistryName().toString());
JsonObject resultObject = new JsonObject();
resultObject.addProperty("item", output.asItem().getRegistryName().toString());
pJson.add("result", resultObject);
}
@Override
@Nonnull
public ResourceLocation getId() {
return id;
}
@Override
@Nonnull
public IRecipeSerializer<?> getType() {
return ActuallyRecipes.EMPOWERING_RECIPE.get();
}

View file

@ -4,7 +4,10 @@ import com.google.gson.JsonObject;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.*;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.JSONUtils;
@ -80,7 +83,8 @@ public class LaserRecipe implements IRecipe<IInventory> {
public LaserRecipe fromJson(ResourceLocation pRecipeId, JsonObject pJson) {
Ingredient ingredient = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "ingredient"));
int energy = JSONUtils.getAsInt(pJson, "energy");
ItemStack result = new ItemStack(JSONUtils.getAsItem(pJson, "result"));
JsonObject resultObject = JSONUtils.getAsJsonObject(pJson, "result");
ItemStack result = new ItemStack(JSONUtils.getAsItem(resultObject, "item"));
return new LaserRecipe(pRecipeId, result, ingredient, energy);
}
@ -117,9 +121,13 @@ public class LaserRecipe implements IRecipe<IInventory> {
@Override
public void serializeRecipeData(JsonObject pJson) {
pJson.add("input", itemIngredient.toJson());
pJson.add("ingredient", itemIngredient.toJson());
pJson.addProperty("energy", energy);
pJson.addProperty("output", output.asItem().getRegistryName().toString());
JsonObject resultObject = new JsonObject();
resultObject.addProperty("item", output.asItem().getRegistryName().toString());
pJson.add("result", resultObject);
}
@Override

View file

@ -242,7 +242,7 @@ public class TileEntityCrusher extends TileEntityInventoryBase implements IButto
outputTwo.setDamage(0);
}
*/
int rand = this.level.random.nextInt(100) + 1;
float rand = this.level.random.nextFloat();
if (rand <= recipe.getSecondChance()) {
if (!StackUtil.isValid(this.inv.getStackInSlot(theSecondOutput))) {
this.inv.setStackInSlot(theSecondOutput, outputTwo.copy());