diff --git a/src/main/java/com/hugman/text/Messenger.java b/src/main/java/com/hugman/text/Messenger.java new file mode 100644 index 0000000..bb15b32 --- /dev/null +++ b/src/main/java/com/hugman/text/Messenger.java @@ -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, 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, 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())); + } +} diff --git a/src/main/java/com/hugman/uhc/UHC.java b/src/main/java/com/hugman/uhc/UHC.java index f6f9206..28daa6a 100644 --- a/src/main/java/com/hugman/uhc/UHC.java +++ b/src/main/java/com/hugman/uhc/UHC.java @@ -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; @@ -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); } diff --git a/src/main/java/com/hugman/uhc/command/UHCCommand.java b/src/main/java/com/hugman/uhc/command/ModulesCommand.java similarity index 57% rename from src/main/java/com/hugman/uhc/command/UHCCommand.java rename to src/main/java/com/hugman/uhc/command/ModulesCommand.java index 0fab2f7..31320b2 100644 --- a/src/main/java/com/hugman/uhc/command/UHCCommand.java +++ b/src/main/java/com/hugman/uhc/command/ModulesCommand.java @@ -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; @@ -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 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 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(); } @@ -81,7 +71,7 @@ private static int displayModules(CommandContext context) t private static int enableModule(CommandContext context, RegistryEntry 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(); } @@ -91,16 +81,16 @@ private static int enableModule(CommandContext 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 context, RegistryEntry 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(); } @@ -110,16 +100,10 @@ private static int disableModule(CommandContext 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 context) { - context.getSource().sendFeedback(() -> Text.of("Available soon... :)"), true); - return Command.SINGLE_SUCCESS; - } } diff --git a/src/main/java/com/hugman/uhc/command/argument/UHCModuleArgument.java b/src/main/java/com/hugman/uhc/command/argument/UHCModuleArgument.java index b9a107d..b041e06 100644 --- a/src/main/java/com/hugman/uhc/command/argument/UHCModuleArgument.java +++ b/src/main/java/com/hugman/uhc/command/argument/UHCModuleArgument.java @@ -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; @@ -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 argumentFromEnabled(String name) { return CommandManager.argument(name, IdentifierArgumentType.identifier()).suggests((ctx, builder) -> { Registry 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(); @@ -44,10 +44,10 @@ public static RequiredArgumentBuilder argumentF return CommandManager.argument(name, IdentifierArgumentType.identifier()).suggests((ctx, builder) -> { Registry 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()))); diff --git a/src/main/java/com/hugman/uhc/game/ModuleManager.java b/src/main/java/com/hugman/uhc/game/ModuleManager.java index 6a05ced..4d94162 100644 --- a/src/main/java/com/hugman/uhc/game/ModuleManager.java +++ b/src/main/java/com/hugman/uhc/game/ModuleManager.java @@ -1,8 +1,8 @@ package com.hugman.uhc.game; +import com.hugman.uhc.UHC; import com.hugman.uhc.modifier.*; import com.hugman.uhc.module.Module; -import com.hugman.uhc.module.ModuleEvents; import eu.pb4.sgui.api.elements.GuiElementBuilder; import eu.pb4.sgui.api.gui.SimpleGui; import net.minecraft.block.BlockState; @@ -24,9 +24,7 @@ import net.minecraft.world.explosion.Explosion; import org.jetbrains.annotations.Nullable; import xyz.nucleoid.plasmid.api.game.GameActivity; -import xyz.nucleoid.plasmid.api.game.event.GamePlayerEvents; -import xyz.nucleoid.stimuli.EventInvokers; -import xyz.nucleoid.stimuli.Stimuli; +import xyz.nucleoid.plasmid.api.game.GameAttachment; import xyz.nucleoid.stimuli.event.DroppedItemsResult; import xyz.nucleoid.stimuli.event.EventResult; import xyz.nucleoid.stimuli.event.block.BlockBreakEvent; @@ -37,9 +35,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.Consumer; import java.util.stream.Stream; public final class ModuleManager { + public static final GameAttachment ATTACHMENT = GameAttachment.create(UHC.id("module_manager")); + private final List> modules; public ModuleManager(List> modules) { @@ -54,7 +55,7 @@ public boolean isEmpty() { return modules.isEmpty(); } - public List getModifiers() { + public List modifiers() { List modifiers = new ArrayList<>(); for (var moduleEntry : modules) { modifiers.addAll(moduleEntry.value().modifiers()); @@ -62,13 +63,17 @@ public List getModifiers() { return modifiers; } - public List> getKeys() { + public List> keys() { return modules.stream().map(moduleRegistryEntry -> moduleRegistryEntry.getKey().orElse(null)).filter(Objects::nonNull).toList(); } - public List getModifiers(ModifierType type) { + public void forEach(Consumer action) { + modules.forEach(moduleEntry -> action.accept(moduleEntry.value())); + } + + public List modifiers(ModifierType type) { //TODO: cache modules so it's quicker to sort by type - return ModuleManager.getModifiers(modules, type); + return ModuleManager.modifiers(modules, type); } public boolean enableModule(RegistryEntry module) { @@ -76,7 +81,6 @@ public boolean enableModule(RegistryEntry module) { return false; } - //TODO: trigger the modifier (they may have something to do when enabled) //TODO: send feedback to all players in game (chat + title) return modules.add(module); @@ -87,7 +91,6 @@ public boolean disableModule(RegistryEntry module) { return false; } - //TODO: trigger the modifier (they may have something to do when disabled) //TODO: send feedback to all players in game (chat + title) return modules.remove(module); } @@ -120,26 +123,12 @@ public SimpleGui buildGui(ServerPlayerEntity player) { return gui; } - public MutableText buildChatMessage() { - var text = Text.literal("\n").append(Text.translatable("text.uhc.enabled_modules").formatted(Formatting.GOLD)); - this.modules.forEach(moduleEntry -> { - var module = moduleEntry.value(); - var style = Style.EMPTY; - if (module.description().isPresent()) { - style = style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, module.description().get().copy())); - } - text.append(Text.literal("\n - ").formatted(Formatting.WHITE)).append(Texts.bracketed(module.name()).setStyle(style.withColor(module.color()))); - }); - text.append("\n"); - return text; - } - /** * Filters a registry entry list of modules by type * * @return A list of modifiers of the specified type */ - public static List getModifiers(List> modules, ModifierType type) { + public static List modifiers(List> modules, ModifierType type) { List modifiers = new ArrayList<>(); for (var moduleEntry : modules) { for (Modifier modifier : moduleEntry.value().modifiers()) { @@ -169,7 +158,7 @@ public void setupListeners(GameActivity activity) { } private EventResult onBlockBroken(ServerPlayerEntity playerEntity, ServerWorld world, BlockPos pos) { - for (TraversalBreakModifier piece : this.getModifiers(ModifierType.TRAVERSAL_BREAK)) { + for (TraversalBreakModifier piece : this.modifiers(ModifierType.TRAVERSAL_BREAK)) { piece.breakBlock(world, playerEntity, pos); } return EventResult.ALLOW; @@ -177,7 +166,7 @@ private EventResult onBlockBroken(ServerPlayerEntity playerEntity, ServerWorld w private EventResult onExplosion(Explosion explosion, List positions) { positions.forEach(pos -> { - for (TraversalBreakModifier piece : this.getModifiers(ModifierType.TRAVERSAL_BREAK)) { + for (TraversalBreakModifier piece : this.modifiers(ModifierType.TRAVERSAL_BREAK)) { piece.breakBlock(explosion.getWorld(), explosion.getCausingEntity(), pos); } }); @@ -188,7 +177,7 @@ private EventResult onExplosion(Explosion explosion, List positions) { private DroppedItemsResult onMobLoot(LivingEntity livingEntity, List itemStacks) { boolean keepOld = true; List stacks = new ArrayList<>(); - for (EntityLootModifier piece : this.getModifiers(ModifierType.ENTITY_LOOT)) { + for (EntityLootModifier piece : this.modifiers(ModifierType.ENTITY_LOOT)) { if (piece.test(livingEntity)) { stacks.addAll(piece.getLoots((ServerWorld) livingEntity.getWorld(), livingEntity)); if (piece.shouldReplace()) keepOld = false; @@ -201,7 +190,7 @@ private DroppedItemsResult onMobLoot(LivingEntity livingEntity, List private DroppedItemsResult onBlockDrop(@Nullable Entity entity, ServerWorld world, BlockPos pos, BlockState state, List itemStacks) { boolean keepOld = true; List stacks = new ArrayList<>(); - for (BlockLootModifier piece : this.getModifiers(ModifierType.BLOCK_LOOT)) { + for (BlockLootModifier piece : this.modifiers(ModifierType.BLOCK_LOOT)) { if (piece.test(state, world.getRandom())) { piece.spawnExperience(world, pos); stacks.addAll(piece.getLoots(world, pos, entity, entity instanceof LivingEntity ? ((LivingEntity) entity).getActiveItem() : ItemStack.EMPTY)); diff --git a/src/main/java/com/hugman/uhc/game/UHCAttachments.java b/src/main/java/com/hugman/uhc/game/UHCAttachments.java deleted file mode 100644 index b2dac6a..0000000 --- a/src/main/java/com/hugman/uhc/game/UHCAttachments.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.hugman.uhc.game; - -import com.hugman.uhc.UHC; -import xyz.nucleoid.plasmid.api.game.GameAttachment; - -public class UHCAttachments { - public static final GameAttachment MODULE_MANAGER = GameAttachment.create(UHC.id("module_manager")); -} diff --git a/src/main/java/com/hugman/uhc/game/UHCBar.java b/src/main/java/com/hugman/uhc/game/UHCBar.java index 895b9f6..ffd5d01 100644 --- a/src/main/java/com/hugman/uhc/game/UHCBar.java +++ b/src/main/java/com/hugman/uhc/game/UHCBar.java @@ -1,5 +1,6 @@ package com.hugman.uhc.game; +import com.hugman.text.Messenger; import com.hugman.uhc.util.TickUtil; import net.minecraft.entity.boss.BossBar; import net.minecraft.server.world.ServerWorld; @@ -12,7 +13,7 @@ public class UHCBar { private final BossBarWidget widget; - private final GameSpace gameSpace; + private final Messenger messenger; private String symbol; private String name; private String message; @@ -21,17 +22,17 @@ public class UHCBar { private long totalTicks = 0; private boolean canTick = false; - private UHCBar(BossBarWidget widget, GameSpace gameSpace) { + private UHCBar(BossBarWidget widget, Messenger messenger) { this.widget = widget; - this.gameSpace = gameSpace; + this.messenger = messenger; } - public static UHCBar create(GlobalWidgets widgets, GameSpace gameSpace) { - return new UHCBar(widgets.addBossBar(gameSpace.getMetadata().sourceConfig().value().name(), BossBar.Color.BLUE, BossBar.Style.PROGRESS), gameSpace); + public static UHCBar create(GlobalWidgets widgets, GameSpace gameSpace, Messenger messenger) { + return new UHCBar(widgets.addBossBar(gameSpace.getMetadata().sourceConfig().value().name(), BossBar.Color.BLUE, BossBar.Style.PROGRESS), messenger); } public void set(String symbol, String name, long totalTicks, long endTick, BossBar.Color color) { - this.symbol = symbol.equals("") ? "" : symbol + " "; + this.symbol = symbol; this.name = name + ".countdown_bar"; this.message = name + ".countdown_text"; this.totalTicks = totalTicks; @@ -41,11 +42,7 @@ public void set(String symbol, String name, long totalTicks, long endTick, BossB } public void set(String name, long totalTicks, long endTick, BossBar.Color color) { - this.set("", name, totalTicks, endTick, color); - } - - public void setMessage(String message) { - this.message = message; + this.set(null, name, totalTicks, endTick, color); } public void setFull(Text title) { @@ -71,7 +68,7 @@ public void tick(ServerWorld world) { sendMessage(seconds); newColor = BossBar.Color.RED; } - this.widget.setTitle(Text.literal(symbol).append(Text.translatable(name, TickUtil.format(ticks)))); + this.widget.setTitle(symbol == null ? Text.translatable(name, TickUtil.format(ticks)) : Text.literal(symbol).append(" ").append(Text.translatable(name, TickUtil.format(ticks)))); this.widget.setStyle(newColor, BossBar.Style.NOTCHED_10); this.widget.setProgress((float) seconds / totalSeconds); } @@ -79,11 +76,13 @@ public void tick(ServerWorld world) { private void sendMessage(long seconds) { float pitch = seconds == 0 ? 1.5F : 1.0F; - gameSpace.getPlayers().forEach(entity -> { - if (this.message != null && seconds != 0) { - entity.sendMessage(Text.literal(symbol).append(Text.translatable(this.message, TickUtil.formatPretty(seconds * 20).formatted(Formatting.RED))).formatted(Formatting.GOLD), false); + if (this.message != null && seconds != 0) { + if (symbol != null) { + messenger.info(symbol, message, TickUtil.formatPretty(seconds * 20).formatted(Formatting.RED)); + } else { + messenger.info(message, TickUtil.formatPretty(seconds * 20).formatted(Formatting.RED)); } - entity.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, pitch); - }); + } + messenger.sound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, pitch); } } diff --git a/src/main/java/com/hugman/uhc/game/UHCPlayerManager.java b/src/main/java/com/hugman/uhc/game/UHCPlayerManager.java index 1592a25..e45281c 100644 --- a/src/main/java/com/hugman/uhc/game/UHCPlayerManager.java +++ b/src/main/java/com/hugman/uhc/game/UHCPlayerManager.java @@ -100,10 +100,6 @@ public void forEachAliveParticipant(final Consumer consumer) }); } - public void forEachParticipant(final Consumer consumer) { - participants.keySet().forEach(consumer); - } - // TEAMS public PlayerSet teamPlayers(GameTeamKey team) { diff --git a/src/main/java/com/hugman/uhc/game/phase/UHCActive.java b/src/main/java/com/hugman/uhc/game/phase/UHCActive.java index fcbc69d..3516c06 100644 --- a/src/main/java/com/hugman/uhc/game/phase/UHCActive.java +++ b/src/main/java/com/hugman/uhc/game/phase/UHCActive.java @@ -1,5 +1,6 @@ package com.hugman.uhc.game.phase; +import com.hugman.text.Messenger; import com.hugman.uhc.UHC; import com.hugman.uhc.config.UHCGameConfig; import com.hugman.uhc.game.*; @@ -56,6 +57,7 @@ public class UHCActive { private final UHCBar bar; private final UHCSideBar sideBar; private final ModuleManager moduleManager; + private final Messenger msg; private long gameStartTick; private long startInvulnerableTick; @@ -71,42 +73,60 @@ public class UHCActive { private boolean isFinished = false; private UHCActive( - GameActivity activity, GameSpace gameSpace, ServerWorld world, - UHCGameConfig config, - GlobalWidgets widgets, + GameActivity activity, + int spawnOffset, UHCPlayerManager playerManager, - ModuleManager moduleManager + UHCTimers timers, + UHCSpawner spawnLogic, + UHCBar bar, + UHCSideBar sideBar, + ModuleManager moduleManager, + Messenger msg ) { - this.activity = activity; this.gameSpace = gameSpace; this.world = world; + this.activity = activity; + this.spawnOffset = spawnOffset; this.playerManager = playerManager; + this.timers = timers; + this.spawnLogic = spawnLogic; + this.bar = bar; + this.sideBar = sideBar; this.moduleManager = moduleManager; + this.msg = msg; + } - this.spawnOffset = config.uhcConfig().value().mapConfig().spawnOffset(); - this.timers = new UHCTimers(config, this.playerManager.participantCount()); - this.spawnLogic = new UHCSpawner(this.world); - this.bar = UHCBar.create(widgets, this.gameSpace); - this.sideBar = UHCSideBar.create(widgets, gameSpace); + private static UHCActive of( + GameActivity activity, + GameSpace gameSpace, + ServerWorld world, + UHCGameConfig config + ) { + var moduleManager = gameSpace.getAttachment(ModuleManager.ATTACHMENT); + assert moduleManager != null; + var playerManager = UHCPlayerManager.of(activity, gameSpace, config); + var widgets = GlobalWidgets.addTo(activity); + var messenger = new Messenger(gameSpace.getPlayers()); + return new UHCActive( + gameSpace, + world, + activity, + config.uhcConfig().value().mapConfig().spawnOffset(), + playerManager, + new UHCTimers(config, playerManager.participantCount()), + new UHCSpawner(world), + UHCBar.create(widgets, gameSpace, messenger), + UHCSideBar.create(widgets, gameSpace), + moduleManager, + messenger + ); } public static void start(GameSpace gameSpace, ServerWorld world, UHCGameConfig config) { gameSpace.setActivity(activity -> { - GlobalWidgets widgets = GlobalWidgets.addTo(activity); - var moduleManager = gameSpace.getAttachment(UHCAttachments.MODULE_MANAGER); - assert moduleManager != null; - var playerManager = UHCPlayerManager.of(activity, gameSpace, config); - UHCActive active = new UHCActive( - activity, - gameSpace, - world, - config, - widgets, - playerManager, - moduleManager - ); + UHCActive active = UHCActive.of(activity, gameSpace, world, config); activity.allow(GameRuleType.CRAFTING); activity.deny(GameRuleType.PORTALS); @@ -128,7 +148,7 @@ public static void start(GameSpace gameSpace, ServerWorld world, UHCGameConfig c activity.listen(PlayerDamageEvent.EVENT, active::onPlayerDamage); activity.listen(PlayerDeathEvent.EVENT, active::onPlayerDeath); - moduleManager.setupListeners(activity); + active.moduleManager.setupListeners(activity); }); } @@ -179,21 +199,21 @@ private void tick() { // Start - Cage chapter (@ 80%) if (worldTime == this.startInvulnerableTick - (timers.getInCagesTime() * 0.8)) { - this.sendModuleListToChat(); + msg.moduleList(this.moduleManager); } // Start - Invulnerable chapter else if (worldTime == this.startInvulnerableTick) { this.dropCages(); - this.sendInfo("text.uhc.dropped_players"); - this.sendInfo("text.uhc.world_will_shrink", TickUtil.formatPretty(this.finaleCagesTick - worldTime)); + msg.info("text.uhc.dropped_players"); + msg.info("text.uhc.world_will_shrink", TickUtil.formatPretty(this.finaleCagesTick - worldTime)); - this.bar.set("šŸ›”", "text.uhc.vulnerable", this.timers.getInvulnerabilityTime(), this.startWarmupTick, BossBar.Color.YELLOW); + this.bar.set(Messenger.SYMBOL_SHIELD, "text.uhc.vulnerable", this.timers.getInvulnerabilityTime(), this.startWarmupTick, BossBar.Color.YELLOW); } // Start - Warmup chapter else if (worldTime == this.startWarmupTick) { this.setInvulnerable(false); - this.sendWarning("šŸ›”", "text.uhc.no_longer_immune"); + msg.danger(Messenger.SYMBOL_SHIELD, "text.uhc.no_longer_immune"); this.bar.set("text.uhc.tp", this.timers.getWarmupTime(), this.finaleCagesTick, BossBar.Color.BLUE); } @@ -206,7 +226,7 @@ else if (worldTime == this.finaleCagesTick) { player.changeGameMode(GameMode.ADVENTURE); }); this.tpToCages(); - this.sendInfo("text.uhc.shrinking_when_pvp"); + msg.info("text.uhc.shrinking_when_pvp"); this.bar.set("text.uhc.dropping", this.timers.getInCagesTime(), this.finaleInvulnerabilityTick, BossBar.Color.PURPLE); } @@ -214,22 +234,22 @@ else if (worldTime == this.finaleCagesTick) { // Finale - Invulnerability chapter else if (worldTime == this.finaleInvulnerabilityTick) { this.dropCages(); - this.sendInfo("text.uhc.dropped_players"); + msg.info("text.uhc.dropped_players"); - this.bar.set("šŸ—”", "text.uhc.pvp", this.timers.getInvulnerabilityTime(), this.reducingTick, BossBar.Color.YELLOW); + this.bar.set(Messenger.SYMBOL_SWORD, "text.uhc.pvp", this.timers.getInvulnerabilityTime(), this.reducingTick, BossBar.Color.YELLOW); } // Finale - Reducing chapter else if (worldTime == this.reducingTick) { this.setInvulnerable(false); - this.sendWarning("šŸ›”", "text.uhc.no_longer_immune"); + msg.danger(Messenger.SYMBOL_SHIELD, "text.uhc.no_longer_immune"); this.setPvp(true); - this.sendWarning("šŸ—”", "text.uhc.pvp_enabled"); + msg.danger(Messenger.SYMBOL_SKULL, "text.uhc.pvp_enabled"); world.getWorldBorder().interpolateSize(this.timers.getStartMapSize(), this.timers.getEndMapSize(), this.timers.getShrinkingTime() * 50L); this.gameSpace.getPlayers().forEach(player -> player.networkHandler.sendPacket(new WorldBorderInterpolateSizeS2CPacket(world.getWorldBorder()))); - this.sendWarning("text.uhc.shrinking_start"); + msg.danger("text.uhc.shrinking_start"); this.bar.set("text.uhc.shrinking_finish", this.timers.getShrinkingTime(), this.deathMatchTick, BossBar.Color.RED); } @@ -239,7 +259,7 @@ else if (worldTime == this.deathMatchTick) { this.bar.setFull(Text.literal("šŸ—”").append(Text.translatable("text.uhc.deathmatchTime")).append("šŸ—”")); world.getWorldBorder().setDamagePerBlock(2.5); world.getWorldBorder().setSafeZone(0.125); - this.sendInfo("šŸ—”", "text.uhc.last_one_wins"); + msg.info(Messenger.SYMBOL_SWORD, "text.uhc.last_one_wins"); this.checkForWinner(); } } @@ -257,9 +277,7 @@ private JoinAcceptorResult acceptPlayer(JoinAcceptor joinAcceptor) { private void playerLeave(ServerPlayerEntity player) { if (playerManager.isParticipant(player)) { if (!playerManager.getParticipant(player).isEliminated()) { - var players = this.gameSpace.getPlayers(); - players.sendMessage(Text.literal("\nā˜  ").append(Text.translatable("text.uhc.player_eliminated", player.getDisplayName())).append("\n").formatted(Formatting.DARK_RED)); - players.playSound(SoundEvents.ENTITY_WITHER_SPAWN); + msg.elimination(player); this.eliminateParticipant(player); } } @@ -291,14 +309,14 @@ public void clearPlayer(ServerPlayerEntity player) { } public void refreshPlayerAttributes(ServerPlayerEntity player) { - for (PlayerAttributeModifier piece : this.moduleManager.getModifiers(ModifierType.PLAYER_ATTRIBUTE)) { + for (PlayerAttributeModifier piece : this.moduleManager.modifiers(ModifierType.PLAYER_ATTRIBUTE)) { piece.refreshAttribute(player); } player.networkHandler.sendPacket(new EntityAttributesS2CPacket(player.getId(), player.getAttributes().getTracked())); } public void applyPlayerEffects(ServerPlayerEntity player) { - for (PermanentEffectModifier piece : this.moduleManager.getModifiers(ModifierType.PERMANENT_EFFECT)) { + for (PermanentEffectModifier piece : this.moduleManager.modifiers(ModifierType.PERMANENT_EFFECT)) { piece.setEffect(player); } } @@ -384,40 +402,13 @@ private void dropCages() { })); } - // MESSAGES - private void sendMessage(String symbol, String s, Formatting f, Object... args) { - this.gameSpace.getPlayers().sendMessage(Text.literal(symbol).append(Text.translatable(s, args)).formatted(f)); - } - - public void sendInfo(String symbol, String s, Object... args) { - this.sendMessage(symbol + " ", s, Formatting.YELLOW, args); - } - - public void sendInfo(String s, Object... args) { - this.sendMessage("", s, Formatting.YELLOW, args); - } - - private void sendWarning(String symbol, String s, Object... args) { - this.sendMessage(symbol + " ", s, Formatting.RED, args); - } - - private void sendWarning(String s, Object... args) { - this.sendMessage("", s, Formatting.RED, args); - } - - public void sendModuleListToChat() { - if (!this.moduleManager.isEmpty()) { - this.gameSpace.getPlayers().sendMessage(this.moduleManager.buildChatMessage()); - this.gameSpace.getPlayers().playSound(SoundEvents.ENTITY_ITEM_PICKUP); - } - } - // GENERAL LISTENERS private void enableModule(RegistryEntry moduleRegistryEntry) { Module module = moduleRegistryEntry.value(); for (Modifier modifier : module.modifiers()) { modifier.enable(this.playerManager); } + msg.moduleAnnouncement("text.module.enabled", moduleRegistryEntry, Formatting.GREEN); } private void disableModule(RegistryEntry moduleRegistryEntry) { @@ -425,6 +416,7 @@ private void disableModule(RegistryEntry moduleRegistryEntry) { for (Modifier modifier : module.modifiers()) { modifier.disable(this.playerManager); } + msg.moduleAnnouncement("text.module.disabled", moduleRegistryEntry, Formatting.RED); } private EventResult onPlayerDamage(ServerPlayerEntity entity, DamageSource damageSource, float v) { @@ -438,9 +430,7 @@ private EventResult onPlayerDamage(ServerPlayerEntity entity, DamageSource damag private EventResult onPlayerDeath(ServerPlayerEntity player, DamageSource source) { if (playerManager.isParticipant(player)) { if (!playerManager.getParticipant(player).isEliminated()) { - PlayerSet players = this.gameSpace.getPlayers(); - players.sendMessage(Text.literal("\nā˜  ").append(source.getDeathMessage(player).copy()).append("!\n").formatted(Formatting.DARK_RED)); - players.playSound(SoundEvents.ENTITY_WITHER_SPAWN); + msg.death(source, player); this.eliminateParticipant(player); return EventResult.DENY; } diff --git a/src/main/java/com/hugman/uhc/game/phase/UHCWaiting.java b/src/main/java/com/hugman/uhc/game/phase/UHCWaiting.java index 6501dac..fe7f742 100644 --- a/src/main/java/com/hugman/uhc/game/phase/UHCWaiting.java +++ b/src/main/java/com/hugman/uhc/game/phase/UHCWaiting.java @@ -2,7 +2,6 @@ import com.hugman.uhc.config.UHCGameConfig; import com.hugman.uhc.game.ModuleManager; -import com.hugman.uhc.game.UHCAttachments; import com.hugman.uhc.game.UHCSpawner; import com.hugman.uhc.map.UHCMap; import net.minecraft.server.world.ServerWorld; @@ -41,7 +40,7 @@ public static GameOpenProcedure open(GameOpenContext context) { var gameSpace = activity.getGameSpace(); UHCWaiting waiting = new UHCWaiting(gameSpace, world, config, teamManager); - gameSpace.setAttachment(UHCAttachments.MODULE_MANAGER, moduleManager); + gameSpace.setAttachment(ModuleManager.ATTACHMENT, moduleManager); activity.listen(GamePlayerEvents.OFFER, JoinOffer::accept); activity.listen(GamePlayerEvents.ACCEPT, waiting::acceptPlayer); diff --git a/src/main/java/com/hugman/uhc/util/TickUtil.java b/src/main/java/com/hugman/uhc/util/TickUtil.java index 803a561..f0a630b 100644 --- a/src/main/java/com/hugman/uhc/util/TickUtil.java +++ b/src/main/java/com/hugman/uhc/util/TickUtil.java @@ -29,10 +29,6 @@ public static int getHours(long t) { return (int) asHours(t) % 24; } - public static boolean blink(long l, int max, int each) { - return l <= max && l % each == 0; - } - public static MutableText format(long t) { if (getHours(t) > 0) { return Text.literal(String.format("%02d:%02d:%02d", getHours(t), getMinutes(t), getSeconds(t))); diff --git a/src/main/resources/data/uhc/lang/en_us.json b/src/main/resources/data/uhc/lang/en_us.json index f5084a4..961be2b 100644 --- a/src/main/resources/data/uhc/lang/en_us.json +++ b/src/main/resources/data/uhc/lang/en_us.json @@ -43,5 +43,16 @@ "text.uhc.world_will_shrink": "You will get teleported in %s. The world will then start shrinking and PvP will get enabled.", "ui.uhc.modules.title": "Active Modules", - "command.uhc.modules.no_modules_activated": "This game has no modules activated!" + "command.uhc.modules.no_modules_activated": "This game has no modules activated!", + + "text.module.enabled": "The %s module has been enabled!", + "text.module.disabled": "The %s module has been disabled!", + "text.module.not_found": "Module with id \"%s\", not found!", + + "command.modules.no_manager": "The module manager is not available! (please contact the developer)", + "command.modules.no_modules_activated": "There are not active modules!", + "command.modules.already_enabled": "The %s module is already enabled!", + "command.modules.already_disabled": "The %s module is already disabled!", + "command.modules.enable.success": "The %s module has been successfully enabled.", + "command.modules.disable.success": "The %s module has been successfully disabled." } \ No newline at end of file