From 44003f4ee626d8cb6a7aa606ddd1b5a93a1ab87c Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 15 May 2023 07:39:29 +0000 Subject: [PATCH] Format code [skip actions] --- .../grasscutter/database/DatabaseHelper.java | 9 +- .../emu/grasscutter/game/HandbookActions.java | 95 ++++++++----------- .../server/dispatch/DispatchClient.java | 30 +++--- .../server/dispatch/IDispatcher.java | 9 +- .../http/documentation/HandbookHandler.java | 31 +++--- .../server/http/handlers/GachaHandler.java | 10 +- .../emu/grasscutter/utils/DispatchUtils.java | 18 ++-- .../utils/objects/HandbookBody.java | 3 +- 8 files changed, 91 insertions(+), 114 deletions(-) diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index 73789aa22..dfac8da12 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -1,5 +1,7 @@ package emu.grasscutter.database; +import static com.mongodb.client.model.Filters.eq; + import com.mongodb.client.result.DeleteResult; import dev.morphia.query.FindOptions; import dev.morphia.query.Sort; @@ -20,12 +22,9 @@ import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.player.Player; import emu.grasscutter.game.quest.GameMainQuest; import emu.grasscutter.game.world.SceneGroupInstance; - import java.util.List; import java.util.stream.Stream; -import static com.mongodb.client.model.Filters.eq; - public final class DatabaseHelper { public static Account createAccount(String username) { return createAccountWithUid(username, 0); @@ -219,8 +218,8 @@ public final class DatabaseHelper { } /** - * Use {@link DatabaseHelper#getPlayerByAccount(Account, Class)} for creating a real player. - * This method is used for fetching the player's data. + * Use {@link DatabaseHelper#getPlayerByAccount(Account, Class)} for creating a real player. This + * method is used for fetching the player's data. * * @param accountId The account's ID. * @return The player. diff --git a/src/main/java/emu/grasscutter/game/HandbookActions.java b/src/main/java/emu/grasscutter/game/HandbookActions.java index 7e3945e18..7a9566012 100644 --- a/src/main/java/emu/grasscutter/game/HandbookActions.java +++ b/src/main/java/emu/grasscutter/game/HandbookActions.java @@ -8,7 +8,6 @@ import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.server.packet.send.PacketAddNoGachaAvatarCardNotify; import emu.grasscutter.utils.objects.HandbookBody.*; - import java.util.Objects; /** Commands executed by the handbook. */ @@ -22,8 +21,7 @@ public interface HandbookActions { static Response grantAvatar(GrantAvatar request) { // Validate the request. if (request.getPlayer() == null || request.getAvatar() == null) { - return Response.builder().status(400) - .message("Invalid request.").build(); + return Response.builder().status(400).message("Invalid request.").build(); } try { @@ -37,12 +35,10 @@ public interface HandbookActions { // Validate the request. if (player == null) { - return Response.builder().status(1) - .message("Player not found.").build(); + return Response.builder().status(1).message("Player not found.").build(); } if (avatarData == null) { - return Response.builder().status(400) - .message("Invalid avatar ID.").build(); + return Response.builder().status(400).message("Invalid avatar ID.").build(); } // Create the new avatar. @@ -50,25 +46,24 @@ public interface HandbookActions { avatar.setLevel(request.getLevel()); avatar.setPromoteLevel(Avatar.getMinPromoteLevel(avatar.getLevel())); Objects.requireNonNull(avatar.getSkillDepot()) - .getSkillsAndEnergySkill() - .forEach(id -> avatar.setSkillLevel(id, request.getTalentLevels())); + .getSkillsAndEnergySkill() + .forEach(id -> avatar.setSkillLevel(id, request.getTalentLevels())); avatar.forceConstellationLevel(request.getConstellations()); avatar.recalcStats(true); avatar.save(); // Add the avatar. player.addAvatar(avatar); - player.sendPacket(new PacketAddNoGachaAvatarCardNotify( - avatar, ActionReason.Gm)); - return Response.builder().status(200) - .message("Avatar granted.").build(); + player.sendPacket(new PacketAddNoGachaAvatarCardNotify(avatar, ActionReason.Gm)); + return Response.builder().status(200).message("Avatar granted.").build(); } catch (NumberFormatException ignored) { - return Response.builder().status(500) - .message("Invalid player UID or avatar ID.").build(); + return Response.builder().status(500).message("Invalid player UID or avatar ID.").build(); } catch (Exception exception) { Grasscutter.getLogger().debug("A handbook command error occurred.", exception); - return Response.builder().status(500) - .message("An error occurred while granting the avatar.").build(); + return Response.builder() + .status(500) + .message("An error occurred while granting the avatar.") + .build(); } } @@ -81,8 +76,7 @@ public interface HandbookActions { static Response giveItem(GiveItem request) { // Validate the request. if (request.getPlayer() == null || request.getItem() == null) { - return Response.builder().status(400) - .message("Invalid request.").build(); + return Response.builder().status(400).message("Invalid request.").build(); } try { @@ -96,12 +90,10 @@ public interface HandbookActions { // Validate the request. if (player == null) { - return Response.builder().status(1) - .message("Player not found.").build(); + return Response.builder().status(1).message("Player not found.").build(); } if (itemData == null) { - return Response.builder().status(400) - .message("Invalid player UID or item ID.").build(); + return Response.builder().status(400).message("Invalid player UID or item ID.").build(); } // Add the item to the player's inventory. @@ -122,15 +114,15 @@ public interface HandbookActions { var itemStack = new GameItem(itemData, (int) amount); player.getInventory().addItem(itemStack, ActionReason.Gm); - return Response.builder().status(200) - .message("Item granted.").build(); + return Response.builder().status(200).message("Item granted.").build(); } catch (NumberFormatException ignored) { - return Response.builder().status(500) - .message("Invalid player UID or item ID.").build(); + return Response.builder().status(500).message("Invalid player UID or item ID.").build(); } catch (Exception exception) { Grasscutter.getLogger().debug("A handbook command error occurred.", exception); - return Response.builder().status(500) - .message("An error occurred while granting the item.").build(); + return Response.builder() + .status(500) + .message("An error occurred while granting the item.") + .build(); } } @@ -143,8 +135,7 @@ public interface HandbookActions { static Response teleportTo(TeleportTo request) { // Validate the request. if (request.getPlayer() == null || request.getScene() == null) { - return Response.builder().status(400) - .message("Invalid request.").build(); + return Response.builder().status(400).message("Invalid request.").build(); } try { @@ -157,15 +148,13 @@ public interface HandbookActions { // Validate the request. if (player == null) { - return Response.builder().status(1) - .message("Player not found.").build(); + return Response.builder().status(1).message("Player not found.").build(); } // Find the scene in the player's world. var scene = player.getWorld().getSceneById(sceneId); if (scene == null) { - return Response.builder().status(400) - .message("Invalid scene ID.").build(); + return Response.builder().status(400).message("Invalid scene ID.").build(); } // Resolve the correct teleport position. @@ -175,15 +164,15 @@ public interface HandbookActions { scene.getWorld().transferPlayerToScene(player, scene.getId(), position); player.getRotation().set(rotation); - return Response.builder().status(200) - .message("Player teleported.").build(); + return Response.builder().status(200).message("Player teleported.").build(); } catch (NumberFormatException ignored) { - return Response.builder().status(400) - .message("Invalid player UID or scene ID.").build(); + return Response.builder().status(400).message("Invalid player UID or scene ID.").build(); } catch (Exception exception) { Grasscutter.getLogger().debug("A handbook command error occurred.", exception); - return Response.builder().status(500) - .message("An error occurred while teleporting to the scene.").build(); + return Response.builder() + .status(500) + .message("An error occurred while teleporting to the scene.") + .build(); } } @@ -196,8 +185,7 @@ public interface HandbookActions { static Response spawnEntity(SpawnEntity request) { // Validate the request. if (request.getPlayer() == null || request.getEntity() == null) { - return Response.builder().status(400) - .message("Invalid request.").build(); + return Response.builder().status(400).message("Invalid request.").build(); } try { @@ -211,20 +199,17 @@ public interface HandbookActions { // Validate the request. if (player == null) { - return Response.builder().status(1) - .message("Player not found.").build(); + return Response.builder().status(1).message("Player not found.").build(); } if (entityData == null) { - return Response.builder().status(400) - .message("Invalid entity ID.").build(); + return Response.builder().status(400).message("Invalid entity ID.").build(); } // Validate request properties. var scene = player.getScene(); var level = request.getLevel(); if (scene == null || level > 200 || level < 1) { - return Response.builder().status(400) - .message("Invalid scene or level.").build(); + return Response.builder().status(400).message("Invalid scene or level.").build(); } // Create the entity. @@ -233,15 +218,15 @@ public interface HandbookActions { scene.addEntity(entity); } - return Response.builder().status(200) - .message("Entity(s) spawned.").build(); + return Response.builder().status(200).message("Entity(s) spawned.").build(); } catch (NumberFormatException ignored) { - return Response.builder().status(400) - .message("Invalid player UID or entity ID.").build(); + return Response.builder().status(400).message("Invalid player UID or entity ID.").build(); } catch (Exception exception) { Grasscutter.getLogger().debug("A handbook command error occurred.", exception); - return Response.builder().status(500) - .message("An error occurred while teleporting to the scene.").build(); + return Response.builder() + .status(500) + .message("An error occurred while teleporting to the scene.") + .build(); } } } diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java index efe71ba97..7a847e66a 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java @@ -1,5 +1,7 @@ package emu.grasscutter.server.dispatch; +import static emu.grasscutter.config.Configuration.DISPATCH_INFO; + import com.google.gson.JsonElement; import com.google.gson.JsonObject; import emu.grasscutter.Grasscutter; @@ -10,12 +12,6 @@ import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.DispatchUtils; import emu.grasscutter.utils.JsonUtils; import emu.grasscutter.utils.objects.HandbookBody; -import lombok.Getter; -import org.java_websocket.WebSocket; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.handshake.ServerHandshake; -import org.slf4j.Logger; - import java.net.ConnectException; import java.net.URI; import java.nio.ByteBuffer; @@ -25,8 +21,11 @@ import java.util.List; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; - -import static emu.grasscutter.config.Configuration.DISPATCH_INFO; +import lombok.Getter; +import org.java_websocket.WebSocket; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; +import org.slf4j.Logger; public final class DispatchClient extends WebSocketClient implements IDispatcher { @Getter private final Logger logger = Grasscutter.getLogger(); @@ -89,12 +88,15 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher var action = HandbookBody.Action.valueOf(actionStr); // Produce a handbook response. - var response = DispatchUtils.performHandbookAction(action, switch (action) { - case GRANT_AVATAR -> JsonUtils.decode(data, HandbookBody.GrantAvatar.class); - case GIVE_ITEM -> JsonUtils.decode(data, HandbookBody.GiveItem.class); - case TELEPORT_TO -> JsonUtils.decode(data, HandbookBody.TeleportTo.class); - case SPAWN_ENTITY -> JsonUtils.decode(data, HandbookBody.SpawnEntity.class); - }); + var response = + DispatchUtils.performHandbookAction( + action, + switch (action) { + case GRANT_AVATAR -> JsonUtils.decode(data, HandbookBody.GrantAvatar.class); + case GIVE_ITEM -> JsonUtils.decode(data, HandbookBody.GiveItem.class); + case TELEPORT_TO -> JsonUtils.decode(data, HandbookBody.TeleportTo.class); + case SPAWN_ENTITY -> JsonUtils.decode(data, HandbookBody.SpawnEntity.class); + }); // Check if the response's status is '1'. if (response.getStatus() == 1) return; diff --git a/src/main/java/emu/grasscutter/server/dispatch/IDispatcher.java b/src/main/java/emu/grasscutter/server/dispatch/IDispatcher.java index 69862c34a..95e53bdc0 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/IDispatcher.java +++ b/src/main/java/emu/grasscutter/server/dispatch/IDispatcher.java @@ -1,22 +1,21 @@ package emu.grasscutter.server.dispatch; +import static emu.grasscutter.config.Configuration.DISPATCH_INFO; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.JsonAdapters.ByteArrayAdapter; -import org.java_websocket.WebSocket; -import org.slf4j.Logger; - import java.nio.charset.StandardCharsets; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; - -import static emu.grasscutter.config.Configuration.DISPATCH_INFO; +import org.java_websocket.WebSocket; +import org.slf4j.Logger; public interface IDispatcher { Gson JSON = diff --git a/src/main/java/emu/grasscutter/server/http/documentation/HandbookHandler.java b/src/main/java/emu/grasscutter/server/http/documentation/HandbookHandler.java index 9eabdbe1e..24f892c08 100644 --- a/src/main/java/emu/grasscutter/server/http/documentation/HandbookHandler.java +++ b/src/main/java/emu/grasscutter/server/http/documentation/HandbookHandler.java @@ -1,5 +1,7 @@ package emu.grasscutter.server.http.documentation; +import static emu.grasscutter.config.Configuration.HANDBOOK; + import emu.grasscutter.server.http.Router; import emu.grasscutter.utils.DispatchUtils; import emu.grasscutter.utils.FileUtils; @@ -8,8 +10,6 @@ import emu.grasscutter.utils.objects.HandbookBody.Action; import io.javalin.Javalin; import io.javalin.http.Context; -import static emu.grasscutter.config.Configuration.HANDBOOK; - /** Handles requests for the new GM Handbook. */ public final class HandbookHandler implements Router { private final byte[] handbook; @@ -74,11 +74,9 @@ public final class HandbookHandler implements Router { // Parse the request body into a class. var request = ctx.bodyAsClass(HandbookBody.GrantAvatar.class); // Get the response. - var response = DispatchUtils.performHandbookAction( - Action.GRANT_AVATAR, request); + var response = DispatchUtils.performHandbookAction(Action.GRANT_AVATAR, request); // Send the response. - ctx.status(response.getStatus() > 100 ? - response.getStatus() : 500).json(response); + ctx.status(response.getStatus() > 100 ? response.getStatus() : 500).json(response); } /** @@ -96,11 +94,9 @@ public final class HandbookHandler implements Router { // Parse the request body into a class. var request = ctx.bodyAsClass(HandbookBody.GiveItem.class); // Get the response. - var response = DispatchUtils.performHandbookAction( - Action.GIVE_ITEM, request); + var response = DispatchUtils.performHandbookAction(Action.GIVE_ITEM, request); // Send the response. - ctx.status(response.getStatus() > 100 ? - response.getStatus() : 500).json(response); + ctx.status(response.getStatus() > 100 ? response.getStatus() : 500).json(response); } /** @@ -118,11 +114,9 @@ public final class HandbookHandler implements Router { // Parse the request body into a class. var request = ctx.bodyAsClass(HandbookBody.TeleportTo.class); // Get the response. - var response = DispatchUtils.performHandbookAction( - Action.TELEPORT_TO, request); + var response = DispatchUtils.performHandbookAction(Action.TELEPORT_TO, request); // Send the response. - ctx.status(response.getStatus() > 100 ? - response.getStatus() : 500).json(response); + ctx.status(response.getStatus() > 100 ? response.getStatus() : 500).json(response); } /** @@ -138,13 +132,10 @@ public final class HandbookHandler implements Router { } // Parse the request body into a class. - var request = ctx.bodyAsClass( - HandbookBody.SpawnEntity.class); + var request = ctx.bodyAsClass(HandbookBody.SpawnEntity.class); // Get the response. - var response = DispatchUtils.performHandbookAction( - Action.SPAWN_ENTITY, request); + var response = DispatchUtils.performHandbookAction(Action.SPAWN_ENTITY, request); // Send the response. - ctx.status(response.getStatus() > 100 ? - response.getStatus() : 500).json(response); + ctx.status(response.getStatus() > 100 ? response.getStatus() : 500).json(response); } } diff --git a/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java b/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java index 1f5b4f74d..23b5ec3bc 100644 --- a/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java +++ b/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java @@ -1,5 +1,7 @@ package emu.grasscutter.server.http.handlers; +import static emu.grasscutter.utils.Language.translate; + import com.google.gson.JsonObject; import emu.grasscutter.Grasscutter; import emu.grasscutter.database.DatabaseHelper; @@ -11,16 +13,13 @@ import emu.grasscutter.utils.Utils; import io.javalin.Javalin; import io.javalin.http.ContentType; import io.javalin.http.Context; -import lombok.Getter; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.LinkedHashSet; - -import static emu.grasscutter.utils.Language.translate; +import lombok.Getter; /** Handles all gacha-related HTTP requests. */ public final class GachaHandler implements Router { @@ -164,7 +163,6 @@ public final class GachaHandler implements Router { public void applyRoutes(Javalin javalin) { javalin.get("/gacha", GachaHandler::gachaRecords); javalin.get("/gacha/details", GachaHandler::gachaDetails); - javalin.get("/gacha/mappings", ctx -> - ctx.result(FileUtils.read(gachaMappingsPath.toString()))); + javalin.get("/gacha/mappings", ctx -> ctx.result(FileUtils.read(gachaMappingsPath.toString()))); } } diff --git a/src/main/java/emu/grasscutter/utils/DispatchUtils.java b/src/main/java/emu/grasscutter/utils/DispatchUtils.java index 73669cbb1..4d8cb3dbe 100644 --- a/src/main/java/emu/grasscutter/utils/DispatchUtils.java +++ b/src/main/java/emu/grasscutter/utils/DispatchUtils.java @@ -1,5 +1,7 @@ package emu.grasscutter.utils; +import static emu.grasscutter.config.Configuration.DISPATCH_INFO; + import com.google.gson.JsonObject; import emu.grasscutter.Grasscutter; import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest; @@ -12,13 +14,10 @@ import emu.grasscutter.server.http.handlers.GachaHandler; import emu.grasscutter.server.http.objects.LoginTokenRequestJson; import emu.grasscutter.utils.objects.HandbookBody; import emu.grasscutter.utils.objects.HandbookBody.*; - -import javax.annotation.Nullable; import java.net.http.HttpClient; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; - -import static emu.grasscutter.config.Configuration.DISPATCH_INFO; +import javax.annotation.Nullable; public interface DispatchUtils { /** HTTP client used for dispatch queries. */ @@ -136,8 +135,9 @@ public interface DispatchUtils { var future = new CompletableFuture(); // Listen for the response. var server = Grasscutter.getDispatchServer(); - server.registerCallback(PacketIds.GmTalkRsp, packet -> - future.complete(IDispatcher.decode(packet, Response.class))); + server.registerCallback( + PacketIds.GmTalkRsp, + packet -> future.complete(IDispatcher.decode(packet, Response.class))); // Broadcast the request. server.sendMessage(PacketIds.GmTalkReq, request); @@ -146,8 +146,10 @@ public interface DispatchUtils { // Wait for the response. yield future.get(5L, TimeUnit.SECONDS); } catch (Exception ignored) { - yield Response.builder().status(400) - .message("No response received from any server.").build(); + yield Response.builder() + .status(400) + .message("No response received from any server.") + .build(); } } case HYBRID, GAME_ONLY -> switch (action) { diff --git a/src/main/java/emu/grasscutter/utils/objects/HandbookBody.java b/src/main/java/emu/grasscutter/utils/objects/HandbookBody.java index e6367d858..a3c31b4e5 100644 --- a/src/main/java/emu/grasscutter/utils/objects/HandbookBody.java +++ b/src/main/java/emu/grasscutter/utils/objects/HandbookBody.java @@ -6,7 +6,8 @@ import lombok.Getter; /** HTTP request object for handbook controls. */ @SuppressWarnings("FieldMayBeFinal") public interface HandbookBody { - @Builder @Getter + @Builder + @Getter class Response { private int status; private String message;