@@ -3,102 +3,142 @@ From: kickash32 <kickash32@gmail.com>
3
3
Date: Mon, 3 Jun 2019 02:02:39 -0400
4
4
Subject: [PATCH] Implement alternative item-despawn-rate
5
5
6
+ Co-authored-by: Noah van der Aa <ndvdaa@gmail.com>
6
7
7
8
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
8
- index ed976e0582dee32ccf3aad49efb0dd773f5f9d80..e129794e5cbf5155a232966ef4ff534c0007f1d9 100644
9
+ index ed976e0582dee32ccf3aad49efb0dd773f5f9d80..f41e292e78b2c6874ee9f0cb4336263581339ca6 100644
9
10
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
10
11
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
11
- @@ -560,5 +560,52 @@ public class PaperWorldConfig {
12
+ @@ -560,5 +560,74 @@ public class PaperWorldConfig {
12
13
Bukkit.getLogger().warning("You have enabled permission-based Anti-Xray checking - depending on your permission plugin, this may cause performance issues");
13
14
}
14
15
}
15
16
- }
16
17
17
18
+ public boolean altItemDespawnRateEnabled;
18
- + public java.util.Map<org.bukkit.Material , Integer> altItemDespawnRateMap;
19
+ + public java.util.Map<net.minecraft.resources.ResourceLocation , Integer> altItemDespawnRateMap = new HashMap<>() ;
19
20
+ private void altItemDespawnRate() {
20
21
+ String path = "alt-item-despawn-rate";
22
+ + // Migrate from bukkit material to Mojang item ids
23
+ + if (PaperConfig.version < 26) {
24
+ + String world = worldName;
25
+ + try {
26
+ + org.bukkit.configuration.ConfigurationSection mapSection = config.getConfigurationSection("world-settings." + world + "." + path + ".items");
27
+ + if (mapSection == null) {
28
+ + world = "default";
29
+ + mapSection = config.getConfigurationSection("world-settings." + world + "." + path + ".items");
30
+ + }
31
+ + if (mapSection != null) {
32
+ + for (String key : mapSection.getKeys(false)) {
33
+ + int val = mapSection.getInt(key);
34
+ + try {
35
+ + // Ignore options that are already valid mojang wise, otherwise we might try to migrate the same config twice and fail.
36
+ + boolean isMojangMaterial = net.minecraft.core.Registry.ITEM.getOptional(new net.minecraft.resources.ResourceLocation(key.toLowerCase())).isPresent();
37
+ + mapSection.set(key, null);
38
+ + String newKey = isMojangMaterial ? key.toLowerCase() : org.bukkit.Material.valueOf(key).getKey().getKey().toLowerCase();
39
+ + mapSection.set(newKey, val);
40
+ + } catch (Exception e) {
41
+ + logError("Could not add item " + key + " to altItemDespawnRateMap: " + e.getMessage());
42
+ + }
43
+ + }
44
+ + config.set("world-settings." + world + "." + path + ".items", mapSection);
45
+ + }
46
+ + } catch (Exception e) {
47
+ + logError("alt-item-despawn-rate was malformatted");
48
+ + return;
49
+ + }
50
+ + }
21
51
+
22
52
+ altItemDespawnRateEnabled = getBoolean(path + ".enabled", false);
23
53
+
24
- + java.util.Map<org.bukkit.Material, Integer> altItemDespawnRateMapDefault = new java.util.EnumMap<>(org.bukkit.Material.class);
25
- + altItemDespawnRateMapDefault.put(org.bukkit.Material.COBBLESTONE, 300);
26
- + for (org.bukkit.Material key : altItemDespawnRateMapDefault.keySet()) {
27
- + config.addDefault("world-settings.default." + path + ".items." + key, altItemDespawnRateMapDefault.get(key));
54
+ + if (config.getConfigurationSection("world-settings.default." + path + ".items") == null) {
55
+ + // Initialize default
56
+ + config.addDefault("world-settings.default." + path + ".items.cobblestone", 300);
28
57
+ }
29
58
+
30
- + java.util.Map<String, Integer> rawMap = new java.util.HashMap<>();
31
- + try {
32
- + org.bukkit.configuration.ConfigurationSection mapSection = config.getConfigurationSection("world-settings." + worldName + "." + path + ".items");
33
- + if (mapSection == null) {
34
- + mapSection = config.getConfigurationSection("world-settings.default." + path + ".items");
35
- + }
36
- + for (String key : mapSection.getKeys(false)) {
37
- + int val = mapSection.getInt(key);
38
- + rawMap.put(key, val);
39
- + }
40
- + }
41
- + catch (Exception e) {
42
- + logError("alt-item-despawn-rate was malformatted");
43
- + altItemDespawnRateEnabled = false;
44
- + }
45
- +
46
- + altItemDespawnRateMap = new java.util.EnumMap<>(org.bukkit.Material.class);
47
59
+ if (!altItemDespawnRateEnabled) {
48
60
+ return;
49
61
+ }
50
62
+
51
- + for(String key : rawMap.keySet()) {
52
- + try {
53
- + altItemDespawnRateMap.put(org.bukkit.Material.valueOf(key), rawMap.get(key));
54
- + } catch (Exception e) {
55
- + logError("Could not add item " + key + " to altItemDespawnRateMap: " + e.getMessage());
56
- + }
63
+ + org.bukkit.configuration.ConfigurationSection mapSection = config.getConfigurationSection("world-settings." + worldName + "." + path + ".items");
64
+ + if (mapSection == null) {
65
+ + mapSection = config.getConfigurationSection("world-settings.default." + path + ".items");
57
66
+ }
58
- + if(altItemDespawnRateEnabled) {
59
- + for(org.bukkit.Material key : altItemDespawnRateMap.keySet()) {
60
- + log("Alternative item despawn rate of " + key + ": " + altItemDespawnRateMap.get(key));
67
+ + if (mapSection != null) {
68
+ + for (String key : mapSection.getKeys(false)) {
69
+ + try {
70
+ + int val = mapSection.getInt(key);
71
+ + net.minecraft.resources.ResourceLocation keyLocation = new net.minecraft.resources.ResourceLocation(key);
72
+ + if (net.minecraft.core.Registry.ITEM.getOptional(keyLocation).isPresent()) {
73
+ + altItemDespawnRateMap.put(keyLocation, val);
74
+ + } else {
75
+ + logError("Could not add item " + key + " to altItemDespawnRateMap: not a valid item");
76
+ + }
77
+ + } catch (Exception e) {
78
+ + logError("Could not add item " + key + " to altItemDespawnRateMap: " + e.getMessage());
79
+ + }
61
80
+ }
62
81
+ }
82
+ +
83
+ + for (net.minecraft.resources.ResourceLocation key : altItemDespawnRateMap.keySet()) {
84
+ + log("Alternative item despawn rate of " + key.getPath() + ": " + altItemDespawnRateMap.get(key));
85
+ + }
63
86
+ }
64
87
+ }
65
88
diff --git a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
66
- index 87c92e73d23a1ebb0646ba0293e1c0d51bb0e059..7b2ac6920d5f0a8dc7cbb552a0e45e2fe471190e 100644
89
+ index 87c92e73d23a1ebb0646ba0293e1c0d51bb0e059..3938bb6aa82bd02359dede6f6b17b7ba6685579b 100644
67
90
--- a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
68
91
+++ b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
69
- @@ -174,7 +174,7 @@ public class ItemEntity extends Entity {
92
+ @@ -54,6 +54,7 @@ public class ItemEntity extends Entity {
93
+ public final float bobOffs;
94
+ private int lastTick = MinecraftServer.currentTick - 1; // CraftBukkit
95
+ public boolean canMobPickup = true; // Paper
96
+ + private int despawnRate = -1; // Paper
97
+
98
+ public ItemEntity(EntityType<? extends ItemEntity> type, Level world) {
99
+ super(type, world);
100
+ @@ -174,7 +175,7 @@ public class ItemEntity extends Entity {
70
101
}
71
102
}
72
103
73
104
- if (!this.level.isClientSide && this.age >= level.spigotConfig.itemDespawnRate) { // Spigot
74
- + if (!this.level.isClientSide && this.age >= this.getDespawnRate() ) { // Spigot // Paper
105
+ + if (!this.level.isClientSide && this.age >= this.despawnRate ) { // Spigot // Paper
75
106
// CraftBukkit start - fire ItemDespawnEvent
76
107
if (org.bukkit.craftbukkit.event.CraftEventFactory.callItemDespawnEvent(this).isCancelled()) {
77
108
this.age = 0;
78
- @@ -198,7 +198 ,7 @@ public class ItemEntity extends Entity {
109
+ @@ -198,7 +199 ,7 @@ public class ItemEntity extends Entity {
79
110
this.lastTick = MinecraftServer.currentTick;
80
111
// CraftBukkit end
81
112
82
113
- if (!this.level.isClientSide && this.age >= level.spigotConfig.itemDespawnRate) { // Spigot
83
- + if (!this.level.isClientSide && this.age >= this.getDespawnRate() ) { // Spigot // Paper
114
+ + if (!this.level.isClientSide && this.age >= this.despawnRate ) { // Spigot // Paper
84
115
// CraftBukkit start - fire ItemDespawnEvent
85
116
if (org.bukkit.craftbukkit.event.CraftEventFactory.callItemDespawnEvent(this).isCancelled()) {
86
117
this.age = 0;
87
- @@ -560,9 +560,16 @@ public class ItemEntity extends Entity {
118
+ @@ -253,7 +254,7 @@ public class ItemEntity extends Entity {
119
+ private boolean isMergable() {
120
+ ItemStack itemstack = this.getItem();
121
+
122
+ - return this.isAlive() && this.pickupDelay != 32767 && this.age != -32768 && this.age < 6000 && itemstack.getCount() < itemstack.getMaxStackSize();
123
+ + return this.isAlive() && this.pickupDelay != 32767 && this.age != -32768 && this.age < this.despawnRate && itemstack.getCount() < itemstack.getMaxStackSize(); // Paper - respect despawn rate in pickup check.
124
+ }
125
+
126
+ private void tryToMerge(ItemEntity other) {
127
+ @@ -497,6 +498,8 @@ public class ItemEntity extends Entity {
128
+ com.google.common.base.Preconditions.checkArgument(!stack.isEmpty(), "Cannot drop air"); // CraftBukkit
129
+ this.getEntityData().set(ItemEntity.DATA_ITEM, stack);
130
+ this.getEntityData().markDirty(ItemEntity.DATA_ITEM); // CraftBukkit - SPIGOT-4591, must mark dirty
131
+ + net.minecraft.resources.ResourceLocation location = net.minecraft.core.Registry.ITEM.getKey(stack.getItem()); // Paper
132
+ + this.despawnRate = level.paperConfig.altItemDespawnRateMap.getOrDefault(location, level.spigotConfig.itemDespawnRate); // Paper
133
+ }
134
+
135
+ @Override
136
+ @@ -560,7 +563,7 @@ public class ItemEntity extends Entity {
88
137
89
138
public void makeFakeItem() {
90
139
this.setNeverPickUp();
91
140
- this.age = level.spigotConfig.itemDespawnRate - 1; // Spigot
92
- + this.age = this.getDespawnRate() - 1; // Spigot
141
+ + this.age = this.despawnRate - 1; // Spigot
93
142
}
94
143
95
- + // Paper start
96
- + public int getDespawnRate(){
97
- + org.bukkit.Material material = this.getItem().getBukkitStack().getType();
98
- + return level.paperConfig.altItemDespawnRateMap.getOrDefault(material, level.spigotConfig.itemDespawnRate);
99
- + }
100
- + // Paper end
101
- +
102
144
public float getSpin(float tickDelta) {
103
- return ((float) this.getAge() + tickDelta) / 20.0F + this.bobOffs;
104
- }
0 commit comments