Skip to content

Commit

Permalink
Refactor messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugman76 committed Dec 7, 2024
1 parent e2b29d3 commit c280d33
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 185 deletions.
109 changes: 109 additions & 0 deletions src/main/java/com/hugman/text/Messenger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.hugman.text;

import com.hugman.uhc.game.ModuleManager;
import com.hugman.uhc.module.Module;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.Texts;
import net.minecraft.util.Formatting;
import xyz.nucleoid.plasmid.api.game.GameSpacePlayers;

/**
* Sends messages to players in a game.
*/
public class Messenger {
public static final String SYMBOL_SKULL = "☠";
public static final String SYMBOL_MODULE = "✨";
public static final String SYMBOL_SHIELD = "🛡";
public static final String SYMBOL_SWORD = "🗡";

private final GameSpacePlayers players;

public Messenger(GameSpacePlayers players) {
this.players = players;
}

public void sound(SoundEvent sound, float volume, float pitch) {
players.playSound(sound, SoundCategory.PLAYERS, volume, pitch);
}

public void info(String symbol, String s, Object... args) {
players.sendMessage(build(symbol, s, Formatting.YELLOW, args));
}

public void info(String s, Object... args) {
players.sendMessage(build(s, Formatting.YELLOW, args));
}

public void danger(String symbol, String s, Object... args) {
players.sendMessage(build(symbol, s, Formatting.RED, args));
}

public void danger(String s, Object... args) {
players.sendMessage(build(s, Formatting.RED, args));
}

public void elimination(ServerPlayerEntity player) {
players.sendMessage(buildElimination(player));
players.playSound(SoundEvents.ENTITY_WITHER_SPAWN);
}

public void death(DamageSource source, ServerPlayerEntity player) {
players.sendMessage(buildDeath(source, player));
players.playSound(SoundEvents.ENTITY_WITHER_SPAWN);
}

public void moduleAnnouncement(String message, RegistryEntry<Module> module, Formatting formatting) {
players.sendMessage(buildModuleAnnouncement(message, module, formatting));
}

public void moduleList(ModuleManager moduleManager) {
if (!moduleManager.isEmpty()) {
players.sendMessage(buildModuleList(moduleManager));
players.playSound(SoundEvents.ENTITY_ITEM_PICKUP);
}
}

private static Text build(String symbol, String s, Formatting f, Object... args) {
return Text.literal(symbol).append(" ").append(Text.translatable(s, args)).formatted(f);
}

private static Text build(String s, Formatting f, Object... args) {
return Text.translatable(s, args).formatted(f);
}

private static Text buildDeath(DamageSource source, ServerPlayerEntity player) {
return Text.literal("\n").append(SYMBOL_SKULL).append(" ").append(source.getDeathMessage(player).copy()).append("!\n").formatted(Formatting.DARK_RED);
}

private static Text buildElimination(ServerPlayerEntity player) {
return Text.literal("\n").append(SYMBOL_SKULL).append(" ").append(Text.translatable("text.uhc.player_eliminated", player.getDisplayName())).append("\n").formatted(Formatting.DARK_RED);
}

private static Text buildModuleAnnouncement(String message, RegistryEntry<Module> module, Formatting formatting) {
return Text.literal("\n\n").append(SYMBOL_MODULE).append(" ").append(Text.translatable(message, moduleSnippet(module.value())).formatted(formatting)).append("\n\n");
}

private static Text buildModuleList(ModuleManager manager) {
var text = Text.literal("\n").append(Text.translatable("text.uhc.enabled_modules").formatted(Formatting.GOLD));
manager.forEach(module -> text.append(Text.literal("\n - ").formatted(Formatting.WHITE)).append(moduleSnippet(module)));
text.append("\n");
return text;
}


private static Text moduleSnippet(Module module) {
var style = Style.EMPTY;
if (module.description().isPresent()) {
style = style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, module.description().get().copy()));
}
return Texts.bracketed(module.name()).setStyle(style.withColor(module.color()));
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/hugman/uhc/UHC.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hugman.uhc;

import com.google.common.reflect.Reflection;
import com.hugman.uhc.command.UHCCommand;
import com.hugman.uhc.command.ModulesCommand;
import com.hugman.uhc.config.UHCGameConfig;
import com.hugman.uhc.game.phase.UHCWaiting;
import com.hugman.uhc.modifier.ModifierType;
Expand All @@ -25,7 +25,7 @@ public void onInitialize() {

UHCRegistryKeys.registerDynamics();

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> UHCCommand.register(dispatcher));
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> ModulesCommand.register(dispatcher));
GameType.register(UHC.id("standard"), UHCGameConfig.CODEC, UHCWaiting::open);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.hugman.uhc.command;

