From 8095977cc9faa585d9fe27f094e2d65c26a9211e Mon Sep 17 00:00:00 2001 From: Vankka Date: Mon, 3 Feb 2025 17:31:27 +0200 Subject: [PATCH] Cleanup code style --- .../FabricGameCommandExecutionHelper.java | 17 ++++++++---- .../fabric/console/FabricConsole.java | 3 ++- .../FabricCommandFeedbackExecutor.java | 10 ++++++- .../mixin/PlayerAdvancementTrackerMixin.java | 1 + .../fabric/module/AbstractFabricModule.java | 1 + .../fabric/module/ban/FabricBanModule.java | 26 ++++++++++++++----- .../module/chat/FabricAdvancementModule.java | 1 + .../fabric/module/chat/FabricChatModule.java | 1 + .../fabric/module/chat/FabricDeathModule.java | 2 +- .../fabric/module/chat/FabricJoinModule.java | 3 ++- .../fabric/module/chat/FabricQuitModule.java | 1 + .../fabric/player/FabricPlayer.java | 1 - .../fabric/player/FabricPlayerProvider.java | 4 ++- .../fabric/plugin/FabricModManager.java | 4 ++- .../FabricRequiredLinkingModule.java | 7 ++++- 15 files changed, 62 insertions(+), 20 deletions(-) diff --git a/fabric/src/main/java/com/discordsrv/fabric/command/game/FabricGameCommandExecutionHelper.java b/fabric/src/main/java/com/discordsrv/fabric/command/game/FabricGameCommandExecutionHelper.java index b3fddd41..66165ec9 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/command/game/FabricGameCommandExecutionHelper.java +++ b/fabric/src/main/java/com/discordsrv/fabric/command/game/FabricGameCommandExecutionHelper.java @@ -22,6 +22,7 @@ import com.discordsrv.common.command.game.abstraction.GameCommandExecutionHelper import com.discordsrv.fabric.FabricDiscordSRV; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.context.ParsedCommandNode; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.RootCommandNode; @@ -35,6 +36,7 @@ import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; public class FabricGameCommandExecutionHelper implements GameCommandExecutionHelper { + protected final FabricDiscordSRV discordSRV; private final CommandDispatcher dispatcher; @@ -60,8 +62,9 @@ public class FabricGameCommandExecutionHelper implements GameCommandExecutionHel return CompletableFuture.completedFuture(data); } - if (!parse.getContext().getNodes().isEmpty()) { - CommandNode lastNode = parse.getContext().getNodes().getLast().getNode(); + List> nodes = parse.getContext().getNodes(); + if (!nodes.isEmpty()) { + CommandNode lastNode = nodes.getLast().getNode(); if (lastNode.getChildren().isEmpty() && lastNode.getRedirect() == null) { // We reached the end of the command tree. Suggest the full command as a valid command. return CompletableFuture.completedFuture(Collections.singletonList(fullCommand)); @@ -75,7 +78,7 @@ public class FabricGameCommandExecutionHelper implements GameCommandExecutionHel if (data.isEmpty()) { // Suggestions are empty, Likely the user is still typing an argument. // If the context is empty, We search all commands from the root. - CommandNode lastNode = !parse.getContext().getNodes().isEmpty() ? parse.getContext().getNodes().getLast().getNode() : parse.getContext().getRootNode(); + CommandNode lastNode = !nodes.isEmpty() ? nodes.getLast().getNode() : parse.getContext().getRootNode(); for (CommandNode child : lastNode.getChildren()) { if (child.getName().toLowerCase().startsWith(parts.getLast().toLowerCase())) { @@ -112,7 +115,12 @@ public class FabricGameCommandExecutionHelper implements GameCommandExecutionHel } private CompletableFuture> getRootCommands() { - return CompletableFuture.completedFuture(dispatcher.getRoot().getChildren().stream().map(CommandNode::getName).collect(Collectors.toList())); + return CompletableFuture.completedFuture( + dispatcher.getRoot().getChildren() + .stream() + .map(CommandNode::getName) + .collect(Collectors.toList()) + ); } // Split the error message if it's too long on a period or a comma. If the message reached 97 characters, split at that point and continue. @@ -143,5 +151,4 @@ public class FabricGameCommandExecutionHelper implements GameCommandExecutionHel return parts; } - } diff --git a/fabric/src/main/java/com/discordsrv/fabric/console/FabricConsole.java b/fabric/src/main/java/com/discordsrv/fabric/console/FabricConsole.java index 529084df..ea023299 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/console/FabricConsole.java +++ b/fabric/src/main/java/com/discordsrv/fabric/console/FabricConsole.java @@ -41,7 +41,8 @@ public class FabricConsole extends FabricCommandSender implements Console { super(discordSRV, discordSRV.getServer().getCommandSource()); this.loggingBackend = Log4JLoggerImpl.getRoot(); - Function, ServerCommandSource> commandSenderProvider = consumer -> new FabricCommandFeedbackExecutor(discordSRV.getServer(), consumer).getCommandSource(); + Function, ServerCommandSource> commandSenderProvider = + consumer -> new FabricCommandFeedbackExecutor(discordSRV.getServer(), consumer).getCommandSource(); this.executorProvider = consumer -> new FabricCommandExecutor(discordSRV, commandSenderProvider.apply(consumer)); } diff --git a/fabric/src/main/java/com/discordsrv/fabric/console/executor/FabricCommandFeedbackExecutor.java b/fabric/src/main/java/com/discordsrv/fabric/console/executor/FabricCommandFeedbackExecutor.java index b7602acb..d506acb2 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/console/executor/FabricCommandFeedbackExecutor.java +++ b/fabric/src/main/java/com/discordsrv/fabric/console/executor/FabricCommandFeedbackExecutor.java @@ -43,7 +43,15 @@ public class FabricCommandFeedbackExecutor implements CommandOutput, Consumer { + protected boolean enabled = false; public AbstractFabricModule(FabricDiscordSRV discordSRV) { diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java index 51fc1ef0..bd8a22bf 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java @@ -21,6 +21,7 @@ package com.discordsrv.fabric.module.ban; import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.api.module.type.PunishmentModule; import com.discordsrv.api.punishment.Punishment; +import com.discordsrv.common.abstraction.player.IPlayer; import com.discordsrv.common.feature.bansync.BanSyncModule; import com.discordsrv.common.util.ComponentUtil; import com.discordsrv.fabric.FabricDiscordSRV; @@ -43,6 +44,7 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; public class FabricBanModule extends AbstractFabricModule implements PunishmentModule.Bans { + private static FabricBanModule instance; public FabricBanModule(FabricDiscordSRV discordSRV) { @@ -55,13 +57,19 @@ public class FabricBanModule extends AbstractFabricModule implements PunishmentM if (instance == null) return; FabricDiscordSRV discordSRV = instance.discordSRV; BanSyncModule module = discordSRV.getModule(BanSyncModule.class); - if (module != null) { - instance.getBan(gameProfile.getId()) - .whenComplete((punishment, t) -> { - if (punishment != null) - module.notifyBanned(Objects.requireNonNull(discordSRV.playerProvider().player(gameProfile.getId())), punishment); - }); + if (module == null) return; + + UUID playerUUID = gameProfile.getId(); + IPlayer player = discordSRV.playerProvider().player(gameProfile.getId()); + if (player == null) { + throw new RuntimeException("Player " + playerUUID + " not present in player provider"); } + + instance.getBan(playerUUID).whenComplete((punishment, t) -> { + if (punishment != null) { + module.notifyBanned(player, punishment); + } + }); } public static void onPardon(GameProfile gameProfile) { @@ -122,7 +130,11 @@ public class FabricBanModule extends AbstractFabricModule implements PunishmentM ServerPlayerEntity serverPlayerEntity = server.getPlayerManager().getPlayer(playerUUID); if (serverPlayerEntity != null) { - serverPlayerEntity.networkHandler.disconnect(reason != null ? discordSRV.getAdventure().asNative(reason.asAdventure()) : Text.translatable("multiplayer.disconnect.banned")); + serverPlayerEntity.networkHandler.disconnect( + reason != null + ? discordSRV.getAdventure().asNative(reason.asAdventure()) + : Text.translatable("multiplayer.disconnect.banned") + ); } } catch (Exception e) { discordSRV.logger().error("Failed to ban player", e); diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java index bf182eea..dd35094c 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java @@ -29,6 +29,7 @@ import net.minecraft.advancement.AdvancementEntry; import net.minecraft.server.network.ServerPlayerEntity; public class FabricAdvancementModule extends AbstractFabricModule { + private static FabricAdvancementModule instance; private final FabricDiscordSRV discordSRV; diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricChatModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricChatModule.java index 3c363973..9818d86a 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricChatModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricChatModule.java @@ -29,6 +29,7 @@ import net.minecraft.network.message.SignedMessage; import net.minecraft.server.network.ServerPlayerEntity; public class FabricChatModule extends AbstractFabricModule { + private final FabricDiscordSRV discordSRV; public FabricChatModule(FabricDiscordSRV discordSRV) { diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricDeathModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricDeathModule.java index 95c0e84f..45e70dba 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricDeathModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricDeathModule.java @@ -31,6 +31,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; public class FabricDeathModule extends AbstractFabricModule { + private final FabricDiscordSRV discordSRV; public FabricDeathModule(FabricDiscordSRV discordSRV) { @@ -60,5 +61,4 @@ public class FabricDeathModule extends AbstractFabricModule { ); } } - } diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricJoinModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricJoinModule.java index 878d97b2..220400b4 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricJoinModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricJoinModule.java @@ -35,6 +35,7 @@ import net.minecraft.text.Text; import java.util.Objects; public class FabricJoinModule extends AbstractFabricModule { + private final FabricDiscordSRV discordSRV; public FabricJoinModule(FabricDiscordSRV discordSRV) { @@ -51,7 +52,7 @@ public class FabricJoinModule extends AbstractFabricModule { ServerPlayerEntity playerEntity = serverPlayNetworkHandler.player; MinecraftComponent component = getJoinMessage(playerEntity); - boolean firstJoin = Objects.requireNonNull(minecraftServer.getUserCache()).findByName(serverPlayNetworkHandler.player.getGameProfile().getName()).isEmpty(); + boolean firstJoin = Objects.requireNonNull(minecraftServer.getUserCache()).findByName(playerEntity.getGameProfile().getName()).isEmpty(); DiscordSRVPlayer player = discordSRV.playerProvider().player(playerEntity); discordSRV.eventBus().publish( diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricQuitModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricQuitModule.java index bb184057..f1d0446e 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricQuitModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricQuitModule.java @@ -32,6 +32,7 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; public class FabricQuitModule extends AbstractFabricModule { + private final FabricDiscordSRV discordSRV; public FabricQuitModule(FabricDiscordSRV discordSRV) { diff --git a/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayer.java b/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayer.java index 4960561c..3fdb7610 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayer.java +++ b/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayer.java @@ -99,5 +99,4 @@ public class FabricPlayer extends FabricCommandSender implements IPlayer { public String toString() { return "FabricPlayer{" + username() + "}"; } - } diff --git a/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayerProvider.java b/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayerProvider.java index b7dcff79..0643da14 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayerProvider.java +++ b/fabric/src/main/java/com/discordsrv/fabric/player/FabricPlayerProvider.java @@ -27,6 +27,7 @@ import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; public class FabricPlayerProvider extends AbstractPlayerProvider { + private boolean enabled = false; public FabricPlayerProvider(FabricDiscordSRV discordSRV) { @@ -39,8 +40,9 @@ public class FabricPlayerProvider extends AbstractPlayerProvider { + private static FabricRequiredLinkingModule instance; private final Cache linkCheckRateLimit; private final Map frozen = new ConcurrentHashMap<>(); @@ -113,7 +114,11 @@ public class FabricRequiredLinkingModule extends ServerRequireLinkingModule= to.getY() && from.getZ() == to.getZ()) { return; }