Files
src
build.gradle
settings.gradle
Code
4 127
src/main/java/com/example/goditems/GodItemsPlugin.java
package com.example.goditems;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
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.EquipmentSlotGroup;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Map;
public final class GodItemsPlugin extends JavaPlugin implements CommandExecutor {
private static final Map<String, Integer> ENCHANTMENTS = Map.of(
"DENSITY", 5,
"SHARPNESS", 5,
"KNOCKBACK", 1,
"MENDING", 1,
"UNBREAKING", 3,
"SWEEPING_EDGE", 3,
"WIND_BURST", 2,
"FIRE_ASPECT", 2
);
@Override
public void onEnable() {
if (getCommand("goditems") != null) {
getCommand("goditems").setExecutor(this);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command.");
return true;
}
if (!player.hasPermission("goditems.use")) {
player.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
return true;
}
giveItem(player, createGodItem("POTATO", ChatColor.GOLD + "God Potato"));
giveItem(player, createGodItem("CARROT", ChatColor.YELLOW + "God Carrot"));
player.sendMessage(ChatColor.GREEN + "You received a God Potato and a God Carrot!");
return true;
}
private ItemStack createGodItem(String materialName, String displayName) {
ItemStack item = XMaterial.matchXMaterial(materialName)
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("Missing material: " + materialName));
ItemMeta meta = item.getItemMeta();
if (meta == null) {
throw new IllegalStateException("Material has no item metadata: " + materialName);
}
meta.setDisplayName(displayName);
ENCHANTMENTS.forEach((enchantmentName, level) ->
XEnchantment.matchXEnchantment(enchantmentName)
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), level, true)));
XAttribute.of("GENERIC_ATTACK_DAMAGE").ifPresent(attribute -> meta.addAttributeModifier(
attribute.get(),
new AttributeModifier(
new NamespacedKey(this, "god_produce_attack_damage"),
11.0,
AttributeModifier.Operation.ADD_NUMBER,
EquipmentSlotGroup.MAINHAND)));
item.setItemMeta(meta);
return item;
}
private void giveItem(Player player, ItemStack item) {
Map<Integer, ItemStack> leftovers = player.getInventory().addItem(item);
leftovers.values().forEach(leftover ->
player.getWorld().dropItemNaturally(player.getLocation(), leftover));
}
}