Skip to content

Commit

Permalink
1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BenceX100 committed Jan 20, 2024
1 parent 7e0a1c0 commit 9ce4148
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.artillexstudios</groupId>
<artifactId>AxGraves</artifactId>
<version>1.4.1</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>AxGraves</name>
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/com/artillexstudios/axgraves/grave/Grave.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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<ItemStack> ar = player.getInventory().addItem(it).values();
if (ar.isEmpty()) {
it.setAmount(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import java.util.concurrent.ConcurrentLinkedQueue;

public class SpawnedGrave {
private static final ConcurrentLinkedQueue<Grave> chests = new ConcurrentLinkedQueue<>();
private static final ConcurrentLinkedQueue<Grave> 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<Grave> getGraves() {
return chests;
return graves;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ItemStack> 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;
}
}
17 changes: 16 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,26 @@ 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":
material: "barrier"
name-contains: "Banned item's name"

# do not edit
version: 5
version: 6

0 comments on commit 9ce4148

Please sign in to comment.