From a600754f0d96be7f4cfb2401aa73460b98a296c7 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 28 Jun 2021 22:38:29 +0100 Subject: [PATCH] Rate options and timings for sensors and behaviors This adds config options to specify the tick rate for sensors and behaviors of different entity types as well as timings for those in order to be able to have some metrics as to which ones might need tweaking. --- .../entity/ai/behavior/Behavior.java.patch | 40 +++++++++++++++++++ .../world/entity/ai/sensing/Sensor.java.patch | 34 ++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 paper-server/patches/sources/net/minecraft/world/entity/ai/behavior/Behavior.java.patch create mode 100644 paper-server/patches/sources/net/minecraft/world/entity/ai/sensing/Sensor.java.patch diff --git a/paper-server/patches/sources/net/minecraft/world/entity/ai/behavior/Behavior.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/ai/behavior/Behavior.java.patch new file mode 100644 index 000000000..dab10a1e2 --- /dev/null +++ b/paper-server/patches/sources/net/minecraft/world/entity/ai/behavior/Behavior.java.patch @@ -0,0 +1,40 @@ +--- a/net/minecraft/world/entity/ai/behavior/Behavior.java ++++ b/net/minecraft/world/entity/ai/behavior/Behavior.java +@@ -14,6 +14,9 @@ + private long endTimestamp; + private final int minDuration; + private final int maxDuration; ++ // Paper start - configurable behavior tick rate and timings ++ private final String configKey; ++ // Paper end - configurable behavior tick rate and timings + + public Behavior(Map, MemoryStatus> requiredMemoryState) { + this(requiredMemoryState, 60); +@@ -27,6 +30,14 @@ + this.minDuration = minRunTime; + this.maxDuration = maxRunTime; + this.entryCondition = requiredMemoryState; ++ // Paper start - configurable behavior tick rate and timings ++ String key = io.papermc.paper.util.MappingEnvironment.reobf() ? io.papermc.paper.util.ObfHelper.INSTANCE.deobfClassName(this.getClass().getName()) : this.getClass().getName(); ++ int lastSeparator = key.lastIndexOf('.'); ++ if (lastSeparator != -1) { ++ key = key.substring(lastSeparator + 1); ++ } ++ this.configKey = key.toLowerCase(java.util.Locale.ROOT); ++ // Paper end - configurable behavior tick rate and timings + } + + @Override +@@ -36,6 +47,12 @@ + + @Override + public final boolean tryStart(ServerLevel world, E entity, long time) { ++ // Paper start - configurable behavior tick rate and timings ++ int tickRate = java.util.Objects.requireNonNullElse(world.paperConfig().tickRates.behavior.get(entity.getType(), this.configKey), -1); ++ if (tickRate > -1 && time < this.endTimestamp + tickRate) { ++ return false; ++ } ++ // Paper end - configurable behavior tick rate and timings + if (this.hasRequiredMemories(entity) && this.checkExtraStartConditions(world, entity)) { + this.status = Behavior.Status.RUNNING; + int i = this.minDuration + world.getRandom().nextInt(this.maxDuration + 1 - this.minDuration); diff --git a/paper-server/patches/sources/net/minecraft/world/entity/ai/sensing/Sensor.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/ai/sensing/Sensor.java.patch new file mode 100644 index 000000000..7d220911a --- /dev/null +++ b/paper-server/patches/sources/net/minecraft/world/entity/ai/sensing/Sensor.java.patch @@ -0,0 +1,34 @@ +--- a/net/minecraft/world/entity/ai/sensing/Sensor.java ++++ b/net/minecraft/world/entity/ai/sensing/Sensor.java +@@ -29,8 +29,19 @@ + .ignoreInvisibilityTesting(); + private final int scanRate; + private long timeToTick; ++ // Paper start - configurable sensor tick rate and timings ++ private final String configKey; ++ // Paper end + + public Sensor(int senseInterval) { ++ // Paper start - configurable sensor tick rate and timings ++ String key = io.papermc.paper.util.MappingEnvironment.reobf() ? io.papermc.paper.util.ObfHelper.INSTANCE.deobfClassName(this.getClass().getName()) : this.getClass().getName(); ++ int lastSeparator = key.lastIndexOf('.'); ++ if (lastSeparator != -1) { ++ key = key.substring(lastSeparator + 1); ++ } ++ this.configKey = key.toLowerCase(java.util.Locale.ROOT); ++ // Paper end + this.scanRate = senseInterval; + this.timeToTick = (long)RANDOM.nextInt(senseInterval); + } +@@ -41,8 +52,10 @@ + + public final void tick(ServerLevel world, E entity) { + if (--this.timeToTick <= 0L) { +- this.timeToTick = (long)this.scanRate; ++ // Paper start - configurable sensor tick rate and timings ++ this.timeToTick = java.util.Objects.requireNonNullElse(world.paperConfig().tickRates.sensor.get(entity.getType(), this.configKey), this.scanRate); + this.updateTargetingConditionRanges(entity); ++ // Paper end + this.doTick(world, entity); + } + }