summaryrefslogtreecommitdiff
path: root/common/darkknight/jewelrycraft/item/ItemRing.java
blob: 9b2ab37e43cd39c08760fb3a440d444de4d1cace (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
package darkknight.jewelrycraft.item;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import cpw.mods.fml.common.network.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import darkknight.jewelrycraft.JewelrycraftMod;
import darkknight.jewelrycraft.util.JewelryNBT;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.client.resources.ResourceManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryEnderChest;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

public class ItemRing extends Item
{
    public Icon jewel;
    private int amplifier;

    public ItemRing(int par1)
    {
        super(par1);
        this.setMaxStackSize(1);
    }

    public void registerIcons(IconRegister iconRegister)
    {
        itemIcon = iconRegister.registerIcon("jewelrycraft:ring");
        jewel = iconRegister.registerIcon("jewelrycraft:jewel");
    }

    @Override
    public boolean requiresMultipleRenderPasses()
    {
        return true;
    }

    @SideOnly(Side.CLIENT)
    public int getColorFromItemStack(ItemStack par1ItemStack, int pass)
    {
        try
        {
            return color(par1ItemStack, pass);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return 16777215;
    }

    public Icon getIcon(ItemStack stack, int pass)
    {
        if (JewelryNBT.jewel(stack) != null) return pass == 0 ? itemIcon : jewel;
        return itemIcon;
    }

    public static int color(ItemStack stack, int pass) throws IOException
    {
        String domain = "", texture;
        ResourceManager rm = Minecraft.getMinecraft().getResourceManager();
        if (pass == 1 && JewelryNBT.ingot(stack) != null && JewelryNBT.jewel(stack) == null)
        {
            if (JewelryNBT.ingot(stack).getIconIndex().getIconName().substring(0, JewelryNBT.ingot(stack).getIconIndex().getIconName().indexOf(":") + 1) != "") domain = JewelryNBT.ingot(stack).getIconIndex().getIconName().substring(0, JewelryNBT.ingot(stack).getIconIndex().getIconName().indexOf(":") + 1).replace(":", " ").trim();
            else domain = "minecraft";
            texture = JewelryNBT.ingot(stack).getIconIndex().getIconName().substring(JewelryNBT.ingot(stack).getIconIndex().getIconName().lastIndexOf(":") + 1) + ".png";
            ResourceLocation ingot = null;
            if (JewelryNBT.ingot(stack).getUnlocalizedName().contains("item")) ingot = new ResourceLocation(domain, "textures/items/" + texture);
            else ingot = new ResourceLocation(domain, "textures/blocks/" + texture);
            BufferedImage bufferedimage = ImageIO.read(rm.getResource(ingot).getInputStream());
            return bufferedimage.getRGB(9, 9);
        }
        else if (JewelryNBT.ingot(stack) != null && JewelryNBT.jewel(stack) != null)
        {
            if (pass == 1)
            {
                if (JewelryNBT.jewel(stack).getIconIndex().getIconName().substring(0, JewelryNBT.jewel(stack).getIconIndex().getIconName().indexOf(":") + 1) != "") domain = JewelryNBT.jewel(stack).getIconIndex().getIconName().substring(0, JewelryNBT.jewel(stack).getIconIndex().getIconName().indexOf(":") + 1).replace(":", " ").trim();
                else domain = "minecraft";
                texture = JewelryNBT.jewel(stack).getIconIndex().getIconName().substring(JewelryNBT.jewel(stack).getIconIndex().getIconName().lastIndexOf(":") + 1) + ".png";
                ResourceLocation jewelLoc = null;
                if (JewelryNBT.jewel(stack).getUnlocalizedName().contains("item")) jewelLoc = new ResourceLocation(domain, "textures/items/" + texture);
                else jewelLoc = new ResourceLocation(domain, "textures/blocks/" + texture);
                BufferedImage bufferedimage = ImageIO.read(rm.getResource(jewelLoc).getInputStream());
                return bufferedimage.getRGB(9, 4);
            }
            if (JewelryNBT.ingot(stack).getIconIndex().getIconName().substring(0, JewelryNBT.ingot(stack).getIconIndex().getIconName().indexOf(":") + 1) != "") domain = JewelryNBT.ingot(stack).getIconIndex().getIconName().substring(0, JewelryNBT.ingot(stack).getIconIndex().getIconName().indexOf(":") + 1).replace(":", " ").trim();
            else domain = "minecraft";
            texture = JewelryNBT.ingot(stack).getIconIndex().getIconName().substring(JewelryNBT.ingot(stack).getIconIndex().getIconName().lastIndexOf(":") + 1) + ".png";
            ResourceLocation ingot = null;
            if (JewelryNBT.ingot(stack).getUnlocalizedName().contains("item")) ingot = new ResourceLocation(domain, "textures/items/" + texture);
            else ingot = new ResourceLocation(domain, "textures/blocks/" + texture);
            BufferedImage bufferedimage = ImageIO.read(rm.getResource(ingot).getInputStream());
            return bufferedimage.getRGB(9, 9);
        }
        return 16777215;
    }

    public String getItemDisplayName(ItemStack stack)
    {
        if (JewelryNBT.ingot(stack) != null && JewelryNBT.jewel(stack) != null && JewelryNBT.isJewelX(stack, new ItemStack(Item.diamond)) && JewelryNBT.isIngotX(stack, new ItemStack(Item.ingotGold))) return "Wedding Ring";
        else if(JewelryNBT.ingot(stack) != null) return JewelryNBT.ingot(stack).getDisplayName().replace("Ingot", " ").trim() + " " + ("" + StatCollector.translateToLocal(this.getUnlocalizedNameInefficiently(stack) + ".name")).trim();
        return ("" + StatCollector.translateToLocal(this.getUnlocalizedNameInefficiently(stack) + ".name")).trim();
    }

    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
    {
        if (!world.isRemote){
            if (JewelryNBT.playerPosX(stack) != -1 && JewelryNBT.playerPosY(stack) != -1 && JewelryNBT.playerPosZ(stack) != -1){
                double posX = JewelryNBT.playerPosX(stack), posY = JewelryNBT.playerPosY(stack), posZ = JewelryNBT.playerPosZ(stack);
                if (JewelryNBT.isJewelX(stack, new ItemStack(Item.enderPearl)) && JewelryNBT.isModifierX(stack, new ItemStack(Item.bed)) && JewelryNBT.dimension(stack) != -2 && JewelryNBT.dimName(stack) != null)
                {
                    int dimension = JewelryNBT.dimension(stack);
                    for (int i = 1; i <= 20; i++) world.spawnParticle("largesmoke", player.posX - 0.5D + Math.random(), player.posY - 1.5D + Math.random(), player.posZ - 0.5D + Math.random(), 0.0D, 0.0D, 0.0D);
                    if (!JewelryNBT.isDimensionX(stack, player.dimension)) player.travelToDimension(dimension);
                    player.setPositionAndUpdate(posX, posY, posZ);
                    for (int i = 1; i <= 300; i++) world.spawnParticle("portal", posX - 0.5D + Math.random(), posY + Math.random(), posZ - 0.5D + Math.random(), 0.0D, 0.0D, 0.0D);
                }
                else if(JewelryNBT.isDimensionX(stack, player.dimension))
                {
                    for (int i = 1; i <= 20; i++) world.spawnParticle("largesmoke", player.posX - 0.5D + Math.random(), player.posY - 1.5D + Math.random(), player.posZ - 0.5D + Math.random(), 0.0D, 0.0D, 0.0D);
                    player.setPositionAndUpdate(posX, posY, posZ);
                    for (int i = 1; i <= 300; i++) world.spawnParticle("portal", posX - 0.5D + Math.random(), posY + Math.random(), posZ - 0.5D + Math.random(), 0.0D, 0.0D, 0.0D);
                }
                else player.addChatMessage("You can't teleport to these coordonates! You need to be in the same dimension they were set!");
            }
            else if(JewelryNBT.isJewelX(stack, new ItemStack(Item.enderPearl)) && JewelryNBT.isModifierX(stack, new ItemStack(Item.bed)) && JewelryNBT.dimension(stack) == -2 && JewelryNBT.playerPosX(stack) == -1 && JewelryNBT.playerPosY(stack) == -1 && JewelryNBT.playerPosZ(stack) == -1){
                JewelryNBT.addCoordonatesAndDimension(stack, player.posX, player.posY, player.posZ, world.provider.dimensionId, world.provider.getDimensionName());
                JewelryNBT.addEnchantment(stack);
            }
            else if (JewelryNBT.isJewelX(stack, new ItemStack(Block.obsidian)) && JewelryNBT.isModifierX(stack, new ItemStack(Item.eyeOfEnder))){
                InventoryEnderChest inventoryenderchest = player.getInventoryEnderChest();
                player.displayGUIChest(inventoryenderchest);
            }
            else if (JewelryNBT.isJewelX(stack, new ItemStack(Item.enderPearl)) && JewelryNBT.isModifierX(stack, new ItemStack(Block.chest))){
                int i = JewelryNBT.blockCoordX(stack), j = JewelryNBT.blockCoordY(stack), k = JewelryNBT.blockCoordZ(stack);
                if (player.getDistance(i + 0.5F, j + 0.5F, k + 0.5F) <= 128 && i != -1 && j != -1 && k != -1){
                    int id = world.getBlockId(i, j, k);
                    if (id != 0 && Block.blocksList[id] != null && Block.blocksList[id].blockID == Block.chest.blockID){
                        TileEntity tile = world.getBlockTileEntity(i, j, k);
                        if (tile != null && tile instanceof TileEntityChest) FMLNetworkHandler.openGui(player, JewelrycraftMod.instance, 0, world, i, j, k);
                    }
                }
                else if(i != -1 && j != -1 && k != -1) player.addChatMessage("Chest out of range! You need to be " + ((int)player.getDistance(i + 0.5F, j + 0.5F, k + 0.5F) - 127) + " blocks closer.");
                else player.addChatMessage("You need to link the ring with a chest first, before using it!");
            }
            else if (JewelryNBT.isJewelX(stack, new ItemStack(Item.enderPearl)) && JewelryNBT.playerPosX(stack) == -1 && JewelryNBT.playerPosY(stack) == -1 && JewelryNBT.playerPosZ(stack) == -1){
                JewelryNBT.addCoordonatesAndDimension(stack, player.posX, player.posY, player.posZ, world.provider.dimensionId, world.provider.getDimensionName());
                JewelryNBT.addEnchantment(stack);
            }
        }
        return stack;
    }

    @Override
    public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity)
    {
        if (!player.worldObj.isRemote && JewelryNBT.isJewelX(stack, new ItemStack(Item.netherStar)) && JewelryNBT.isModifierX(stack, new ItemStack(Block.chest)) && JewelryNBT.entity(stack, player) == null){
            JewelryNBT.addEntity(stack, entity);
            JewelryNBT.addEntityID(stack, entity);
            entity.setDead();
            JewelryNBT.addEnchantment(stack);
        }
        return true;
    }

    /**
     * allows items to add custom lines of information to the mouseover
     * description
     */
    @Override
    @SuppressWarnings(
            { "rawtypes", "unchecked" })
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4)
    {
        if (stack.hasTagCompound() && stack.getDisplayName() != "Wedding Ring")
        {
            ItemStack ingot = JewelryNBT.ingot(stack);
            if (ingot != null) list.add("Ingot: " + EnumChatFormatting.YELLOW + ingot.getDisplayName());

            ItemStack jewel = JewelryNBT.jewel(stack);
            if (jewel != null) list.add("Jewel: " + EnumChatFormatting.BLUE + jewel.getDisplayName());

            ItemStack modifier = JewelryNBT.modifier(stack);
            if(modifier != null) list.add("Modifier: " + EnumChatFormatting.DARK_PURPLE + modifier.getDisplayName());

            double playerPosX = JewelryNBT.playerPosX(stack), playerPosY = JewelryNBT.playerPosY(stack), playerPosZ = JewelryNBT.playerPosZ(stack);
            if(playerPosX != -1 && playerPosY != -1 && playerPosZ != -1) list.add(EnumChatFormatting.YELLOW + "X: " + EnumChatFormatting.GRAY + (int) playerPosX + EnumChatFormatting.YELLOW + " Y: " + EnumChatFormatting.GRAY + (int) playerPosY + EnumChatFormatting.YELLOW + " Z: " + EnumChatFormatting.GRAY + (int) playerPosZ);

            int posX = JewelryNBT.blockCoordX(stack), posY = JewelryNBT.blockCoordY(stack), posZ = JewelryNBT.blockCoordZ(stack);            
            if(posX != -1 && posY != -1 && posZ != -1) list.add(EnumChatFormatting.YELLOW + "X: " + EnumChatFormatting.GRAY + (int) posX + EnumChatFormatting.YELLOW + " Y: " + EnumChatFormatting.GRAY + (int) posY + EnumChatFormatting.YELLOW + " Z: " + EnumChatFormatting.GRAY + (int) posZ);

            String name = JewelryNBT.dimName(stack);
            if(name != null) list.add("Dimension: " + EnumChatFormatting.DARK_GREEN + name);

            EntityLivingBase entity = JewelryNBT.entity(stack, player);
            if (entity != null) list.add("Entity: " + EnumChatFormatting.GOLD + entity.getEntityName());
        }
    }

    public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int i, int j, int k, int side, float par8, float par9, float par10)
    {
        if (!world.isRemote)
        {
            EntityLivingBase entity = JewelryNBT.entity(stack, player);
            if(entity != null){
                entity.setLocationAndAngles(i + 0.5D, j + 1D, k + 0.5D, MathHelper.wrapAngleTo180_float(world.rand.nextFloat() * 360.0F), 0.0F);
                world.spawnEntityInWorld(entity);
                JewelryNBT.removeEntity(stack);
            }
            if (JewelryNBT.isJewelX(stack, new ItemStack(Item.enderPearl)) && JewelryNBT.isModifierX(stack, new ItemStack(Block.chest)) && world.getBlockId(i, j, k) == Block.chest.blockID) JewelryNBT.addBlockCoordonates(stack, i, j, k);
        }
        return true;
    }

    @Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5)
    {
        amplifier = 0;
        if (!world.isRemote){
            EntityPlayer entityplayer = (EntityPlayer) entity;
            if (JewelryNBT.isJewelX(stack, new ItemStack(Item.diamond))) amplifier = 1;
            else if (JewelryNBT.isJewelX(stack, new ItemStack(Item.emerald))) amplifier = 2;
            else if (JewelryNBT.isJewelX(stack, new ItemStack(Item.netherStar))) amplifier = 7;
            
            if (JewelryNBT.isModifierX(stack, new ItemStack(Item.blazePowder)) && entityplayer != null) entityplayer.addPotionEffect(new PotionEffect(Potion.fireResistance.id, 4, amplifier));
            else if (JewelryNBT.isModifierX(stack, new ItemStack(Item.sugar)) && entityplayer != null) entityplayer.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 4, amplifier));
            else if (JewelryNBT.isModifierX(stack, new ItemStack(Item.pickaxeIron)) && entityplayer != null) entityplayer.addPotionEffect(new PotionEffect(Potion.digSpeed.id, 4, amplifier));
            else if (JewelryNBT.isModifierX(stack, new ItemStack(Item.feather)) && entityplayer != null)
            {
                entityplayer.addPotionEffect(new PotionEffect(Potion.jump.id, 4, amplifier));
                entityplayer.fallDistance=0;
            }
            else if (JewelryNBT.isModifierX(stack, new ItemStack(Item.potion, 1, 8270)) && entityplayer != null) entityplayer.addPotionEffect(new PotionEffect(Potion.invisibility.id, 4, amplifier));
        }
    }
}