import com.hugman.uhc.command.argument.UHCModuleArgument;
import com.hugman.uhc.config.UHCGameConfig;
import com.hugman.uhc.game.UHCAttachments;
import com.hugman.uhc.game.ModuleManager;
import com.hugman.uhc.module.Module;
import com.hugman.uhc.module.ModuleEvents;
import com.mojang.brigadier.Command;
Expand All @@ -21,51 +20,42 @@

import java.util.Objects;

public class UHCCommand {
private static final SimpleCommandExceptionType NO_MANAGER_ACTIVATED = new SimpleCommandExceptionType(Text.translatable("command.uhc.modules.no_manager")); //TODO: translate this
private static final SimpleCommandExceptionType NO_MODULES_ACTIVATED = new SimpleCommandExceptionType(Text.translatable("command.uhc.modules.no_modules_activated")); //TODO: translate this
private static final SimpleCommandExceptionType COULD_NOT_ENABLE_MODULE = new SimpleCommandExceptionType(Text.translatable("command.uhc.modules.enable.error")); //TODO: translate this
private static final SimpleCommandExceptionType COULD_NOT_DISABLE_MODULE = new SimpleCommandExceptionType(Text.translatable("command.uhc.modules.disable.error")); //TODO: translate this
public class ModulesCommand {
private static final SimpleCommandExceptionType NO_MANAGER_ACTIVATED = new SimpleCommandExceptionType(Text.translatable("command.modules.no_manager"));
private static final SimpleCommandExceptionType NO_MODULES_ACTIVATED = new SimpleCommandExceptionType(Text.translatable("command.modules.no_modules_activated"));
private static final SimpleCommandExceptionType ALREADY_ENABLED = new SimpleCommandExceptionType(Text.translatable("command.modules.already_enabled"));
private static final SimpleCommandExceptionType ALREADY_DISABLED = new SimpleCommandExceptionType(Text.translatable("command.modules.already_disabled"));

private static final String MODULE_ARG = "module";

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
CommandManager.literal("uhc")
.then(CommandManager.literal("create")
.requires(UHCCommand::isNotUHC)
.executes(UHCCommand::createUHC))
.then(CommandManager.literal("modules")
.requires(UHCCommand::isUHC)
.executes(UHCCommand::displayModules)
.then(CommandManager.literal("enable")
.then(UHCModuleArgument.argumentFromDisabled("module")
.executes(context -> enableModule(context, UHCModuleArgument.get(context, MODULE_ARG)))))
.then(CommandManager.literal("disable")
.then(UHCModuleArgument.argumentFromEnabled("module")
.executes(context -> disableModule(context, UHCModuleArgument.get(context, MODULE_ARG)))))
)
CommandManager.literal("modules")
.requires(ModulesCommand::supportsModules)
.executes(ModulesCommand::displayModules)
.then(CommandManager.literal("enable")
.then(UHCModuleArgument.argumentFromDisabled("module")
.executes(context -> enableModule(context, UHCModuleArgument.get(context, MODULE_ARG)))))
.then(CommandManager.literal("disable")
.then(UHCModuleArgument.argumentFromEnabled("module")
.executes(context -> disableModule(context, UHCModuleArgument.get(context, MODULE_ARG)))))
);
}

public static boolean isUHC(ServerCommandSource source) {
public static boolean supportsModules(ServerCommandSource source) {
GameSpace gameSpace = GameSpaceManager.get().byWorld(source.getWorld());
if (gameSpace == null) {
return false;
}
if (!(gameSpace.getMetadata().sourceConfig().value().config() instanceof UHCGameConfig)) {
if (!(gameSpace.getAttachment(ModuleManager.ATTACHMENT) instanceof ModuleManager)) {
return false;
}
return true;
}

public static boolean isNotUHC(ServerCommandSource source) {
return !isUHC(source);
}

private static int displayModules(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(source.getWorld())).getAttachment(UHCAttachments.MODULE_MANAGER);
var manager = Objects.requireNonNull(GameSpaceManager.get().byPlayer(source.getPlayer())).getAttachment(ModuleManager.ATTACHMENT);
if (manager == null) {
throw NO_MANAGER_ACTIVATED.create();
}
Expand All @@ -81,7 +71,7 @@ private static int displayModules(CommandContext<ServerCommandSource> context) t
private static int enableModule(CommandContext<ServerCommandSource> context, RegistryEntry<Module> module) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
var space = Objects.requireNonNull(GameSpaceManager.get().byWorld(source.getWorld()));
var manager = space.getAttachment(UHCAttachments.MODULE_MANAGER);
var manager = space.getAttachment(ModuleManager.ATTACHMENT);
if (manager == null) {
throw NO_MANAGER_ACTIVATED.create();
}
Expand All @@ -91,16 +81,16 @@ private static int enableModule(CommandContext<ServerCommandSource> context, Reg
(invokers.get(ModuleEvents.ENABLE)).onEnable(module);
}

source.sendFeedback(() -> Text.translatable("command.uhc.modules.enable.success", module.value().name()), true); //TODO: translate this
source.sendFeedback(() -> Text.translatable("command.modules.enable.success", module.value().name()), true);
return Command.SINGLE_SUCCESS;
} else {
throw COULD_NOT_ENABLE_MODULE.create();
throw ALREADY_ENABLED.create();
}
}

