Skip to content

Commit

Permalink
Made XP orb drops now configurable via the config,yml
Browse files Browse the repository at this point in the history
  • Loading branch information
dringewald committed Feb 24, 2025
1 parent 1b87138 commit c96bdbe
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 113 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,18 @@ I really **enjoyed** the plugin, so I decided to **update and improve it further
| `powermining.enchant.plow.all` | Allows enchanting all Plows | `op` |

## **📌 Change Log**
### 🆕 **Latest Update - February 16, 2025**
### 🆕 **Latest Update (v1.2) - February 24, 2025**
- **💠 XP Orbs** - Made a config setting for XP drops
- **🐞 Silk Touch** - Fixed a bug where silk touch still gave XP drops.
- **🛠️ Config Migrator** - Fixed config migrator accidentally deleting recipes.

### **📌 Previous Updates**
#### 🆕 **February 16, 2025**
- **💼 Jobs** - Fixed plugin not working, when [Jobs Reborn](https://www.spigotmc.org/resources/jobs-reborn.4216/) wasn't installed.
- **🔨 Anvils** - Fixed Anvils not adding durability when the Item with lower durability was on the right site.
- **✨ Enchantments** - Fixed Unbreaking enchantment not working correctly.
- **🛠️ Durability** - Fixed Items with enchantments not breaking correctly.

### **📌 Previous Updates**
#### 📅 **February 15, 2025**
- **💼 Jobs support** - Added support for [Jobs Reborn](https://www.spigotmc.org/resources/jobs-reborn.4216/).

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>jodelle.powermining</groupId>
<artifactId>JodellePowerMining</artifactId>
<version>1.1.2</version>
<version>1.2</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -20,7 +20,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<version>3.14.0</version>
<configuration>
<source>21</source>
<target>21</target>
Expand Down Expand Up @@ -112,7 +112,7 @@
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.13-SNAPSHOT</version>
<version>7.0.13</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
85 changes: 53 additions & 32 deletions src/main/java/jodelle/powermining/listeners/BlockBreakListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -21,6 +23,7 @@
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Random;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -60,83 +63,83 @@ public BlockBreakListener(@Nonnull PowerMining plugin) {
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
ItemStack handItem = player.getInventory().getItemInMainHand();

// Ensure the player is holding an item
if (handItem == null || handItem.getType() == Material.AIR) {
return;
}

// Debug message if tool has a custom name
if (handItem.getItemMeta() != null && handItem.getItemMeta().hasDisplayName()) {
debuggingMessages.sendConsoleMessage(
ChatColor.RED + "Broke a block with item: " + handItem.getItemMeta().getDisplayName());
}

// Perform basic verifications (permissions, tool type, sneaking)
if (basicVerifications(player, handItem)) {
return;
}

final Block centerBlock = event.getBlock();
final String playerName = player.getName();
final PlayerInteractListener pil = (plugin.getPlayerInteractHandler() != null)
? plugin.getPlayerInteractHandler().getListener()
final PlayerInteractListener pil = (plugin.getPlayerInteractHandler() != null)
? plugin.getPlayerInteractHandler().getListener()
: null;

if (pil == null) {
debuggingMessages.sendConsoleMessage(ChatColor.RED + "PlayerInteractListener is null.");
return;
}

final BlockFace blockFace = pil.getBlockFaceByPlayerName(playerName);

int radius = Math.max(0, plugin.getConfig().getInt("Radius", Reference.RADIUS) - 1);
int depth = Math.max(0, plugin.getConfig().getInt("Depth", Reference.DEPTH) - 1);

List<Block> surroundingBlocks = PowerUtils.getSurroundingBlocks(blockFace, centerBlock, radius, depth);

if (surroundingBlocks.isEmpty()) {
debuggingMessages.sendConsoleMessage(ChatColor.RED + "No surrounding blocks found.");
return;
}

// Handle durability reduction for the main block first
if (player.getGameMode().equals(GameMode.SURVIVAL)) {
PowerUtils.reduceDurability(player, handItem);

// Schedule a delayed inventory update to ensure proper tool breaking
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
player.updateInventory();
}
}, 1L);
}

