Files
src
build.gradle
settings.gradle
Code
5 208
src/main/java/com/kodari/fishingloot/FishingLootPlugin.java
package com.kodari.fishingloot;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public final class FishingLootPlugin extends JavaPlugin implements Listener {
private static final Set<String> VANILLA_FISH = Set.of("COD", "SALMON", "TROPICAL_FISH", "PUFFERFISH");
private final List<LootDrop> customLoot = new ArrayList<>();
private double totalCustomChance;
@Override
public void onEnable() {
saveDefaultConfig();
loadCustomLoot();
getServer().getPluginManager().registerEvents(this, this);
}
private void loadCustomLoot() {
customLoot.clear();
totalCustomChance = 0.0;
ConfigurationSection lootSection = getConfig().getConfigurationSection("fishing.custom-loot");
if (lootSection == null) {
getLogger().warning("Missing fishing.custom-loot configuration; custom drops are disabled.");
return;
}
for (String id : lootSection.getKeys(false)) {
ConfigurationSection entry = lootSection.getConfigurationSection(id);
if (entry == null) {
continue;
}
double chance = entry.getDouble("chance", 0.0);
if (!Double.isFinite(chance) || chance <= 0.0) {
getLogger().warning("Ignoring fishing loot entry with invalid chance: " + id);
continue;
}
String command = entry.getString("command", "").trim();
ItemStack item = null;
String material = entry.getString("material", "").trim();
if (!material.isEmpty()) {
item = XMaterial.matchXMaterial(material).map(XMaterial::parseItem).orElse(null);
if (item == null) {
getLogger().warning("Ignoring fishing loot entry with unknown material: " + id);
continue;
}
}
if (item == null && command.isEmpty()) {
getLogger().warning("Fishing loot entry " + id + " has no item or command; its reward will be skipped.");
}
customLoot.add(new LootDrop(id, chance, item, command));
totalCustomChance += chance;
}
if (totalCustomChance > 85.0) {
getLogger().warning("Configured custom fishing chances exceed the 85% vanilla fish share; custom loot can replace all vanilla fish catches.");
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerFish(PlayerFishEvent event) {
if (event.getState() != PlayerFishEvent.State.CAUGHT_FISH || totalCustomChance <= 0.0) {
return;
}
Entity caught = event.getCaught();
if (!(caught instanceof Item caughtItem)) {
return;
}
String caughtMaterial = XMaterial.matchXMaterial(caughtItem.getItemStack().getType()).name();
if (!VANILLA_FISH.contains(caughtMaterial)) {
return;
}
double customChance = Math.min(totalCustomChance, 85.0);
if (ThreadLocalRandom.current().nextDouble(85.0) >= customChance) {
return;
}
LootDrop selected = selectLoot(ThreadLocalRandom.current().nextDouble(totalCustomChance));
if (selected == null) {
return;
}
if (!selected.command().isEmpty()) {
String command = selected.command()
.replace("%player%", event.getPlayer().getName())
.replace("%uuid%", event.getPlayer().getUniqueId().toString());
if (Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)) {
caughtItem.remove();
} else {
getLogger().warning("Could not dispatch configured reward command for fishing loot " + selected.id() + ".");
}
return;
}
if (selected.item() != null) {
caughtItem.setItemStack(selected.item().clone());
}
}
private LootDrop selectLoot(double roll) {
double cumulativeChance = 0.0;
for (LootDrop drop : customLoot) {
cumulativeChance += drop.chance();
if (roll < cumulativeChance) {
return drop;
}
}
return null;
}
private record LootDrop(String id, double chance, ItemStack item, String command) {
}
}