halogamer
📎 1 file 2,117 chars
📄
pasted-code-1790865411424.txt
txt 83 lines
▶
Make me a Minecraft Paper 1.21.11 plugin that makes Wither Skeleton skulls much easier to obtain.

### Features

* Increase the Wither Skeleton skull drop chance.
* Make the skull drop chance fully configurable through `config.yml`.
* Allow the server owner to set any drop percentage they want, such as `5.0`, `25.0`, `50.0`, or `100.0`.
* Do not change any other Wither Skeleton drops or mechanics.
* Make sure the configured chance is applied consistently whenever a Wither Skeleton can drop a skull.

### Configuration

Create a simple `config.yml`, for example:

```yaml
skull-drop-chance: 25.0
```

The value should represent the percentage chance of a Wither Skeleton dropping a skull.

### Commands

Add a main command such as:

```text
/witherskull
```

Add these subcommands:

```text
/witherskull reload
```

* Reloads `config.yml` without requiring a server restart.
* Sends a confirmation message to the command sender.

```text
/witherskull chance
```

* Displays the currently configured skull drop chance.

```text
/witherskull chance 
```

* Changes the skull drop chance.
* Saves the new value to `config.yml`.
* Applies the new chance immediately.
* Example:

```text
/witherskull chance 50
```

### Permissions

Add permissions such as:

```text
witherskull.reload
witherskull.chance
```

* `/witherskull reload` requires `witherskull.reload`.
* `/witherskull chance` and `/witherskull chance ` require `witherskull.chance`.
* Give operators access by default.

### Validation

* Prevent invalid percentages below `0` or above `100`.
* Handle invalid command arguments with a helpful usage message.
* Handle invalid config values safely and fall back to a default value.
* Include tab completion for subcommands and sensible percentage suggestions.

### Compatibility

* Target Paper 1.21.11.
* Provide all required plugin files, including `plugin.yml` or `paper-plugin.yml` as appropriate.
* Keep the code clean, organized, and easy to modify.
* Do not alter unrelated Minecraft mechanics.
Kodari AI
KodariOct 1 at 2:37 pm 1
WitherSkullChance
A Paper 1.21.11 plugin that makes Wither Skeleton skulls easier to obtain by overriding the default drop chance with a fully configurable percentage. Features a config.yml with a skull-drop-chance setting, a /witherskull command with reload and chance subcommands, and permission checks for witherskull.reload and witherskull.chance. Includes validation to clamp values between 0 and 100, tab completion for subcommands and percentages, and safe fallback to a default value on invalid config. The plugin applies the configured chance consistently whenever a Wither Skeleton can drop a skull, without altering any other drops or mechanics.
  1. Create the Paper 1.21.11 build and plugin metadata.

  2. Implement configurable skull-drop replacement, validated commands, permissions, and tab completion.

Created the Paper 1.21.11 plugin with configurable skull drops, /witherskull reload, /witherskull chance [percentage], operator-default permissions, validation, and tab completion. The configured chance replaces the ordinary player-kill skull chance; charged-creeper skull drops and other drops remain unchanged.

Kodari can make mistakes. Review important code before using.
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]>");
    }
}