summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/EntityItemFrame.java
blob: d1d73f91f31354af8fd1bd67b0eca85ce648b1c6 (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
package net.minecraft.server;

public class EntityItemFrame extends EntityHanging {

    private float e = 1.0F;

    public EntityItemFrame(World world) {
        super(world);
    }

    public EntityItemFrame(World world, int i, int j, int k, int l) {
        super(world, i, j, k, l);
        this.setDirection(l);
    }

    protected void c() {
        this.getDataWatcher().add(2, 5);
        this.getDataWatcher().a(3, Byte.valueOf((byte) 0));
    }

    public boolean damageEntity(DamageSource damagesource, float f) {
        if (this.isInvulnerable()) {
            return false;
        } else if (this.getItem() != null) {
            if (!this.world.isStatic) {
                // CraftBukkit start - fire EntityDamageEvent
                if (org.bukkit.craftbukkit.event.CraftEventFactory.handleNonLivingEntityDamageEvent(this, damagesource, f, false) || this.dead) {
                    return true;
                }
                // CraftBukkit end

                this.b(damagesource.getEntity(), false);
                this.setItem((ItemStack) null);
            }

            return true;
        } else {
            return super.damageEntity(damagesource, f);
        }
    }

    public int f() {
        return 9;
    }

    public int i() {
        return 9;
    }

    public void b(Entity entity) {
        this.b(entity, true);
    }

    public void b(Entity entity, boolean flag) {
        ItemStack itemstack = this.getItem();

        if (entity instanceof EntityHuman) {
            EntityHuman entityhuman = (EntityHuman) entity;

            if (entityhuman.abilities.canInstantlyBuild) {
                this.b(itemstack);
                return;
            }
        }

        if (flag) {
            this.a(new ItemStack(Items.ITEM_FRAME), 0.0F);
        }

        if (itemstack != null && this.random.nextFloat() < this.e) {
            itemstack = itemstack.cloneItemStack();
            this.b(itemstack);
            this.a(itemstack, 0.0F);
        }
    }

    private void b(ItemStack itemstack) {
        if (itemstack != null) {
            if (itemstack.getItem() == Items.MAP) {
                WorldMap worldmap = ((ItemWorldMap) itemstack.getItem()).getSavedMap(itemstack, this.world);

                worldmap.decorations.remove("frame-" + this.getId());
            }

            itemstack.a((EntityItemFrame) null);
        }
    }

    public ItemStack getItem() {
        return this.getDataWatcher().getItemStack(2);
    }

    public void setItem(ItemStack itemstack) {
        if (itemstack != null) {
            itemstack = itemstack.cloneItemStack();
            itemstack.count = 1;
            itemstack.a(this);
        }

        this.getDataWatcher().watch(2, itemstack);
        this.getDataWatcher().update(2);
    }

    public int getRotation() {
        return this.getDataWatcher().getByte(3);
    }

    public void setRotation(int i) {
        this.getDataWatcher().watch(3, Byte.valueOf((byte) (i % 4)));
    }

    public void b(NBTTagCompound nbttagcompound) {
        if (this.getItem() != null) {
            nbttagcompound.set("Item", this.getItem().save(new NBTTagCompound()));
            nbttagcompound.setByte("ItemRotation", (byte) this.getRotation());
            nbttagcompound.setFloat("ItemDropChance", this.e);
        }

        super.b(nbttagcompound);
    }

    public void a(NBTTagCompound nbttagcompound) {
        NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("Item");

        if (nbttagcompound1 != null && !nbttagcompound1.isEmpty()) {
            this.setItem(ItemStack.createStack(nbttagcompound1));
            this.setRotation(nbttagcompound.getByte("ItemRotation"));
            if (nbttagcompound.hasKeyOfType("ItemDropChance", 99)) {
                this.e = nbttagcompound.getFloat("ItemDropChance");
            }
        }

        super.a(nbttagcompound);
    }

    public boolean c(EntityHuman entityhuman) {
        if (this.getItem() == null) {
            ItemStack itemstack = entityhuman.be();

            if (itemstack != null && !this.world.isStatic) {
                this.setItem(itemstack);
                if (!entityhuman.abilities.canInstantlyBuild && --itemstack.count <= 0) {
                    entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, (ItemStack) null);
                }
            }
        } else if (!this.world.isStatic) {
            this.setRotation(this.getRotation() + 1);
        }

        return true;
    }
}