Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Beginnings of anvil plan gui
  • Loading branch information
alcatrazEscapee committed Dec 17, 2018
1 parent 9881e20 commit aa9c7f3
Show file tree
Hide file tree
Showing 32 changed files with 453 additions and 279 deletions.
Expand Up @@ -10,18 +10,19 @@

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;

import net.dries007.tfc.api.capability.heat.ItemHeatHandler;
import net.dries007.tfc.objects.recipes.anvil.AnvilRecipe;
import net.dries007.tfc.api.recipes.AnvilRecipe;
import net.dries007.tfc.util.forge.ForgeStep;
import net.dries007.tfc.util.forge.ForgeSteps;

public class ForgeableHandler extends ItemHeatHandler implements IForgeable
{
private final ForgeSteps steps;
private int work;
private String recipeName;
private ResourceLocation recipeName;

public ForgeableHandler(@Nullable NBTTagCompound nbt, float heatCapacity, float meltingPoint)
{
Expand Down Expand Up @@ -53,15 +54,15 @@ public void setWork(int work)

@Override
@Nullable
public String getRecipeName()
public ResourceLocation getRecipeName()
{
return recipeName;
}

@Override
public void setRecipe(@Nullable AnvilRecipe recipe)
{
recipeName = (recipe == null ? null : recipe.getName());
recipeName = (recipe == null ? null : recipe.getRegistryName());
}

@Override
Expand Down Expand Up @@ -96,7 +97,7 @@ public NBTTagCompound serializeNBT()
nbt.setTag("steps", steps.serializeNBT());
if (recipeName != null)
{
nbt.setString("recipe", recipeName);
nbt.setString("recipe", recipeName.toString());
}

return nbt;
Expand All @@ -108,7 +109,7 @@ public void deserializeNBT(@Nullable NBTTagCompound nbt)
if (nbt != null)
{
work = nbt.getInteger("work");
recipeName = nbt.hasKey("recipe") ? nbt.getString("recipe") : null; // stops defaulting to empty string
recipeName = nbt.hasKey("recipe") ? new ResourceLocation(nbt.getString("recipe")) : null; // stops defaulting to empty string
steps.deserializeNBT(nbt.getCompoundTag("steps"));
}
super.deserializeNBT(nbt);
Expand Down
Expand Up @@ -8,8 +8,10 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.minecraft.util.ResourceLocation;

import net.dries007.tfc.api.capability.heat.IItemHeat;
import net.dries007.tfc.objects.recipes.anvil.AnvilRecipe;
import net.dries007.tfc.api.recipes.AnvilRecipe;
import net.dries007.tfc.util.forge.ForgeStep;
import net.dries007.tfc.util.forge.ForgeSteps;

Expand All @@ -31,12 +33,11 @@ public interface IForgeable extends IItemHeat
void setWork(int work);

/**
* Gets the current saved recipe name.
* This can be used with {@link net.dries007.tfc.objects.recipes.anvil.AnvilRecipeManager} to get the actual recipe on TE loading
* Gets the current saved recipe's registry name
* Returns null if no recipe name is currently saved
*/
@Nullable
String getRecipeName();
ResourceLocation getRecipeName();

/**
* Sets the recipe name from an {@link AnvilRecipe}. If null, sets the recipe name to null
Expand Down
Expand Up @@ -3,7 +3,7 @@
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.api.types;
package net.dries007.tfc.api.recipes;

import java.util.function.Predicate;
import javax.annotation.Nonnull;
Expand All @@ -13,6 +13,7 @@
import net.minecraftforge.registries.IForgeRegistryEntry;

import net.dries007.tfc.api.registries.TFCRegistries;
import net.dries007.tfc.api.types.Metal;

public class AlloyRecipe extends IForgeRegistryEntry.Impl<AlloyRecipe>
{
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/net/dries007/tfc/api/recipes/AnvilRecipe.java
@@ -0,0 +1,76 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.api.recipes;

import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistryEntry;

import net.dries007.tfc.api.types.Metal;
import net.dries007.tfc.objects.te.TEAnvilTFC;
import net.dries007.tfc.util.forge.ForgeRule;

@ParametersAreNonnullByDefault
public class AnvilRecipe extends IForgeRegistryEntry.Impl<AnvilRecipe>
{
private static final Random RNG = new Random();

private final ForgeRule[] rules;
private final ItemStack output;
private final ItemStack input;
private final Metal.Tier minTier;
private long workingSeed;

public AnvilRecipe(ResourceLocation name, ItemStack input, ItemStack output, Metal.Tier minTier, ForgeRule... rules) throws IllegalArgumentException
{
this.input = input;
this.output = output;
if (input.isEmpty() || output.isEmpty())
throw new IllegalArgumentException("Input and output are not allowed to be empty");

this.rules = rules;
if (rules.length == 0 || rules.length > 3)
throw new IllegalArgumentException("Rules length must be within the closed interval [1, 3]");

this.minTier = minTier;

setRegistryName(name);
}

public boolean matches(ItemStack input)
{
return this.input.isItemEqual(input);
}

@Nonnull
public ItemStack getOutput()
{
return output.copy();
}

public ForgeRule[] getRules()
{
return rules;
}

public int getTarget(long worldSeed)
{
RNG.setSeed(worldSeed + workingSeed);
return RNG.nextInt(TEAnvilTFC.WORK_MAX + 1);
}

AnvilRecipe withSeed(long seed)
{
workingSeed = seed;
return this;
}


}
Expand Up @@ -3,12 +3,11 @@
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.objects.recipes.anvil;
package net.dries007.tfc.api.recipes;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import net.minecraft.item.ItemStack;
Expand All @@ -29,17 +28,9 @@ public static AnvilRecipe get(ItemStack input)
return recipes.stream().filter(x -> x.matches(input)).findFirst().orElse(null);
}

public static boolean hasRecipe(ItemStack input)
{
return get(input) != null;
}

public static void add(AnvilRecipe recipe)
{
if (AnvilRecipe.assertValid(recipe))
{
recipes.add(recipe.withSeed(++workingSeed));
}
recipes.add(recipe.withSeed(++workingSeed));
}

public static void add(Metal.ItemType inputType, Metal.ItemType outputType, boolean onlyToolMetals, ForgeRule... rules)
Expand All @@ -55,7 +46,7 @@ public static void add(Metal.ItemType inputType, Metal.ItemType outputType, bool
ItemStack output = new ItemStack(ItemMetal.get(metal, outputType));
if (!input.isEmpty() && !output.isEmpty())
{
add(new AnvilRecipe(input, output, metal.getTier(), rules));
add(new AnvilRecipe(output.getItem().getRegistryName(), input, output, metal.getTier(), rules));
}
}
}
Expand All @@ -73,52 +64,10 @@ public static void add(Metal.ItemType inputType, Function<Metal, ItemStack> outp
ItemStack output = outputGenerator.apply(metal);
if (!input.isEmpty() && output != null && !output.isEmpty())
{
add(new AnvilRecipe(input, output, metal.getTier(), rules));
add(new AnvilRecipe(output.getItem().getRegistryName(), input, output, metal.getTier(), rules));
}
}
}

@Nullable
public AnvilRecipe getByName(@Nullable String name)
{
return recipes.stream().filter(x -> x.getName().equals(name)).findFirst().orElse(null);
}

@Nullable
public AnvilRecipe getPrevious(@Nullable AnvilRecipe recipe, ItemStack input)
{
List<AnvilRecipe> list = getAllMatching(input);
if (list.size() == 0)
return null;

int idx = list.indexOf(recipe);
if (idx == -1)
return recipe;
else if (idx == 0)
return list.get(list.size() - 1);
else
return list.get(idx - 1);
}

@Nullable
public AnvilRecipe getNext(@Nullable AnvilRecipe recipe, ItemStack input)
{
List<AnvilRecipe> list = getAllMatching(input);
if (list.size() == 0)
return null;

int idx = list.indexOf(recipe);
if (idx == -1)
return recipe;
else if (idx + 1 >= list.size())
return list.get(0);
else
return list.get(idx + 1);
}

private List<AnvilRecipe> getAllMatching(ItemStack input)
{
return recipes.stream().filter(x -> x.matches(input)).collect(Collectors.toList());
}

}
Expand Up @@ -3,13 +3,14 @@
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.api.types;
package net.dries007.tfc.api.recipes;

import java.util.function.Function;

import net.minecraft.item.ItemStack;
import net.minecraftforge.registries.IForgeRegistryEntry;

import net.dries007.tfc.api.types.RockCategory;
import net.dries007.tfc.api.util.IRockObject;
import net.dries007.tfc.util.SimpleCraftMatrix;

Expand Down
Expand Up @@ -3,11 +3,11 @@
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.objects.recipes;
package net.dries007.tfc.api.recipes;

import net.minecraftforge.registries.IForgeRegistryEntry;

public class WeldingRecipe extends IForgeRegistryEntry.Impl<WeldingRecipe>
{

// todo: everything here
}
Expand Up @@ -12,6 +12,10 @@
import net.minecraftforge.registries.IForgeRegistry;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.recipes.AlloyRecipe;
import net.dries007.tfc.api.recipes.AnvilRecipe;
import net.dries007.tfc.api.recipes.KnappingRecipe;
import net.dries007.tfc.api.recipes.WeldingRecipe;
import net.dries007.tfc.api.types.*;

/**
Expand All @@ -27,6 +31,8 @@ public class TFCRegistries

public static final IForgeRegistry<AlloyRecipe> ALLOYS = GameRegistry.findRegistry(AlloyRecipe.class);
public static final IForgeRegistry<KnappingRecipe> KNAPPING = GameRegistry.findRegistry(KnappingRecipe.class);
public static final IForgeRegistry<AnvilRecipe> ANVIL = GameRegistry.findRegistry(AnvilRecipe.class);
public static final IForgeRegistry<WeldingRecipe> WELDING = GameRegistry.findRegistry(WeldingRecipe.class);

static
{
Expand Down
Expand Up @@ -22,4 +22,6 @@ public final class TFCRegistryNames

public static final ResourceLocation ALLOY_RECIPE = new ResourceLocation(MOD_ID, "alloy_recipe");
public static final ResourceLocation KNAPPING_RECIPE = new ResourceLocation(MOD_ID, "knapping_recipe");
public static final ResourceLocation ANVIL_RECIPE = new ResourceLocation(MOD_ID, "anvil_recipe");
public static final ResourceLocation WELDING_RECIPE = new ResourceLocation(MOD_ID, "welding_recipe");
}
7 changes: 6 additions & 1 deletion src/main/java/net/dries007/tfc/client/TFCGuiHandler.java
Expand Up @@ -18,7 +18,7 @@
import net.minecraftforge.fml.common.network.IGuiHandler;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.types.KnappingRecipe;
import net.dries007.tfc.api.recipes.KnappingRecipe;
import net.dries007.tfc.api.types.Rock;
import net.dries007.tfc.api.util.IRockObject;
import net.dries007.tfc.client.gui.*;
Expand Down Expand Up @@ -77,6 +77,8 @@ public Container getServerGuiElement(int ID, EntityPlayer player, World world, i
return new ContainerCharcoalForge(player.inventory, Helpers.getTE(world, pos, TECharcoalForge.class));
case ANVIL:
return new ContainerAnvilTFC(player.inventory, Helpers.getTE(world, pos, TEAnvilTFC.class));
case ANVIL_PLAN:
return new ContainerAnvilPlan(player.inventory, Helpers.getTE(world, pos, TEAnvilTFC.class));
case KNAPPING_STONE:
return new ContainerKnapping(KnappingRecipe.Type.STONE, player.inventory, stack.getItem() instanceof ItemRock ? stack : player.getHeldItemOffhand());
case KNAPPING_CLAY:
Expand Down Expand Up @@ -113,6 +115,8 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
return new GuiCharcoalForge(container, player.inventory, Helpers.getTE(world, pos, TECharcoalForge.class));
case ANVIL:
return new GuiAnvilTFC(container, player.inventory, Helpers.getTE(world, pos, TEAnvilTFC.class));
case ANVIL_PLAN:
return new GuiAnvilPlan(container, player.inventory, Helpers.getTE(world, pos, TEAnvilTFC.class));
case KNAPPING_STONE:
ItemStack stack = player.getHeldItemMainhand();
Rock rock = stack.getItem() instanceof IRockObject ? ((IRockObject) stack.getItem()).getRock(stack) :
Expand Down Expand Up @@ -142,6 +146,7 @@ public enum Type
KNAPPING_LEATHER,
CHARCOAL_FORGE,
ANVIL,
ANVIL_PLAN,
NULL;

private static Type[] values = values();
Expand Down

0 comments on commit aa9c7f3

Please sign in to comment.