Skip to content

Commit 039af3c

Browse files
authored
Merge pull request #3327 from Multiverse/5.3
5.3
2 parents bd09d6c + 09ec4f8 commit 039af3c

6 files changed

Lines changed: 92 additions & 24 deletions

File tree

src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ public boolean getEnforceFlight() {
190190
return configHandle.get(configNodes.enforceFlight);
191191
}
192192

193+
@ApiStatus.AvailableSince("5.3")
194+
public Try<Void> setApplyEntitySpawnRate(boolean applyEntitySpawnRate) {
195+
return configHandle.set(configNodes.applyEntitySpawnRate, applyEntitySpawnRate);
196+
}
197+
198+
@ApiStatus.AvailableSince("5.3")
199+
public boolean getApplyEntitySpawnRate() {
200+
return configHandle.get(configNodes.applyEntitySpawnRate);
201+
}
202+
203+
@ApiStatus.AvailableSince("5.3")
204+
public Try<Void> setApplyEntitySpawnLimit(boolean applyEntitySpawnLimit) {
205+
return configHandle.set(configNodes.applyEntitySpawnLimit, applyEntitySpawnLimit);
206+
}
207+
208+
@ApiStatus.AvailableSince("5.3")
209+
public boolean getApplyEntitySpawnLimit() {
210+
return configHandle.get(configNodes.applyEntitySpawnLimit);
211+
}
212+
193213
/**
194214
* {@inheritDoc}
195215
*/

src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ private <N extends Node> N node(N node) {
137137
.name("enforce-flight")
138138
.build());
139139

