Skip to content

Commit 0e6b3d6

Browse files
committed
Extract method + Small Refactor
1 parent 2fd0f8a commit 0e6b3d6

File tree

1 file changed

+39
-30
lines changed
  • src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids

1 file changed

+39
-30
lines changed

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/MinerAndroid.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,50 +75,59 @@ public AndroidType getAndroidType() {
7575
protected void dig(Block b, BlockMenu menu, Block block) {
7676
Collection<ItemStack> drops = block.getDrops(effectivePickaxe);
7777

78-
if (!SlimefunTag.UNBREAKABLE_MATERIALS.isTagged(block.getType()) && !drops.isEmpty()) {
79-
OfflinePlayer owner = Bukkit.getOfflinePlayer(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")));
78+
if (!canBreakBlock(block, b)) {
79+
return;
80+
}
8081

81-
if (Slimefun.getProtectionManager().hasPermission(owner, block.getLocation(), Interaction.BREAK_BLOCK)) {
82-
AndroidMineEvent event = new AndroidMineEvent(block, new AndroidInstance(this, b));
83-
Bukkit.getPluginManager().callEvent(event);
82+
AndroidMineEvent event = new AndroidMineEvent(block, new AndroidInstance(this, b));
83+
Bukkit.getPluginManager().callEvent(event);
8484

85-
if (event.isCancelled()) {
86-
return;
87-
}
85+
if (event.isCancelled()) {
86+
return;
87+
}
8888

89-
// We only want to break non-Slimefun blocks
90-
if (!BlockStorage.hasBlockInfo(block)) {
91-
breakBlock(menu, drops, block);
92-
}
93-
}
89+
// We only want to break non-Slimefun blocks
90+
if (!BlockStorage.hasBlockInfo(block)) {
91+
breakBlock(menu, drops, block);
9492
}
9593
}
9694

95+
@ParametersAreNonnullByDefault
96+
protected boolean canBreakBlock(Block block, Block b) {
97+
Collection<ItemStack> drops = block.getDrops(effectivePickaxe);
98+
if (SlimefunTag.UNBREAKABLE_MATERIALS.isTagged(block.getType()) || drops.isEmpty()) {
99+
return false;
100+
}
101+
102+
String ownerName = BlockStorage.getLocationInfo(b.getLocation(), "owner");
103+
OfflinePlayer owner = Bukkit.getOfflinePlayer(UUID.fromString(ownerName));
104+
if (!Slimefun.getProtectionManager().hasPermission(owner, block.getLocation(), Interaction.BREAK_BLOCK)) {
105+
return false;
106+
}
107+
108+
return true;
109+
}
110+
97111
@Override
98112
@ParametersAreNonnullByDefault
99113
protected void moveAndDig(Block b, BlockMenu menu, BlockFace face, Block block) {
100114
Collection<ItemStack> drops = block.getDrops(effectivePickaxe);
101115

102-
if (!SlimefunTag.UNBREAKABLE_MATERIALS.isTagged(block.getType()) && !drops.isEmpty()) {
103-
OfflinePlayer owner = Bukkit.getOfflinePlayer(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")));
116+
if (!canBreakBlock(block, b)) {
117+
move(b, face, block);
118+
return;
119+
}
104120

105-
if (Slimefun.getProtectionManager().hasPermission(owner, block.getLocation(), Interaction.BREAK_BLOCK)) {
106-
AndroidMineEvent event = new AndroidMineEvent(block, new AndroidInstance(this, b));
107-
Bukkit.getPluginManager().callEvent(event);
121+
AndroidMineEvent event = new AndroidMineEvent(block, new AndroidInstance(this, b));
122+
Bukkit.getPluginManager().callEvent(event);
108123

109-
if (event.isCancelled()) {
110-
return;
111-
}
124+
if (event.isCancelled()) {
125+
return;
126+
}
112127

113-
// We only want to break non-Slimefun blocks
114-
if (!BlockStorage.hasBlockInfo(block)) {
115-
breakBlock(menu, drops, block);
116-
move(b, face, block);
117-
}
118-
} else {
119-
move(b, face, block);
120-
}
121-
} else {
128+
// We only want to break non-Slimefun blocks
129+
if (!BlockStorage.hasBlockInfo(block)) {
130+
breakBlock(menu, drops, block);
122131
move(b, face, block);
123132
}
124133
}

0 commit comments

Comments
 (0)