Skip to content

Commit

Permalink
Merge pull request #20 from ChafficPlugins/18-bugfix-farming-xp-with-…
Browse files Browse the repository at this point in the history
…guarded-blocks

#18 fix: fixed a bug where blocks dropped xp even though the break event was cancelled
  • Loading branch information
Chafficui authored May 18, 2022
2 parents e19af94 + 8b12fb8 commit 9ce783a
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.chafficplugins.mininglevels.api.MiningLevel;
import de.chafficplugins.mininglevels.api.MiningPlayer;
import de.chafficplugins.mininglevels.utils.MathUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -84,22 +85,29 @@ public void onBlockBreak(final BlockBreakEvent event) {
sendDebug(event.getPlayer(), "BlockBreakEvent: " + "Config options disallow block to drop xp.");
return;
}
miningPlayer.alterXp(block.getXp());
MiningLevel level = miningPlayer.getLevel();
ItemStack itemInUse = event.getPlayer().getInventory().getItemInMainHand();
if (!isMiningItem(itemInUse.getType())) {
sendDebug(event.getPlayer(), "BlockBreakEvent: " + "The held " + itemInUse.getType() + " item is not a mining item.");
return;
}

if (MathUtils.randomDouble(0, 100) < level.getExtraOreProbability()) {
Block actualBlock = event.getBlock();
int amount = (int) MathUtils.randomDouble(1, level.getMaxExtraOre());
for (int i = 0; i < amount; i++) {
event.getPlayer().getWorld().dropItemNaturally(actualBlock.getLocation(), actualBlock.getDrops().iterator().next());
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (event.isCancelled()) {
sendDebug(event.getPlayer(), "BlockBreakEvent: " + "Event cancelled.");
return;
}
sendDebug(event.getPlayer(), "BlockBreakEvent: " + "Dropped " + amount + " extra ores.");
}
miningPlayer.alterXp(block.getXp());
MiningLevel level = miningPlayer.getLevel();

if (MathUtils.randomDouble(0, 100) < level.getExtraOreProbability()) {
Block actualBlock = event.getBlock();
int amount = (int) MathUtils.randomDouble(1, level.getMaxExtraOre());
for (int i = 0; i < amount; i++) {
event.getPlayer().getWorld().dropItemNaturally(actualBlock.getLocation(), actualBlock.getDrops().iterator().next());
}
sendDebug(event.getPlayer(), "BlockBreakEvent: " + "Dropped " + amount + " extra ores.");
}
} , 2L);
}
} else {
sendDebug(event.getPlayer(), "BlockBreakEvent: " + "Error: Player is not registered to the plugin!");
Expand Down

0 comments on commit 9ce783a

Please sign in to comment.