/
BlockHelperImpl.java
252 lines (221 loc) · 9.22 KB
/
BlockHelperImpl.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.denizenscript.denizen.nms.v1_14.helpers;
import com.denizenscript.denizen.nms.v1_14.impl.blocks.BlockDataImpl;
import com.denizenscript.denizen.nms.v1_14.impl.jnbt.CompoundTagImpl;
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.blocks.ModernBlockData;
import com.denizenscript.denizen.nms.interfaces.BlockData;
import com.denizenscript.denizen.nms.interfaces.BlockHelper;
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizen.nms.util.ReflectionHelper;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.server.v1_14_R1.*;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlockState;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftSkull;
import org.bukkit.craftbukkit.v1_14_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_14_R1.util.CraftLegacy;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.material.MaterialData;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class BlockHelperImpl implements BlockHelper {
@Override
public List<Location> getBlocksList(PortalCreateEvent event) {
List<Location> blocks = new ArrayList<>();
for (org.bukkit.block.BlockState block : event.getBlocks()) {
blocks.add(block.getLocation());
}
return blocks;
}
public <T extends TileEntity> T getTE(CraftBlockEntityState<T> cbs) {
try {
Field f = CraftBlockEntityState.class.getDeclaredField("tileEntity");
f.setAccessible(true);
return (T) f.get(cbs);
}
catch (IllegalAccessException e) {
Debug.echoError(e);
}
catch (NoSuchFieldException e) {
Debug.echoError(e);
}
return null;
}
@Override
public int idFor(Material mat) {
if (mat.isLegacy()) {
return mat.getId();
}
return CraftLegacy.toLegacy(mat).getId();
}
@Override
public MaterialData getFlowerpotContents(Block block) {
throw new UnsupportedOperationException("As of Minecraft version 1.13 potted flowers each have their own material, such as POTTED_CACTUS.");
}
@Override
public void setFlowerpotContents(Block block, MaterialData data) {
throw new UnsupportedOperationException("As of Minecraft version 1.13 potted flowers each have their own material, such as POTTED_CACTUS.");
}
@Override
public PlayerProfile getPlayerProfile(Skull skull) {
GameProfile profile = getTE(((CraftSkull) skull)).gameProfile;
if (profile == null) {
return null;
}
String name = profile.getName();
UUID id = profile.getId();
com.mojang.authlib.properties.Property property = Iterables.getFirst(profile.getProperties().get("textures"), null);
return new PlayerProfile(name, id, property != null ? property.getValue() : null);
}
@Override
public void setPlayerProfile(Skull skull, PlayerProfile playerProfile) {
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
if (playerProfile.hasTexture()) {
gameProfile.getProperties().put("textures",
new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
}
TileEntitySkull tileEntity = getTE((CraftSkull) skull);
tileEntity.setGameProfile(gameProfile);
skull.update();
}
@Override
public CompoundTag getNbtData(Block block) {
NMSHandler.getChunkHelper().changeChunkServerThread(block.getWorld());
org.bukkit.block.BlockState state = block.getState();
NMSHandler.getChunkHelper().restoreServerThread(block.getWorld());
TileEntity tileEntity = getTE((CraftBlockEntityState) state);
if (tileEntity == null) {
return null;
}
return CompoundTagImpl.fromNMSTag(tileEntity.b());
}
@Override
public void setNbtData(Block block, CompoundTag compoundTag) {
NMSHandler.getChunkHelper().changeChunkServerThread(block.getWorld());
org.bukkit.block.BlockState state = block.getState();
NMSHandler.getChunkHelper().restoreServerThread(block.getWorld());
TileEntity tileEntity = getTE((CraftBlockEntityState) state);
if (tileEntity == null) {
return;
}
tileEntity.load(((CompoundTagImpl) compoundTag).toNMSTag());
tileEntity.update();
}
@Override
public BlockData getBlockData(Material material, byte data) {
return new BlockDataImpl(material, data);
}
@Override
public BlockData getBlockData(ModernBlockData data) {
return new BlockDataImpl(data.data);
}
@Override
public BlockData getBlockData(Block block) {
return new BlockDataImpl(block);
}
@Override
public BlockData getBlockData(String compressedString) {
return BlockDataImpl.fromCompressedString(compressedString);
}
private static net.minecraft.server.v1_14_R1.Block getBlockFrom(Material material) {
if (material == Material.FLOWER_POT) {
return Blocks.FLOWER_POT;
}
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(material));
if (is == null) {
return null;
}
Item item = is.getItem();
if (!(item instanceof ItemBlock)) {
return null;
}
return ((ItemBlock) item).getBlock();
}
@Override
public boolean hasBlock(Material material) {
return getBlockFrom(material) != null;
}
@Override
public boolean setBlockResistance(Material material, float resistance) {
net.minecraft.server.v1_14_R1.Block block = getBlockFrom(material);
if (block == null) {
return false;
}
// protected final float durability;
ReflectionHelper.setFieldValue(net.minecraft.server.v1_14_R1.Block.class, "durability", block, resistance);
return true;
}
@Override
public float getBlockResistance(Material material) {
net.minecraft.server.v1_14_R1.Block block = getBlockFrom(material);
if (block == null) {
return 0;
}
return ReflectionHelper.getFieldValue(net.minecraft.server.v1_14_R1.Block.class, "durability", block);
}
@Override
public boolean isSafeBlock(Material material) {
// this is presumably more accurate these days
return !material.isSolid();
}
@Override
public org.bukkit.block.BlockState generateBlockState(Material mat) {
return new CraftBlockState(mat);
}
// protected final Material material;
public static final Field BLOCK_MATERIAL = ReflectionHelper.getFields(net.minecraft.server.v1_14_R1.Block.class).get("material");
// private final EnumPistonReaction R;
public static final MethodHandle MATERIAL_PUSH_REACTION_SETTER = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_14_R1.Material.class, "R");
// public final float strength;
public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_14_R1.Block.class, "strength");
public net.minecraft.server.v1_14_R1.Block getMaterialBlock(Material bukkitMaterial) {
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}
public net.minecraft.server.v1_14_R1.Material getInternalMaterial(Material bukkitMaterial) {
try {
return (net.minecraft.server.v1_14_R1.Material) BLOCK_MATERIAL.get(getMaterialBlock(bukkitMaterial));
}
catch (Throwable ex) {
Debug.echoError(ex);
return null;
}
}
@Override
public String getPushReaction(Material mat) {
return getInternalMaterial(mat).getPushReaction().name();
}
@Override
public void setPushReaction(Material mat, String reaction) {
try {
MATERIAL_PUSH_REACTION_SETTER.invoke(getInternalMaterial(mat), EnumPistonReaction.valueOf(reaction));
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
@Override
public float getBlockStength(Material mat) {
return getMaterialBlock(mat).strength;
}
@Override
public void setBlockStrength(Material mat, float strength) {
try {
BLOCK_STRENGTH_SETTER.invoke(getMaterialBlock(mat), strength);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}