for (Block block : surroundingBlocks) {
int exp = checkAndBreakBlock(player, handItem, block);

// Handle durability reduction per surrounding block
if (player.getGameMode().equals(GameMode.SURVIVAL)) {
PowerUtils.reduceDurability(player, handItem);

// Schedule a delayed inventory update to prevent tool reappearing
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
player.updateInventory();
}
}, 1L);
}

// Handle XP drops
if (exp > 0) {
BlockExpEvent expEvent = new BlockExpEvent(block, exp);
plugin.getServer().getPluginManager().callEvent(expEvent);

ExperienceOrb orb = block.getWorld().spawn(block.getLocation(), ExperienceOrb.class);
orb.setExperience(expEvent.getExpToDrop());
}
}
}
}

/**
* Checks and breaks a block if it is compatible with the PowerTool.
Expand Down Expand Up @@ -172,18 +175,30 @@ private int checkAndBreakBlock(Player player, ItemStack handItem, @Nonnull Block
.sendConsoleMessage(ChatColor.GREEN + "✅ Jobs Reborn notified for block: " + block.getType());
}

// XP Drops
int expToDrop = switch (blockMat) {
case COAL_ORE, DEEPSLATE_COAL_ORE -> (int) (Math.random() * 3); // 0-2 XP
case DIAMOND_ORE, DEEPSLATE_DIAMOND_ORE, EMERALD_ORE, DEEPSLATE_EMERALD_ORE ->
3 + (int) (Math.random() * 5); // 3-7 XP
case REDSTONE_ORE, DEEPSLATE_REDSTONE_ORE, LAPIS_ORE, DEEPSLATE_LAPIS_ORE ->
2 + (int) (Math.random() * 4); // 2-6 XP
case NETHER_QUARTZ_ORE -> 2 + (int) (Math.random() * 3); // 2-5 XP
case NETHER_GOLD_ORE -> 1 + (int) (Math.random() * 5); // 1-5 XP
case SPAWNER -> 15 + (int) (Math.random() * 30); // 15-43 XP
default -> 0; // No XP for blocks not listed
};
// Retrieve XP drop configuration for this block
int expToDrop = 0;
try {
ConfigurationSection xpDropsSection = plugin.getConfig().getConfigurationSection("xp-drops");
if (xpDropsSection == null) {
plugin.getLogger().warning("XP drops configuration section 'xp-drops' not found. Using default XP of 0 for " + blockMat);
} else {
ConfigurationSection blockSection = xpDropsSection.getConfigurationSection(blockMat.toString());
if (blockSection == null) {
debuggingMessages.sendConsoleMessage("XP drop configuration for block " + blockMat + " not found. Using default XP of 0.");
} else {
int minXp = blockSection.getInt("min", 0);
int maxXp = blockSection.getInt("max", minXp);
if (maxXp < minXp) {
plugin.getLogger().warning("Invalid XP configuration for block " + blockMat + ": max (" + maxXp + ") is less than min (" + minXp + "). Using default XP of 0.");
} else {
// Calculate a random XP value within the range (inclusive)
expToDrop = minXp + new Random().nextInt(maxXp - minXp + 1);
}
}
}
} catch (Exception e) {
plugin.getLogger().severe("Error while retrieving XP drop configuration for block " + blockMat + ": " + e.getMessage());
}

// Break the block naturally if conditions are met
if (block.breakNaturally(handItem) && player.getGameMode().equals(GameMode.SURVIVAL)) {
Expand All @@ -192,7 +207,13 @@ private int checkAndBreakBlock(Player player, ItemStack handItem, @Nonnull Block
}
}

return expToDrop; // Return XP only for eligible blocks
if (handItem != null && handItem.getItemMeta() != null && handItem.getItemMeta().hasEnchant(Enchantment.SILK_TOUCH)) {
debuggingMessages.sendConsoleMessage("Silk Touch detected on tool; no XP will be dropped for block " + blockMat);
return expToDrop = 0;
}
else {
return expToDrop;
}
}

return 0; // Return 0 XP if conditions aren't met
Expand Down
Loading

0 comments on commit c96bdbe

Please sign in to comment.