summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/item/ItemThiefGloves.java
blob: decf34ea235488cec206f66375cfb0b8663f335f (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
package darkknight.jewelrycraft.item;

import java.util.Iterator;
import java.util.List;
import java.util.Random;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.village.MerchantRecipe;
import net.minecraft.village.MerchantRecipeList;
import org.lwjgl.input.Keyboard;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.Side;
import darkknight.jewelrycraft.util.JewelrycraftUtil;
import darkknight.jewelrycraft.util.PlayerUtils;

public class ItemThiefGloves extends Item
{
    public Random rand = new Random();
    
    public ItemThiefGloves()
    {
        super();
        setCreativeTab(CreativeTabs.tabTools);
        setMaxStackSize(1);
        setMaxDamage(10);
    }
    
    /**
     * @param stack
     * @param player
     * @param entity
     * @return
     */
    @Override
    public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity)
    {
        if (entity instanceof EntityVillager){
            EntityVillager villager = (EntityVillager)entity;
            int wealth = (Integer)ReflectionHelper.getPrivateValue(EntityVillager.class, villager, "wealth", "field_70956_bz");
            MerchantRecipeList buyingList = (MerchantRecipeList)ReflectionHelper.getPrivateValue(EntityVillager.class, villager, "buyingList", "field_70963_i");
            int chance = 5;
            boolean areOtherVillagersAround = false, canTheySeeYou = false;
            AxisAlignedBB axisalignedbb = villager.boundingBox.expand(4.0D, 4.0D, 4.0D);
            List entities = villager.worldObj.getEntitiesWithinAABBExcludingEntity(villager, axisalignedbb);
            for(Object s: entities)
                if (s instanceof EntityVillager){
                    areOtherVillagersAround = true;
                    chance += rand.nextInt(2);
                    if (((EntityVillager)s).canEntityBeSeen(player)){
                        chance += 2;
                        canTheySeeYou = true;
                    }
                }
            if (villager.canEntityBeSeen(player)) chance += 5;
            if (player.isPotionActive(Potion.invisibility)) chance -= 0.8 * chance;
            if (player.capabilities.isCreativeMode) chance = 1;
            int steal = rand.nextInt(chance);
            if (steal == 0){
                villager.dropItem(Items.emerald, wealth);
                ReflectionHelper.setPrivateValue(EntityVillager.class, villager, 0, "wealth", "field_70956_bz");
            }
            if (buyingList != null){
                Iterator<?> iterator = buyingList.iterator();
                if (steal == 0){
                    while (iterator.hasNext()){
                        MerchantRecipe recipe = (MerchantRecipe)iterator.next();
                        int toolUses = (Integer)ReflectionHelper.getPrivateValue(MerchantRecipe.class, recipe, "toolUses", "field_77400_d");
                        int quantity;
                        if (recipe.getItemToSell().isStackable()) quantity = recipe.getItemToSell().stackSize * (7 - toolUses);
                        else quantity = recipe.getItemToSell().stackSize;
                        ItemStack s = new ItemStack(recipe.getItemToSell().getItem(), quantity, recipe.getItemToSell().getItemDamage());
                        s.setTagCompound(recipe.getItemToSell().getTagCompound());
                        if (player.inventory.addItemStackToInventory(s)) ;
                        else villager.entityDropItem(s, 0);
                        if (!player.capabilities.isCreativeMode) JewelrycraftUtil.addCursePoints(player, 5);
                        player.addChatMessage(new ChatComponentText("Villager #" + villager.getProfession() + ": Hmmm... I seem to have lost my " + s.getDisplayName() + "!"));
                        stack.damageItem(1, player);
                    }
                    buyingList.clear();
                    ReflectionHelper.setPrivateValue(EntityVillager.class, villager, 300, "timeUntilReset", "field_70961_j");
                    ReflectionHelper.setPrivateValue(EntityVillager.class, villager, true, "needsInitilization", "field_70959_by");
                    player.addChatMessage(new ChatComponentText("You hear a faint whisper in your ear: "));
                    player.addChatMessage(new ChatComponentText(EnumChatFormatting.DARK_PURPLE + "Those who steal but don't get caught get rewarded and do not."));
                    player.addChatMessage(new ChatComponentText(EnumChatFormatting.DARK_PURPLE + "Embrace the path you have gone, for the darkness will not"));
                    player.addChatMessage(new ChatComponentText(EnumChatFormatting.DARK_PURPLE + "dwell on."));
                }else{
                    stack.damageItem(1, player);
                    if (!player.capabilities.isCreativeMode) JewelrycraftUtil.addCursePoints(player, 25);
                    if (player.isPotionActive(Potion.invisibility)){
                        player.addChatMessage(new ChatComponentText("Villager #" + villager.getProfession() + " sensed a strange presence around him, making him cling on to his items. You didn't get anything."));
                    }
                    else{
                        if (areOtherVillagersAround){
                            if (!canTheySeeYou){
                                player.addChatMessage(new ChatComponentText("As he was passing by, a random villager caught you trying to steal from Villager #" + villager.getProfession() + "."));
                                player.addChatMessage(new ChatComponentText("Villager #" + villager.getProfession() + " curses you for the attempt."));
                                return true;
                            }
                            else{
                                player.addChatMessage(new ChatComponentText("A villager nearby saw you trying to steal from Villager #" + villager.getProfession() + "."));
                                player.addChatMessage(new ChatComponentText("Villager #" + villager.getProfession() + " curses you for the attempt."));
                                return true;
                            }
                        }else{
                            player.addChatMessage(new ChatComponentText("Villager #" + villager.getProfession() + " caught you trying to steal from him."));
                            player.addChatMessage(new ChatComponentText("Villager #" + villager.getProfession() + " curses you for the attempt."));
                            return true;
                        }
                    }
                }
            }
            return true;
        }else return super.itemInteractionForEntity(stack, player, entity);
    }
    
    /**
     * @param stack
     * @param player
     * @param list
     * @param par4
     */
    @Override
    @SuppressWarnings ("unchecked")
    public void addInformation(ItemStack stack, EntityPlayer player, @SuppressWarnings ("rawtypes") List list, boolean par4)
    {
        if (!shouldAddAdditionalInfo()) list.add(EnumChatFormatting.GRAY + additionalInfoInstructions());
        else{
            list.add(EnumChatFormatting.GRAY + "Right click with the gloves,");
            list.add(EnumChatFormatting.GRAY + "while sneaking, on a villager");
            list.add(EnumChatFormatting.GRAY + "to steal his stuff.");
        }
    }
    
    /**
     * @return
     */
    public static boolean shouldAddAdditionalInfo()
    {
        if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) return true;
        return false;
    }
    
    /**
     * @return
     */
    public static String additionalInfoInstructions()
    {
        String message = "\247oPress \247b<SHIFT>\2477\247o for more information.";
        return message;
    }
}