summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/events/BucketHandler.java
blob: c11f2ee4c6024b238d7869af3cb12c63cdcf1eea (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
/**
 * Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com
 *
 * BuildCraft is distributed under the terms of the Minecraft Mod Public License
 * 1.0, or MMPL. Please check the contents of the license located in
 * http://www.mod-buildcraft.com/MMPL-1.0.txt
 */
package darkknight.jewelrycraft.events;

import java.util.HashMap;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.FillBucketEvent;
import cpw.mods.fml.common.eventhandler.Event.Result;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import darkknight.jewelrycraft.JewelrycraftMod;
import darkknight.jewelrycraft.block.BlockMoltenMetal;
import darkknight.jewelrycraft.util.JewelryNBT;

public class BucketHandler
{
    public static BucketHandler INSTANCE = new BucketHandler();
    public Map<Block, Item> buckets = new HashMap<Block, Item>();
    
    /**
     * 
     */
    private BucketHandler()
    {}
    
    /**
     * @param event
     */
    @SubscribeEvent
    public void onBucketFill(FillBucketEvent event)
    {
        ItemStack result = fillCustomBucket(event.world, event.target);
        if (result == null) return;
        event.result = result;
        event.setResult(Result.ALLOW);
    }
    
    /**
     * @param world
     * @param pos
     * @return
     */
    private ItemStack fillCustomBucket(World world, MovingObjectPosition pos)
    {
        Block block = world.getBlock(pos.blockX, pos.blockY, pos.blockZ);
        Item bucket = buckets.get(block);
        if (bucket != null && world.getBlock(pos.blockX, pos.blockY, pos.blockZ) != Blocks.air && world.getBlock(pos.blockX, pos.blockY, pos.blockZ) instanceof BlockMoltenMetal){
            world.setBlockToAir(pos.blockX, pos.blockY, pos.blockZ);
            ItemStack item = new ItemStack(bucket);
            String ingotData = JewelrycraftMod.saveData.getString(pos.blockX + " " + pos.blockY + " " + pos.blockZ + " " + world.provider.dimensionId);
            if (ingotData != null && ingotData != ""){
                String[] splitData = ingotData.split(":");
                if (splitData.length == 2){
                    int itemID, itemDamage;
                    try{
                        itemID = Integer.parseInt(splitData[0]);
                        itemDamage = Integer.parseInt(splitData[1]);
                        JewelryNBT.addMetal(item, new ItemStack(Item.getItemById(itemID), 1, itemDamage));
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
            return item;
        }else return null;
    }
}