2021-06-11 12:02:28 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Aikar <aikar@aikar.co>
|
|
|
|
Date: Wed, 27 Mar 2019 23:01:33 -0400
|
|
|
|
Subject: [PATCH] PlayerDeathEvent#getItemsToKeep
|
|
|
|
|
|
|
|
Exposes a mutable array on items a player should keep on death
|
|
|
|
|
|
|
|
Example Usage: https://gist.github.com/aikar/5bb202de6057a051a950ce1f29feb0b4
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
2022-03-16 15:57:51 +00:00
|
|
|
index 7a187691057a90b71c910dbdcaa6539d334b04ad..9441eae174c4441f7f4c9be69d39b415db01910c 100644
|
2021-06-11 12:02:28 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
2022-03-16 15:57:51 +00:00
|
|
|
@@ -761,6 +761,46 @@ public class ServerPlayer extends Player {
|
2021-06-11 12:02:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
+ // Paper start - process inventory
|
|
|
|
+ private static void processKeep(org.bukkit.event.entity.PlayerDeathEvent event, NonNullList<ItemStack> inv) {
|
|
|
|
+ List<org.bukkit.inventory.ItemStack> itemsToKeep = event.getItemsToKeep();
|
|
|
|
+ if (inv == null) {
|
|
|
|
+ // remainder of items left in toKeep - plugin added stuff on death that wasn't in the initial loot?
|
|
|
|
+ if (!itemsToKeep.isEmpty()) {
|
|
|
|
+ for (org.bukkit.inventory.ItemStack itemStack : itemsToKeep) {
|
|
|
|
+ event.getEntity().getInventory().addItem(itemStack);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < inv.size(); ++i) {
|
|
|
|
+ ItemStack item = inv.get(i);
|
|
|
|
+ if (EnchantmentHelper.hasVanishingCurse(item) || itemsToKeep.isEmpty() || item.isEmpty()) {
|
2021-06-13 11:40:34 +00:00
|
|
|
+ inv.set(i, ItemStack.EMPTY);
|
2021-06-11 12:02:28 +00:00
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ final org.bukkit.inventory.ItemStack bukkitStack = item.getBukkitStack();
|
|
|
|
+ boolean keep = false;
|
|
|
|
+ final Iterator<org.bukkit.inventory.ItemStack> iterator = itemsToKeep.iterator();
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ final org.bukkit.inventory.ItemStack itemStack = iterator.next();
|
|
|
|
+ if (bukkitStack.equals(itemStack)) {
|
|
|
|
+ iterator.remove();
|
|
|
|
+ keep = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!keep) {
|
2021-06-13 11:40:34 +00:00
|
|
|
+ inv.set(i, ItemStack.EMPTY);
|
2021-06-11 12:02:28 +00:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
+
|
|
|
|
@Override
|
|
|
|
public void die(DamageSource source) {
|
|
|
|
boolean flag = this.level.getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
|
2022-03-16 15:57:51 +00:00
|
|
|
@@ -846,7 +886,12 @@ public class ServerPlayer extends Player {
|
2021-09-25 19:06:37 +00:00
|
|
|
this.dropExperience();
|
2021-06-11 12:02:28 +00:00
|
|
|
// we clean the player's inventory after the EntityDeathEvent is called so plugins can get the exact state of the inventory.
|
|
|
|
if (!event.getKeepInventory()) {
|
2021-09-25 19:06:37 +00:00
|
|
|
- this.getInventory().clearContent();
|
2021-06-11 12:02:28 +00:00
|
|
|
+ // Paper start - replace logic
|
2021-06-17 21:39:36 +00:00
|
|
|
+ for (NonNullList<ItemStack> inv : this.getInventory().compartments) {
|
2021-06-11 12:02:28 +00:00
|
|
|
+ processKeep(event, inv);
|
|
|
|
+ }
|
|
|
|
+ processKeep(event, null);
|
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setCamera(this); // Remove spectated target
|