Skip to content

Commit 6265372

Browse files
committed
Default spawn rate and limit to "unset" instead of server wide's default
1 parent 9fa4772 commit 6265372

File tree

9 files changed

+651
-54
lines changed

9 files changed

+651
-54
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public boolean shouldAllowSpawn(Entity entity) {
3838
}
3939

4040
public void applyConfigToWorld() {
41-
spawnCategoriesConfig.values()
42-
.forEach(SpawnCategoryConfig::applyConfigToWorld);
41+
for (SpawnCategory category : SpawnCategory.values()) {
42+
getSpawnCategoryConfig(category).applyConfigToWorld();
43+
}
4344
}
4445

4546
@Override
@@ -61,14 +62,18 @@ public ConfigurationSection toSection() {
6162
@ApiStatus.Internal
6263
public static EntitySpawnConfig fromSection(CoreConfig config, ConfigurationSection section) {
6364
Map<SpawnCategory, SpawnCategoryConfig> spawnCategoriesConfig = new LinkedHashMap<>();
64-
section.getValues(false).forEach((key, value) -> {
65-
if (!(value instanceof ConfigurationSection sectionPart)) {
66-
Logging.warning("Invalid spawn category config for " + key + ": " + value);
67-
return;
65+
Map<String, Object> existingCategories = section.getValues(false);
66+
for (SpawnCategory category : SpawnCategory.values()) {
67+
Object value = existingCategories.get(category.toString().toLowerCase(Locale.ENGLISH));
68+
if (!(value instanceof ConfigurationSection)) {
69+
if (value != null) {
70+
Logging.warning("Invalid spawn category config for " + category + ": " + value);
71+
}
72+
value = section.createSection(category.toString());
6873
}
69-
SpawnCategory spawnCategory = SpawnCategory.valueOf(key.toUpperCase(Locale.ENGLISH));
70-
spawnCategoriesConfig.put(spawnCategory, new SpawnCategoryConfig(config, spawnCategory, sectionPart));
71-
});
74+
ConfigurationSection sectionPart = (ConfigurationSection) value;
75+
spawnCategoriesConfig.put(category, new SpawnCategoryConfig(config, category, sectionPart));
76+
}
7277
return new EntitySpawnConfig(config, spawnCategoriesConfig);
7378
}
7479

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

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.mvplugins.multiverse.core.config.node.ListConfigNode;
1616
import org.mvplugins.multiverse.core.config.node.Node;
1717
import org.mvplugins.multiverse.core.config.node.NodeGroup;
18+
import org.mvplugins.multiverse.core.config.node.serializer.NodeSerializer;
1819
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
1920
import org.mvplugins.multiverse.core.world.MultiverseWorld;
2021

@@ -71,25 +72,28 @@ private void applyTickPerSpawns(World bukkitWorld) {
7172
Logging.finer("World %s %s skipping setTicksPerSpawns due to core config", world.getName(), spawnCategory);
7273
return;
7374
}
74-
if (!isSpawn()) {
75-
if (getExceptions().isEmpty()) {
76-
Logging.finer("World %s %s setTicksPerSpawns: 0", world.getName(), spawnCategory);
77-
bukkitWorld.setTicksPerSpawns(spawnCategory, 0);
78-
} else {
79-
Logging.finer("World %s %s setTicksPerSpawns: -1", world.getName(), spawnCategory);
80-
bukkitWorld.setTicksPerSpawns(spawnCategory, -1);
81-
}
82-
} else {
83-
Logging.finer("World %s %s setTicksPerSpawns: %d", world.getName(), spawnCategory, getTickRate());
84-
bukkitWorld.setTicksPerSpawns(spawnCategory, getTickRate());
75+
if (!isSpawn() && getExceptions().isEmpty()) {
76+
Logging.finer("World %s %s setTicksPerSpawns: 0", world.getName(), spawnCategory);
77+
bukkitWorld.setTicksPerSpawns(spawnCategory, 0);
78+
return;
79+
}
80+
if (getTickRate() == -2) {
81+
Logging.finer("World %s %s skipping setTicksPerSpawns as tick-rate is UNSET", world.getName(), spawnCategory);
82+
return;
8583
}
84+
Logging.finer("World %s %s setTicksPerSpawns: %d", world.getName(), spawnCategory, getTickRate());
85+
bukkitWorld.setTicksPerSpawns(spawnCategory, getTickRate());
8686
}
8787

8888
private void applySpawnLimit(World bukkitWorld) {
8989
if (!config.getApplyEntitySpawnLimit()) {
9090
Logging.finer("Skipping World %s %s setSpawnLimit due to core config", world.getName(), spawnCategory);
9191
return;
9292
}
93+
if (getSpawnLimit() == -2) {
94+
Logging.finer("World %s %s skipping setSpawnLimit as spawn-limit is UNSET", world.getName(), spawnCategory);
95+
return;
96+
}
9397
Logging.finer("World %s %s setSpawnLimit: %d", world.getName(), spawnCategory, getSpawnLimit());
9498
bukkitWorld.setSpawnLimit(spawnCategory, getSpawnLimit());
9599
}
@@ -166,25 +170,33 @@ private <N extends Node> N node(N node) {
166170
.build());
167171

168172
final ConfigNode<Integer> tickRate = node(ConfigNode.builder("tick-rate", Integer.class)
169-
.defaultValue(-1)
170-
.suggester(input -> List.of("-1", "10", "100", "400", "1000"))
173+
.defaultValue(-2)
174+
.suggester(input -> List.of("@unset", "@bukkit", "10", "100", "400", "1000"))
175+
.serializer(SpawnValueSerializer.INSTANCE)
171176
.onLoadAndChange((oldValue, newValue) -> applyConfigToWorld())
172177
.onChange((sender, oldValue, newValue) -> {
173178
if (!config.getApplyEntitySpawnRate()) {
174179
sender.sendMessage(ChatColor.RED + "Warning: Changing tick rates has no effect because " +
175180
"'apply-entity-spawn-rate' is disabled in the core config.");
181+
} else if (newValue == -2) {
182+
sender.sendMessage(ChatColor.YELLOW + "Note: Setting tick-rate to '@unset' may not reset to " +
183+
"the server default until the world is reloaded or server is restarted.");
176184
}
177185
})
178186
.build());
179187

180188
final ConfigNode<Integer> spawnLimit = node(ConfigNode.builder("spawn-limit", Integer.class)
181-
.defaultValue(-1)
182-
.suggester(input -> List.of("-1", "10", "100", "400", "1000"))
189+
.defaultValue(-2)
190+
.suggester(input -> List.of("@unset", "@bukkit", "10", "100", "400", "1000"))
191+
.serializer(SpawnValueSerializer.INSTANCE)
183192
.onLoadAndChange((oldValue, newValue) -> applyConfigToWorld())
184193
.onChange((sender, oldValue, newValue) -> {
185194
if (!config.getApplyEntitySpawnLimit()) {
186195
sender.sendMessage(ChatColor.RED + "Warning: Changing spawn limits has no effect because " +
187196
"'apply-entity-spawn-limit' is disabled in the core config.");
197+
} else if (newValue == -2) {
198+
sender.sendMessage(ChatColor.YELLOW + "Note: Setting spawn-limit to '@unset' may not reset to " +
199+
"the server default until the world is reloaded or server is restarted.");
188200
}
189201
})
190202
.build());
@@ -197,4 +209,29 @@ private <N extends Node> N node(N node) {
197209
.onLoadAndChange((oldValue, newValue) -> applyConfigToWorld())
198210
.build());
199211
}
212+
213+
private static class SpawnValueSerializer implements NodeSerializer<Integer> {
214+
private static final SpawnValueSerializer INSTANCE = new SpawnValueSerializer();
215+
216+
@Override
217+
public Integer deserialize(Object object, Class<Integer> type) {
218+
String str = String.valueOf(object);
219+
if (str.equalsIgnoreCase("@unset")) {
220+
return -2;
221+
}
222+
if (str.equalsIgnoreCase("@bukkit")) {
223+
return -1;
224+
}
225+
return Integer.parseInt(str);
226+
}
227+
228+
@Override
229+
public Object serialize(Integer object, Class<Integer> type) {
230+
return switch (object) {
231+
case -2 -> "@unset";
232+
case -1 -> "@bukkit";
233+
default -> object;
234+
};
235+
}
236+
}
200237
}

