Files
src
build.gradle
Code
4 258
src/main/java/com/kodari/mobcatcher/MobCatcherPlugin.java
package com.kodari.mobcatcher;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
public final class MobCatcherPlugin extends JavaPlugin implements CommandExecutor {
private NamespacedKey rodKey;
@Override
public void onEnable() {
rodKey = new NamespacedKey(this, "mob_catcher_rod");
getServer().getPluginManager().registerEvents(new MobCatcherListener(this, rodKey), this);
getCommand("mobcatcher").setExecutor(this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be used by a player.");
return true;
}
Material rodMaterial = XMaterial.matchXMaterial("FISHING_ROD")
.map(XMaterial::parseMaterial)
.orElse(null);
if (rodMaterial == null) {
player.sendMessage(ChatColor.RED + "Fishing rods are unavailable on this server version.");
return true;
}
ItemStack rod = new ItemStack(rodMaterial);
ItemMeta meta = rod.getItemMeta();
if (meta == null) {
player.sendMessage(ChatColor.RED + "Could not create the Mob Catcher rod.");
return true;
}
meta.setDisplayName(ChatColor.GOLD + "Mob Catcher");
meta.setLore(List.of(
ChatColor.GRAY + "Catches dropped items with its hook.",
ChatColor.GRAY + "Hooks entities to capture and release them."
));
meta.getPersistentDataContainer().set(rodKey, PersistentDataType.BYTE, (byte) 1);
XEnchantment.matchXEnchantment("LURE").ifPresent(enchantment ->
meta.addEnchant(enchantment.getEnchant(), 1, true));
rod.setItemMeta(meta);
player.getInventory().addItem(rod).values().forEach(item ->
player.getWorld().dropItemNaturally(player.getLocation(), item));
player.sendMessage(ChatColor.GREEN + "You received a " + ChatColor.GOLD + "Mob Catcher" + ChatColor.GREEN + " rod.");
return true;
}
}