diff --git a/src/main/java/com/io/yutian/refinesystem/RefineSystem.java b/src/main/java/com/io/yutian/refinesystem/RefineSystem.java index bd97c85..c2d854b 100644 --- a/src/main/java/com/io/yutian/refinesystem/RefineSystem.java +++ b/src/main/java/com/io/yutian/refinesystem/RefineSystem.java @@ -65,6 +65,13 @@ public final class RefineSystem extends JavaPlugin { NBTItem item = new NBTItem(itemStack1); item.setString("refineType", "雨天洗炼符"); player.getInventory().addItem(item.getItem()); + ItemStack itemStack2 = new ItemStack(Material.EMERALD); + ItemMeta itemMeta2 = itemStack2.getItemMeta(); + itemMeta2.setDisplayName("§f雨天去除洗炼符"); + itemStack2.setItemMeta(itemMeta1); + NBTItem item2 = new NBTItem(itemStack2); + item2.setString("removeRefine", "true"); + player.getInventory().addItem(item2.getItem()); sender.sendMessage("§8§l[§a§l!§8§l] §7获取物品成功"); return true; } diff --git a/src/main/java/com/io/yutian/refinesystem/listener/GuiListener.java b/src/main/java/com/io/yutian/refinesystem/listener/GuiListener.java index f829d48..9d66f28 100644 --- a/src/main/java/com/io/yutian/refinesystem/listener/GuiListener.java +++ b/src/main/java/com/io/yutian/refinesystem/listener/GuiListener.java @@ -1,12 +1,19 @@ package com.io.yutian.refinesystem.listener; +import com.io.yutian.refinesystem.RefineSystemSetting; import com.io.yutian.refinesystem.gui.GuiHolder; +import com.io.yutian.refinesystem.util.ItemStackUtil; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; public class GuiListener implements Listener { @@ -17,6 +24,37 @@ public class GuiListener implements Listener { if (holder != null && holder instanceof GuiHolder) { GuiHolder guiHolder = (GuiHolder) holder; guiHolder.onClick(event); + return; + } + onClick(event); + } + + private void onClick(InventoryClickEvent event) { + if (event.getInventory().getType() == InventoryType.CRAFTING) { + ItemStack cursor = event.getCursor(); + if (!ItemStackUtil.isRemoveRefineItem(cursor)) { + return; + } + ItemStack current = event.getCurrentItem(); + if (current == null || current.getType() == Material.AIR) { + return; + } + event.setCancelled(true); + Player player = (Player) event.getWhoClicked(); + if (!ItemStackUtil.isRefined(current)) { + player.sendMessage("§8§l[§c§l!§8§l] §7该物品无法重置洗练"); + player.playSound(player.getLocation(), Sound.ENTITY_BLAZE_DEATH, 1.6F, 1.8F); + return; + } + if (cursor.getAmount() == 1) { + event.getWhoClicked().setItemOnCursor(null); + } else { + cursor.setAmount(cursor.getAmount() - 1); + } + ItemStack newItemStack = ItemStackUtil.replaceAllLore(current, RefineSystemSetting.getDetectionLore()); + event.setCurrentItem(newItemStack); + player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1); + player.sendMessage("§8§l[§a§l!§8§l] §7成功重置洗练"); } } diff --git a/src/main/java/com/io/yutian/refinesystem/util/ItemStackUtil.java b/src/main/java/com/io/yutian/refinesystem/util/ItemStackUtil.java index 5f6dffd..c7b1e35 100644 --- a/src/main/java/com/io/yutian/refinesystem/util/ItemStackUtil.java +++ b/src/main/java/com/io/yutian/refinesystem/util/ItemStackUtil.java @@ -7,6 +7,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -40,6 +41,17 @@ public class ItemStackUtil { return false; } + public static boolean isRemoveRefineItem(ItemStack itemStack) { + if (itemStack == null || itemStack.getType() == Material.AIR) { + return false; + } + NBTItem nbtItem = new NBTItem(itemStack); + if (nbtItem.hasKey("removeRefine")) { + return true; + } + return false; + } + public static String getRefineType(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return null; @@ -70,6 +82,54 @@ public class ItemStackUtil { return -1; } + public static boolean isRefined(ItemStack itemStack) { + if (itemStack == null || itemStack.getType() == Material.AIR) { + return false; + } + ItemMeta itemMeta = itemStack.getItemMeta(); + if (!itemMeta.hasLore()) { + return false; + } + List lore = itemMeta.getLore(); + for (String s : lore) { + if (s.contains("§7(被动技)")) { + return true; + } + } + return false; + } + + public static ItemStack replaceAllLore(ItemStack itemStack, String newLore) { + if (itemStack == null || itemStack.getType() == Material.AIR) { + return itemStack; + } + ItemMeta itemMeta = itemStack.getItemMeta(); + if (!itemMeta.hasLore()) { + return itemStack; + } + List lore = itemMeta.getLore(); + int index = -1; + for (int i = 0; i < lore.size(); i++) { + String s = lore.get(i); + if (s.contains("§7(被动技)")) { + if (index == -1) { + index = i; + } + } + } + Iterator iterator = lore.iterator(); + while (iterator.hasNext()) { + String s = iterator.next(); + if (s.contains("§7(被动技)")) { + iterator.remove(); + } + } + lore.add(newLore); + itemMeta.setLore(lore); + itemStack.setItemMeta(itemMeta); + return itemStack; + } + public static ItemStack replaceLore(ItemStack itemStack, String oldLore, List newLores) { ItemStack itemStack1 = itemStack.clone(); ItemMeta itemMeta = itemStack1.getItemMeta(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 946e3c5..6d14caa 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -10,7 +10,7 @@ options: # NbtKey 雨天洗炼符: # 洗炼成功率 - chance: 1.0 + chance: 0.5 # 武器需要达到什么品质 needQuality: 1 # 可随机添加多少个词条