src/test/resources/worlds/default_worlds.yml

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,47 @@ world:
3333
z: 48.0
3434
pitch: 0.0
3535
yaw: 0.0
36-
spawning: {}
36+
spawning:
37+
monster:
38+
exceptions: [ ]
39+
spawn: true
40+
spawn-limit: '@unset'
41+
tick-rate: '@unset'
42+
animal:
43+
exceptions: [ ]
44+
spawn: true
45+
spawn-limit: '@unset'
46+
tick-rate: '@unset'
47+
water_animal:
48+
exceptions: [ ]
49+
spawn: true
50+
spawn-limit: '@unset'
51+
tick-rate: '@unset'
52+
water_ambient:
53+
exceptions: [ ]
54+
spawn: true
55+
spawn-limit: '@unset'
56+
tick-rate: '@unset'
57+
water_underground_creature:
58+
exceptions: [ ]
59+
spawn: true
60+
spawn-limit: '@unset'
61+
tick-rate: '@unset'
62+
ambient:
63+
exceptions: [ ]
64+
spawn: true
65+
spawn-limit: '@unset'
66+
tick-rate: '@unset'
67+
axolotl:
68+
exceptions: [ ]
69+
spawn: true
70+
spawn-limit: '@unset'
71+
tick-rate: '@unset'
72+
misc:
73+
exceptions: [ ]
74+
spawn: true
75+
spawn-limit: '@unset'
76+
tick-rate: '@unset'
3777
world-blacklist: []
3878
version: 1.2
3979
world_nether:
@@ -71,6 +111,46 @@ world_nether:
71111
z: 48.0
72112
pitch: 0.0
73113
yaw: 0.0
74-
spawning: {}
114+
spawning:
115+
monster:
116+
exceptions: [ ]
117+
spawn: true
118+
spawn-limit: '@unset'
119+
tick-rate: '@unset'
120+
animal:
121+
exceptions: [ ]
122+
spawn: true
123+
spawn-limit: '@unset'
124+
tick-rate: '@unset'
125+
water_animal:
126+
exceptions: [ ]
127+
spawn: true
128+
spawn-limit: '@unset'
129+
tick-rate: '@unset'
130+
water_ambient:
131+
exceptions: [ ]
132+
spawn: true
133+
spawn-limit: '@unset'
134+
tick-rate: '@unset'
135+
water_underground_creature:
136+
exceptions: [ ]
137+
spawn: true
138+
spawn-limit: '@unset'
139+
tick-rate: '@unset'
140+
ambient:
141+
exceptions: [ ]
142+
spawn: true
143+
spawn-limit: '@unset'
144+
tick-rate: '@unset'
145+
axolotl:
146+
exceptions: [ ]
147+
spawn: true
148+
spawn-limit: '@unset'
149+
tick-rate: '@unset'
150+
misc:
151+
exceptions: [ ]
152+
spawn: true
153+
spawn-limit: '@unset'
154+
tick-rate: '@unset'
75155
world-blacklist: []
76156
version: 1.2

