SteamWar/MissileWars
Archiviert
13
0

Update MWTeam.givePlayerItem and ScriptedItem.createItemStack
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2024-07-22 10:12:04 +02:00
Ursprung b5b6fe26e2
Commit 59a8ef91e0
2 geänderte Dateien mit 34 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -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);

Datei anzeigen

@ -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.<eventName>": 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;
}
}