Files
src
build.gradle
settings.gradle
Code
5 332
src/main/java/com/kodari/sclootboost/SCLootBoost.java
package com.kodari.sclootboost;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.world.LootGenerateEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.loot.LootContext;
import org.bukkit.loot.LootTable;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
public final class SCLootBoost extends JavaPlugin implements Listener {
private final Random random = new Random();
@Override
public void onEnable() {
saveDefaultConfig();
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler(ignoreCancelled = true)
public void onLootGenerate(LootGenerateEvent event) {
ConfigurationSection structures = getConfig().getConfigurationSection("structures");
if (structures == null) return;
String key = event.getLootTable().getKey().toString();
for (String id : structures.getKeys(false)) {
ConfigurationSection structure = structures.getConfigurationSection(id);
if (structure != null && key.equalsIgnoreCase(structure.getString("loot-table", ""))) {
event.setLoot(generate(structure, event.getLootContext().getLocation(), event.getLoot()));
return;
}
}
}
private List<ItemStack> generate(ConfigurationSection structure, Location location, Collection<ItemStack> vanilla) {
ConfigurationSection world = structure.getConfigurationSection("worlds." + location.getWorld().getName());
boolean enabled = world != null && world.contains("enabled") ? world.getBoolean("enabled") : structure.getBoolean("enabled", true);
if (!enabled) return new ArrayList<>();
double multiplier = world != null && world.contains("multiplier") ? world.getDouble("multiplier") : structure.getDouble("multiplier", 1.0);
if (!Double.isFinite(multiplier)) multiplier = 1.0;
multiplier = Math.max(0, Math.min(100, multiplier));
ConfigurationSection rules = world != null && world.contains("vanilla-items") ? world.getConfigurationSection("vanilla-items") : structure.getConfigurationSection("vanilla-items");
List<ItemStack> result = new ArrayList<>();
for (ItemStack source : vanilla) {
if (source == null || source.getType().isAir()) continue;
ConfigurationSection rule = rules == null ? null : rules.getConfigurationSection(source.getType().name());
if (rule != null && !rule.getBoolean("enabled", true)) continue;
int amount = source.getAmount();
if (rule != null && (rule.contains("min") || rule.contains("max"))) {
int min = Math.max(0, Math.min(10000, rule.getInt("min", amount)));
int max = Math.max(min, Math.min(10000, rule.getInt("max", amount)));
amount = min + random.nextInt(max - min + 1);
}
addStacks(result, source, amount, multiplier);
}
List<Map<?, ?>> custom = world != null && world.contains("custom-items") ? world.getMapList("custom-items") : structure.getMapList("custom-items");
for (Map<?, ?> entry : custom) {
Object material = entry.get("material");
if (material == null) continue;
ItemStack item = XMaterial.matchXMaterial(material.toString()).map(XMaterial::parseItem).orElse(null);
if (item == null || item.getType().isAir()) continue;
double chance = number(entry.get("chance"), 100);
if (random.nextDouble() * 100 >= chance) continue;
int min = (int) Math.max(0, Math.min(10000, number(entry.get("min"), 1)));
int max = (int) Math.max(min, Math.min(10000, number(entry.get("max"), min)));
ItemMeta meta = item.getItemMeta();
if (meta != null) {
if (entry.get("name") != null) meta.setDisplayName(color(entry.get("name").toString()));
if (entry.get("lore") instanceof List<?>) {
List<String> lore = new ArrayList<>();
for (Object line : (List<?>) entry.get("lore")) lore.add(color(String.valueOf(line)));
meta.setLore(lore);
}
item.setItemMeta(meta);
}
addStacks(result, item, min + random.nextInt(max - min + 1), multiplier);
}
return result;
}
private double number(Object value, double fallback) {
if (value == null) return fallback;
try {
double parsed = Double.parseDouble(value.toString());
return Double.isFinite(parsed) ? parsed : fallback;
} catch (NumberFormatException ex) {
return fallback;
}
}
private void addStacks(List<ItemStack> result, ItemStack source, int amount, double multiplier) {
int total = (int) Math.min(100000, Math.round(amount * multiplier));
int stackSize = Math.max(1, source.getMaxStackSize());
while (total > 0) {
ItemStack stack = source.clone();
stack.setAmount(Math.min(total, stackSize));
result.add(stack);
total -= stack.getAmount();
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("sclootboost.admin")) {
send(sender, "&cAnda tidak memiliki izin.");
return true;
}
if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
send(sender, "&6SCLootBoost: &e/lootboost help|reload|list|info|enable|disable|preview");
send(sender, "&7Gunakan /lootboost <info|enable|disable|preview> <struktur>.");
return true;
}
String action = args[0].toLowerCase(Locale.ROOT);
if (action.equals("reload")) {
reloadConfig();
send(sender, "&aKonfigurasi dimuat ulang.");
return true;
}
ConfigurationSection structures = getConfig().getConfigurationSection("structures");
if (action.equals("list")) {
send(sender, "&6Struktur: &e" + (structures == null ? "Tidak ada" : String.join(", ", structures.getKeys(false))));
return true;
}
if (!action.equals("info") && !action.equals("enable") && !action.equals("disable") && !action.equals("preview")) {
send(sender, "&cPerintah tidak dikenal. Gunakan /lootboost help.");
return true;
}
if (args.length != 2 || structures == null || !structures.isConfigurationSection(args[1])) {
send(sender, "&cGunakan /lootboost " + action + " <struktur>. Lihat /lootboost list.");
return true;
}
ConfigurationSection structure = structures.getConfigurationSection(args[1]);
if (action.equals("enable") || action.equals("disable")) {
boolean enabled = action.equals("enable");
structure.set("enabled", enabled);
saveConfig();
send(sender, "&aStruktur " + args[1] + (enabled ? " diaktifkan" : " dinonaktifkan") + " secara default. Override world tetap berlaku.");
return true;
}
if (action.equals("info")) {
send(sender, "&6" + args[1] + " &7| Loot table: &f" + structure.getString("loot-table", "tidak diatur"));
send(sender, "&7Default: " + (structure.getBoolean("enabled", true) ? "&aaktif" : "&cnonaktif") + " &7| Multiplier: &f" + structure.getDouble("multiplier", 1.0));
ConfigurationSection worlds = structure.getConfigurationSection("worlds");
send(sender, "&7Override world: &f" + (worlds == null || worlds.getKeys(false).isEmpty() ? "tidak ada" : String.join(", ", worlds.getKeys(false))));
return true;
}
if (!(sender instanceof Player)) {
send(sender, "&cPreview hanya dapat digunakan oleh pemain.");
return true;
}
Player player = (Player) sender;
NamespacedKey key = NamespacedKey.fromString(structure.getString("loot-table", ""));
LootTable table = key == null ? null : Bukkit.getLootTable(key);
if (table == null) {
send(sender, "&cLoot table tidak ditemukan. Periksa konfigurasi struktur.");
return true;
}
Location location = player.getLocation();
Collection<ItemStack> vanilla = table.populateLoot(random, new LootContext.Builder(location).build());
List<ItemStack> loot = generate(structure, location, vanilla);
PreviewHolder holder = new PreviewHolder();
Inventory inventory = Bukkit.createInventory(holder, 54, color("&6Preview: " + args[1]));
holder.inventory = inventory;
for (int i = 0; i < Math.min(loot.size(), inventory.getSize()); i++) inventory.setItem(i, loot.get(i));
player.openInventory(inventory);
if (loot.size() > inventory.getSize()) send(player, "&ePreview hanya menampilkan 54 stack pertama dari " + loot.size() + " stack.");
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (!sender.hasPermission("sclootboost.admin")) return Collections.emptyList();
List<String> options = new ArrayList<>();
if (args.length == 1) Collections.addAll(options, "help", "reload", "list", "info", "enable", "disable", "preview");
else if (args.length == 2 && (args[0].equalsIgnoreCase("info") || args[0].equalsIgnoreCase("enable") || args[0].equalsIgnoreCase("disable") || args[0].equalsIgnoreCase("preview"))) {
ConfigurationSection structures = getConfig().getConfigurationSection("structures");
if (structures != null) options.addAll(structures.getKeys(false));