src/test/resources/worlds/delete_worlds.yml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,46 @@ world_nether:
3333
z: 48.0
3434
pitch: 0.0
3535
yaw: 0.0
36-
spawning: {}
36+
spawning:
37+
monster:
38+
exceptions: [ ]
39+
spawn: true
40+
spawn-limit: '@unset'
41+
tick-rate: '@unset'
42+
animal:
43+
exceptions: [ ]
44+
spawn: true
45+
spawn-limit: '@unset'
46+
tick-rate: '@unset'
47+
water_animal:
48+
exceptions: [ ]
49+
spawn: true
50+
spawn-limit: '@unset'
51+
tick-rate: '@unset'
52+
water_ambient:
53+
exceptions: [ ]
54+
spawn: true
55+
spawn-limit: '@unset'
56+
tick-rate: '@unset'
57+
water_underground_creature:
58+
exceptions: [ ]
59+
spawn: true
60+
spawn-limit: '@unset'
61+
tick-rate: '@unset'
62+
ambient:
63+
exceptions: [ ]
64+
spawn: true
65+
spawn-limit: '@unset'
66+
tick-rate: '@unset'
67+
axolotl:
68+
exceptions: [ ]
69+
spawn: true
70+
spawn-limit: '@unset'
71+
tick-rate: '@unset'
72+
misc:
73+
exceptions: [ ]
74+
spawn: true
75+
spawn-limit: '@unset'
76+
tick-rate: '@unset'
3777
world-blacklist: []
3878
version: 1.2

