From 59a8ef91e05e4fb3035dd31fbba47b33e11c67d6 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 22 Jul 2024 10:12:04 +0200 Subject: [PATCH] Update MWTeam.givePlayerItem and ScriptedItem.createItemStack --- src/de/steamwar/misslewars/MWTeam.java | 30 ++++++++++++++++--- .../misslewars/scripts/ScriptedItem.java | 9 +++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 5078681..0de8321 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,11 +19,15 @@ package de.steamwar.misslewars; -import org.bukkit.*; +import org.bukkit.ChatColor; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Team; @@ -81,6 +85,15 @@ public class MWTeam { } } + private int getUsesOfItem(ItemStack itemStack) { + ItemMeta itemMeta = itemStack.getItemMeta(); + if (itemMeta instanceof Damageable) { + Damageable damageable = (Damageable) itemMeta; + return itemStack.getType().getMaxDurability() - damageable.getDamage(); + } + return 1_000_000; + } + public void givePlayerItem(ItemStack item) { if (players.isEmpty()) { return; @@ -89,9 +102,18 @@ public class MWTeam { players.addLast(p); Inventory inventory = p.getInventory(); - for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot + for (int i = 0; i <= 35; i++) { // 35 is the last normal inventory slot ItemStack itemStack = inventory.getItem(i); - if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() + item.getAmount() <= itemStack.getMaxStackSize()) { + if (itemStack != null && itemStack.isSimilar(item) && getUsesOfItem(itemStack) + getUsesOfItem(item) <= itemStack.getType().getMaxDurability()) { + int uses = getUsesOfItem(itemStack) + getUsesOfItem(item); + ItemMeta itemMeta = itemStack.getItemMeta(); + Damageable damageable = (Damageable) itemMeta; + damageable.setDamage(itemStack.getType().getMaxDurability() - uses); + itemStack.setItemMeta(damageable); + inventory.setItem(i, itemStack); + p.updateInventory(); + return; + } else if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() + item.getAmount() <= 64) { itemStack.setAmount(itemStack.getAmount() + item.getAmount()); inventory.setItem(i, itemStack); p.updateInventory(); @@ -99,7 +121,7 @@ public class MWTeam { } } - for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot + for (int i = 0; i <= 35; i++) { // 35 is the last normal inventory slot ItemStack itemStack = inventory.getItem(i); if (itemStack == null || itemStack.getType().equals(Material.AIR)) { inventory.setItem(i, item); diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index 9f453d5..4e1587d 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -26,6 +26,7 @@ import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.LingeringPotion; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffect; @@ -45,6 +46,7 @@ public class ScriptedItem { // "lore": Lore array (OPTIONAL STRING.ARRAY) // "amount": Item amount (OPTIONAL [default 1] INT) // "potion": Object with key value pairs for PotionType and Object containing duration and amplifier (OPTIONAL OBJECT) + // "uses": Uses of Item (OPTIONAL INT) // "EVENT.": Event (OPTIONAL JSONobject.ARRAY) // - onClick // - onHit @@ -99,6 +101,12 @@ public class ScriptedItem { }); } + if (jsonObject.has("uses") && itemMeta instanceof Damageable) { + int uses = jsonObject.getAsJsonPrimitive("uses").getAsInt(); + Damageable damageable = (Damageable) itemMeta; + damageable.setDamage(itemStack.getType().getMaxDurability() - uses); + } + itemStack.setItemMeta(itemMeta); return itemStack; } @@ -116,5 +124,4 @@ public class ScriptedItem { public String getEntityName() { return entityName; } - }