Files
src
build.gradle
settings.gradle
Code
5 218
src/main/java/com/kodari/dashwarden/DashWardenPlugin.java
package com.kodari.dashwarden;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
import java.util.Map;
public final class DashWardenPlugin extends JavaPlugin {
private NamespacedKey weaponKey;
@Override
public void onEnable() {
weaponKey = new NamespacedKey(this, "weapon_type");
registerRecipes();
getServer().getPluginManager().registerEvents(new AbilityListener(this, weaponKey), this);
}
private void registerRecipes() {
addRecipe("dash_sword", createWeapon("dash_sword", "Dash Sword", "Press F to dash up to 9 blocks."),
new String[]{" F ", " F ", " S "}, Map.of('F', "FEATHER", 'S', "DIAMOND_SWORD"));
addRecipe("warden_blade", createWeapon("warden_blade", "Warden Blade", "Press F to fire a 3-damage beam."),
new String[]{" S ", " C ", " E "}, Map.of('S', "DIAMOND_SWORD", 'C', "SCULK_CATALYST", 'E', "ECHO_SHARD"));
addRecipe("thunder_blade", createWeapon("thunder_blade", "Thunder Blade", "Press F to strike for 2.5 damage."),
new String[]{" S ", " R ", " I "}, Map.of('S', "IRON_SWORD", 'R', "LIGHTNING_ROD", 'I', "COPPER_INGOT"));
}
private ItemStack createWeapon(String id, String name, String abilityDescription) {
ItemStack item = XMaterial.matchXMaterial("NETHERITE_SWORD")
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("NETHERITE_SWORD is unavailable"));
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.AQUA + name);
meta.setLore(List.of(ChatColor.GRAY + abilityDescription));
meta.getPersistentDataContainer().set(weaponKey, PersistentDataType.STRING, id);
item.setItemMeta(meta);
return item;
}
private void addRecipe(String id, ItemStack result, String[] shape, Map<Character, String> ingredients) {
NamespacedKey key = new NamespacedKey(this, id);
Bukkit.removeRecipe(key);
ShapedRecipe recipe = new ShapedRecipe(key, result);
recipe.shape(shape);
for (Map.Entry<Character, String> entry : ingredients.entrySet()) {
Material material = XMaterial.matchXMaterial(entry.getValue())
.map(XMaterial::parseMaterial)
.orElse(null);
if (material == null) {
getLogger().warning("Skipping recipe " + id + ": material " + entry.getValue() + " is unavailable.");
return;
}
recipe.setIngredient(entry.getKey(), material);
}
Bukkit.addRecipe(recipe);
}
}