From 9ce4148835490ce44947c8b139ccd29f19da393d Mon Sep 17 00:00:00 2001 From: BenceX100 <52270269+BenceX100@users.noreply.github.com> Date: Sat, 20 Jan 2024 09:22:19 +0100 Subject: [PATCH] 1.5.0 --- pom.xml | 2 +- .../artillexstudios/axgraves/grave/Grave.java | 29 +++++++++- .../axgraves/grave/SpawnedGrave.java | 8 +-- .../axgraves/listeners/DeathListener.java | 5 +- .../axgraves/utils/InventoryUtils.java | 53 +++++++++++++++++++ src/main/resources/config.yml | 17 +++++- 6 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/artillexstudios/axgraves/utils/InventoryUtils.java diff --git a/pom.xml b/pom.xml index faf5458..6f863e0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.artillexstudios AxGraves - 1.4.1 + 1.5.0 jar AxGraves diff --git a/src/main/java/com/artillexstudios/axgraves/grave/Grave.java b/src/main/java/com/artillexstudios/axgraves/grave/Grave.java index 4ba53cc..39c60e8 100644 --- a/src/main/java/com/artillexstudios/axgraves/grave/Grave.java +++ b/src/main/java/com/artillexstudios/axgraves/grave/Grave.java @@ -12,6 +12,7 @@ import com.artillexstudios.axgraves.api.events.GraveOpenEvent; import com.artillexstudios.axgraves.utils.BlacklistUtils; import com.artillexstudios.axgraves.utils.ExperienceUtils; +import com.artillexstudios.axgraves.utils.InventoryUtils; import com.artillexstudios.axgraves.utils.LocationUtils; import com.artillexstudios.axgraves.utils.Utils; import dev.triumphteam.gui.guis.Gui; @@ -52,7 +53,7 @@ public Grave(Location loc, @NotNull Player player, @NotNull ItemStack[] itemsAr, this.player = player; this.playerName = player.getName(); - final ItemStack[] items = Arrays.stream(itemsAr).filter(Objects::nonNull).toArray(ItemStack[]::new); + final ItemStack[] items = Arrays.stream(InventoryUtils.reOrderInventory(player.getInventory(), itemsAr)).filter(Objects::nonNull).toArray(ItemStack[]::new); this.gui = Gui.storage() .title(StringUtils.format(MESSAGES.getString("gui-name").replace("%player%", playerName))) .rows(items.length % 9 == 0 ? items.length / 9 : 1 + (items.length / 9)) @@ -147,6 +148,32 @@ public void interact(@NotNull Player player, org.bukkit.inventory.EquipmentSlot for (ItemStack it : gui.getInventory().getContents()) { if (it == null) continue; + if (CONFIG.getBoolean("auto-equip-armor", true)) { + if (it.getType().toString().endsWith("_HELMET") && player.getInventory().getHelmet() == null) { + player.getInventory().setHelmet(it); + it.setAmount(0); + continue; + } + + if (it.getType().toString().endsWith("_CHESTPLATE") && player.getInventory().getChestplate() == null) { + player.getInventory().setChestplate(it); + it.setAmount(0); + continue; + } + + if (it.getType().toString().endsWith("_LEGGINGS") && player.getInventory().getLeggings() == null) { + player.getInventory().setLeggings(it); + it.setAmount(0); + continue; + } + + if (it.getType().toString().endsWith("_BOOTS") && player.getInventory().getBoots() == null) { + player.getInventory().setBoots(it); + it.setAmount(0); + continue; + } + } + final Collection ar = player.getInventory().addItem(it).values(); if (ar.isEmpty()) { it.setAmount(0); diff --git a/src/main/java/com/artillexstudios/axgraves/grave/SpawnedGrave.java b/src/main/java/com/artillexstudios/axgraves/grave/SpawnedGrave.java index fa02945..d4c314f 100644 --- a/src/main/java/com/artillexstudios/axgraves/grave/SpawnedGrave.java +++ b/src/main/java/com/artillexstudios/axgraves/grave/SpawnedGrave.java @@ -3,17 +3,17 @@ import java.util.concurrent.ConcurrentLinkedQueue; public class SpawnedGrave { - private static final ConcurrentLinkedQueue chests = new ConcurrentLinkedQueue<>(); + private static final ConcurrentLinkedQueue graves = new ConcurrentLinkedQueue<>(); public static void addGrave(Grave grave) { - chests.add(grave); + graves.add(grave); } public static void removeGrave(Grave grave) { - chests.remove(grave); + graves.remove(grave); } public static ConcurrentLinkedQueue getGraves() { - return chests; + return graves; } } diff --git a/src/main/java/com/artillexstudios/axgraves/listeners/DeathListener.java b/src/main/java/com/artillexstudios/axgraves/listeners/DeathListener.java index fe25ccf..4695e1f 100644 --- a/src/main/java/com/artillexstudios/axgraves/listeners/DeathListener.java +++ b/src/main/java/com/artillexstudios/axgraves/listeners/DeathListener.java @@ -29,10 +29,11 @@ public void onDeath(@NotNull PlayerDeathEvent event) { Grave grave = null; + final int xp = Math.round(ExperienceUtils.getExp(player) * CONFIG.getFloat("xp-keep-percentage", 1f)); if (!event.getKeepInventory()) { - grave = new Grave(player.getLocation(), player, event.getDrops().toArray(new ItemStack[0]), ExperienceUtils.getExp(player)); + grave = new Grave(player.getLocation(), player, event.getDrops().toArray(new ItemStack[0]), xp); } else if (CONFIG.getBoolean("override-keep-inventory", true)) { - grave = new Grave(player.getLocation(), player, player.getInventory().getContents(), ExperienceUtils.getExp(player)); + grave = new Grave(player.getLocation(), player, player.getInventory().getContents(), xp); player.setLevel(0); player.setTotalExperience(0); player.getInventory().clear(); diff --git a/src/main/java/com/artillexstudios/axgraves/utils/InventoryUtils.java b/src/main/java/com/artillexstudios/axgraves/utils/InventoryUtils.java new file mode 100644 index 0000000..5eb6206 --- /dev/null +++ b/src/main/java/com/artillexstudios/axgraves/utils/InventoryUtils.java @@ -0,0 +1,53 @@ +package com.artillexstudios.axgraves.utils; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; + +import static com.artillexstudios.axgraves.AxGraves.CONFIG; + +public class InventoryUtils { + + @NotNull + public static ItemStack[] reOrderInventory(@NotNull PlayerInventory inventory, @NotNull ItemStack[] keptItems) { + final ArrayList itemsBefore = new ArrayList<>(Arrays.asList(keptItems)); + final ItemStack[] items = new ItemStack[itemsBefore.size()]; + int n = 0; + + for (String str : CONFIG.getStringList("grave-item-order")) { + switch (str) { + case "ARMOR" -> { + for (ItemStack it : inventory.getArmorContents()) { + if (!itemsBefore.contains(it)) continue; + items[n] = it; + itemsBefore.remove(it); + n++; + } + } + case "HAND" -> { + if (!itemsBefore.contains(inventory.getItemInMainHand())) continue; + items[n] = inventory.getItemInMainHand(); + itemsBefore.remove(inventory.getItemInMainHand()); + n++; + } + case "OFFHAND" -> { + if (!itemsBefore.contains(inventory.getItemInOffHand())) continue; + items[n] = inventory.getItemInOffHand(); + itemsBefore.remove(inventory.getItemInOffHand()); + n++; + } + } + } + + for (ItemStack it : itemsBefore) { + if (!itemsBefore.contains(it)) continue; + items[n] = it; + n++; + } + + return items; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 237e8b1..d4292ec 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -47,6 +47,21 @@ auto-rotation: # false: everyone can open the grave interact-only-own: false +# should the player lose any xp on death? +# this is a percentage, so 0.5 would be 50% xp +xp-keep-percentage: 1.0 + +# what order should items be put in the grave? +# all the other items will be put AFTER these +# values: ARMOR, HAND, OFFHAND +grave-item-order: + - "ARMOR" + - "HAND" + - "OFFHAND" + +# should the armor parts be auto equipped? +auto-equip-armor: true + # items that will be removed on death and will not show up in graves blacklisted-items: "1": @@ -54,4 +69,4 @@ blacklisted-items: name-contains: "Banned item's name" # do not edit -version: 5 \ No newline at end of file +version: 6 \ No newline at end of file