/
BlockDataImpl.java
101 lines (85 loc) · 3.18 KB
/
BlockDataImpl.java
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
package com.denizenscript.denizen.nms.v1_12.impl.blocks;
import com.denizenscript.denizen.nms.v1_12.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.nms.interfaces.BlockData;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTagBuilder;
import net.minecraft.server.v1_12_R1.BlockPosition;
import net.minecraft.server.v1_12_R1.NBTTagCompound;
import net.minecraft.server.v1_12_R1.TileEntity;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
public class BlockDataImpl implements BlockData {
private Material material;
private byte data;
public BlockDataImpl() {
}
public BlockDataImpl(Material mat, byte dat) {
material = mat;
data = dat;
}
public BlockDataImpl(Block block) {
material = block.getType();
data = block.getData();
TileEntity te = ((CraftWorld) block.getWorld()).getHandle().getTileEntity(
new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (te != null) {
NBTTagCompound compound = new NBTTagCompound();
te.save(compound);
ctag = (CompoundTagImpl) CompoundTagImpl.fromNMSTag(compound);
}
}
public void setBlock(Block block, boolean physics) {
block.setTypeIdAndData(material.getId(), data, physics);
if (ctag != null) {
CompoundTagBuilder builder = ctag.createBuilder();
builder.putInt("x", block.getX());
builder.putInt("y", block.getY());
builder.putInt("z", block.getZ());
ctag = (CompoundTagImpl) builder.build();
// TODO: make this work!
BlockPosition blockPos = new BlockPosition(block.getX(), block.getY(), block.getZ());
TileEntity te = ((CraftWorld) block.getWorld()).getHandle().getTileEntity(blockPos);
te.load(ctag.toNMSTag());
}
}
public String toCompressedFormat() {
return "{" + material.getId() + ":" + data + "}";
}
public static BlockData fromCompressedString(String str) {
BlockData data = new BlockDataImpl();
String inner = str.substring(1, str.length() - 1);
String[] datas = inner.split(":");
data.setMaterial(Material.getMaterial(Integer.parseInt(datas[0])));
data.setData(Byte.parseByte(datas[1]));
if (data.getMaterial() == null) {
throw new RuntimeException("Null material: " + datas[0]);
}
return data;
}
CompoundTagImpl ctag = null;
@Override
public CompoundTag getCompoundTag() {
return ctag;
}
@Override
public void setCompoundTag(CompoundTag tag) {
ctag = (CompoundTagImpl) tag;
}
@Override
public Material getMaterial() {
return material;
}
@Override
public void setMaterial(Material material) {
this.material = material;
}
@Override
public byte getData() {
return data;
}
@Override
public void setData(byte data) {
this.data = data;
}
}