src/test/resources/worlds/edgecase_worlds.yml

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,46 @@ world:
3434
pitch: 0.0
3535
yaw: 0.0
3636
spawning:
37+
monster:
38+
exceptions: [ ]
39+
spawn: true
40+
spawn-limit: '@unset'
41+
tick-rate: '@unset'
3742
animal:
3843
spawn: falSe
3944
tick-rate: 123
40-
spawn-limit: -1
41-
exceptions: [cow]
45+
spawn-limit: '@unset'
46+
exceptions: [ cow ]
47+
water_animal:
48+
exceptions: [ ]
49+
spawn: true
50+
spawn-limit: '@unset'
51+
tick-rate: '@unset'
52+
water_ambient:
53+
exceptions: [ ]
54+
spawn: true
55+
spawn-limit: '@unset'
56+
tick-rate: '@unset'
57+
water_underground_creature:
58+
exceptions: [ ]
59+
spawn: true
60+
spawn-limit: '@unset'
61+
tick-rate: '@unset'
62+
ambient:
63+
exceptions: [ ]
64+
spawn: true
65+
spawn-limit: '@unset'
66+
tick-rate: '@unset'
67+
axolotl:
68+
exceptions: [ ]
69+
spawn: true
70+
spawn-limit: '@unset'
71+
tick-rate: '@unset'
72+
misc:
73+
exceptions: [ ]
74+
spawn: true
75+
spawn-limit: '@unset'
76+
tick-rate: '@unset'
4277
world-blacklist: # should be parsed as list of string
4378
- a
4479
- 1
@@ -78,6 +113,46 @@ world_nether:
78113
z: 48.0
79114
pitch: 0.0
80115
yaw: 0.0
81-
spawning: {}
116+
spawning:
117+
monster:
118+
exceptions: [ ]
119+
spawn: true
120+
spawn-limit: '@unset'
121+
tick-rate: '@unset'
122+
animal:
123+
exceptions: [ ]
124+
spawn: true
125+
spawn-limit: '@unset'
126+
tick-rate: '@unset'
127+
water_animal:
128+
exceptions: [ ]
129+
spawn: true
130+
spawn-limit: '@unset'
131+
tick-rate: '@unset'
132+
water_ambient:
133+
exceptions: [ ]
134+
spawn: true
135+
spawn-limit: '@unset'
136+
tick-rate: '@unset'
137+
water_underground_creature:
138+
exceptions: [ ]
139+
spawn: true
140+
spawn-limit: '@unset'
141+
tick-rate: '@unset'
142+
ambient:
143+
exceptions: [ ]
144+
spawn: true
145+
spawn-limit: '@unset'
146+
tick-rate: '@unset'
147+
axolotl:
148+
exceptions: [ ]
149+
spawn: true
150+
spawn-limit: '@unset'
151+
tick-rate: '@unset'
152+
misc:
153+
exceptions: [ ]
154+
spawn: true
155+
spawn-limit: '@unset'
156+
tick-rate: '@unset'
82157
world-blacklist: []
83158
version: 1.2

0 commit comments

Comments
 (0)