summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2014-10-20 23:18:11 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2014-10-20 23:18:11 +0100
commit63f3efabb3755eb8f1837f70abff609eae150c43 (patch)
treebc2c1ea8325ab6d6e47243ab8cda5a9094020304
parenta054e659fc612aa721c7ca060a35e4ea33a3b42f (diff)
downloadnoctis-63f3efabb3755eb8f1837f70abff609eae150c43.tar.bz2
Catalysed recipes now work, for now, glass swords, simples.
-rw-r--r--src/main/java/net/flarn/noctis/Noctis.java3
-rw-r--r--src/main/java/net/flarn/noctis/init/Recipes.java9
-rw-r--r--src/main/java/net/flarn/noctis/item/ItemCatalyst.java38
-rw-r--r--src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java94
4 files changed, 139 insertions, 5 deletions
diff --git a/src/main/java/net/flarn/noctis/Noctis.java b/src/main/java/net/flarn/noctis/Noctis.java
index 19a73f7..54a53b1 100644
--- a/src/main/java/net/flarn/noctis/Noctis.java
+++ b/src/main/java/net/flarn/noctis/Noctis.java
@@ -30,9 +30,10 @@ public class Noctis {
}
@Mod.EventHandler
- public void init(FMLInitializationEvent envent) {
+ public void init(FMLInitializationEvent event) {
// Guis, TileEntity, recipes, etc. (general event handlers)
Recipes.init();
+ Recipes.initCatalysedRecipes();
}
@Mod.EventHandler
diff --git a/src/main/java/net/flarn/noctis/init/Recipes.java b/src/main/java/net/flarn/noctis/init/Recipes.java
index 3a48102..225c547 100644
--- a/src/main/java/net/flarn/noctis/init/Recipes.java
+++ b/src/main/java/net/flarn/noctis/init/Recipes.java
@@ -2,6 +2,10 @@ package net.flarn.noctis.init;
import cpw.mods.fml.common.registry.GameRegistry;
import net.flarn.noctis.handler.ConfigHandler;
+import net.flarn.noctis.item.ItemCatalyst;
+import net.flarn.noctis.recipes.CatalysedRecipe;
+import net.flarn.noctis.utils.TimeHelper;
+import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@@ -22,4 +26,9 @@ public class Recipes {
}
GameRegistry.addRecipe(glassSwordRecipe);
}
+
+ public static void initCatalysedRecipes() {
+ CatalysedRecipe.newRecipe(ItemCatalyst.CatalystType.LIGHT, new ItemStack(ModItems.glassSword),
+ new ItemStack[] { new ItemStack(Blocks.glass, 2, 0), new ItemStack(Items.stick) }, 10, TimeHelper.Times.NIGHT);
+ }
}
diff --git a/src/main/java/net/flarn/noctis/item/ItemCatalyst.java b/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
index f8e27c0..c396c91 100644
--- a/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
+++ b/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
@@ -2,6 +2,7 @@ package net.flarn.noctis.item;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
+import net.flarn.noctis.recipes.CatalysedRecipe;
import net.flarn.noctis.refs.*;
import net.flarn.noctis.utils.*;
import net.minecraft.client.renderer.texture.IIconRegister;
@@ -10,6 +11,7 @@ import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
+import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
@@ -48,6 +50,10 @@ public class ItemCatalyst extends ItemNoctisBase {
@SideOnly(Side.CLIENT)
private IIcon[] icons;
+ public static CatalystType getCatalystType(ItemStack s) {
+ return CatalystType.fromIndex(NBTHelper.getInt(s, Tags.General.TYPE, 0));
+ }
+
public ItemCatalyst() {
super();
this.setMaxDamage(Numbers.MAX_CATALYST_POWER);
@@ -108,6 +114,7 @@ public class ItemCatalyst extends ItemNoctisBase {
}
ItemStack s = entityItem.getEntityItem();
TimeHelper.Times nowTime = TimeHelper.Times.fromCelestialAngle(entityItem.worldObj.getCelestialAngle(0F));
+ boolean canSeeSky = entityItem.worldObj.canBlockSeeTheSky((int) entityItem.posX, (int) entityItem.posY, (int) entityItem.posZ);
LogHelper.warn("It is currently: " + nowTime);
if (nowTime.meetsNeed(TimeHelper.Times.NIGHT)) {
@@ -116,18 +123,41 @@ public class ItemCatalyst extends ItemNoctisBase {
entityItem.age = 0;
}
/* Charge us up a little */
- if (!entityItem.worldObj.isRemote) {
- s.setItemDamage(MathsUtil.clampInt(0, Numbers.MAX_CATALYST_POWER, s.getItemDamage() - 1));
+ if (!entityItem.worldObj.isRemote && canSeeSky) {
+ adjustEnergy(s, 1);
}
} else {
+ if (!entityItem.worldObj.isRemote && canSeeSky) {
+ adjustEnergy(s, -20);
+ }
+ }
+
+ AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(entityItem.posX - 0.5, entityItem.posY, entityItem.posZ - 0.5,
+ entityItem.posX + 0.5, entityItem.posY + 1.0, entityItem.posZ + 0.5);
+ List entities = entityItem.worldObj.getEntitiesWithinAABB(EntityItem.class, axisalignedbb);
+ List recipes = CatalysedRecipe.getPossibleRecipes(entities, getCatalystType(s), nowTime, getEnergy(s));
+ if (!recipes.isEmpty()) {
+ /* Something could be crafted, so craft the first thing returned */
if (!entityItem.worldObj.isRemote) {
- s.setItemDamage(MathsUtil.clampInt(0, Numbers.MAX_CATALYST_POWER, s.getItemDamage() + 20));
+ CatalysedRecipe recipe = (CatalysedRecipe)recipes.get(0);
+ recipe.consumeIngredients(entities);
+ EntityItem newItem = new EntityItem(entityItem.worldObj, entityItem.posX, entityItem.posY, entityItem.posZ,
+ recipe.output.copy());
+ entityItem.worldObj.spawnEntityInWorld(newItem);
}
}
- LogHelper.warn("Stack is " + s);
+
return false;
}
+ public static int getEnergy(ItemStack s) {
+ return Numbers.MAX_CATALYST_POWER - s.getItemDamage();
+ }
+
+ public static void adjustEnergy(ItemStack s, int by) {
+ s.setItemDamage(MathsUtil.clampInt(0, Numbers.MAX_CATALYST_POWER, s.getItemDamage() - by));
+ }
+
@Override
public ItemStack onItemRightClick(ItemStack s, World w, EntityPlayer p)
{
diff --git a/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java b/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java
new file mode 100644
index 0000000..8b3bb6a
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java
@@ -0,0 +1,94 @@
+package net.flarn.noctis.recipes;
+
+import net.flarn.noctis.item.ItemCatalyst;
+import net.flarn.noctis.utils.TimeHelper;
+import net.minecraft.entity.item.EntityItem;
+import net.minecraft.item.ItemStack;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class CatalysedRecipe {
+ public final ItemCatalyst.CatalystType catalyst;
+ public final ItemStack ingredients[];
+ public final ItemStack output;
+ public final int energy;
+ public final TimeHelper.Times timeNeeded;
+
+ private static List<CatalysedRecipe> recipes = new LinkedList();
+
+ private CatalysedRecipe(ItemCatalyst.CatalystType _catalyst,
+ ItemStack _output,
+ ItemStack[] _ingredients,
+ int _energy,
+ TimeHelper.Times _timeNeeded) {
+ catalyst = _catalyst;
+ ingredients = _ingredients;
+ output = _output;
+ energy = _energy;
+ timeNeeded = _timeNeeded;
+ }
+
+ public static void newRecipe(ItemCatalyst.CatalystType _catalyst,
+ ItemStack _output,
+ ItemStack[] _ingredients,
+ int _energy,
+ TimeHelper.Times _timeNeeded) {
+ recipes.add(new CatalysedRecipe(_catalyst, _output, _ingredients, _energy, _timeNeeded));
+ }
+
+ public boolean areIngredientsPresent(List entities) {
+ return checkForIngredientsAndConsume(entities, false);
+ }
+
+ private boolean checkForIngredientsAndConsume(List entities, boolean consume) {
+ int remaining[] = new int[this.ingredients.length];
+ int totalremaining = 0;
+ for (int i = 0; i < this.ingredients.length; ++i) {
+ remaining[i] = this.ingredients[i].stackSize;
+ totalremaining += remaining[i];
+ }
+ for (Object o : entities) {
+ if (o instanceof EntityItem) {
+ ItemStack s = ((EntityItem)o).getEntityItem();
+ if (s != null) {
+ int available = s.stackSize;
+ for (int i = 0; (i < remaining.length) && (available > 0); ++i) {
+ if (s.isItemEqual(this.ingredients[i]) && remaining[i] > 0) {
+ int toconsume = Math.min(available, remaining[i]);
+ remaining[i] -= toconsume;
+ available -= toconsume;
+ totalremaining -= toconsume;
+ if (consume) {
+ s.stackSize -= toconsume;
+ if (s.stackSize == 0) {
+ ((EntityItem) o).setDead();
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return (totalremaining == 0);
+ }
+
+ public boolean consumeIngredients(List entities) {
+ return checkForIngredientsAndConsume(entities, true);
+ }
+
+ public static List<CatalysedRecipe> getPossibleRecipes(List entities, ItemCatalyst.CatalystType catalyst,
+ TimeHelper.Times now, int energyavailable) {
+ List<CatalysedRecipe> ret = new LinkedList();
+ for (CatalysedRecipe r : recipes) {
+ if (r.energy <= energyavailable &&
+ now.meetsNeed(r.timeNeeded) &&
+ r.catalyst == catalyst &&
+ r.areIngredientsPresent(entities)) {
+ ret.add(r);
+ }
+ }
+ return ret;
+ }
+
+}