9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-22 08:29:28 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0027-Petal-reduce-work-done-by-game-event-system.patch
Dreeam d06fb16da3 Updated Upstream (Paper/Gale/Purpur)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@30fdfb1a [ci/skip] Fix docs for DamageResistant (#11992)
PaperMC/Paper@6b7650d8 Only add goat horn once (#12001)
PaperMC/Paper@30046e04 Fix a rare crash with a concurrent modification of scaled health attributes (#12002)
PaperMC/Paper@88bbead1 Flush regionfiles on save configuration option
PaperMC/Paper@336ea9df Check for empty when sending equipment changes (#12008)
PaperMC/Paper@939bb782 Add RayTraceConfigurationBuilder (#11907)
PaperMC/Paper@81bb82f5 Fix wrong piston world border check (#12007)
PaperMC/Paper@ce95b5d6 Use proper default for setting null display background color (#12010)
PaperMC/Paper@2477f1f6 [ci/skip] fix and improvements for docs in ConsumeEffect component (#11998)
PaperMC/Paper@fb5b173c Add PlayerClientLoadedWorldEvent (#11940)
PaperMC/Paper@3af5e771 Add Player#give (#11995)
PaperMC/Paper@7e21cb81 fix PlayerChangedMainHandEvent javadoc (#12020)
PaperMC/Paper@5a34bf04 Correctly retrun true for empty input shapes in EntityGetter#isUnobstructed
PaperMC/Paper@a392d475 Make Watchdog thread extend TickThread

Gale Changes:
Dreeam-qwq/Gale@f9080a7e Updated Upstream (Paper)
Dreeam-qwq/Gale@ff0596c1 [ci/skip] Fix upstream commit sh on mac
Dreeam-qwq/Gale@24970274 [ci/skip] Hermanez - Wutaf
Dreeam-qwq/Gale@85eabf60 [ci/skip] cleanup
Dreeam-qwq/Gale@7d9faf00 [ci/skip] cleanup & drop xor-shift random
Dreeam-qwq/Gale@7af04981 [ci/skip] cleanup
Dreeam-qwq/Gale@4d5d39df [ci/skip] Remove useless params standardize in upstream commit generator
Dreeam-qwq/Gale@964f16ff Updated Upstream (Paper)
Dreeam-qwq/Gale@0566a223 [ci/skip] cleanup
Dreeam-qwq/Gale@5e3f6740 [ci/skip] cleanup work finished
Dreeam-qwq/Gale@98a66cfb Updated Upstream (Paper)
Dreeam-qwq/Gale@f7736578 [ci/skip] Update upstreamCommit.sh
Dreeam-qwq/Gale@1c46c816 Updated Upstream (Paper)
Dreeam-qwq/Gale@2b0a4c09 [ci/skip] Skip tests during auto update validate phase

Purpur Changes:
PurpurMC/Purpur@4a0a86b9 Updated Upstream (Paper)
PurpurMC/Purpur@7399988c Fix hover in /plugins
PurpurMC/Purpur@5e5857dc [ci/skip] modify ci skip references in paper upstream commits
PurpurMC/Purpur@5583a3f1 Updated Upstream (Paper)
2025-01-27 20:08:18 -05:00

203 lines
12 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: peaches94 <peachescu94@gmail.com>
Date: Sun, 10 Jul 2022 13:29:20 -0500
Subject: [PATCH] Petal: reduce work done by game event system
Original license: GPL v3
Original project: https://github.com/Bloom-host/Petal
1. going into game event dispatching can be expensive so run the checks before dispatching
2. EuclideanGameEventListenerRegistry is not used concurrently so we ban that usage for improved performance with allays
diff --git a/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java b/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java
index 1638eccef431fb68775af624110f1968f0c6dabd..62038854696bd946f58e0e8d26da02415c34e4b1 100644
--- a/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java
+++ b/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java
@@ -65,7 +65,7 @@ public class SculkCatalystBlockEntity extends BlockEntity implements GameEventLi
return this.catalystListener;
}
- public static class CatalystListener implements GameEventListener {
+ public class CatalystListener implements GameEventListener { // Leaf - petal - reduce work done by game event system
public static final int PULSE_TICKS = 8;
final SculkSpreader sculkSpreader;
private final BlockState blockState;
@@ -127,6 +127,13 @@ public class SculkCatalystBlockEntity extends BlockEntity implements GameEventLi
level.playSound(null, pos, SoundEvents.SCULK_CATALYST_BLOOM, SoundSource.BLOCKS, 2.0F, 0.6F + random.nextFloat() * 0.4F);
}
+ // Leaf start - petal - reduce work done by game event system
+ @Override
+ public boolean listensToEvent(GameEvent gameEvent, GameEvent.Context context) {
+ return !SculkCatalystBlockEntity.this.isRemoved() && gameEvent == GameEvent.ENTITY_DIE.value() && context.sourceEntity() instanceof LivingEntity;
+ }
+ // Leaf end - petal - reduce work done by game event system
+
private void tryAwardItSpreadsAdvancement(Level level, LivingEntity entity) {
if (entity.getLastHurtByMob() instanceof ServerPlayer serverPlayer) {
DamageSource damageSource = entity.getLastDamageSource() == null
diff --git a/net/minecraft/world/level/chunk/LevelChunk.java b/net/minecraft/world/level/chunk/LevelChunk.java
index 95972e7d5e0357ff5884f1cb2f7596c5029f999d..290163335cf3967e2745442fd7d4d4fa16fb7bc0 100644
--- a/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/net/minecraft/world/level/chunk/LevelChunk.java
@@ -79,7 +79,19 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
private Supplier<FullChunkStatus> fullStatus;
@Nullable
private LevelChunk.PostLoadProcessor postLoad;
- private final Int2ObjectMap<GameEventListenerRegistry> gameEventListenerRegistrySections;
+ // Leaf start - petal - reduce work done by game event system
+ private final GameEventListenerRegistry[] gameEventListenerRegistrySections;
+ private static final int GAME_EVENT_DISPATCHER_RADIUS = 2;
+
+ private static int getGameEventSectionIndex(int sectionIndex) {
+ return sectionIndex + GAME_EVENT_DISPATCHER_RADIUS;
+ }
+
+ private static int getGameEventSectionLength(int sectionCount) {
+ return sectionCount + (GAME_EVENT_DISPATCHER_RADIUS * 2);
+ }
+
+ // Leaf end - petal - reduce work done by game event system
private final LevelChunkTicks<Block> blockTicks;
private final LevelChunkTicks<Fluid> fluidTicks;
private LevelChunk.UnsavedListener unsavedListener = chunkPos -> {};
@@ -154,7 +166,7 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
) {
super(pos, data, level, net.minecraft.server.MinecraftServer.getServer().registryAccess().lookupOrThrow(Registries.BIOME), inhabitedTime, sections, blendingData); // Paper - Anti-Xray - The world isn't ready yet, use server singleton for registry
this.level = (ServerLevel) level; // CraftBukkit - type
- this.gameEventListenerRegistrySections = new Int2ObjectOpenHashMap<>();
+ this.gameEventListenerRegistrySections = new GameEventListenerRegistry[getGameEventSectionLength(this.getSectionsCount())]; // Leaf - petal - reduce work done by game event system
for (Heightmap.Types types : Heightmap.Types.values()) {
if (ChunkStatus.FULL.heightmapsAfter().contains(types)) {
@@ -266,10 +278,27 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
@Override
public GameEventListenerRegistry getListenerRegistry(int sectionY) {
- return this.level instanceof ServerLevel serverLevel
- ? this.gameEventListenerRegistrySections
- .computeIfAbsent(sectionY, i -> new EuclideanGameEventListenerRegistry(serverLevel, sectionY, this::removeGameEventListenerRegistry))
- : super.getListenerRegistry(sectionY);
+ // Leaf start - petal - reduce work done by game event system
+ if (this.level instanceof ServerLevel serverLevel) {
+ int sectionIndex = getGameEventSectionIndex(this.getSectionIndexFromSectionY(sectionY));
+
+ // drop game events that are too far away (32 blocks) from loaded sections
+ // this matches the highest radius of game events in the game
+ if (sectionIndex < 0 || sectionIndex >= this.gameEventListenerRegistrySections.length) {
+ return GameEventListenerRegistry.NOOP;
+ }
+
+ var dispatcher = this.gameEventListenerRegistrySections[sectionIndex];
+
+ if (dispatcher == null) {
+ dispatcher = this.gameEventListenerRegistrySections[sectionIndex] = new EuclideanGameEventListenerRegistry(serverLevel, sectionY, this::removeGameEventListenerRegistry);
+ }
+
+ return dispatcher;
+ }
+
+ return super.getListenerRegistry(sectionY);
+ // Leaf end - petal - reduce work done by game event system
}
// Paper start - Perf: Reduce instructions and provide final method
@@ -609,7 +638,7 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
}
private void removeGameEventListenerRegistry(int sectionY) {
- this.gameEventListenerRegistrySections.remove(sectionY);
+ this.gameEventListenerRegistrySections[getGameEventSectionIndex(this.getSectionIndexFromSectionY(sectionY))] = null; // Leaf - petal - reduce work done by game event system
}
private void removeBlockEntityTicker(BlockPos pos) {
diff --git a/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java b/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java
index 5175fc90a1fc61c832c6697997a97ae199b195ac..fc56ef2d5ed813db51e35b635e373b6f8035593b 100644
--- a/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java
+++ b/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java
@@ -14,8 +14,8 @@ import net.minecraft.world.phys.Vec3;
public class EuclideanGameEventListenerRegistry implements GameEventListenerRegistry {
private final List<GameEventListener> listeners = Lists.newArrayList();
- private final Set<GameEventListener> listenersToRemove = Sets.newHashSet();
- private final List<GameEventListener> listenersToAdd = Lists.newArrayList();
+ //private final Set<GameEventListener> listenersToRemove = Sets.newHashSet(); // Leaf - petal - reduce work done by game event system - Not necessary
+ //private final List<GameEventListener> listenersToAdd = Lists.newArrayList(); // Leaf - petal - reduce work done by game event system
private boolean processing;
private final ServerLevel level;
private final int sectionY;
@@ -35,7 +35,7 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi
@Override
public void register(GameEventListener listener) {
if (this.processing) {
- this.listenersToAdd.add(listener);
+ throw new java.util.ConcurrentModificationException(); // Leaf - petal - reduce work done by game event system - Disallow concurrent modification
} else {
this.listeners.add(listener);
}
@@ -46,7 +46,7 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi
@Override
public void unregister(GameEventListener listener) {
if (this.processing) {
- this.listenersToRemove.add(listener);
+ throw new java.util.ConcurrentModificationException(); // Leaf - petal - reduce work done by game event system - Disallow concurrent modification
} else {
this.listeners.remove(listener);
}
@@ -66,7 +66,7 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi
while (iterator.hasNext()) {
GameEventListener gameEventListener = iterator.next();
- if (this.listenersToRemove.remove(gameEventListener)) {
+ if (false) { // Leaf - petal - reduce work done by game event system - Disallow concurrent modification
iterator.remove();
} else {
Optional<Vec3> postableListenerPosition = getPostableListenerPosition(this.level, pos, gameEventListener);
@@ -80,6 +80,8 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi
this.processing = false;
}
+ // Leaf start - petal - reduce work done by game event system
+ /*
if (!this.listenersToAdd.isEmpty()) {
this.listeners.addAll(this.listenersToAdd);
this.listenersToAdd.clear();
@@ -89,6 +91,8 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi
this.listeners.removeAll(this.listenersToRemove);
this.listenersToRemove.clear();
}
+ */
+ // Leaf end - petal - reduce work done by game event system
return flag;
}
diff --git a/net/minecraft/world/level/gameevent/GameEventDispatcher.java b/net/minecraft/world/level/gameevent/GameEventDispatcher.java
index 1e9b066ef468ae840eda3c1f6c4b68111a5e862c..1074ab996b48782a76d5afeb6fc790bdd33210ee 100644
--- a/net/minecraft/world/level/gameevent/GameEventDispatcher.java
+++ b/net/minecraft/world/level/gameevent/GameEventDispatcher.java
@@ -44,6 +44,7 @@ public class GameEventDispatcher {
int sectionPosCoord5 = SectionPos.blockToSectionCoord(blockPos.getZ() + notificationRadius);
List<GameEvent.ListenerInfo> list = new ArrayList<>();
GameEventListenerRegistry.ListenerVisitor listenerVisitor = (listener, pos1) -> {
+ if (!listener.listensToEvent(gameEvent.value(), context)) return; // Leaf - petal - reduce work done by game event system - If they don't listen, ignore
if (listener.getDeliveryMode() == GameEventListener.DeliveryMode.BY_DISTANCE) {
list.add(new GameEvent.ListenerInfo(gameEvent, pos, context, listener, pos1));
} else {
diff --git a/net/minecraft/world/level/gameevent/GameEventListener.java b/net/minecraft/world/level/gameevent/GameEventListener.java
index 5a31b5f1e75dd7b412ab577ea6621b7e87fc0590..4d991ab3290646ec3fd6645154abfa5b4e42d00a 100644
--- a/net/minecraft/world/level/gameevent/GameEventListener.java
+++ b/net/minecraft/world/level/gameevent/GameEventListener.java
@@ -23,4 +23,11 @@ public interface GameEventListener {
public interface Provider<T extends GameEventListener> {
T getListener();
}
+
+ // Leaf start - petal - reduce work done by game event system
+ // Add check for seeing if this listener cares about an event
+ default boolean listensToEvent(GameEvent gameEvent, GameEvent.Context context) {
+ return true;
+ }
+ // Leaf end - petal - reduce work done by game event system
}