From 615bde1cb8469286f34a0218fc6a08b21d8ca66c Mon Sep 17 00:00:00 2001 From: canitzp Date: Fri, 18 Sep 2020 07:56:55 +0200 Subject: [PATCH] Added CoffeeMachineIngredient and CoffeeMachineIngredientFactory --- .../api/ActuallyAdditionsAPI.java | 6 +- .../common/ActuallyAdditions.java | 2 - .../common/items/lens/Lenses.java | 6 +- .../recipes/CoffeeMachineIngredient.java | 51 ++++++++++ .../CoffeeMachineIngredientFactory.java | 99 +++++++++++++++++++ .../actuallyadditions/recipes/_factories.json | 3 +- 6 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredient.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredientFactory.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/api/ActuallyAdditionsAPI.java b/src/main/java/de/ellpeck/actuallyadditions/api/ActuallyAdditionsAPI.java index e5d2ff67c..3e8e599ea 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/api/ActuallyAdditionsAPI.java +++ b/src/main/java/de/ellpeck/actuallyadditions/api/ActuallyAdditionsAPI.java @@ -14,6 +14,8 @@ import net.minecraft.block.BlockState; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.Ingredient; +import net.minecraftforge.registries.IForgeRegistry; +import net.minecraftforge.registries.RegistryBuilder; import java.util.ArrayList; import java.util.HashMap; @@ -25,7 +27,9 @@ public final class ActuallyAdditionsAPI { public static final String MOD_ID = "actuallyadditions"; public static final String API_ID = MOD_ID + "api"; public static final String API_VERSION = "34"; - + + public static final IForgeRegistry LENS_REGISTRY = new RegistryBuilder().disableSync().disableSaving().disableOverrides().create(); + public static final List CRUSHER_RECIPES = new ArrayList<>(); public static final List BALL_OF_FUR_RETURN_ITEMS = new ArrayList<>(); public static final List TREASURE_CHEST_LOOT = new ArrayList<>(); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/ActuallyAdditions.java b/src/main/java/de/ellpeck/actuallyadditions/common/ActuallyAdditions.java index 9456fe355..62f4f9781 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/ActuallyAdditions.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/ActuallyAdditions.java @@ -65,8 +65,6 @@ public class ActuallyAdditions { public static final Logger LOGGER = LogManager.getLogger(MODID); public static ActuallyAdditions INSTANCE; - - public static final IForgeRegistry LENS_REGISTRY = new RegistryBuilder().disableSync().disableSaving().disableOverrides().create(); public static boolean commonCapsLoaded = false; diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/lens/Lenses.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/lens/Lenses.java index 1ff660530..ca06bdbe2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/lens/Lenses.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/lens/Lenses.java @@ -1,5 +1,6 @@ package de.ellpeck.actuallyadditions.common.items.lens; +import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.lens.Lens; import de.ellpeck.actuallyadditions.api.lens.LensConversion; import de.ellpeck.actuallyadditions.common.ActuallyAdditions; @@ -8,7 +9,7 @@ import net.minecraftforge.registries.DeferredRegister; public final class Lenses { - public static final DeferredRegister LENSES = DeferredRegister.create(ActuallyAdditions.LENS_REGISTRY, ActuallyAdditions.MODID); + public static final DeferredRegister LENSES = DeferredRegister.create(ActuallyAdditionsAPI.LENS_REGISTRY, ActuallyAdditions.MODID); public static final RegistryObject LENS_CONVERSION = LENSES.register("conversion", LensConversion::new); public static final RegistryObject LENS_DETONATION = LENSES.register("detonation", LensDetonation::new); @@ -19,5 +20,4 @@ public final class Lenses { public static final RegistryObject LENS_DISENCHANTING = LENSES.register("disenchanting", LensDisenchanting::new); public static final RegistryObject LENS_MINING = LENSES.register("mining", LensMining::new); - -} +} \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredient.java b/src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredient.java new file mode 100644 index 000000000..3d497e351 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredient.java @@ -0,0 +1,51 @@ +package de.ellpeck.actuallyadditions.recipes; + +import net.minecraft.item.crafting.IRecipeSerializer; +import net.minecraft.item.crafting.IRecipeType; +import net.minecraft.item.crafting.Ingredient; +import net.minecraft.potion.EffectInstance; +import net.minecraft.util.ResourceLocation; + +public class CoffeeMachineIngredient implements IDummyRecipe{ + + public static final IRecipeType COFFEE_MACHINE_RECIPE_TYPE = IRecipeType.register("actuallyadditions:coffee_machine"); + + protected final ResourceLocation recipeId; + protected final Ingredient input; + protected final int maxAmplifier; + protected final EffectInstance[] effects; + + public CoffeeMachineIngredient(ResourceLocation recipeId, Ingredient input, int maxAmplifier, EffectInstance[] effects){ + this.recipeId = recipeId; + this.input = input; + this.maxAmplifier = maxAmplifier; + this.effects = effects; + } + + public Ingredient getInput(){ + return input; + } + + public int getMaxAmplifier(){ + return maxAmplifier; + } + + public EffectInstance[] getEffects(){ + return effects; + } + + @Override + public ResourceLocation getId(){ + return this.recipeId; + } + + @Override + public IRecipeSerializer getSerializer(){ + return CoffeeMachineIngredientFactory.INSTANCE; + } + + @Override + public IRecipeType getType(){ + return COFFEE_MACHINE_RECIPE_TYPE; + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredientFactory.java b/src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredientFactory.java new file mode 100644 index 000000000..15c13a7ba --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/recipes/CoffeeMachineIngredientFactory.java @@ -0,0 +1,99 @@ +package de.ellpeck.actuallyadditions.recipes; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.item.crafting.IRecipeSerializer; +import net.minecraft.item.crafting.Ingredient; +import net.minecraft.nbt.*; +import net.minecraft.network.PacketBuffer; +import net.minecraft.potion.EffectInstance; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.util.Constants; +import net.minecraftforge.registries.ForgeRegistryEntry; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + +public class CoffeeMachineIngredientFactory extends ForgeRegistryEntry> implements IRecipeSerializer { + + public static final CoffeeMachineIngredientFactory INSTANCE = IRecipeSerializer.register("actuallyadditions:coffee_machine", new CoffeeMachineIngredientFactory()); + + @Override + public CoffeeMachineIngredient read(ResourceLocation recipeId, JsonObject json){ + Ingredient input = null; + EffectInstance[] effects = new EffectInstance[0]; + int maxAmplifier = 0; + + if(json.has("input")){ + input = Ingredient.deserialize(json.get("input")); + } + + if(json.has("effects")){ + if(json.get("effects").isJsonArray()){ + List effectInstances = new ArrayList<>(); + for(JsonElement element : json.get("effects").getAsJsonArray()){ + if(element.isJsonObject()){ + try{ + CompoundNBT tagFromJson = JsonToNBT.getTagFromJson(element.toString()); + effectInstances.add(EffectInstance.read(tagFromJson)); + } catch(CommandSyntaxException e){ + throw new JsonSyntaxException("Can't deserialize Potion Effect from json for recipe!", e); + } + } + } + if(!effectInstances.isEmpty()){ + effects = effectInstances.toArray(new EffectInstance[0]); + } else { + throw new JsonSyntaxException("No Potion effects assigned for recipe!"); + } + } + } + + if(json.has("max_amplifier") && json.get("max_amplifier").isJsonPrimitive()){ + maxAmplifier = json.get("max_amplifier").getAsInt(); + } + + if(input != null && effects.length > 0){ + return new CoffeeMachineIngredient(recipeId, input, maxAmplifier, effects); + } else { + return null; + } + } + + @Nullable + @Override + public CoffeeMachineIngredient read(ResourceLocation recipeId, PacketBuffer buffer){ + Ingredient input = Ingredient.read(buffer); + CompoundNBT nbt = buffer.readCompoundTag(); + int maxAmplifier = buffer.readVarInt(); + + List effectsList = new ArrayList<>(); + ListNBT effectsNBTList = nbt.getList("effectsList", Constants.NBT.TAG_COMPOUND); + for(INBT inbt : effectsNBTList){ + EffectInstance effect = EffectInstance.read((CompoundNBT) inbt); + effectsList.add(effect); + } + + return new CoffeeMachineIngredient(recipeId, input, maxAmplifier, effectsList.toArray(new EffectInstance[0])); + } + + @Override + public void write(PacketBuffer buffer, CoffeeMachineIngredient recipe){ + recipe.getInput().write(buffer); + + ListNBT effectsList = new ListNBT(); + for(EffectInstance effect : recipe.getEffects()){ + CompoundNBT effectTag = new CompoundNBT(); + effect.write(effectTag); + effectsList.add(effectTag); + } + CompoundNBT nbt = new CompoundNBT(); + nbt.put("effectsList", effectsList); + buffer.writeCompoundTag(nbt); + + buffer.writeVarInt(recipe.getMaxAmplifier()); + } +} diff --git a/src/main/resources/assets/actuallyadditions/recipes/_factories.json b/src/main/resources/assets/actuallyadditions/recipes/_factories.json index de353d32d..ea2e4d9c8 100644 --- a/src/main/resources/assets/actuallyadditions/recipes/_factories.json +++ b/src/main/resources/assets/actuallyadditions/recipes/_factories.json @@ -1,5 +1,6 @@ { "recipes": { - "atomic_reconstructor": "de.ellpeck.actuallyadditions.recipes.AtomicReconstructorRecipeFactory" + "atomic_reconstructor": "de.ellpeck.actuallyadditions.recipes.AtomicReconstructorRecipeFactory", + "coffee_machine_ingredient": "de.ellpeck.actuallyadditions.recipes.CoffeeMachineIngredientFactory" } } \ No newline at end of file