Skip to content

Commit

Permalink
Cleanup code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Vankka committed Feb 3, 2025
1 parent d5d9697 commit 8095977
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -35,6 +36,7 @@
import java.util.stream.Collectors;

public class FabricGameCommandExecutionHelper implements GameCommandExecutionHelper {

protected final FabricDiscordSRV discordSRV;
private final CommandDispatcher<ServerCommandSource> dispatcher;

Expand All @@ -60,8 +62,9 @@ public CompletableFuture<List<String>> suggestCommands(List<String> parts) {
return CompletableFuture.completedFuture(data);
}

if (!parse.getContext().getNodes().isEmpty()) {
CommandNode<ServerCommandSource> lastNode = parse.getContext().getNodes().getLast().getNode();
List<ParsedCommandNode<ServerCommandSource>> nodes = parse.getContext().getNodes();
if (!nodes.isEmpty()) {
CommandNode<ServerCommandSource> 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));
Expand All @@ -75,7 +78,7 @@ public CompletableFuture<List<String>> suggestCommands(List<String> parts) {
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<ServerCommandSource> lastNode = !parse.getContext().getNodes().isEmpty() ? parse.getContext().getNodes().getLast().getNode() : parse.getContext().getRootNode();
CommandNode<ServerCommandSource> lastNode = !nodes.isEmpty() ? nodes.getLast().getNode() : parse.getContext().getRootNode();

for (CommandNode<ServerCommandSource> child : lastNode.getChildren()) {
if (child.getName().toLowerCase().startsWith(parts.getLast().toLowerCase())) {
Expand Down Expand Up @@ -112,7 +115,12 @@ public boolean isSameCommand(String command1, String command2) {
}

private CompletableFuture<List<String>> 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.
Expand Down Expand Up @@ -143,5 +151,4 @@ private List<String> splitErrorMessage(String message) {

return parts;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public FabricConsole(FabricDiscordSRV discordSRV) {
super(discordSRV, discordSRV.getServer().getCommandSource());
this.loggingBackend = Log4JLoggerImpl.getRoot();

Function<Consumer<Component>, ServerCommandSource> commandSenderProvider = consumer -> new FabricCommandFeedbackExecutor(discordSRV.getServer(), consumer).getCommandSource();
Function<Consumer<Component>, ServerCommandSource> commandSenderProvider =
consumer -> new FabricCommandFeedbackExecutor(discordSRV.getServer(), consumer).getCommandSource();
this.executorProvider = consumer -> new FabricCommandExecutor(discordSRV, commandSenderProvider.apply(consumer));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ public FabricCommandFeedbackExecutor(MinecraftServer server, Consumer<Component>
public ServerCommandSource getCommandSource() {
ServerWorld serverWorld = server.getOverworld();
return new ServerCommandSource(
this, serverWorld == null ? Vec3d.ZERO : Vec3d.of(serverWorld.getSpawnPos()), Vec2f.ZERO, serverWorld, 4, "DiscordSRV", Text.literal("DiscordSRV"), server, null
this,
serverWorld == null ? Vec3d.ZERO : Vec3d.of(serverWorld.getSpawnPos()),
Vec2f.ZERO,
serverWorld,
4,
"DiscordSRV",
Text.literal("DiscordSRV"),
server,
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

@Mixin(PlayerAdvancementTracker.class)
public class PlayerAdvancementTrackerMixin {

@Shadow
private ServerPlayerEntity owner;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.discordsrv.fabric.FabricDiscordSRV;

public abstract class AbstractFabricModule extends AbstractModule<FabricDiscordSRV> {

protected boolean enabled = false;

public AbstractFabricModule(FabricDiscordSRV discordSRV) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -43,6 +44,7 @@
import java.util.concurrent.CompletableFuture;

public class FabricBanModule extends AbstractFabricModule implements PunishmentModule.Bans {

private static FabricBanModule instance;

public FabricBanModule(FabricDiscordSRV discordSRV) {
Expand All @@ -55,13 +57,19 @@ public static void onBan(GameProfile gameProfile) {
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) {
Expand Down Expand Up @@ -122,7 +130,11 @@ public CompletableFuture<Void> addBan(

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.server.network.ServerPlayerEntity;

public class FabricAdvancementModule extends AbstractFabricModule {

private static FabricAdvancementModule instance;
private final FabricDiscordSRV discordSRV;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.server.network.ServerPlayerEntity;

public class FabricChatModule extends AbstractFabricModule {

private final FabricDiscordSRV discordSRV;

public FabricChatModule(FabricDiscordSRV discordSRV) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraft.text.Text;

public class FabricDeathModule extends AbstractFabricModule {

private final FabricDiscordSRV discordSRV;

public FabricDeathModule(FabricDiscordSRV discordSRV) {
Expand Down Expand Up @@ -60,5 +61,4 @@ private void onDeath(LivingEntity livingEntity, DamageSource damageSource) {
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Objects;

public class FabricJoinModule extends AbstractFabricModule {

private final FabricDiscordSRV discordSRV;

public FabricJoinModule(FabricDiscordSRV discordSRV) {
Expand All @@ -51,7 +52,7 @@ private void onJoin(ServerPlayNetworkHandler serverPlayNetworkHandler, PacketSen

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.util.Formatting;

public class FabricQuitModule extends AbstractFabricModule {

private final FabricDiscordSRV discordSRV;

public FabricQuitModule(FabricDiscordSRV discordSRV) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,4 @@ public void removeChatSuggestions(Collection<String> suggestions) {
public String toString() {
return "FabricPlayer{" + username() + "}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.minecraft.server.network.ServerPlayerEntity;

public class FabricPlayerProvider extends AbstractPlayerProvider<FabricPlayer, FabricDiscordSRV> {

private boolean enabled = false;

public FabricPlayerProvider(FabricDiscordSRV discordSRV) {
Expand All @@ -39,8 +40,9 @@ public FabricPlayerProvider(FabricDiscordSRV discordSRV) {
@Override
public void subscribe() {
enabled = true;
if (discordSRV.getServer() == null || discordSRV.getServer().getPlayerManager() == null)
if (discordSRV.getServer() == null || discordSRV.getServer().getPlayerManager() == null) {
return; // Server not started yet, So there's no players to add
}

// Add players that are already connected
for (ServerPlayerEntity player : discordSRV.getServer().getPlayerManager().getPlayerList()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public List<Plugin> getPlugins() {
id,
modContainer.getMetadata().getName(),
modContainer.getMetadata().getVersion().toString(),
modContainer.getMetadata().getAuthors().stream().map(Person::getName).collect(Collectors.toList())
modContainer.getMetadata().getAuthors().stream()
.map(Person::getName)
.collect(Collectors.toList())
);
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@


public class FabricRequiredLinkingModule extends ServerRequireLinkingModule<FabricDiscordSRV> {

private static FabricRequiredLinkingModule instance;
private final Cache<UUID, Boolean> linkCheckRateLimit;
private final Map<UUID, Component> frozen = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -113,7 +114,11 @@ public static void onPlayerMove(ServerPlayerEntity player, PlayerMoveC2SPacket p
}

BlockPos from = player.getBlockPos();
BlockPos to = new BlockPos(MathHelper.floor(packet.getX(player.getX())), MathHelper.floor(packet.getY(player.getY())), MathHelper.floor(packet.getZ(player.getZ())));
BlockPos to = new BlockPos(
MathHelper.floor(packet.getX(player.getX())),
MathHelper.floor(packet.getY(player.getY())),
MathHelper.floor(packet.getZ(player.getZ()))
);
if (from.getX() == to.getX() && from.getY() >= to.getY() && from.getZ() == to.getZ()) {
return;
}
Expand Down

0 comments on commit 8095977

Please sign in to comment.