Skip to content

Commit d3808e8

Browse files
authored
Add server config for physics steps (#1244)
This adds a server config that physics steps are written to. The runtime physics config is kept, but updated from the config value instead of not being saved anywhere. I used a server config so the data is saved per world instead of per instance. Closes #1234
1 parent 7cfccf4 commit d3808e8

7 files changed

Lines changed: 40 additions & 4 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/SableConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public final class SableConfig {
1818
public static final ModConfigSpec.IntValue SUB_LEVEL_PUNCH_COOLDOWN_TICKS;
1919
public static final ModConfigSpec.BooleanValue DISABLE_UDP_PIPELINE;
2020
public static final ModConfigSpec.BooleanValue ATTEMPT_UDP_NETWORKING;
21-
public static final ModConfigSpec.BooleanValue VERBOSE_SERIALIZATION_LOGGING;
2221
public static final ModConfigSpec.BooleanValue SUB_LEVEL_SAVING_LOG_MESSAGE;
22+
public static final ModConfigSpec.BooleanValue VERBOSE_SERIALIZATION_LOGGING;
2323

2424
static {
2525
final ModConfigSpec.Builder builder = new ModConfigSpec.Builder();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.ryanhcode.sable;
2+
3+
import net.neoforged.neoforge.common.ModConfigSpec;
4+
5+
public final class SableServerConfig {
6+
7+
public static final ModConfigSpec SPEC;
8+
9+
public static final ModConfigSpec.IntValue SUB_LEVEL_SUBSTEPS_PER_TICK;
10+
11+
static {
12+
final ModConfigSpec.Builder builder = new ModConfigSpec.Builder();
13+
14+
SUB_LEVEL_SUBSTEPS_PER_TICK = builder
15+
.comment("How many times the physics simulation is stepped in every second. Higher values will be significantly more performance intensive, but will have higher accuracy.")
16+
.defineInRange("sub_level_substeps_per_tick", 2, 1, 10);
17+
18+
SPEC = builder.build();
19+
}
20+
}

common/src/main/java/dev/ryanhcode/sable/config/SubLevelSettingsScreen.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ryanhcode.sable.config;
22

3+
import dev.ryanhcode.sable.SableServerConfig;
34
import dev.ryanhcode.sable.api.sublevel.SubLevelContainer;
45
import dev.ryanhcode.sable.physics.config.PhysicsConfigData;
56
import dev.ryanhcode.sable.sublevel.system.SubLevelPhysicsSystem;
@@ -10,6 +11,7 @@
1011
import net.minecraft.client.server.IntegratedServer;
1112
import net.minecraft.network.chat.Component;
1213
import net.minecraft.server.level.ServerLevel;
14+
import net.neoforged.neoforge.common.ModConfigSpec;
1315

1416
public class SubLevelSettingsScreen extends OptionsSubScreen {
1517
public static final Component TITLE = Component.translatable("options.sable_menu");
@@ -22,18 +24,20 @@ public SubLevelSettingsScreen(final Screen optionsScreen, final Options options,
2224
protected void addOptions() {
2325
final IntegratedServer singleplayerServer = this.minecraft.getSingleplayerServer();
2426

25-
27+
final ModConfigSpec.Range<?> range = SableServerConfig.SUB_LEVEL_SUBSTEPS_PER_TICK.getSpec().getRange();
2628
this.list.addBig(new OptionInstance<>(
2729
"options.physics_steps",
2830
OptionInstance.cachedConstantTooltip(Component.translatable("options.physics_steps.tooltip")),
2931
(component, substeps) -> Options.genericValueLabel(component, Component.translatable("options.physics_steps_template", substeps * 20)),
30-
new OptionInstance.IntRange(1, 10, false),
32+
new OptionInstance.IntRange((Integer) range.getMin(), (Integer) range.getMax(), false),
3133
SubLevelContainer.getContainer(singleplayerServer.overworld()).physicsSystem().getConfig().substepsPerTick,
3234
steps -> {
35+
SableServerConfig.SUB_LEVEL_SUBSTEPS_PER_TICK.set(steps);
36+
SableServerConfig.SPEC.save();
3337
for (final ServerLevel level : singleplayerServer.getAllLevels()) {
3438
final SubLevelPhysicsSystem physicsSystem = SubLevelContainer.getContainer(level).physicsSystem();
3539
final PhysicsConfigData config = physicsSystem.getConfig();
36-
config.substepsPerTick = steps;
40+
config.updateFromConfig();
3741
physicsSystem.getPipeline().updateConfigFrom(config);
3842
}
3943
}

common/src/main/java/dev/ryanhcode/sable/physics/config/PhysicsConfigData.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev.ryanhcode.sable.physics.config;
22

3+
import dev.ryanhcode.sable.SableConfig;
4+
import dev.ryanhcode.sable.SableServerConfig;
5+
36
public class PhysicsConfigData {
47
/**
58
* The number of solver iterations run by the constraints solver for calculating forces.
@@ -28,4 +31,8 @@ public class PhysicsConfigData {
2831
* Physics ticks done per game tick in the physics pipeline.
2932
*/
3033
public int substepsPerTick = 2;
34+
35+
public void updateFromConfig() {
36+
this.substepsPerTick = SableServerConfig.SUB_LEVEL_SUBSTEPS_PER_TICK.getAsInt();
37+
}
3138
}

common/src/main/java/dev/ryanhcode/sable/sublevel/system/SubLevelPhysicsSystem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public void initialize() {
168168
final Vector3d gravity = new Vector3d(DimensionPhysicsData.getGravity(this.level));
169169
final double universalDrag = DimensionPhysicsData.getUniversalDrag(this.level);
170170

171+
this.config.updateFromConfig();
171172
this.pipeline.init(gravity, universalDrag);
172173
this.pipeline.updateConfigFrom(this.config);
173174
}

fabric/src/main/java/dev/ryanhcode/sable/fabric/SableFabric.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.ryanhcode.sable.Sable;
44
import dev.ryanhcode.sable.SableCommonEvents;
55
import dev.ryanhcode.sable.SableConfig;
6+
import dev.ryanhcode.sable.SableServerConfig;
67
import dev.ryanhcode.sable.command.SableCommand;
78
import dev.ryanhcode.sable.command.argument.SubLevelSelectorModifiers;
89
import dev.ryanhcode.sable.index.SableAttributes;
@@ -43,5 +44,6 @@ public void onInitialize() {
4344
ServerLifecycleEvents.SYNC_DATA_PACK_CONTENTS.register((player, joined) -> SableCommonEvents.syncDataPacket(packet -> player.connection.send(packet)));
4445

4546
NeoForgeConfigRegistry.INSTANCE.register(Sable.MOD_ID, ModConfig.Type.COMMON, SableConfig.SPEC);
47+
NeoForgeConfigRegistry.INSTANCE.register(Sable.MOD_ID, ModConfig.Type.SERVER, SableServerConfig.SPEC);
4648
}
4749
}

neoforge/src/main/java/dev/ryanhcode/sable/neoforge/SableNeoForge.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.ryanhcode.sable.Sable;
44
import dev.ryanhcode.sable.SableCommonEvents;
55
import dev.ryanhcode.sable.SableConfig;
6+
import dev.ryanhcode.sable.SableServerConfig;
67
import dev.ryanhcode.sable.command.SableCommand;
78
import dev.ryanhcode.sable.command.argument.SubLevelSelectorModifiers;
89
import dev.ryanhcode.sable.index.SableAttributes;
@@ -42,6 +43,7 @@ public SableNeoForge(final ModContainer modContainer, final IEventBus modBus) {
4243
attributes.register(modBus);
4344

4445
modContainer.registerConfig(ModConfig.Type.COMMON, SableConfig.SPEC);
46+
modContainer.registerConfig(ModConfig.Type.SERVER, SableServerConfig.SPEC);
4547

4648
CrashReportCallables.registerHeader(Sable::getCrashHeader);
4749
}

0 commit comments

Comments
 (0)