summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/entities/EntityHeart.java
blob: f84a23aef5e8f6b00a7066219d5c2871e0a0958a (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
/**
 * 
 */
package darkknight.jewelrycraft.entities;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import darkknight.jewelrycraft.JewelrycraftMod;
import darkknight.jewelrycraft.config.ConfigHandler;
import darkknight.jewelrycraft.network.PacketSendClientPlayerInfo;
import darkknight.jewelrycraft.util.JewelrycraftUtil;
import darkknight.jewelrycraft.util.PlayerUtils;
import darkknight.jewelrycraft.util.Variables;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

/**
 * @author Sorin
 */
public class EntityHeart extends EntityLiving
{
    public EntityHeart(World world)
    {
        super(world);
        this.setSize(0.4F, 0.4F);
    }
    
    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(getQuantity());
    }
    
    @Override
    public void updateEntityActionState()
    {
        if (ConfigHandler.HEARTS_DESPAWN)
            ++this.entityAge;
        this.setCanPickUpLoot(true);
    }
    
    public void onLivingUpdate()
    {
        super.onLivingUpdate();
        if (ConfigHandler.HEARTS_DESPAWN && this.entityAge > ConfigHandler.HEART_DESPAWN_TIME)
            this.setDead();
    }
    
    protected void collideWithEntity(Entity entity)
    {
        super.collideWithEntity(entity);
        if (!this.worldObj.isRemote && entity instanceof EntityHeart && getType().equals(((EntityHeart)entity).getType())) {
            setQuantity(getQuantity() + ((EntityHeart)entity).getQuantity());
            entity.setDead();
        }
    }
    
    @Override
    public void onCollideWithPlayer(EntityPlayer player)
    {
        if (!this.worldObj.isRemote) {
            NBTTagCompound playerInfo = PlayerUtils.getModPlayerPersistTag(player, Variables.MODID);
            if (playerInfo != null) {
                if (getType().equals("Red") && player.getHealth() < player.getMaxHealth()) {
                    float healAmount = player.getMaxHealth() - player.getHealth();
                    if (getQuantity() > healAmount) {
                        player.heal(healAmount);
                        this.setQuantity(getQuantity() - healAmount);
                    }else{
                        player.heal(getQuantity());
                        this.setDead();
                    }
                }else
                    if (getType().equals("White") && playerInfo.getFloat("WhiteHeart") > 0.1F) {
                    playerInfo.setFloat(getType() + "Heart", 0F);
                    if (player.getMaxHealth() < 40F) {
                        player.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(player.getMaxHealth() + 2f);
                        player.setHealth(player.getHealth() + 2f);
                    }
                    JewelrycraftMod.netWrapper.sendTo(new PacketSendClientPlayerInfo(playerInfo), (EntityPlayerMP)player);
                    this.setDead();
                }else
                        if (!getType().equals("Red") && ((getType().equals("Black") && playerInfo.getFloat("BlackHeart") <= ConfigHandler.MAX_BLACK_HEARTS_PICKUP) || (getType().equals("Blue") && playerInfo.getFloat("BlueHeart") <= ConfigHandler.MAX_BLUE_HEARTS_PICKUP) || getType().equals("White"))) {
                    if (playerInfo.hasKey(getType() + "Heart"))
                        playerInfo.setFloat(getType() + "Heart", playerInfo.getFloat(getType() + "Heart") + getQuantity());
                    else playerInfo.setFloat(getType() + "Heart", getQuantity());
                    JewelrycraftMod.netWrapper.sendTo(new PacketSendClientPlayerInfo(playerInfo), (EntityPlayerMP)player);
                    this.setDead();
                }
            }
        }
    }
    
    public void onDeath(DamageSource source)
    {
        super.onDeath(source);
        if (source.getEntity() != null && source.getEntity() instanceof EntityPlayer)
            ((EntityPlayer)source.getEntity()).addChatMessage(new ChatComponentText(StatCollector.translateToLocal("chatmessage." + Variables.MODID + ".heartKilled." + this.getType())));
    }
    
    @SideOnly (Side.CLIENT)
    public boolean canRenderOnFire()
    {
        return false;
    }
    
    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(16, "Red");
        this.dataWatcher.addObject(17, 2f);
    }
    
    public void writeEntityToNBT(NBTTagCompound nbt)
    {
        super.writeEntityToNBT(nbt);
        nbt.setString("Type", getType());
        nbt.setFloat("Quantity", getQuantity());
    }
    
    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound nbt)
    {
        super.readEntityFromNBT(nbt);
        setType(nbt.getString("Type"));
        setQuantity(nbt.getFloat("Quantity"));
    }
    
    public String getType()
    {
        return this.dataWatcher.getWatchableObjectString(16);
    }
    
    public void setType(String type)
    {
        this.dataWatcher.updateObject(16, type);
    }
    
    public Float getQuantity()
    {
        return this.dataWatcher.getWatchableObjectFloat(17);
    }
    
    public void setQuantity(Float qty)
    {
        this.dataWatcher.updateObject(17, qty);
    }
    
    public EnumCreatureAttribute getCreatureAttribute()
    {
        return JewelrycraftUtil.HEART;
    }
    
    public boolean isEntityInvulnerable()
    {
        return false;
    }
    
    public boolean canPickUpLoot()
    {
        return false;
    }
}