Allow changing client config in settings

Client config is no longer stored in TBM_Config.toml.
This commit is contained in:
Stephen Seo 2024-01-31 14:06:23 +09:00
parent aac4f9b6aa
commit c0fddb6955
5 changed files with 87 additions and 86 deletions

View file

@ -0,0 +1,66 @@
package com.burnedkirby.TurnBasedMinecraft.client;
import com.burnedkirby.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import net.neoforged.neoforge.common.ModConfigSpec;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ClientConfig {
public final ModConfigSpec.ConfigValue<List<? extends String>> battleMusicList;
public final ModConfigSpec.ConfigValue<List<? extends String>> sillyMusicList;
public final ModConfigSpec.IntValue sillyMusicThreshold;
public final ModConfigSpec.BooleanValue volumeAffectedByMusicVolume;
public final ModConfigSpec.DoubleValue musicVolume;
public static final ClientConfig CLIENT;
public static final ModConfigSpec CLIENT_SPEC;
ClientConfig(ModConfigSpec.Builder builder) {
builder.push("music");
List<String> battleMusicList = new ArrayList<String>(8);
battleMusicList.add("monster");
battleMusicList.add("animal");
battleMusicList.add("boss");
battleMusicList.add("player");
this.battleMusicList = builder
.comment("What categories of mobs that play \"battle\" music")
.translation(TurnBasedMinecraftMod.MODID + ".clientconfig.battle_music_list")
.defineList("battleMusicList", battleMusicList, (v) -> v instanceof String);
List<String> sillyMusicList = new ArrayList<String>(4);
sillyMusicList.add("passive");
this.sillyMusicList = builder
.comment("What categories of mobs that play \"silly\" music")
.translation(TurnBasedMinecraftMod.MODID + ".clientconfig.silly_music_list")
.defineList("sillyMusicList", sillyMusicList, (v) -> true);
this.sillyMusicThreshold = builder
.comment("Minimum percentage of silly entities in battle to use silly music")
.translation(TurnBasedMinecraftMod.MODID + ".clientconfig.silly_percentage")
.defineInRange("sillyMusicThreshold", 40, 0, 100);
this.volumeAffectedByMusicVolume = builder
.comment("If \"true\", music volume will be affected by global Music volume setting")
.translation(TurnBasedMinecraftMod.MODID + ".clientconfig.volume_affected_by_volume")
.define("volumeAffectedByMusicVolume", true);
this.musicVolume = builder
.comment("Volume of battle/silly music as a percentage between 0.0 and 1.0")
.translation(TurnBasedMinecraftMod.MODID + ".clientconfig.music_volume")
.defineInRange("musicVolume", 0.8, 0.0, 1.0);
builder.pop();
}
static {
Pair<ClientConfig, ModConfigSpec> pair = new ModConfigSpec.Builder().configure(ClientConfig::new);
CLIENT = pair.getKey();
CLIENT_SPEC = pair.getValue();
}
}

View file

