2021-06-11 14:02:28 +02: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
|
|
|
|
|
2022-11-20 00:53:20 +01:00
|
|
|
== AT ==
|
|
|
|
public net.minecraft.world.entity.player.Inventory compartments
|
|
|
|
|
2021-06-11 14:02:28 +02:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
2024-06-14 23:07:44 +02:00
|
|
|
index ecfc5f59fcc3b256929a7d93972bf63272734a5d..b7419b254295b3b2575ce2c6b5e53b1f3f9a3c41 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
2024-06-13 22:14:13 +02:00
|
|
|
@@ -899,6 +899,46 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
|
2021-06-11 14:02:28 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-21 13:56:22 +01:00
|
|
|
+ // Paper start - PlayerDeathEvent#getItemsToKeep
|
2021-06-11 14:02:28 +02:00
|
|
|
+ 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);
|
2024-06-14 13:07:50 +02:00
|
|
|
+ if (EnchantmentHelper.has(item, EnchantmentEffectComponents.PREVENT_EQUIPMENT_DROP) || itemsToKeep.isEmpty() || item.isEmpty()) {
|
2021-06-13 13:40:34 +02:00
|
|
|
+ inv.set(i, ItemStack.EMPTY);
|
2021-06-11 14:02:28 +02: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 13:40:34 +02:00
|
|
|
+ inv.set(i, ItemStack.EMPTY);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
2024-01-21 13:56:22 +01:00
|
|
|
+ // Paper end - PlayerDeathEvent#getItemsToKeep
|
2021-06-11 14:02:28 +02:00
|
|
|
+
|
|
|
|
@Override
|
2022-06-08 04:25:49 +02:00
|
|
|
public void die(DamageSource damageSource) {
|
2023-12-29 20:57:32 +01:00
|
|
|
// this.gameEvent(GameEvent.ENTITY_DIE); // Paper - move below event cancellation check
|
2024-06-13 22:14:13 +02:00
|
|
|
@@ -983,7 +1023,12 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
|
|
|
|
this.dropExperience(damageSource.getEntity());
|
2021-06-11 14:02:28 +02: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 21:06:37 +02:00
|
|
|
- this.getInventory().clearContent();
|
2024-01-21 13:56:22 +01:00
|
|
|
+ // Paper start - PlayerDeathEvent#getItemsToKeep
|
2021-06-17 23:39:36 +02:00
|
|
|
+ for (NonNullList<ItemStack> inv : this.getInventory().compartments) {
|
2021-06-11 14:02:28 +02:00
|
|
|
+ processKeep(event, inv);
|
|
|
|
+ }
|
|
|
|
+ processKeep(event, null);
|
2024-01-21 13:56:22 +01:00
|
|
|
+ // Paper end - PlayerDeathEvent#getItemsToKeep
|
2021-06-11 14:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setCamera(this); // Remove spectated target
|