private static int disableModule(CommandContext<ServerCommandSource> context, RegistryEntry<Module> module) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(source.getWorld())).getAttachment(UHCAttachments.MODULE_MANAGER);
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(source.getWorld())).getAttachment(ModuleManager.ATTACHMENT);
if (manager == null) {
throw NO_MANAGER_ACTIVATED.create();
}
Expand All @@ -110,16 +100,10 @@ private static int disableModule(CommandContext<ServerCommandSource> context, Re
(invokers.get(ModuleEvents.DISABLE)).onDisable(module);
}

source.sendFeedback(() -> Text.translatable("command.uhc.modules.disable.success", module.value().name()), true); //TODO: translate this
source.sendFeedback(() -> Text.translatable("command.modules.disable.success", module.value().name()), true);
return Command.SINGLE_SUCCESS;
} else {
throw COULD_NOT_DISABLE_MODULE.create();
throw ALREADY_DISABLED.create();
}
}


private static int createUHC(CommandContext<ServerCommandSource> context) {
context.getSource().sendFeedback(() -> Text.of("Available soon... :)"), true);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hugman.uhc.command.argument;

import com.hugman.uhc.game.UHCAttachments;
import com.hugman.uhc.game.ModuleManager;
import com.hugman.uhc.module.Module;
import com.hugman.uhc.registry.UHCRegistryKeys;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
Expand All @@ -23,17 +23,17 @@
import java.util.Objects;

public final class UHCModuleArgument {
private static final DynamicCommandExceptionType MODULE_NOT_FOUND = new DynamicCommandExceptionType((id) -> Text.stringifiedTranslatable("text.plasmid.game_config.game_not_found", id)); //TODO: change
private static final DynamicCommandExceptionType MODULE_NOT_FOUND = new DynamicCommandExceptionType((id) -> Text.stringifiedTranslatable("text.module.not_found", id)); //TODO: change

public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argumentFromEnabled(String name) {
return CommandManager.argument(name, IdentifierArgumentType.identifier()).suggests((ctx, builder) -> {
Registry<Module> registry = ctx.getSource().getRegistryManager().getOrThrow(UHCRegistryKeys.MODULE);
String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(ctx.getSource().getWorld())).getAttachment(UHCAttachments.MODULE_MANAGER);
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(ctx.getSource().getWorld())).getAttachment(ModuleManager.ATTACHMENT);
if (manager == null) {
return builder.buildFuture();
}
var enabledKeys = manager.getKeys();
var enabledKeys = manager.keys();
CommandSource.forEachMatching(enabledKeys, remaining, RegistryKey::getValue, (key) -> registry.getOptional(key)
.ifPresent((entry) -> builder.suggest(key.getValue().toString(), entry.value().name())));
return builder.buildFuture();
Expand All @@ -44,10 +44,10 @@ public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argumentF
return CommandManager.argument(name, IdentifierArgumentType.identifier()).suggests((ctx, builder) -> {
Registry<Module> registry = ctx.getSource().getRegistryManager().getOrThrow(UHCRegistryKeys.MODULE);
String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(ctx.getSource().getWorld())).getAttachment(UHCAttachments.MODULE_MANAGER);
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(ctx.getSource().getWorld())).getAttachment(ModuleManager.ATTACHMENT);
var candidates = new ArrayList<>(registry.getKeys());
if (manager != null) {
candidates.removeAll(manager.getKeys());
candidates.removeAll(manager.keys());
}
CommandSource.forEachMatching(candidates, remaining, RegistryKey::getValue, (key) -> registry.getOptional(key)
.ifPresent((entry) -> builder.suggest(key.getValue().toString(), entry.value().name())));
Expand Down
Loading

0 comments on commit c280d33

Please sign in to comment.