@ -94,13 +94,21 @@ public class ClientProxy extends CommonProxy {
@Override
public void playBattleMusic() {
Options gs = Minecraft.getInstance().options;
battleMusic.playBattle(gs.getSoundSourceVolume(SoundSource.MUSIC) * gs.getSoundSourceVolume(SoundSource.MASTER));
if (ClientConfig.CLIENT.volumeAffectedByMusicVolume.get()) {
battleMusic.playBattle(gs.getSoundSourceVolume(SoundSource.MUSIC) * gs.getSoundSourceVolume(SoundSource.MASTER) * ClientConfig.CLIENT.musicVolume.get().floatValue());
} else {
battleMusic.playBattle(gs.getSoundSourceVolume(SoundSource.MASTER) * ClientConfig.CLIENT.musicVolume.get().floatValue());
}
}
@Override
public void playSillyMusic() {
Options gs = Minecraft.getInstance().options;
battleMusic.playSilly(gs.getSoundSourceVolume(SoundSource.MUSIC) * gs.getSoundSourceVolume(SoundSource.MASTER));
if (ClientConfig.CLIENT.volumeAffectedByMusicVolume.get()) {
battleMusic.playSilly(gs.getSoundSourceVolume(SoundSource.MUSIC) * gs.getSoundSourceVolume(SoundSource.MASTER) * ClientConfig.CLIENT.musicVolume.get().floatValue());
} else {
battleMusic.playSilly(gs.getSoundSourceVolume(SoundSource.MASTER) * ClientConfig.CLIENT.musicVolume.get().floatValue());
}
}
@Override
@ -116,9 +124,9 @@ public class ClientProxy extends CommonProxy {
if (localBattle == null) {
return;
}
if (type == null || type.isEmpty() || getConfig().isBattleMusicType(type)) {
if (type == null || type.isEmpty() || ClientConfig.CLIENT.battleMusicList.get().contains(type)) {
++battleMusicCount;
} else if (getConfig().isSillyMusicType(type)) {
} else if (ClientConfig.CLIENT.sillyMusicList.get().contains(type)) {
++sillyMusicCount;
} else {
++battleMusicCount;
@ -132,9 +140,9 @@ public class ClientProxy extends CommonProxy {
battleMusicCount = 0;
sillyMusicCount = 0;
return;
} else if (type == null || type.isEmpty() || getConfig().isBattleMusicType(type)) {
} else if (type == null || type.isEmpty() || ClientConfig.CLIENT.battleMusicList.get().contains(type)) {
--battleMusicCount;
} else if (getConfig().isSillyMusicType(type)) {
} else if (ClientConfig.CLIENT.sillyMusicList.get().contains(type)) {
--sillyMusicCount;
} else {
--battleMusicCount;
@ -185,7 +193,7 @@ public class ClientProxy extends CommonProxy {
percentage = 100.0f * (float) sillyMusicCount / (float) (sillyMusicCount + battleMusicCount);
}
if (percentage >= (float) getConfig().getSillyMusicThreshold()) {
if (percentage >= (float) ClientConfig.CLIENT.sillyMusicThreshold.get()) {
if (battleMusic.isPlaying()) {
if (!battleMusic.isPlayingSilly() && battleMusic.hasSillyMusic()) {
stopMusic(false);

View file

@ -37,10 +37,7 @@ public class Config
private int fleeBadProbability = 35;
private int minimumHitPercentage = 4;
private int maxInBattle = 8;
private Set<String> musicBattleTypes;
private Set<String> musicSillyTypes;
private boolean freezeCombatantsInBattle = false;
private int sillyMusicThreshold = 40;
private int configVersion = 0;
private Set<Integer> battleIgnoringPlayers = null;
private boolean onlyOPsSelfDisableTB = true;
@ -63,8 +60,6 @@ public class Config
customEntityInfoMap = new HashMap<String, EntityInfo>();
ignoreBattleTypes = new HashSet<String>();
this.logger = logger;
musicBattleTypes = new HashSet<String>();
musicSillyTypes = new HashSet<String>();
battleIgnoringPlayers = new HashSet<Integer>();
possibleIgnoreHurtDamageSources = new HashSet<String>();
ignoreHurtDamageSources = new HashSet<String>();
@ -156,56 +151,6 @@ public class Config
{
CommentedFileConfig conf = getConfigObj(configFile);
// client config
try {
Collection<String> battle_music_categories = conf.get("client_config.battle_music");
if (battle_music_categories != null) {
for (String category : battle_music_categories) {
musicBattleTypes.add(category);
}
} else {
musicBattleTypes.add("monster");
musicBattleTypes.add("animal");
musicBattleTypes.add("boss");
musicBattleTypes.add("player");
logNotFound("client_config.battle_music");
}
} catch (ClassCastException e) {
musicBattleTypes.add("monster");
musicBattleTypes.add("animal");
musicBattleTypes.add("boss");
musicBattleTypes.add("player");
logTOMLInvalidValue("client_config.battle_music");
}
try {
Collection<String> silly_music_categories = conf.get("client_config.silly_music");
if (silly_music_categories != null) {
for (String category : silly_music_categories) {
musicSillyTypes.add(category);
}
} else {
musicSillyTypes.add("passive");
logNotFound("client_config.silly_music");
}
} catch (ClassCastException e) {
musicSillyTypes.add("passive");
logTOMLInvalidValue("client_config.silly_music");
}
try {
OptionalInt silly_music_threshold = conf.getOptionalInt("client_config.silly_music_threshold");
if(silly_music_threshold.isPresent()) {
this.sillyMusicThreshold = silly_music_threshold.getAsInt();
} else {
this.sillyMusicThreshold = 40;
logNotFound("client_config.silly_music_threshold", "40");
}
} catch (ClassCastException e) {
this.sillyMusicThreshold = 40;
logTOMLInvalidValue("client_config.silly_music_threshold", "40");
}
// server_config
try {
OptionalInt leave_battle_cooldown = conf.getOptionalInt("server_config.leave_battle_cooldown");
@ -1328,16 +1273,6 @@ public class Config
this.maxInBattle = maxInBattle;
}
public boolean isBattleMusicType(String type)
{
return musicBattleTypes.contains(type.toLowerCase());
}
public boolean isSillyMusicType(String type)
{
return musicSillyTypes.contains(type.toLowerCase());
}
public boolean isFreezeCombatantsEnabled()
{
return freezeCombatantsInBattle;
@ -1347,11 +1282,6 @@ public class Config
freezeCombatantsInBattle = enabled;
}
public int getSillyMusicThreshold()
{
return sillyMusicThreshold;
}
public int getConfigVersion()
{
return configVersion;

View file

@ -1,6 +1,7 @@
package com.burnedkirby.TurnBasedMinecraft.common;
import ca.weblite.objc.Client;
import com.burnedkirby.TurnBasedMinecraft.client.ClientConfig;
import com.burnedkirby.TurnBasedMinecraft.client.ClientProxy;
import com.burnedkirby.TurnBasedMinecraft.common.networking.*;
import com.mojang.brigadier.LiteralMessage;
@ -21,6 +22,9 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.config.ModConfigEvent;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
@ -67,6 +71,7 @@ public class TurnBasedMinecraftMod {
eventBus.addListener(this::secondInitServer);
eventBus.addListener(this::registerNetwork);
NeoForge.EVENT_BUS.register(this);
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ClientConfig.CLIENT_SPEC);
}
private void registerNetwork(final RegisterPayloadHandlerEvent event) {

View file

@ -6,16 +6,8 @@ version = 10
# "false"!
do_not_overwrite = false
[client_config]
# What categories play this type of music (battle_music). Unknown categories will default to this type.
battle_music = ["monster", "animal", "boss", "player"]
# What categories play this type of music (silly_music).
silly_music = ["passive"]
# Minimum percentage of silly entities in battle to use silly music.
silly_music_threshold = 40
# Note that client config is no longer stored here but rather in the settings.
[server_config]