Files
src
Code
2 56
src/main/java/com/kodari/battleaxe/BattleAxePlugin.java
package com.kodari.battleaxe;
import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public final class BattleAxePlugin extends JavaPlugin {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("battleaxe.give")) {
player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
return true;
}
ItemStack axe = XMaterial.matchXMaterial("NETHERITE_AXE").map(XMaterial::parseItem).orElse(null);
Attribute damage = XAttribute.of("attack_damage").map(XAttribute::get).orElse(null);
Attribute speed = XAttribute.of("attack_speed").map(XAttribute::get).orElse(null);
if (axe == null || damage == null || speed == null) {
player.sendMessage(ChatColor.RED + "The battle axe is not supported on this server version.");
return true;
}
ItemMeta meta = axe.getItemMeta();
meta.setDisplayName(ChatColor.GOLD + "Battle Axe");
meta.addAttributeModifier(damage, XAttribute.createModifier("battleaxe:damage", 10.25, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND));
meta.addAttributeModifier(speed, XAttribute.createModifier("battleaxe:speed", -2.4, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND));
axe.setItemMeta(meta);
player.getInventory().addItem(axe).values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
player.sendMessage(ChatColor.GREEN + "You received a battle axe.");
return true;
}
}