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>
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -712,15 +712,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
|
||||
|
||||
Reference in New Issue
Block a user