forked from CloudburstMC/Cloudburst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemFrameBlockEntity.java
More file actions
109 lines (90 loc) · 3.02 KB
/
Copy pathItemFrameBlockEntity.java
File metadata and controls
109 lines (90 loc) · 3.02 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
package org.cloudburstmc.server.blockentity.impl;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtMapBuilder;
import org.cloudburstmc.server.block.BlockTypes;
import org.cloudburstmc.server.blockentity.BlockEntityType;
import org.cloudburstmc.server.blockentity.ItemFrame;
import org.cloudburstmc.server.item.ItemStack;
import org.cloudburstmc.server.item.ItemUtils;
import org.cloudburstmc.server.level.chunk.Chunk;
import java.util.Objects;
/**
* Created by Pub4Game on 03.07.2016.
*/
public class ItemFrameBlockEntity extends BaseBlockEntity implements ItemFrame {
private ItemStack item;
private float itemRotation;
private float itemDropChance = 1.0f;
public ItemFrameBlockEntity(BlockEntityType<?> type, Chunk chunk, Vector3i position) {
super(type, chunk, position);
}
@Override
public void loadAdditionalData(NbtMap tag) {
super.loadAdditionalData(tag);
tag.listenForCompound("Item", itemTag -> {
this.item = ItemUtils.deserializeItem(itemTag);
});
tag.listenForFloat("ItemRotation", value -> this.itemRotation = value);
tag.listenForFloat("ItemDropChance", value -> this.itemDropChance = value);
}
@Override
protected void saveClientData(NbtMapBuilder tag) {
super.saveClientData(tag);
if (this.item != null && !this.item.isNull()) {
tag.putCompound("Item", ItemUtils.serializeItem(this.item));
tag.putFloat("ItemRotation", this.itemRotation);
tag.putFloat("ItemDropChance", this.itemDropChance);
}
}
@Override
public boolean isValid() {
return this.getBlockState().getType() == BlockTypes.FRAME;
}
@Override
public int getItemRotation() {
return (int) this.itemRotation;
}
@Override
public void setItemRotation(int itemRotation) {
if (this.itemRotation != itemRotation) {
this.itemRotation = (byte) itemRotation;
this.setDirty();
this.getLevel().updateComparatorOutputLevel(this.getPosition());
}
}
@Override
public ItemStack getItem() {
return this.item;
}
@Override
public void setItem(ItemStack item) {
if (!Objects.equals(this.item, item)) {
this.setDirty();
this.getLevel().updateComparatorOutputLevel(this.getPosition());
}
}
@Override
public float getItemDropChance() {
return this.itemDropChance;
}
@Override
public void setItemDropChance(float itemDropChance) {
if (this.itemDropChance != itemDropChance) {
this.itemDropChance = itemDropChance;
this.setDirty();
}
}
public void setDirty() {
this.spawnToAll();
super.setDirty();
}
@Override
public int getAnalogOutput() {
return this.getItem() == null || this.getItem().isNull() ? 0 : this.getItemRotation() % 8 + 1;
}
@Override
public boolean isSpawnable() {
return true;
}
}