aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0282-PlayerDeathEvent-getItemsToKeep.patch
blob: dd5b9f834cccc780eff413e429c2dd1ad61bb8b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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

== AT ==
public net.minecraft.world.entity.player.Inventory compartments

diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index 62412b37d4f7d37b3fec0966ab700c2ae4d8cada..c5eb4766d95b1ed91be8e8631200f56096a17766 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -1157,6 +1157,46 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
         });
     }
 
+    // Paper start - PlayerDeathEvent#getItemsToKeep
+    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.has(item, EnchantmentEffectComponents.PREVENT_EQUIPMENT_DROP) || itemsToKeep.isEmpty() || item.isEmpty()) {
+                inv.set(i, ItemStack.EMPTY);
+                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) {
+                inv.set(i, ItemStack.EMPTY);
+            }
+        }
+    }
+    // Paper end - PlayerDeathEvent#getItemsToKeep
+
     @Override
     public void die(DamageSource damageSource) {
         // this.gameEvent(GameEvent.ENTITY_DIE); // Paper - move below event cancellation check
@@ -1241,7 +1281,12 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
         this.dropExperience(this.serverLevel(), damageSource.getEntity());
         // we clean the player's inventory after the EntityDeathEvent is called so plugins can get the exact state of the inventory.
         if (!event.getKeepInventory()) {
-            this.getInventory().clearContent();
+            // Paper start - PlayerDeathEvent#getItemsToKeep
+            for (NonNullList<ItemStack> inv : this.getInventory().compartments) {
+                processKeep(event, inv);
+            }
+            processKeep(event, null);
+            // Paper end - PlayerDeathEvent#getItemsToKeep
         }
 
         this.setCamera(this); // Remove spectated target