Files
src
build.gradle
Code
3 234
src/main/java/com/kodari/maidwearables/MaidWearablesPlugin.java
package com.kodari.maidwearables;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Color;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
import java.util.Map;
public final class MaidWearablesPlugin extends JavaPlugin implements Listener {
private NamespacedKey wearableIdKey;
private List<Palette> palettes;
@Override
public void onEnable() {
wearableIdKey = new NamespacedKey(this, "wearable_id");
palettes = List.of(
new Palette("black", "Black", "BLACK_DYE", 0x29242B),
new Palette("white", "White", "WHITE_DYE", 0xF2EEE7),
new Palette("pink", "Pink", "PINK_DYE", 0xF08DB7),
new Palette("blue", "Blue", "LIGHT_BLUE_DYE", 0x65B9E8),
new Palette("red", "Red", "RED_DYE", 0xC83D43),
new Palette("purple", "Purple", "PURPLE_DYE", 0x9957C8)
);
getServer().getPluginManager().registerEvents(this, this);
registerRecipes();
}
private void registerRecipes() {
for (Palette palette : palettes) {
Color color = Color.fromRGB(palette.rgb());
ItemStack headband = wearable("headband_" + palette.id(), palette.label() + " Maid Headband", "LEATHER_HELMET", color);
shapeless("headband_" + palette.id(), headband, "LEATHER", palette.dye());
ItemStack chestplate = wearable("maid_" + palette.id() + "_chestplate", palette.label() + " Maid Bodice", "LEATHER_CHESTPLATE", color);
shaped("maid_" + palette.id() + "_chestplate", chestplate,
new String[]{"L L", "LDL", "LLL"}, Map.of('L', "LEATHER", 'D', palette.dye()));
ItemStack leggings = wearable("maid_" + palette.id() + "_leggings", palette.label() + " Maid Leggings", "LEATHER_LEGGINGS", color);
shaped("maid_" + palette.id() + "_leggings", leggings,
new String[]{"LLL", "L L", "LDL"}, Map.of('L', "LEATHER", 'D', palette.dye()));
ItemStack boots = wearable("maid_" + palette.id() + "_boots", palette.label() + " Maid Boots", "LEATHER_BOOTS", color);
shaped("maid_" + palette.id() + "_boots", boots,
new String[]{"L L", "LDL"}, Map.of('L', "LEATHER", 'D', palette.dye()));
ItemStack skirt = wearable("skirt_" + palette.id(), palette.label() + " Maid Skirt", "LEATHER_LEGGINGS", color);
shaped("skirt_" + palette.id(), skirt,
new String[]{"LLL", "L L", "D "}, Map.of('L', "LEATHER", 'D', palette.dye()));
ItemStack tail = wearable("tail_" + palette.id(), palette.label() + " Cat Tail", "LEATHER_LEGGINGS", color);
shaped("tail_" + palette.id(), tail,
new String[]{"SDS", " H "}, Map.of('S', "STRING", 'D', palette.dye(), 'H', "RABBIT_HIDE"));
}
ItemStack catEars = wearable("cat_ears", "Cat Ears", "LEATHER_HELMET", Color.fromRGB(0x29242B));
shaped("cat_ears", catEars,
new String[]{"W W", " H "}, Map.of('W', "BLACK_WOOL", 'H', "RABBIT_HIDE"));
shapeless("craft_cat_maid_headband",
wearable("cat_maid_headband", "Cat-Ear Maid Headband", "LEATHER_HELMET", Color.WHITE),
"LEATHER_HELMET", "LEATHER_HELMET");
shapeless("craft_leggingtail",
wearable("leggingtail", "Leggingtail", "LEATHER_LEGGINGS", Color.WHITE),
"LEATHER_LEGGINGS", "LEATHER_LEGGINGS");
}
private ItemStack wearable(String id, String name, String materialName, Color color) {
ItemStack item = new ItemStack(material(materialName));
ItemMeta meta = item.getItemMeta();
if (meta instanceof LeatherArmorMeta leatherMeta) {
leatherMeta.setColor(color);
}
meta.setDisplayName("§f" + name);
meta.setLore(List.of("§7Wearable cosmetic item"));
meta.getPersistentDataContainer().set(wearableIdKey, PersistentDataType.STRING, id);
item.setItemMeta(meta);
return item;
}
private void shaped(String key, ItemStack result, String[] shape, Map<Character, String> ingredients) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, key), result);
recipe.shape(shape);
ingredients.forEach((character, materialName) -> recipe.setIngredient(character, material(materialName)));
getServer().addRecipe(recipe);
}
private void shapeless(String key, ItemStack result, String... ingredients) {
ShapelessRecipe recipe = new ShapelessRecipe(new NamespacedKey(this, key), result);
for (String ingredient : ingredients) {
recipe.addIngredient(material(ingredient));
}
getServer().addRecipe(recipe);
}
private Material material(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElseThrow(() -> new IllegalStateException("Unsupported material: " + name));
}
@EventHandler
public void onPrepareCraft(PrepareItemCraftEvent event) {
String recipeKey = recipeKey(event.getRecipe());
if (!isCombinationRecipe(recipeKey)) {
return;
}
ItemStack result = combinationResult(recipeKey, event.getInventory().getMatrix());
event.getInventory().setResult(result);
}
@EventHandler
public void onCraft(CraftItemEvent event) {
String recipeKey = recipeKey(event.getRecipe());
if (isCombinationRecipe(recipeKey)
&& combinationResult(recipeKey, ((CraftingInventory) event.getInventory()).getMatrix()) == null) {
event.setCancelled(true);
}
}
private String recipeKey(org.bukkit.inventory.Recipe recipe) {
return recipe instanceof Keyed keyed ? keyed.getKey().getKey() : "";
}
private boolean isCombinationRecipe(String key) {
return key.equals("craft_cat_maid_headband") || key.equals("craft_leggingtail");
}
private ItemStack combinationResult(String recipeKey, ItemStack[] matrix) {
ItemStack first = null;
ItemStack second = null;
String firstId = null;
String secondId = null;
for (ItemStack item : matrix) {
String id = wearableId(item);
if (id == null) {
continue;
}
if (first == null) {
first = item;
firstId = id;
} else if (second == null) {
second = item;
secondId = id;
}
}
if (first == null || second == null) {
return null;
}
if (recipeKey.equals("craft_cat_maid_headband")) {
ItemStack band = firstId.startsWith("headband_") ? first : secondId.startsWith("headband_") ? second : null;
String otherId = band == first ? secondId : band == second ? firstId : "";
if (band == null || !otherId.equals("cat_ears")) {
return null;
}
return wearable("cat_maid_headband", "Cat-Ear Maid Headband", "LEATHER_HELMET", armorColor(band));
}
if (recipeKey.equals("craft_leggingtail")) {
String pantsId = isPants(firstId) ? firstId : isPants(secondId) ? secondId : null;
String tailId = firstId.startsWith("tail_") ? firstId : secondId.startsWith("tail_") ? secondId : null;
ItemStack pants = isPants(firstId) ? first : isPants(secondId) ? second : null;
if (pantsId == null || tailId == null || pants == null) {
return null;
}
return wearable("leggingtail", "Leggingtail", "LEATHER_LEGGINGS", armorColor(pants));
}
return null;
}
private boolean isPants(String id) {
return id.startsWith("maid_") && id.endsWith("_leggings") || id.startsWith("skirt_");
}
private String wearableId(ItemStack item) {
if (item == null || item.getType().isAir() || !item.hasItemMeta()) {
return null;
}
return item.getItemMeta().getPersistentDataContainer().get(wearableIdKey, PersistentDataType.STRING);
}
private Color armorColor(ItemStack item) {
if (item.hasItemMeta() && item.getItemMeta() instanceof LeatherArmorMeta leatherMeta) {
return leatherMeta.getColor();