140+
final ConfigNode<Boolean> applyEntitySpawnRate = node(ConfigNode.builder("world.apply-entity-spawn-rate", Boolean.class)
141+
.comment("")
142+
.comment("Sets whether Multiverse will apply the world's entity `tick-rate` config in worlds.yml.")
143+
.comment("If disabled, the `tick-rate` config in worlds.yml will be ignored.")
144+
.comment("Disable this if you want paper-world.yml or another plugin to handle entity spawn rate per world.")
145+
.defaultValue(true)
146+
.name("apply-entity-spawn-rate")
147+
.build());
148+
149+
final ConfigNode<Boolean> applyEntitySpawnLimit = node(ConfigNode.builder("world.apply-entity-spawn-limit", Boolean.class)
150+
.comment("")
151+
.comment("Sets whether Multiverse will apply the world's entity `spawn-limit` config when a world is loaded.")
152+
.comment("If disabled, the `spawn-limit` config in worlds.yml will be ignored.")
153+
.comment("Disable this if you want paper-world.yml or another plugin to handle entity limits per world.")
154+
.defaultValue(true)
155+
.name("apply-entity-spawn-limit")
156+
.build());
157+
140158
final ConfigNode<Boolean> autoPurgeEntities = node(ConfigNode.builder("world.auto-purge-entities", Boolean.class)
141159
.comment("")
142160
.comment("Sets whether Multiverse will purge entities on world load based world's entity spawn config.")

src/main/java/org/mvplugins/multiverse/core/world/WorldConfigNodes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.jetbrains.annotations.NotNull;
1717

1818
import org.mvplugins.multiverse.core.MultiverseCore;
19+
import org.mvplugins.multiverse.core.config.CoreConfig;
1920
import org.mvplugins.multiverse.core.config.node.serializer.NodeSerializer;
2021
import org.mvplugins.multiverse.core.event.world.MVWorldPropertyChangedEvent;
2122
import org.mvplugins.multiverse.core.config.node.ConfigNode;
@@ -37,11 +38,13 @@ final class WorldConfigNodes {
3738
private final NodeGroup nodes = new NodeGroup();
3839
private WorldManager worldManager;
3940
private EnforcementHandler enforcementHandler;
41+
private CoreConfig config;
4042
private MultiverseWorld world = null;
4143

4244
WorldConfigNodes(@NotNull MultiverseCore multiverseCore) {
4345
this.worldManager = multiverseCore.getServiceLocator().getService(WorldManager.class);
4446
this.enforcementHandler = multiverseCore.getServiceLocator().getService(EnforcementHandler.class);
47+
this.config = multiverseCore.getServiceLocator().getService(CoreConfig.class);
4548
}
4649

4750
MultiverseWorld getWorld() {
@@ -244,15 +247,15 @@ public Object serialize(Material object, Class<Material> type) {
244247
}));
245248

246249
final ConfigNode<EntitySpawnConfig> enititySpawnConfig = node(ConfigNode.builder("spawning", EntitySpawnConfig.class)
247-
.defaultValue(() -> EntitySpawnConfig.fromSection(new MemoryConfiguration()))
250+
.defaultValue(() -> EntitySpawnConfig.fromSection(config, new MemoryConfiguration()))
248251
.hidden()
249252
.serializer(new NodeSerializer<>() {
250253
@Override
251254
public EntitySpawnConfig deserialize(Object object, Class<EntitySpawnConfig> type) {
252255
ConfigurationSection spawnSection = (object instanceof ConfigurationSection section)
253256
? section
254257
: new MemoryConfiguration();
255-
return EntitySpawnConfig.fromSection(spawnSection);
258+
return EntitySpawnConfig.fromSection(config, spawnSection);
256259
}
257260

258261
@Override

src/main/java/org/mvplugins/multiverse/core/world/entity/EntitySpawnConfig.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
package org.mvplugins.multiverse.core.world.entity;
22

33
import com.dumptruckman.minecraft.util.Logging;
4-
import org.bukkit.World;
54
import org.bukkit.configuration.ConfigurationSection;
65
import org.bukkit.configuration.MemoryConfiguration;
76
import org.bukkit.entity.Entity;
87
import org.bukkit.entity.SpawnCategory;
98
import org.jetbrains.annotations.ApiStatus;
9+
import org.mvplugins.multiverse.core.config.CoreConfig;
1010
import org.mvplugins.multiverse.core.utils.StringFormatter;
1111
import org.mvplugins.multiverse.core.world.MultiverseWorld;
1212

13-
import java.util.HashMap;
13+
import java.util.LinkedHashMap;
1414
import java.util.Map;
1515

1616
public final class EntitySpawnConfig {
1717

18+
private final CoreConfig config;
1819
private final Map<SpawnCategory, SpawnCategoryConfig> spawnCategoriesConfig;
1920

20-
EntitySpawnConfig(Map<SpawnCategory, SpawnCategoryConfig> spawnCategoriesConfig) {
21+
EntitySpawnConfig(CoreConfig config, Map<SpawnCategory, SpawnCategoryConfig> spawnCategoriesConfig) {
22+
this.config = config;
2123
this.spawnCategoriesConfig = spawnCategoriesConfig;
2224
}
2325

2426
public SpawnCategoryConfig getSpawnCategoryConfig(SpawnCategory spawnCategory) {
2527
return spawnCategoriesConfig.computeIfAbsent(spawnCategory,
26-
computeSpawnCategory -> new SpawnCategoryConfig(computeSpawnCategory, new MemoryConfiguration()));
28+
computeSpawnCategory -> new SpawnCategoryConfig(
29+
config,
30+
computeSpawnCategory,
31+
new MemoryConfiguration()
32+
));
2733
}
2834

2935
public boolean shouldAllowSpawn(Entity entity) {
@@ -52,17 +58,17 @@ public ConfigurationSection toSection() {
5258
}
5359

5460
@ApiStatus.Internal
55-
public static EntitySpawnConfig fromSection(ConfigurationSection section) {
56-
Map<SpawnCategory, SpawnCategoryConfig> spawnCategoriesConfig = new HashMap<>();
61+
public static EntitySpawnConfig fromSection(CoreConfig config, ConfigurationSection section) {
62+
Map<SpawnCategory, SpawnCategoryConfig> spawnCategoriesConfig = new LinkedHashMap<>();
5763
section.getValues(false).forEach((key, value) -> {
5864
if (!(value instanceof ConfigurationSection sectionPart)) {
5965
Logging.warning("Invalid spawn category config for " + key + ": " + value);
6066
return;
6167
}
6268
SpawnCategory spawnCategory = SpawnCategory.valueOf(key.toUpperCase());
63-
spawnCategoriesConfig.put(spawnCategory, new SpawnCategoryConfig(spawnCategory, sectionPart));
69+
spawnCategoriesConfig.put(spawnCategory, new SpawnCategoryConfig(config, spawnCategory, sectionPart));
6470
});
65-
return new EntitySpawnConfig(spawnCategoriesConfig);
71+
return new EntitySpawnConfig(config, spawnCategoriesConfig);
6672
}
6773

6874
@ApiStatus.Internal

src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.entity.Entity;
88
import org.bukkit.entity.EntityType;
99
import org.bukkit.entity.SpawnCategory;
10+
import org.mvplugins.multiverse.core.config.CoreConfig;
1011
import org.mvplugins.multiverse.core.config.handle.MemoryConfigurationHandle;
1112
import org.mvplugins.multiverse.core.config.handle.StringPropertyHandle;
1213
import org.mvplugins.multiverse.core.config.node.ConfigNode;
@@ -21,14 +22,16 @@
2122

2223
public final class SpawnCategoryConfig {
2324

25+
private final CoreConfig config;
2426
private final SpawnCategory spawnCategory;
2527
private final MemoryConfigurationHandle handle;
2628
private final StringPropertyHandle stringPropertyHandle;
2729
private final Nodes nodes;
2830

2931
private MultiverseWorld world;
3032

31-
SpawnCategoryConfig(SpawnCategory spawnCategory, ConfigurationSection section) {
33+
SpawnCategoryConfig(CoreConfig config, SpawnCategory spawnCategory, ConfigurationSection section) {
34+
this.config = config;
3235
this.spawnCategory = spawnCategory;
3336
this.nodes = new Nodes();
3437
this.handle = MemoryConfigurationHandle.builder(section, nodes.nodes)
@@ -57,21 +60,37 @@ void applyConfigToWorld() {
5760
return;
5861
}
5962
loadedWorld.getBukkitWorld().peek(bukkitWorld -> {
60-
if (!isSpawn()) {
61-
if (getExceptions().isEmpty()) {
62-
Logging.finer("World %s %s setTicksPerSpawns: 0", world.getName(), spawnCategory);
63-
bukkitWorld.setTicksPerSpawns(spawnCategory, 0);
64-
} else {
65-
Logging.finer("World %s %s setTicksPerSpawns: -1", world.getName(), spawnCategory);
66-
bukkitWorld.setTicksPerSpawns(spawnCategory, -1);
67-
}
63+
applyTickPerSpawns(bukkitWorld);
64+
applySpawnLimit(bukkitWorld);
65+
});
66+
}
67+
68+
private void applyTickPerSpawns(World bukkitWorld) {
69+
if (!config.getApplyEntitySpawnLimit()) {
70+
Logging.finer("World %s %s skipping setTicksPerSpawns due to core config", world.getName(), spawnCategory);
71+
return;
72+
}
73+
if (!isSpawn()) {
74+
if (getExceptions().isEmpty()) {
75+
Logging.finer("World %s %s setTicksPerSpawns: 0", world.getName(), spawnCategory);
76+
bukkitWorld.setTicksPerSpawns(spawnCategory, 0);
6877
} else {
69-
Logging.finer("World %s %s setTicksPerSpawns: %d", world.getName(), spawnCategory, getTickRate());
70-
bukkitWorld.setTicksPerSpawns(spawnCategory, getTickRate());
78+
Logging.finer("World %s %s setTicksPerSpawns: -1", world.getName(), spawnCategory);
79+
bukkitWorld.setTicksPerSpawns(spawnCategory, -1);
7180
}
72-
Logging.finer("World %s %s setSpawnLimit: %d", world.getName(), spawnCategory, getSpawnLimit());
73-
bukkitWorld.setSpawnLimit(spawnCategory, getSpawnLimit());
74-
});
81+
} else {
82+
Logging.finer("World %s %s setTicksPerSpawns: %d", world.getName(), spawnCategory, getTickRate());
83+
bukkitWorld.setTicksPerSpawns(spawnCategory, getTickRate());
84+
}
85+
}
86+
87+
private void applySpawnLimit(World bukkitWorld) {
88+
if (!config.getApplyEntitySpawnLimit()) {
89+
Logging.finer("Skipping World %s %s setSpawnLimit due to core config", world.getName(), spawnCategory);
90+
return;
91+
}
92+
Logging.finer("World %s %s setSpawnLimit: %d", world.getName(), spawnCategory, getSpawnLimit());
93+
bukkitWorld.setSpawnLimit(spawnCategory, getSpawnLimit());
7594
}
7695

7796
public StringPropertyHandle getStringPropertyHandle() {

src/test/resources/configs/fresh_config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ world:
44
enforce-access: false
55
enforce-gamemode: true
66
enforce-flight: true
7+
apply-entity-spawn-rate: true
8+
apply-entity-spawn-limit: true
79
auto-purge-entities: false
810
world-name-format:
911
nether: '%overworld%_nether'

0 commit comments

Comments
 (0)