summaryrefslogtreecommitdiff
path: root/ihl/processing/metallurgy/Crucible.java
blob: a435e49c44826f47cf4bc0632ef4a98a68b32dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package ihl.processing.metallurgy;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.oredict.OreDictionary;
import ic2.api.item.IItemHudInfo;
import ic2.api.recipe.IRecipeInput;
import ic2.api.recipe.RecipeInputOreDict;
import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.item.IHandHeldInventory;
import ic2.core.util.LiquidUtil;
import ic2.core.util.StackUtil;
import ihl.IHLCreativeTab;
import ihl.IHLModInfo;
import ihl.recipes.UniversalRecipeInput;
import ihl.recipes.UniversalRecipeManager;
import ihl.recipes.UniversalRecipeOutput;

public class Crucible extends Item implements IHandHeldInventory, IFluidContainerItem, IItemHudInfo{

	public int capacity=864;
	private String itemName;
	protected static final UniversalRecipeManager recipeManager = new UniversalRecipeManager("crucible");
	
	@SideOnly(Side.CLIENT)
	private IIcon iconHot;

	public Crucible() {
		super();
		this.itemName="crucible";
		this.setUnlocalizedName(itemName);
		GameRegistry.registerItem(this, this.itemName);
		this.setCreativeTab(IHLCreativeTab.tab);
		this.maxStackSize=1;
		this.setCreativeTab(IHLCreativeTab.tab);
		this.setMaxDamage(0);
	}
	
	public static void addRecipe(String string, FluidStack output) 
	{
		recipeManager.addRecipe(new UniversalRecipeInput(null, Arrays.asList(new IRecipeInput[] {new RecipeInputOreDict(string)})), new UniversalRecipeOutput(Arrays.asList(new FluidStack[] {output}),null,20));
	}

	@Override
	public IHasGui getInventory(EntityPlayer entityPlayer, ItemStack itemStack) 
	{
		return new CrucibleInventory(entityPlayer, itemStack);
	}
	
