-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscUtilsImpl.java
82 lines (73 loc) · 3.46 KB
/
MiscUtilsImpl.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
package de.cubeside.nmsutils.v1_19_R2;
import de.cubeside.nmsutils.MiscUtils;
import de.cubeside.nmsutils.NMSUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockBehaviour.BlockStateBase;
import net.minecraft.world.level.block.state.BlockBehaviour.Properties;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MaterialColor;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_19_R2.util.CraftMagicNumbers;
public class MiscUtilsImpl implements MiscUtils {
private final NMSUtilsImpl nmsUtils;
private Field fieldBlockBehaviour_properties;
private MaterialColor transparentColor;
private Field fieldBlockStateBase_materialColor;
public MiscUtilsImpl(NMSUtilsImpl nmsUtils) {
this.nmsUtils = nmsUtils;
}
@Override
public NMSUtils getNMSUtils() {
return nmsUtils;
}
@Override
public void setBlockMapColorTransparent(Material m) {
if (fieldBlockBehaviour_properties == null) {
Class<Properties> classBlockProperties = BlockBehaviour.Properties.class;
Class<BlockBehaviour> classBlockBehaviour = BlockBehaviour.class;
fieldBlockBehaviour_properties = null;
for (Field f : classBlockBehaviour.getDeclaredFields()) {
if (f.getType() == classBlockProperties) {
fieldBlockBehaviour_properties = f;
fieldBlockBehaviour_properties.setAccessible(true);
}
}
if (fieldBlockBehaviour_properties == null) {
throw new IllegalStateException("Could not find block properties field!");
}
Class<BlockStateBase> classBlockStateBase = BlockBehaviour.BlockStateBase.class;
for (Field f : classBlockStateBase.getDeclaredFields()) {
if (f.getType() == MaterialColor.class) {
fieldBlockStateBase_materialColor = f;
fieldBlockStateBase_materialColor.setAccessible(true);
}
}
if (fieldBlockStateBase_materialColor == null) {
throw new IllegalStateException("Could not find BlockStateBase materialColor field!");
}
try {
Constructor<MaterialColor> constructorMaterialColor = MaterialColor.class.getDeclaredConstructor(int.class, int.class);
constructorMaterialColor.setAccessible(true);
transparentColor = constructorMaterialColor.newInstance(0, 0);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Could not create custom transparent MaterialColor!");
}
}
if (!m.isBlock()) {
throw new IllegalArgumentException("Material must be a block");
}
Block b = CraftMagicNumbers.getBlock(m);
try {
BlockBehaviour.Properties properties = (Properties) fieldBlockBehaviour_properties.get(b);
properties.color(transparentColor);
for (BlockState state : b.getStateDefinition().getPossibleStates()) {
fieldBlockStateBase_materialColor.set(state, transparentColor);
}
} catch (IllegalAccessException e) {
throw new IllegalStateException("Could not set the MaterialColor!");
}
}
}