Skip to content

Commit

Permalink
Several fixes and new api for experience merging/stacking (#9242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker authored May 29, 2024
1 parent ed85aac commit a31dc90
Show file tree
Hide file tree
Showing 693 changed files with 318 additions and 240 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 19 Dec 2017 22:56:24 -0500
Subject: [PATCH] ExperienceOrbMergeEvent
Subject: [PATCH] ExperienceOrb merging/stacking API

Adds ExperienceOrbMergeEvent
Fired when the server is about to merge 2 experience orbs
Plugins can cancel this if they want to ensure experience orbs do not lose important
metadata such as spawn reason, or conditionally move data from source to target.

Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>

diff --git a/src/main/java/com/destroystokyo/paper/event/entity/ExperienceOrbMergeEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/ExperienceOrbMergeEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..c520e5517861c4686806df233d1ef5e6bfb76ad3
Expand Down Expand Up @@ -104,3 +107,30 @@ index 0000000000000000000000000000000000000000..c520e5517861c4686806df233d1ef5e6
+ return HANDLER_LIST;
+ }
+}
diff --git a/src/main/java/org/bukkit/entity/ExperienceOrb.java b/src/main/java/org/bukkit/entity/ExperienceOrb.java
index dec70bbfaf73a9d525b2c45682b804c684e1645b..0fe4a7f300287f38dbe15862787f387aba74397b 100644
--- a/src/main/java/org/bukkit/entity/ExperienceOrb.java
+++ b/src/main/java/org/bukkit/entity/ExperienceOrb.java
@@ -21,6 +21,22 @@ public interface ExperienceOrb extends Entity {
* @param value Amount of experience
*/
public void setExperience(int value);
+
+ // Paper start - expose count
+ /**
+ * Get the stacked count for this experience orb.
+ *
+ * @return the count
+ */
+ int getCount();
+
+ /**
+ * Sets the stacked count for this experience orb.
+ *
+ * @param count the new count
+ */
+ void setCount(int count);
+ // Paper end

// Paper start
/**
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ index 1f29ed95ef3d1904a014715028d9d591fe39231f..1a829f79e6f9e03ead745e13ece4d1b5
+++ b/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
@@ -41,9 +41,63 @@ public class ExperienceOrb extends Entity {
public int value;
private int count;
public int count;
private Player followingPlayer;
+ // Paper start
+ @javax.annotation.Nullable
Expand Down
113 changes: 113 additions & 0 deletions patches/server/0361-ExperienceOrb-merging-stacking-API-and-fixes.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: BillyGalbreath <Blake.Galbreath@GMail.com>
Date: Fri, 10 Nov 2017 23:03:12 -0500
Subject: [PATCH] ExperienceOrb merging/stacking API and fixes

Adds an option for maximum exp value when merging orbs

Adds ExperienceOrbMergeEvent
Fired when the server is about to merge 2 experience orbs
as entities. Plugins can cancel it if they want to ensure experience orbs do not lose important
metadata such as spawn reason, or conditionally move data from source to target.

Fixes an issue where the stacked count was not taking into account
for mending repairs and when merging with spigot's merge-on-spawn
logic

== AT ==
public net.minecraft.world.entity.ExperienceOrb count

Co-authored-by: Aikar <aikar@aikar.co>
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>

diff --git a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java b/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
index 8007d323023655052acd0cf1f3a753101e9ee74a..a933061d4f0c45a34b5678c2b317b670f20b8a45 100644
--- a/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
+++ b/src/main/java/net/minecraft/world/entity/ExperienceOrb.java
@@ -241,6 +241,7 @@ public class ExperienceOrb extends Entity {
}

private static boolean tryMergeToExisting(ServerLevel world, Vec3 pos, int amount) {
+ // Paper - TODO some other event for this kind of merge
AABB axisalignedbb = AABB.ofSize(pos, 1.0D, 1.0D, 1.0D);
int j = world.getRandom().nextInt(40);
List<ExperienceOrb> list = world.getEntities(EntityTypeTest.forClass(ExperienceOrb.class), axisalignedbb, (entityexperienceorb) -> {
@@ -267,6 +268,11 @@ public class ExperienceOrb extends Entity {
}

private void merge(ExperienceOrb other) {
+ // Paper start - call orb merge event
+ if (!new com.destroystokyo.paper.event.entity.ExperienceOrbMergeEvent((org.bukkit.entity.ExperienceOrb) this.getBukkitEntity(), (org.bukkit.entity.ExperienceOrb) other.getBukkitEntity()).callEvent()) {
+ return;
+ }
+ // Paper end - call orb merge event
this.count += other.count;
this.age = Math.min(this.age, other.age);
other.discard(EntityRemoveEvent.Cause.MERGE); // CraftBukkit - add Bukkit remove cause
@@ -353,7 +359,7 @@ public class ExperienceOrb extends Entity {

itemstack.setDamageValue(itemstack.getDamageValue() - j);
int k = amount - this.durabilityToXp(j);
- this.value = k; // CraftBukkit - update exp value of orb for PlayerItemMendEvent calls
+ // this.value = k; // CraftBukkit - update exp value of orb for PlayerItemMendEvent calls // Paper - the value field should not be mutated here because it doesn't take "count" into account

return k > 0 ? this.repairPlayerItems(player, k) : 0;
} else {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftExperienceOrb.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftExperienceOrb.java
index 5a7d314ec0562e472f5dc45924a7b24841cff126..650e4a01cecc4cc08e7ff9ebcc4c367084351f21 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftExperienceOrb.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftExperienceOrb.java
@@ -18,6 +18,18 @@ public class CraftExperienceOrb extends CraftEntity implements ExperienceOrb {
this.getHandle().value = value;
}

+ // Paper start - expose count
+ @Override
+ public int getCount() {
+ return this.getHandle().count;
+ }
+
+ @Override
+ public void setCount(final int count) {
+ this.getHandle().count = count;
+ }
+ // Paper end
+
// Paper start
public java.util.UUID getTriggerEntityId() {
return getHandle().triggerEntityId;
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index ac4a8c9d4f727f3caa16f6dc5497d69f9db52aab..53f5eb1682c88abf7b09e16f010ebbd8fe2ac059 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -703,15 +703,29 @@ public class CraftEventFactory {
if (entity instanceof net.minecraft.world.entity.ExperienceOrb xp) {
double radius = world.spigotConfig.expMerge;
if (radius > 0) {
+ // Paper start - Maximum exp value when merging; Whole section has been tweaked, see comments for specifics
+ final long maxValue = world.paperConfig().entities.behavior.experienceMergeMaxValue;
+ final boolean mergeUnconditionally = maxValue <= 0;
+ if (mergeUnconditionally || xp.value < maxValue) { // Paper - Skip iteration if unnecessary
+
List<Entity> entities = world.getEntities(entity, entity.getBoundingBox().inflate(radius, radius, radius));
for (Entity e : entities) {
if (e instanceof net.minecraft.world.entity.ExperienceOrb loopItem) {
- if (!loopItem.isRemoved()) {
+ // Paper start
+ if (!loopItem.isRemoved() && xp.count == loopItem.count && (mergeUnconditionally || loopItem.value < maxValue) && new com.destroystokyo.paper.event.entity.ExperienceOrbMergeEvent((org.bukkit.entity.ExperienceOrb) entity.getBukkitEntity(), (org.bukkit.entity.ExperienceOrb) loopItem.getBukkitEntity()).callEvent()) { // Paper - ExperienceOrbMergeEvent
+ long newTotal = (long)xp.value + (long)loopItem.value;
+ if ((int) newTotal < 0) continue; // Overflow
+ if (!mergeUnconditionally && newTotal > maxValue) {
+ loopItem.value = (int) (newTotal - maxValue);
+ xp.value = (int) maxValue;
+ } else {
xp.value += loopItem.value;
loopItem.discard(null); // Add Bukkit remove cause
+ } // Paper end - Maximum exp value when merging
}
}
}
+ } // Paper end - End iteration skip check - All tweaking ends here
}
}
// Spigot end

This file was deleted.

23 changes: 0 additions & 23 deletions patches/server/0362-ExperienceOrbMergeEvent.patch

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Add permission for command blocks


diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
index cd6f34ee326228036f8c025e4e6d04e0c15ba06f..2f0d56c78584abe27ef050258827103fb60d5785 100644
index cbb17ce25cb6218bcf95c2f1c3d0288eeee13a46..84c31fe944fc39109d48b6dc68d0c2b15bef6cf2 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
@@ -400,7 +400,7 @@ public class ServerPlayerGameMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ index 3d33504eac6af17c8833de11226968d52f96232f..4d7c2832a9cd9a88b99c837a02df7fa9

@Override
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index dde6d567521a08b416ee50b1249b653b0bf6e87a..9750c0309a9dae50b62bfbe5f29a2a6f3c981e62 100644
index bf6c81300d29c445a1012d1159866547c202d135..e6a24484ef11a1d815a85126aec221547479f7d3 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -1238,7 +1238,7 @@ public class ServerPlayer extends Player {
Expand All @@ -102,7 +102,7 @@ index 299a2e78f4a83d224038c80287636a5d6b9b7450..95d20facdc43a356fd2e82f5d597f52e
}

diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 01224af0835a00343923767186dceb53900c0832..e7535d4739f217a8fda2c15c74d44637825a0322 100644
index 45799f96978a68a79b4c89e17e9b543dec99a8b1..72f64528092f92adf60dadb7d1b5dc38c7a8d4ee 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -962,8 +962,8 @@ public final class CraftServer implements Server {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ wants it to collect even faster, they can restore that setting back to 1 instead
Not adding it to .getType() though to keep behavior consistent with vanilla for performance reasons.

diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index e7535d4739f217a8fda2c15c74d44637825a0322..d996a0d5d14448e49a8b6730425dd5b50abc1e72 100644
index 72f64528092f92adf60dadb7d1b5dc38c7a8d4ee..38da9a19546c979c4bfd4ab23a34b77266911a24 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -375,7 +375,7 @@ public final class CraftServer implements Server {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ index 9c2fe69ced7a46bbd8b0fbe10fa67d0a39b0f375..e40d9dbdbe5359c38af6d764d01c9be4

private void setupRecipeList(Container input, ItemStack stack) {
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 1e9835647a0f9eb1e1a264467a62af85af664594..461b25d3d0660f973e6db670899bce05010e7c49 100644
index 54aa1a462bf4d9649f34d79aab1f59e14b7962a5..c3c7cfc33d07b87e034fb050b0a1ca1d8b971aa6 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1650,6 +1650,12 @@ public class CraftEventFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Don't check chunk for portal on world gen entity add


diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index c640d68c35b4454bfa0ae1764dee341041d9c31e..4bf48ce1824e404906a551ceec720c8e485adb44 100644
index 5280bae3ad8f9c137e58add8a8d056df81de9928..33658a45d9f61640ab2b56be3fecb5b2552bca84 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -3624,7 +3624,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Brand support


diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index 9750c0309a9dae50b62bfbe5f29a2a6f3c981e62..38e62a9251b0b0cceb0a350a649897f357293428 100644
index e6a24484ef11a1d815a85126aec221547479f7d3..ffd9ff7db5180cb351839a3ce331353d16b662d2 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -290,6 +290,7 @@ public class ServerPlayer extends Player {
Expand Down Expand Up @@ -57,7 +57,7 @@ index 289a74e35836717bd20c777e9fc8c17722e90411..a5dce1e83e63292054b21ec693ec3006
} catch (Exception ex) {
ServerGamePacketListenerImpl.LOGGER.error("Couldn\'t dispatch custom payload", ex);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 4131b83117e399d7113442def85101d20c493d06..b614ee4e0e217f4adf45f45f53486376bee6533f 100644
index 776532b4818d15a5f4cfd35d0c076d4774615681..9a9c6e43c96689171a2767f93aea8856db1b7287 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -3112,6 +3112,13 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Add a new event, BellRingEvent, to trigger whenever a player rings a
village bell. Passes along the bell block and the player who rang it.

diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 461b25d3d0660f973e6db670899bce05010e7c49..737fbe02c630da0a95dbed49ff152fb5c7ec4d37 100644
index c3c7cfc33d07b87e034fb050b0a1ca1d8b971aa6..c6408feaff6a5bd8529004e6c2e1aa76ddc8c861 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -367,10 +367,11 @@ public class CraftEventFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ index bc9c68c4df11ece9a9cba9b8cff1182c1d21551f..5d8b1fb9ee96ca397b8f3a0629bc4273
}

diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 9012d34db1768189ac68b9348e609d77948488d3..77e672f6146e4a9b32bead526b7dfcbe54c1829e 100644
index 184d8bdca8aa06b6a1f00bab03e8c2688a391663..0b98618cb0b0e9c4b13415ab0e535cdfbd55b224 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -165,6 +165,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand Down
Loading

0 comments on commit a31dc90

Please sign in to comment.