Skip to content

Commit

Permalink
fix: avoid creating invalid tuning ID, avoid removing built-in tunings
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlige committed Nov 28, 2024
1 parent eeef142 commit 3e9e22f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/main/java/com/github/zhenlige/xennote/Xennote.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.zhenlige.xennote.payload.BlockTuningPayload;
import com.google.common.collect.ImmutableSet;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
Expand Down Expand Up @@ -57,6 +58,9 @@ public class Xennote implements ModInitializer {
Registries.BLOCK_ENTITY_TYPE, Identifier.of(MOD_ID, "note_block_entity"),
new BlockEntityType<>(XenNoteBlockEntity::new, ImmutableSet.of(NOTE_BLOCK))
);
public static final DynamicCommandExceptionType INVALID_TUNING_ID = new DynamicCommandExceptionType(
o -> Text.translatable("argument.tuning.invalid_id", o)
);

@Override
public void onInitialize() {
Expand Down Expand Up @@ -159,11 +163,15 @@ private static void registerCommand() {
MinecraftServer server = context.getSource().getServer();
WorldTunings tunings = WorldTunings.getServerState(server);
String id = StringArgumentType.getString(context, "id");
Tuning tuning = TuningArgumentType.getTuning(context, "tuning");
tunings.tunings.put(id, tuning);
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList())
ServerPlayNetworking.send(player, new UpdateTuningPayload(id, Optional.of(tuning)));
return 1;
if (id.matches("[A-Za-z_]\\w*")) {
Tuning tuning = TuningArgumentType.getTuning(context, "tuning");
tunings.tunings.put(id, tuning);
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList())
ServerPlayNetworking.send(player, new UpdateTuningPayload(id, Optional.of(tuning)));
return 1;
} else {
throw INVALID_TUNING_ID.create(id);
}
})
)
)
Expand All @@ -174,6 +182,8 @@ private static void registerCommand() {
WorldTunings tunings = WorldTunings.getServerState(server);
String id = StringArgumentType.getString(context, "id");
tunings.tunings.remove(id);
if (WorldTunings.BUILT_IN_TUNINGS.containsKey(id))
tunings.tunings.put(id, WorldTunings.BUILT_IN_TUNINGS.get(id));
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList())
ServerPlayNetworking.send(player, new UpdateTuningPayload(id, Optional.empty()));
return 1;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/xennote/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"option.xennote.prime_map": "Prime mapping",
"option.xennote.prime": "Prime",
"option.xennote.map_to": "Mapped to interval",
"argument.tuning.invalid": "Invalid tuning argument: %s"
"argument.tuning.invalid": "Invalid tuning argument: %s",
"argument.tuning.invalid_id": "Invalid tuning ID: %s"
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/xennote/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"option.xennote.prime_map": "质数映射",
"option.xennote.prime": "质数",
"option.xennote.map_to": "映射到音程",
"argument.tuning.invalid": "无效调律参数:%s"
"argument.tuning.invalid": "无效调律参数:%s",
"argument.tuning.invalid_id": "无效调律ID:%s"
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/xennote/lang/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"option.xennote.prime_map": "質數映射",
"option.xennote.prime": "質數",
"option.xennote.map_to": "映射到音程",
"argument.tuning.invalid": "無效調律參數:%s"
"argument.tuning.invalid": "無效調律參數:%s",
"argument.tuning.invalid_id": "無效調律ID:%s"

}

0 comments on commit 3e9e22f

Please sign in to comment.