forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockEntityItemFrame.java
More file actions
181 lines (148 loc) · 5.35 KB
/
BlockEntityItemFrame.java
File metadata and controls
181 lines (148 loc) · 5.35 KB
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
package cn.nukkit.blockentity;
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockID;
import cn.nukkit.event.block.ItemFrameDropItemEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.item.RuntimeItems;
import cn.nukkit.level.GameRule;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.LevelEventPacket;
import java.util.concurrent.ThreadLocalRandom;
/**
* Created by Pub4Game on 03.07.2016.
*/
public class BlockEntityItemFrame extends BlockEntitySpawnable {
private Item item_;
public BlockEntityItemFrame(FullChunk chunk, CompoundTag nbt) {
super(chunk, nbt);
}
@Override
protected void initBlockEntity() {
if (!namedTag.contains("Item")) {
namedTag.putCompound("Item", NBTIO.putItemHelper(item_ = new ItemBlock(Block.get(BlockID.AIR))));
}
if (!namedTag.contains("ItemRotation")) {
namedTag.putByte("ItemRotation", 0);
}
if (!namedTag.contains("ItemDropChance")) {
namedTag.putFloat("ItemDropChance", 1.0f);
}
this.level.updateComparatorOutputLevel(this);
super.initBlockEntity();
}
@Override
public String getName() {
return "Item Frame";
}
@Override
public boolean isBlockEntityValid() {
return level.getBlockIdAt(chunk, (int) x, (int) y, (int) z) == Block.ITEM_FRAME_BLOCK;
}
public int getItemRotation() {
return this.namedTag.getByte("ItemRotation");
}
public void setItemRotation(int itemRotation) {
this.namedTag.putByte("ItemRotation", itemRotation);
this.level.updateComparatorOutputLevel(this);
this.setDirty();
}
public Item getItem() {
if (item_ == null) {
CompoundTag NBTTag = this.namedTag.getCompound("Item");
item_ = NBTIO.getItemHelper(NBTTag);
}
return item_;
}
public void setItem(Item item) {
this.setItem(item, true);
}
public void setItem(Item item, boolean setChanged) {
item_ = item;
this.namedTag.putCompound("Item", NBTIO.putItemHelper(item));
if (setChanged) {
this.setDirty();
}
this.level.updateComparatorOutputLevel(this);
}
public float getItemDropChance() {
return this.namedTag.getFloat("ItemDropChance");
}
public void setItemDropChance(float chance) {
this.namedTag.putFloat("ItemDropChance", chance);
super.setDirty(); // No need to spawnToAll
}
@Override
public void setDirty() {
super.setDirty();
this.spawnToAll();
}
@Override
public CompoundTag getSpawnCompound() {
if (!this.namedTag.contains("Item")) {
this.setItem(new ItemBlock(Block.get(BlockID.AIR)), false);
}
CompoundTag itemOriginal = namedTag.getCompound("Item");
CompoundTag tag = new CompoundTag()
.putString("id", this instanceof BlockEntityItemFrameGlow ? BlockEntity.GLOW_ITEM_FRAME : BlockEntity.ITEM_FRAME)
.putInt("x", (int) this.x)
.putInt("y", (int) this.y)
.putInt("z", (int) this.z);
int itemId = itemOriginal.getShort("id");
if (itemId != Item.AIR) {
CompoundTag item;
item = itemOriginal.copy();
item.setName("Item");
String identifier = RuntimeItems.getMapping().toRuntime(itemId, itemOriginal.getShort("Damage")).getIdentifier();
item.putString("Name", identifier);
item.remove("id");
if (itemId == Item.MAP) {
item.getCompound("tag").remove("Colors");
} else {
item.getCompound("tag").remove("Items");
}
tag.putCompound("Item", item)
.putByte("ItemRotation", this.getItemRotation());
}
return tag;
}
public int getAnalogOutput() {
return this.getItem() == null || this.getItem().getId() == 0 ? 0 : this.getItemRotation() % 8 + 1;
}
public boolean dropItem(Player player) {
Item item = this.getItem();
if (item != null && item.getId() != Item.AIR) {
if (player != null) {
ItemFrameDropItemEvent event = new ItemFrameDropItemEvent(player, this.getBlock(), this, item);
this.level.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
this.spawnTo(player);
return true;
}
}
this.setItem(Item.get(Item.AIR));
this.setItemRotation(0);
if (this.getItemDropChance() > ThreadLocalRandom.current().nextFloat()) {
this.level.dropItem(this.add(0.5, 0, 0.5), item);
}
this.level.addLevelEvent(this, LevelEventPacket.EVENT_SOUND_ITEM_FRAME_ITEM_REMOVED);
return true;
}
return false;
}
@Override
public void onBreak() {
Item item = null;
if (level.getGameRules().getBoolean(GameRule.DO_TILE_DROPS)) {
item = getItem();
}
this.namedTag.remove("Item");
item_ = null;
if (item != null && item.getId() != BlockID.AIR) {
level.dropItem(this, item);
}
}
}