Skip to content

Commit b256c1e

Browse files
committed
Added some reflection to allow using block data values for genbuckets on versions < 1.13.
1 parent a1c00f8 commit b256c1e

File tree

6 files changed

+92
-30
lines changed

6 files changed

+92
-30
lines changed

GenBucketPlugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<artifactId>GenBucketPlugin</artifactId>
1010
<packaging>jar</packaging>
1111
<name>GenBucketPlugin</name>
12-
<version>1.0.3</version>
12+
<version>1.0.4</version>
1313

1414
<repositories>
1515
<repository>

GenBucketPlugin/src/main/java/codes/biscuit/genbucket/timers/GenningTimer.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import codes.biscuit.genbucket.GenBucket;
44
import codes.biscuit.genbucket.utils.Bucket;
5+
import codes.biscuit.genbucket.utils.ReflectionUtils;
56
import org.bukkit.Chunk;
67
import org.bukkit.Material;
78
import org.bukkit.block.Block;
89
import org.bukkit.block.BlockFace;
910
import org.bukkit.entity.Player;
1011
import org.bukkit.scheduler.BukkitRunnable;
1112

13+
import java.lang.reflect.InvocationTargetException;
14+
import java.lang.reflect.Method;
15+
1216
public class GenningTimer extends BukkitRunnable {
1317

1418
private Player p;
@@ -37,7 +41,7 @@ public GenningTimer(Player p, Bucket bucket, Block startingBlock, BlockFace dire
3741
public void run() {
3842
if (blockCounter < limit && !(currentBlock.getY() > main.getConfigValues().getMaxY()) &&
3943
main.getHookUtils().canGenBlock(p, currentBlock.getLocation(), direction != BlockFace.UP && direction != BlockFace.DOWN) &&
40-
(main.getConfigValues().getIgnoredBlockList().contains(currentBlock.getType()) || (bucket.isPatch() && currentBlock.getType() == bucket.getBlockMaterial()))) {
44+
(main.getConfigValues().getIgnoredBlockList().contains(currentBlock.getType()) || (bucket.isPatch() && currentBlock.getType() == bucket.getBlockItem().getType()))) {
4145
if (previousChunk == null || !previousChunk.equals(currentBlock.getChunk())) { // Check every chunk only once for efficiency.
4246
previousChunk = currentBlock.getChunk();
4347
if (!main.getHookUtils().canGenChunk(p, currentBlock.getChunk())) {
@@ -46,7 +50,16 @@ public void run() {
4650
}
4751
}
4852
main.getHookUtils().logBlock(p, currentBlock.getLocation(), currentBlock.getType(), currentBlock.getData());
49-
currentBlock.setType(bucket.getBlockMaterial());
53+
currentBlock.setType(bucket.getBlockItem().getType());
54+
if (!ReflectionUtils.getVersion().contains("1_13")) {
55+
Method setData = ReflectionUtils.getMethod(currentBlock.getClass(), "setData", byte.class);
56+
if (setData != null) {
57+
try {
58+
setData.invoke(currentBlock, bucket.getBlockItem().getData().getData());
59+
} catch (IllegalAccessException | InvocationTargetException ignored) {
60+
}
61+
}
62+
}
5063
if (bucket.isByChunk() && currentBlock.getChunk() != currentBlock.getRelative(direction).getChunk()) {
5164
chunkCounter++;
5265
if (chunkCounter >= main.getConfigValues().getMaxChunks()) cancel();

GenBucketPlugin/src/main/java/codes/biscuit/genbucket/utils/Bucket.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package codes.biscuit.genbucket.utils;
22

3-
import org.bukkit.Material;
43
import org.bukkit.inventory.ItemStack;
54

65
public class Bucket {
76

87
private String id;
98
private ItemStack item;
10-
private Material blockMaterial;
9+
private ItemStack blockItem;
1110
private Direction direction;
1211
private boolean byChunk;
1312
private boolean isPatch;
@@ -42,14 +41,6 @@ void setGuiItem(ItemStack guiItem) {
4241
this.guiItem = guiItem;
4342
}
4443

45-
public Material getBlockMaterial() {
46-
return blockMaterial;
47-
}
48-
49-
void setBlockMaterial(Material blockMaterial) {
50-
this.blockMaterial = blockMaterial;
51-
}
52-
5344
public Direction getDirection() {
5445
return direction;
5546
}
@@ -106,6 +97,14 @@ void setPlacePrice(double placePrice) {
10697
this.placePrice = placePrice;
10798
}
10899

100+
public ItemStack getBlockItem() {
101+
return blockItem;
102+
}
103+
104+
void setBlockItem(ItemStack blockItem) {
105+
this.blockItem = blockItem;
106+
}
107+
109108
@SuppressWarnings("unused")
110109
public enum Direction {
111110
ANY,

GenBucketPlugin/src/main/java/codes/biscuit/genbucket/utils/ConfigValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void loadBuckets() {
2929
main.getUtils().addGlow(item);
3030
}
3131
bucket.setItem(item);
32-
bucket.setBlockMaterial(main.getUtils().materialFromString(main.getConfig().getString("items."+bucketId+".block.material")));
32+
bucket.setBlockItem(main.getUtils().itemFromString(main.getConfig().getString("items."+bucketId+".block.material")));
3333
try {
3434
bucket.setDirection(Bucket.Direction.valueOf(main.getConfig().getString("items."+bucketId+".block.direction").toUpperCase()));
3535
} catch (IllegalArgumentException ex) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package codes.biscuit.genbucket.utils;
2+
3+
import org.bukkit.Bukkit;
4+
5+
import java.lang.reflect.Method;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class ReflectionUtils {
10+
11+
/*
12+
* The server version string to location NMS & OBC classes
13+
*/
14+
private static String versionString;
15+
16+
/*
17+
* Cache of methods that we've found in particular classes
18+
*/
19+
private static Map<Class<?>, Map<String, Method>> loadedMethods = new HashMap<>();
20+
21+
/**
22+
* Gets the version string for NMS & OBC class paths
23+
*
24+
* @return The version string of OBC and NMS packages
25+
*/
26+
public static String getVersion() {
27+
if (versionString == null) {
28+
String name = Bukkit.getServer().getClass().getPackage().getName();
29+
versionString = name.substring(name.lastIndexOf('.') + 1) + ".";
30+
}
31+
32+
return versionString;
33+
}
34+
35+
/**
36+
* Get a method from a class that has the specific paramaters
37+
*
38+
* @param clazz The class we are searching
39+
* @param methodName The name of the method
40+
* @param params Any parameters that the method has
41+
* @return The method with appropriate paramaters
42+
*/
43+
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
44+
if (!loadedMethods.containsKey(clazz)) {
45+
loadedMethods.put(clazz, new HashMap<>());
46+
}
47+
48+
Map<String, Method> methods = loadedMethods.get(clazz);
49+
50+
if (methods.containsKey(methodName)) {
51+
return methods.get(methodName);
52+
}
53+
54+
try {
55+
Method method = clazz.getMethod(methodName, params);
56+
methods.put(methodName, method);
57+
loadedMethods.put(clazz, methods);
58+
return method;
59+
} catch (Exception e) {
60+
e.printStackTrace();
61+
methods.put(methodName, null);
62+
loadedMethods.put(clazz, methods);
63+
return null;
64+
}
65+
}
66+
}

GenBucketPlugin/src/main/java/codes/biscuit/genbucket/utils/Utils.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,6 @@ ItemStack itemFromString(String rawItem) {
6666
return new ItemStack(material, 1, damage);
6767
}
6868

69-
Material materialFromString(String rawMaterial) {
70-
Material material;
71-
String[] rawSplit;
72-
if (rawMaterial.contains(":")) {
73-
rawSplit = rawMaterial.split(":");
74-
} else {
75-
rawSplit = new String[] {rawMaterial};
76-
}
77-
try {
78-
material = Material.valueOf(rawSplit[0]);
79-
} catch (IllegalArgumentException ex) {
80-
material = Material.DIRT;
81-
}
82-
return material;
83-
}
84-
8569
public ItemStack addGlow(ItemStack item) {
8670
item.addUnsafeEnchantment(Enchantment.LUCK, 1);
8771
ItemMeta meta = item.getItemMeta();

0 commit comments

Comments
 (0)