2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: kickash32 <kickash32@gmail.com>
Date: Mon, 3 Jun 2019 02:02:39 -0400
Subject: [PATCH] Implement alternative item-despawn-rate
2021-06-14 04:41:44 +02:00
2022-05-05 00:29:15 +02:00
Co-authored-by: Noah van der Aa <ndvdaa@gmail.com>
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2022-06-08 18:13:02 +02:00
index 9e1db210ded0830d0dcfaa34936e66bbf51fc1fc..1563d22762458b9ced45be29d60eef07bfc45bb3 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2022-06-06 02:17:27 +02:00
@@ -586,5 +586,74 @@ public class PaperWorldConfig {
2021-11-29 12:55:13 +01:00
Bukkit.getLogger().warning("You have enabled permission-based Anti-Xray checking - depending on your permission plugin, this may cause performance issues");
}
2021-06-11 14:02:28 +02:00
}
2021-11-24 10:01:27 +01:00
-}
2021-06-15 15:20:52 +02:00
2021-06-11 14:02:28 +02:00
+ public boolean altItemDespawnRateEnabled;
2022-06-08 18:13:02 +02:00
+ public java.util.Map<net.minecraft.resources.ResourceLocation, Integer> altItemDespawnRateMap = new java.util.HashMap<>();
2021-06-11 14:02:28 +02:00
+ private void altItemDespawnRate() {
+ String path = "alt-item-despawn-rate";
2022-05-05 00:29:15 +02:00
+ // Migrate from bukkit material to Mojang item ids
+ if (PaperConfig.version < 26) {
+ String world = worldName;
+ try {
+ org.bukkit.configuration.ConfigurationSection mapSection = config.getConfigurationSection("world-settings." + world + "." + path + ".items");
+ if (mapSection == null) {
+ world = "default";
+ mapSection = config.getConfigurationSection("world-settings." + world + "." + path + ".items");
+ }
+ if (mapSection != null) {
+ for (String key : mapSection.getKeys(false)) {
+ int val = mapSection.getInt(key);
+ try {
+ // Ignore options that are already valid mojang wise, otherwise we might try to migrate the same config twice and fail.
+ boolean isMojangMaterial = net.minecraft.core.Registry.ITEM.getOptional(new net.minecraft.resources.ResourceLocation(key.toLowerCase())).isPresent();
+ mapSection.set(key, null);
+ String newKey = isMojangMaterial ? key.toLowerCase() : org.bukkit.Material.valueOf(key).getKey().getKey().toLowerCase();
+ mapSection.set(newKey, val);
+ } catch (Exception e) {
+ logError("Could not add item " + key + " to altItemDespawnRateMap: " + e.getMessage());
+ }
+ }
+ config.set("world-settings." + world + "." + path + ".items", mapSection);
+ }
+ } catch (Exception e) {
+ logError("alt-item-despawn-rate was malformatted");
+ return;
+ }
+ }
2021-06-11 14:02:28 +02:00
+
+ altItemDespawnRateEnabled = getBoolean(path + ".enabled", false);
+
2022-05-05 00:29:15 +02:00
+ if (config.getConfigurationSection("world-settings.default." + path + ".items") == null) {
+ // Initialize default
+ config.addDefault("world-settings.default." + path + ".items.cobblestone", 300);
2021-06-11 14:02:28 +02:00
+ }
+
+ if (!altItemDespawnRateEnabled) {
+ return;
+ }
+
2022-05-05 00:29:15 +02:00
+ org.bukkit.configuration.ConfigurationSection mapSection = config.getConfigurationSection("world-settings." + worldName + "." + path + ".items");
+ if (mapSection == null) {
+ mapSection = config.getConfigurationSection("world-settings.default." + path + ".items");
2021-06-11 14:02:28 +02:00
+ }
2022-05-05 00:29:15 +02:00
+ if (mapSection != null) {
+ for (String key : mapSection.getKeys(false)) {
+ try {
+ int val = mapSection.getInt(key);
+ net.minecraft.resources.ResourceLocation keyLocation = new net.minecraft.resources.ResourceLocation(key);
+ if (net.minecraft.core.Registry.ITEM.getOptional(keyLocation).isPresent()) {
+ altItemDespawnRateMap.put(keyLocation, val);
+ } else {
+ logError("Could not add item " + key + " to altItemDespawnRateMap: not a valid item");
+ }
+ } catch (Exception e) {
+ logError("Could not add item " + key + " to altItemDespawnRateMap: " + e.getMessage());
+ }
2021-06-11 14:02:28 +02:00
+ }
+ }
2022-05-05 00:29:15 +02:00
+
+ for (net.minecraft.resources.ResourceLocation key : altItemDespawnRateMap.keySet()) {
+ log("Alternative item despawn rate of " + key.getPath() + ": " + altItemDespawnRateMap.get(key));
+ }
2021-06-11 14:02:28 +02:00
+ }
2021-11-24 10:01:27 +01:00
+}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
2022-06-08 06:06:41 +02:00
index 85fc33183ff01611bfa90ddd33a9c885d1dd7172..3364316079dba81778869380b70e621cb9ac6269 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
2022-05-05 00:29:15 +02:00
@@ -54,6 +54,7 @@ public class ItemEntity extends Entity {
public final float bobOffs;
private int lastTick = MinecraftServer.currentTick - 1; // CraftBukkit
public boolean canMobPickup = true; // Paper
+ private int despawnRate = -1; // Paper
public ItemEntity(EntityType<? extends ItemEntity> type, Level world) {
super(type, world);
2022-06-08 06:06:41 +02:00
@@ -184,7 +185,7 @@ public class ItemEntity extends Entity {
2021-06-11 14:02:28 +02:00
}
}
- if (!this.level.isClientSide && this.age >= level.spigotConfig.itemDespawnRate) { // Spigot
2022-05-05 00:29:15 +02:00
+ if (!this.level.isClientSide && this.age >= this.despawnRate) { // Spigot // Paper
2021-06-11 14:02:28 +02:00
// CraftBukkit start - fire ItemDespawnEvent
if (org.bukkit.craftbukkit.event.CraftEventFactory.callItemDespawnEvent(this).isCancelled()) {
this.age = 0;
2022-06-08 06:06:41 +02:00
@@ -208,7 +209,7 @@ public class ItemEntity extends Entity {
2021-06-11 14:02:28 +02:00
this.lastTick = MinecraftServer.currentTick;
// CraftBukkit end
- if (!this.level.isClientSide && this.age >= level.spigotConfig.itemDespawnRate) { // Spigot
2022-05-05 00:29:15 +02:00
+ if (!this.level.isClientSide && this.age >= this.despawnRate) { // Spigot // Paper
2021-06-11 14:02:28 +02:00
// CraftBukkit start - fire ItemDespawnEvent
if (org.bukkit.craftbukkit.event.CraftEventFactory.callItemDespawnEvent(this).isCancelled()) {
this.age = 0;
2022-06-08 06:06:41 +02:00
@@ -263,7 +264,7 @@ public class ItemEntity extends Entity {
2022-05-05 00:29:15 +02:00
private boolean isMergable() {
ItemStack itemstack = this.getItem();
- return this.isAlive() && this.pickupDelay != 32767 && this.age != -32768 && this.age < 6000 && itemstack.getCount() < itemstack.getMaxStackSize();
+ return this.isAlive() && this.pickupDelay != 32767 && this.age != -32768 && this.age < this.despawnRate && itemstack.getCount() < itemstack.getMaxStackSize(); // Paper - respect despawn rate in pickup check.
}
private void tryToMerge(ItemEntity other) {
2022-06-08 06:06:41 +02:00
@@ -507,6 +508,8 @@ public class ItemEntity extends Entity {
2022-05-05 00:29:15 +02:00
com.google.common.base.Preconditions.checkArgument(!stack.isEmpty(), "Cannot drop air"); // CraftBukkit
this.getEntityData().set(ItemEntity.DATA_ITEM, stack);
this.getEntityData().markDirty(ItemEntity.DATA_ITEM); // CraftBukkit - SPIGOT-4591, must mark dirty
+ net.minecraft.resources.ResourceLocation location = net.minecraft.core.Registry.ITEM.getKey(stack.getItem()); // Paper
+ this.despawnRate = level.paperConfig.altItemDespawnRateMap.getOrDefault(location, level.spigotConfig.itemDespawnRate); // Paper
}
@Override
2022-06-08 06:06:41 +02:00
@@ -570,7 +573,7 @@ public class ItemEntity extends Entity {
2021-06-11 14:02:28 +02:00
public void makeFakeItem() {
this.setNeverPickUp();
- this.age = level.spigotConfig.itemDespawnRate - 1; // Spigot
2022-05-05 00:29:15 +02:00
+ this.age = this.despawnRate - 1; // Spigot
2021-06-11 14:02:28 +02:00
}
2021-06-14 04:41:44 +02:00
public float getSpin(float tickDelta) {