Files
src
build.gradle
settings.gradle
Code
4 149
src/main/java/com/kodari/starrysword/StarrySkyShortSwordPlugin.java
package com.kodari.starrysword;
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.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public final class StarrySkyShortSwordPlugin extends org.bukkit.plugin.java.JavaPlugin implements CommandExecutor {
private NamespacedKey swordKey;
@Override
public void onEnable() {
swordKey = new NamespacedKey(this, "starry_sky_short_sword");
getCommand("starsword").setExecutor(this);
registerRecipe();
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&c只有玩家可以获得星空短剑。"));
return true;
}
player.getInventory().addItem(createSword());
player.sendMessage(color("&8✦ &5✦ &d星空短剑 &5✦ &8✦ &7已进入你的背包。"));
return true;
}
private ItemStack createSword() {
ItemStack sword = XMaterial.matchXMaterial("NETHERITE_SWORD")
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("NETHERITE_SWORD is unavailable"));
ItemMeta meta = sword.getItemMeta();
meta.setDisplayName(color("&8✦ &5✦ &d星空短剑 &5✦ &8✦"));
meta.setLore(Arrays.asList(
color("&8✦ &5✧ &d✦ &5✧ &8✦"),
color("&7划开夜幕的紫色星光"),
color("&8✦ &5✧ &d✦ &5✧ &8✦")
));
meta.setCustomModelData(24001);
meta.getPersistentDataContainer().set(swordKey, PersistentDataType.BYTE, (byte) 1);
XEnchantment.matchXEnchantment("SHARPNESS")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 5, true));
XEnchantment.matchXEnchantment("UNBREAKING")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 3, true));
sword.setItemMeta(meta);
return sword;
}
private void registerRecipe() {
Material obsidian = material("OBSIDIAN");
Material purpleDye = material("PURPLE_DYE");
Material netherStar = material("NETHER_STAR");
Material amethyst = material("AMETHYST_SHARD");
Material stick = material("STICK");
if (obsidian == null || purpleDye == null || netherStar == null || amethyst == null || stick == null) {
return;
}
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "starry_sky_short_sword"), createSword());
recipe.shape("OPO", "NAN", " S ");
recipe.setIngredient('O', obsidian);
recipe.setIngredient('P', purpleDye);
recipe.setIngredient('N', netherStar);
recipe.setIngredient('A', amethyst);
recipe.setIngredient('S', stick);
getServer().addRecipe(recipe);
}
private Material material(String name) {
return XMaterial.matchXMaterial(name).map(XMaterial::parseMaterial).orElse(null);
}
private String color(String message) {
return ChatColor.translateAlternateColorCodes('&', message);
}
}