Files
src
build.gradle
settings.gradle
Code
5 207
src/main/java/com/kodari/witherskull/WitherSkullPlugin.java
package com.kodari.witherskull;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ThreadLocalRandom;
public final class WitherSkullPlugin extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private static final double DEFAULT_CHANCE = 25.0;
private static final String SKULL_MATERIAL_NAME = "WITHER_SKELETON_SKULL";
private double skullDropChance;
@Override
public void onEnable() {
saveDefaultConfig();
reloadSkullDropChance();
Bukkit.getPluginManager().registerEvents(this, this);
if (getCommand("witherskull") != null) {
getCommand("witherskull").setExecutor(this);
getCommand("witherskull").setTabCompleter(this);
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onWitherSkeletonDeath(EntityDeathEvent event) {
if (event.getEntityType() != EntityType.WITHER_SKELETON || event.getEntity().getKiller() == null) {
return;
}
XMaterial skullMaterial = XMaterial.matchXMaterial(SKULL_MATERIAL_NAME).orElse(null);
if (skullMaterial == null) {
return;
}
event.getDrops().removeIf(drop -> XMaterial.matchXMaterial(drop.getType()) == skullMaterial);
if (ThreadLocalRandom.current().nextDouble(100.0) >= skullDropChance) {
return;
}
ItemStack skull = skullMaterial.parseItem();
if (skull != null) {
event.getDrops().add(skull);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
if (!sender.hasPermission("witherskull.reload")) {
sender.sendMessage("§cYou do not have permission to reload this plugin.");
return true;
}
reloadConfig();
reloadSkullDropChance();
sender.sendMessage("§aWitherSkull configuration reloaded. Current chance: §e" + skullDropChance + "%");
return true;
}
if (args.length >= 1 && args[0].equalsIgnoreCase("chance")) {
if (!sender.hasPermission("witherskull.chance")) {
sender.sendMessage("§cYou do not have permission to view or change the skull drop chance.");
return true;
}
if (args.length == 1) {
sender.sendMessage("§aWither Skeleton skull drop chance: §e" + skullDropChance + "%");
return true;
}
if (args.length != 2) {
sendUsage(sender, label);
return true;
}
final double newChance;
try {
newChance = Double.parseDouble(args[1]);
} catch (NumberFormatException exception) {
sender.sendMessage("§cEnter a valid percentage from 0 to 100.");
return true;
}
if (!isValidChance(newChance)) {
sender.sendMessage("§cThe percentage must be between 0 and 100.");
return true;
}
skullDropChance = newChance;
getConfig().set("skull-drop-chance", newChance);
saveConfig();
sender.sendMessage("§aWither Skeleton skull drop chance set to §e" + skullDropChance + "%§a.");
return true;
}
sendUsage(sender, label);
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
List<String> suggestions = new ArrayList<>();
if (args.length == 1) {
if (sender.hasPermission("witherskull.reload")) {
suggestions.add("reload");
}
if (sender.hasPermission("witherskull.chance")) {
suggestions.add("chance");
}
return filterSuggestions(suggestions, args[0]);
}
if (args.length == 2 && args[0].equalsIgnoreCase("chance")
&& sender.hasPermission("witherskull.chance")) {
return filterSuggestions(List.of("0", "5", "25", "50", "100"), args[1]);
}
return suggestions;
}
private void reloadSkullDropChance() {
Object configuredValue = getConfig().get("skull-drop-chance");
if (configuredValue instanceof Number number && isValidChance(number.doubleValue())) {
skullDropChance = number.doubleValue();
} else {
skullDropChance = DEFAULT_CHANCE;
}
}
private boolean isValidChance(double chance) {
return Double.isFinite(chance) && chance >= 0.0 && chance <= 100.0;
}
private List<String> filterSuggestions(List<String> choices, String input) {
String prefix = input.toLowerCase(Locale.ROOT);
return choices.stream().filter(choice -> choice.startsWith(prefix)).toList();
}
private void sendUsage(CommandSender sender, String label) {
sender.sendMessage("§cUsage: /" + label + " <reload|chance [percentage]>");
}
}