    @Override
	public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityPlayer)
    {
        if (IC2.platform.isSimulating())
        {
            IC2.platform.launchGui(entityPlayer, this.getInventory(entityPlayer, itemStack));
        }
        return itemStack;
    }

    /**
     * allows items to add custom lines of information to the mouseover description
     */
    @Override
	public void addInformation(ItemStack itemStack, EntityPlayer player, List info, boolean b)
    {
        super.addInformation(itemStack, player, info, b);
        FluidStack fs = this.getFluid(itemStack);

        if (fs != null)
        {
            info.add("< " + FluidRegistry.getFluidName(fs) + ", " + fs.amount + " mB >");
        }
        else
        {
            info.add(StatCollector.translateToLocal("ic2.item.FluidContainer.Empty"));
        }
    }

    @Override
	public List<String> getHudInfo(ItemStack itemStack)
    {
        LinkedList info = new LinkedList();
        FluidStack fs = this.getFluid(itemStack);

        if (fs != null)
        {
            info.add("< " + FluidRegistry.getFluidName(fs) + ", " + fs.amount + " mB >");
        }
        else
        {
            info.add(StatCollector.translateToLocal("ic2.item.FluidContainer.Empty"));
        }

        return info;
    }

    @Override
	public FluidStack getFluid(ItemStack stack)
    {
        NBTTagCompound nbtTagCompound = StackUtil.getOrCreateNbtData(stack);
        NBTTagCompound fluidTag = nbtTagCompound.getCompoundTag("Fluid");
        return FluidStack.loadFluidStackFromNBT(fluidTag);
    }

    public boolean isEmpty(ItemStack stack)
    {
        return this.getFluid(stack) == null;
    }

    @Override
	public int getCapacity(ItemStack container)
    {
        return this.capacity;
    }

    public boolean canfill(Fluid var1)
    {
        return true;
    }
    
    public ItemStack processContent(ItemStack itemStack, IInventory inventoryContainer)
    {
    	CrucibleInventory inventory = new CrucibleInventory(inventoryContainer, itemStack);
    	ItemStack content = inventory.getStackInSlot(0);
    	if(content!=null)
    	{
    		UniversalRecipeOutput routput = recipeManager.getOutputFor(null, Arrays.asList(new ItemStack [] {content}),false, false);
    		if(routput!=null && !routput.getFluidOutputs().isEmpty())
    		{
				FluidStack fluidStack = routput.getFluidOutputs().get(0).copy();
				fluidStack.amount *= content.stackSize;
				itemStack.stackTagCompound = new NBTTagCompound();
				this.fill(itemStack, fluidStack, true);
				return itemStack;
    		}
    	}
    	return itemStack;
    }

    @Override
	public int fill(ItemStack stack, FluidStack resource, boolean doFill)
    {
        if (stack.stackSize != 1)
        {
            return 0;
        }
        else if (resource == null)
        {
            return 0;
        }
        else if (!this.canfill(resource.getFluid()))
        {
            return 0;
        }
        else
        {
            NBTTagCompound nbtTagCompound = StackUtil.getOrCreateNbtData(stack);
            NBTTagCompound fluidTag = nbtTagCompound.getCompoundTag("Fluid");
            FluidStack fs = FluidStack.loadFluidStackFromNBT(fluidTag);

            if (fs == null)
            {
                fs = new FluidStack(resource, 0);
            }

            if (!fs.isFluidEqual(resource))
            {
                return 0;
            }
            else
            {
                int amount = Math.min(this.capacity - fs.amount, resource.amount);

                if (doFill && amount > 0)
                {
                    fs.amount += amount;
                    fs.writeToNBT(fluidTag);
                    nbtTagCompound.setTag("Fluid", fluidTag);
                    if(fs.getFluid().getTemperature()>900)stack.setItemDamage(1);
                }

                return amount;
            }
        }
    }

    @Override
	public FluidStack drain(ItemStack stack, int maxDrain, boolean doDrain)
    {
        if (stack.stackSize != 1)
        {
            return null;
        }
        else
        {
            NBTTagCompound nbtTagCompound = StackUtil.getOrCreateNbtData(stack);
            NBTTagCompound fluidTag = nbtTagCompound.getCompoundTag("Fluid");
            FluidStack fs = FluidStack.loadFluidStackFromNBT(fluidTag);

            if (fs == null)
            {
                return null;
            }
            else
            {
                maxDrain = Math.min(fs.amount, maxDrain);

                if (doDrain)
                {
                    fs.amount -= maxDrain;

                    if (fs.amount <= 0)
                    {
                        nbtTagCompound.removeTag("Fluid");
                        stack.setItemDamage(0);
                    }
                    else
                    {
                        fs.writeToNBT(fluidTag);
                        nbtTagCompound.setTag("Fluid", fluidTag);
                    }
                }

                return new FluidStack(fs, maxDrain);
            }
        }
    }
	private String getFormattedFluidNameFromMaterialName(String input)
	{
		if(input.length()<6)
		{
			return "";
		}
   		String input2 = input.substring(5);
   		return "molten."+input2.toLowerCase();
	}
	
	private String getOreName(ItemStack stack)
	{
		int[] arrayIDs = OreDictionary.getOreIDs(stack);
		for(int i=0; i<arrayIDs.length;i++)
		{
			if(OreDictionary.getOreName(arrayIDs[i]).startsWith("ingot"))
			{
				return OreDictionary.getOreName(arrayIDs[i]);
			}
		}
		return "";
	}
	
	@Override
	@SideOnly(Side.CLIENT)
	public void registerIcons(IIconRegister register) 
	{
		itemIcon=register.registerIcon(IHLModInfo.MODID + ":crucible");
		iconHot=register.registerIcon(IHLModInfo.MODID + ":crucibleHot");
	}
	
	@Override
	@SideOnly(Side.CLIENT)
    public IIcon getIconFromDamage(int meta)
    {
    	switch(meta)
    	{
    	case 0:
   			return this.itemIcon;
    	case 1:
   			return this.iconHot;
   		default:
   			return this.itemIcon;
    	}
    }
	
    @Override
	public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float xOffset, float yOffset, float zOffset)
    {
        if (!IC2.platform.isSimulating())
        {
            return false;
        }
        else if (this.interactWithTank(stack, player, world, x, y, z, side))
        {
            return true;
        }
		return false;
    }
    

    private boolean interactWithTank(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side)
    {
        if (!IC2.platform.isSimulating())
        {
            return false;
        }
        else
        {
            TileEntity te = world.getTileEntity(x, y, z);

            if (!(te instanceof IFluidHandler))
            {
                return false;
            }
            else
            {
                IFluidHandler handler = (IFluidHandler)te;
                ForgeDirection dir = ForgeDirection.getOrientation(side);
                FluidStack fs = this.getFluid(stack);
                int amount;

                if (fs != null && (!player.isSneaking() || fs.amount >= this.capacity))
                {
                    amount = handler.fill(dir, fs, false);

                    if (amount <= 0)
                    {
                        return false;
                    }
                    else
                    {
                        fs = LiquidUtil.drainContainerStack(stack, player, amount, false);

                        if (fs != null && fs.amount > 0)
                        {
                            handler.fill(dir, fs, true);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    amount = fs == null ? this.capacity : this.capacity - fs.amount;
                    FluidStack input = handler.drain(dir, amount, false);

                    if (input != null && input.amount > 0)
                    {
                        amount = LiquidUtil.fillContainerStack(stack, player, input, false);

                        if (amount <= 0)
                        {
                            return false;
                        }
                        else
                        {
                            handler.drain(dir, amount, true);
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
    }

	public static Map<UniversalRecipeInput, UniversalRecipeOutput> getRecipes() {
		return recipeManager.getRecipes();
	}

}