Skip to content

Commit 663aad5

Browse files
committed
Replaced reflection with abstraction (because reflection bad and slow!11). Downwards buckets with gravity now have an option to be guided with a block under. All config values are now cached so performance has increased 1000000% (not literally but still should be faster xd).
1 parent b256c1e commit 663aad5

File tree

15 files changed

+312
-299
lines changed

15 files changed

+312
-299
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package codes.biscuit.genbucket.hooks;
2+
3+
import org.bukkit.block.Block;
4+
5+
public interface MinecraftAbstraction {
6+
7+
void setBlockData(Block block, byte data);
8+
}

GenBucketPlugin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@
7575
<version>1.9.0</version>
7676
<scope>provided</scope>
7777
</dependency>
78+
<dependency>
79+
<artifactId>MC_1_8</artifactId>
80+
<groupId>codes.biscuit.genbucket</groupId>
81+
<version>MC_1_8</version>
82+
<scope>compile</scope>
83+
</dependency>
7884
<dependency>
7985
<artifactId>WG_6</artifactId>
8086
<groupId>codes.biscuit.genbucket</groupId>

GenBucketPlugin/src/main/java/codes/biscuit/genbucket/GenBucket.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class GenBucket extends JavaPlugin {
1616
private Utils utils;
1717
private HookUtils hookUtils;
1818
private BucketManager bucketManager;
19+
private boolean underOneThirteen = false;
1920

2021
@Override
2122
public void onEnable() {
@@ -32,6 +33,11 @@ public void onEnable() {
3233
utils.registerRecipes();
3334
utils.updateConfig();
3435
configValues.loadBuckets();
36+
String bukkitVersion = Bukkit.getVersion();
37+
bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1);
38+
if (!bukkitVersion.equals("1.13")) {
39+
underOneThirteen = true;
40+
}
3541
}
3642

3743
public ConfigValues getConfigValues() {
@@ -49,4 +55,8 @@ public HookUtils getHookUtils() {
4955
public BucketManager getBucketManager() {
5056
return bucketManager;
5157
}
58+
59+
public boolean isUnderOneThirteen() {
60+
return underOneThirteen;
61+
}
5262
}

GenBucketPlugin/src/main/java/codes/biscuit/genbucket/hooks/HookUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public HookUtils(GenBucket main) {
4848
main.getLogger().info("Hooked into CoreProtect");
4949
enabledHooks.put(Hooks.COREPROTECT, new CoreProtectHook());
5050
}
51+
if (main.isUnderOneThirteen()) {
52+
main.getLogger().info("Hooked into Minecraft < 1.13");
53+
enabledHooks.put(Hooks.MINECRAFTONEEIGHT, new Minecraft_1_8());
54+
}
55+
}
56+
57+
public void setData(Block block, byte data) {
58+
if (enabledHooks.containsKey(Hooks.MINECRAFTONEEIGHT)) {
59+
MinecraftAbstraction minecraftHook = (MinecraftAbstraction)enabledHooks.get(Hooks.MINECRAFTONEEIGHT);
60+
minecraftHook.setBlockData(block, data);
61+
}
5162
}
5263

5364
public boolean canPlaceHere(Player p, Location loc) {
@@ -248,6 +259,7 @@ enum Hooks {
248259
MASSIVECOREFACTIONS,
249260
COREPROTECT,
250261
WORLDGUARD,
251-
WORLDBORDER
262+
WORLDBORDER,
263+
MINECRAFTONEEIGHT
252264
}
253265
}

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

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

33
import codes.biscuit.genbucket.GenBucket;
44
import codes.biscuit.genbucket.utils.Bucket;
5-
import codes.biscuit.genbucket.utils.ReflectionUtils;
65
import org.bukkit.Chunk;
76
import org.bukkit.Material;
87
import org.bukkit.block.Block;
98
import org.bukkit.block.BlockFace;
109
import org.bukkit.entity.Player;
1110
import org.bukkit.scheduler.BukkitRunnable;
1211

13-
import java.lang.reflect.InvocationTargetException;
14-
import java.lang.reflect.Method;
15-
1612
public class GenningTimer extends BukkitRunnable {
1713

1814
private Player p;
@@ -36,12 +32,13 @@ public GenningTimer(Player p, Bucket bucket, Block startingBlock, BlockFace dire
3632
this.limit = limit;
3733
}
3834

39-
@SuppressWarnings("deprecation")
35+
@SuppressWarnings({"deprecation"})
4036
@Override
4137
public void run() {
4238
if (blockCounter < limit && !(currentBlock.getY() > main.getConfigValues().getMaxY()) &&
4339
main.getHookUtils().canGenBlock(p, currentBlock.getLocation(), direction != BlockFace.UP && direction != BlockFace.DOWN) &&
44-
(main.getConfigValues().getIgnoredBlockList().contains(currentBlock.getType()) || (bucket.isPatch() && currentBlock.getType() == bucket.getBlockItem().getType()))) {
40+
(main.getConfigValues().getIgnoredBlockList().contains(currentBlock.getType()) || (bucket.isPatch() && currentBlock.getType() == bucket.getBlockItem().getType()) ||
41+
(bucket.getBlockItem().getType().hasGravity() && direction == BlockFace.DOWN && main.getConfigValues().addBlockUnderGravity() && currentBlock.getType() == main.getConfigValues().getGravityBlock().getType()))) {
4542
if (previousChunk == null || !previousChunk.equals(currentBlock.getChunk())) { // Check every chunk only once for efficiency.
4643
previousChunk = currentBlock.getChunk();
4744
if (!main.getHookUtils().canGenChunk(p, currentBlock.getChunk())) {
@@ -51,12 +48,17 @@ public void run() {
5148
}
5249
main.getHookUtils().logBlock(p, currentBlock.getLocation(), currentBlock.getType(), currentBlock.getData());
5350
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) {
51+
if (main.isUnderOneThirteen()) {
52+
main.getHookUtils().setData(currentBlock, bucket.getBlockItem().getData().getData());
53+
}
54+
if (bucket.getBlockItem().getType().hasGravity() && direction == BlockFace.DOWN && main.getConfigValues().addBlockUnderGravity()) {
55+
Block underblock = currentBlock.getRelative(BlockFace.DOWN);
56+
if (!(underblock.getY() > main.getConfigValues().getMaxY()) &&
57+
main.getHookUtils().canGenBlock(p, underblock.getLocation(), false) &&
58+
(main.getConfigValues().getIgnoredBlockList().contains(underblock.getType()) || (bucket.isPatch() && underblock.getType() == bucket.getBlockItem().getType()))) {
59+
underblock.setType(main.getConfigValues().getGravityBlock().getType());
60+
if (main.isUnderOneThirteen()) {
61+
main.getHookUtils().setData(underblock, main.getConfigValues().getGravityBlock().getData().getData());
6062
}
6163
}
6264
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.bukkit.inventory.ItemStack;
44

5+
import java.util.List;
6+
import java.util.Map;
7+
58
public class Bucket {
69

710
private String id;
@@ -10,12 +13,14 @@ public class Bucket {
1013
private Direction direction;
1114
private boolean byChunk;
1215
private boolean isPatch;
13-
// private int width;
1416
private ItemStack guiItem;
1517
private int slot;
1618
private double buyPrice;
1719
private boolean infinite;
1820
private double placePrice;
21+
private Map<Character, ItemStack> ingredients;
22+
private List<String> recipeShape;
23+
private int recipeAmount;
1924

2025
Bucket(String id) {
2126
this.id = id;
@@ -105,6 +110,30 @@ void setBlockItem(ItemStack blockItem) {
105110
this.blockItem = blockItem;
106111
}
107112

113+
List<String> getRecipeShape() {
114+
return recipeShape;
115+
}
116+
117+
void setRecipeShape(List<String> recipeShape) {
118+
this.recipeShape = recipeShape;
119+
}
120+
121+
Map<Character, ItemStack> getIngredients() {
122+
return ingredients;
123+
}
124+
125+
void setIngredients(Map<Character, ItemStack> ingredients) {
126+
this.ingredients = ingredients;
127+
}
128+
129+
int getRecipeAmount() {
130+
return recipeAmount;
131+
}
132+
133+
void setRecipeAmount(int recipeAmount) {
134+
this.recipeAmount = recipeAmount;
135+
}
136+
108137
@SuppressWarnings("unused")
109138
public enum Direction {
110139
ANY,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Bucket matchBucket(ItemStack item) {
2727
for (Map.Entry<String, Bucket> entry : buckets.entrySet()) {
2828
Bucket bucket = getBucket(entry.getKey());
2929
ItemStack otherItem = bucket.getItem();
30-
if (otherItem.getType() == item.getType() && ((item.hasItemMeta() && otherItem.hasItemMeta()) || (!item.hasItemMeta() && !otherItem.hasItemMeta()))
30+
if (otherItem.getType() == item.getType() && ((item.hasItemMeta() && otherItem.getItemMeta().hasDisplayName()) || (!item.hasItemMeta() && !otherItem.getItemMeta().hasDisplayName()))
3131
&& otherItem.getItemMeta().getDisplayName().equals(item.getItemMeta().getDisplayName()) && ((!otherItem.getItemMeta().hasLore() && !item.getItemMeta().hasLore()) || (otherItem.getItemMeta().getLore().containsAll(item.getItemMeta().getLore())))) {
3232
return bucket;
3333
}

0 commit comments

Comments
 (0)