Format code [skip actions]

This commit is contained in:
github-actions 2023-05-30 19:40:39 +00:00
parent 36a35c11aa
commit c69a889e66
13 changed files with 145 additions and 146 deletions

View File

@ -1,5 +1,8 @@
package emu.grasscutter;
import static emu.grasscutter.config.Configuration.SERVER;
import static emu.grasscutter.utils.lang.Language.translate;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import emu.grasscutter.auth.AuthenticationSystem;
@ -31,6 +34,16 @@ import emu.grasscutter.utils.StartupArguments;
import emu.grasscutter.utils.Utils;
import emu.grasscutter.utils.lang.Language;
import io.netty.util.concurrent.FastThreadLocalThread;
import java.io.File;
import java.io.FileWriter;
import java.io.IOError;
import java.io.IOException;
import java.util.Calendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
import org.jline.reader.EndOfFileException;
@ -42,20 +55,6 @@ import org.jline.terminal.TerminalBuilder;
import org.reflections.Reflections;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOError;
import java.io.IOException;
import java.util.Calendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static emu.grasscutter.config.Configuration.SERVER;
import static emu.grasscutter.utils.lang.Language.translate;
public final class Grasscutter {
public static final File configFile = new File("./config.json");
public static final Reflections reflector = new Reflections("emu.grasscutter");
@ -81,12 +80,16 @@ public final class Grasscutter {
private static LineReader consoleLineReader = null;
@Getter private static final ExecutorService threadPool = new ThreadPoolExecutor(
6, 6, 60, TimeUnit.SECONDS,
new LinkedBlockingDeque<>(),
FastThreadLocalThread::new,
new ThreadPoolExecutor.AbortPolicy()
);
@Getter
private static final ExecutorService threadPool =
new ThreadPoolExecutor(
6,
6,
60,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(),
FastThreadLocalThread::new,
new ThreadPoolExecutor.AbortPolicy());
static {
// Declare logback configuration.

View File

@ -1,5 +1,7 @@
package emu.grasscutter.database;
import static com.mongodb.client.model.Filters.eq;
import dev.morphia.query.FindOptions;
import dev.morphia.query.Sort;
import dev.morphia.query.experimental.filters.Filters;
@ -21,20 +23,20 @@ import emu.grasscutter.game.quest.GameMainQuest;
import emu.grasscutter.game.world.SceneGroupInstance;
import emu.grasscutter.utils.objects.Returnable;
import io.netty.util.concurrent.FastThreadLocalThread;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Stream;
import static com.mongodb.client.model.Filters.eq;
public final class DatabaseHelper {
private static final ExecutorService eventExecutor = new ThreadPoolExecutor(
6, 6, 60, TimeUnit.SECONDS,
new LinkedBlockingDeque<>(),
FastThreadLocalThread::new,
new ThreadPoolExecutor.AbortPolicy()
);
private static final ExecutorService eventExecutor =
new ThreadPoolExecutor(
6,
6,
60,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(),
FastThreadLocalThread::new,
new ThreadPoolExecutor.AbortPolicy());
/**
* Saves an object on the account datastore.
@ -42,8 +44,7 @@ public final class DatabaseHelper {
* @param object The object to save.
*/
public static void saveAccountAsync(Object object) {
DatabaseHelper.eventExecutor.submit(() ->
DatabaseManager.getAccountDatastore().save(object));
DatabaseHelper.eventExecutor.submit(() -> DatabaseManager.getAccountDatastore().save(object));
}
/**
@ -52,13 +53,11 @@ public final class DatabaseHelper {
* @param object The object to save.
*/
public static void saveGameAsync(Object object) {
DatabaseHelper.eventExecutor.submit(() ->
DatabaseHelper.saveGameAsync(object));
DatabaseHelper.eventExecutor.submit(() -> DatabaseHelper.saveGameAsync(object));
}
/**
* Runs a runnable on the event executor.
* Should be limited to database-related operations.
* Runs a runnable on the event executor. Should be limited to database-related operations.
*
* @param runnable The runnable to run.
*/
@ -76,13 +75,14 @@ public final class DatabaseHelper {
var future = new CompletableFuture<T>();
// Run the task on the event executor.
DatabaseHelper.eventExecutor.submit(() -> {
try {
future.complete(task.invoke());
} catch (Exception e) {
future.completeExceptionally(e);
}
});
DatabaseHelper.eventExecutor.submit(
() -> {
try {
future.complete(task.invoke());
} catch (Exception e) {
future.completeExceptionally(e);
}
});
return future;
}
@ -221,33 +221,45 @@ public final class DatabaseHelper {
}
int uid = player.getUid();
DatabaseHelper.asyncOperation(() -> {
// Delete data from collections
DatabaseManager.getGameDatabase().getCollection("achievements").deleteMany(eq("uid", uid));
DatabaseManager.getGameDatabase().getCollection("activities").deleteMany(eq("uid", uid));
DatabaseManager.getGameDatabase().getCollection("homes").deleteMany(eq("ownerUid", uid));
DatabaseManager.getGameDatabase().getCollection("mail").deleteMany(eq("ownerUid", uid));
DatabaseManager.getGameDatabase().getCollection("avatars").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("gachas").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("items").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("quests").deleteMany(eq("ownerUid", uid));
DatabaseManager.getGameDatabase().getCollection("battlepass").deleteMany(eq("ownerUid", uid));
DatabaseHelper.asyncOperation(
() -> {
// Delete data from collections
DatabaseManager.getGameDatabase()
.getCollection("achievements")
.deleteMany(eq("uid", uid));
DatabaseManager.getGameDatabase().getCollection("activities").deleteMany(eq("uid", uid));
DatabaseManager.getGameDatabase().getCollection("homes").deleteMany(eq("ownerUid", uid));
DatabaseManager.getGameDatabase().getCollection("mail").deleteMany(eq("ownerUid", uid));
DatabaseManager.getGameDatabase().getCollection("avatars").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("gachas").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("items").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("quests").deleteMany(eq("ownerUid", uid));
DatabaseManager.getGameDatabase()
.getCollection("battlepass")
.deleteMany(eq("ownerUid", uid));
// Delete friendships.
// Here, we need to make sure to not only delete the deleted account's friendships,
// but also all friendship entries for that account's friends.
DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("friendId", uid));
// Delete friendships.
// Here, we need to make sure to not only delete the deleted account's friendships,
// but also all friendship entries for that account's friends.
DatabaseManager.getGameDatabase()
.getCollection("friendships")
.deleteMany(eq("ownerId", uid));
DatabaseManager.getGameDatabase()
.getCollection("friendships")
.deleteMany(eq("friendId", uid));
// Delete the player last.
DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("id", uid)).delete();
// Delete the player last.
DatabaseManager.getGameDatastore()
.find(Player.class)
.filter(Filters.eq("id", uid))
.delete();
// Finally, delete the account itself.
DatabaseManager.getAccountDatastore()
.find(Account.class)
.filter(Filters.eq("id", target.getId()))
.delete();
});
// Finally, delete the account itself.
DatabaseManager.getAccountDatastore()
.find(Account.class)
.filter(Filters.eq("id", target.getId()))
.delete();
});
}
public static <T> Stream<T> getByGameClass(Class<T> classType) {
@ -349,10 +361,10 @@ public final class DatabaseHelper {
*/
public static List<Avatar> getAvatars(Player player) {
return DatabaseManager.getGameDatastore()
.find(Avatar.class)
.filter(Filters.eq("ownerId", player.getUid()))
.stream()
.toList();
.find(Avatar.class)
.filter(Filters.eq("ownerId", player.getUid()))
.stream()
.toList();
}
public static void saveItem(GameItem item) {
@ -360,8 +372,7 @@ public final class DatabaseHelper {
}
public static void deleteItem(GameItem item) {
DatabaseHelper.asyncOperation(() ->
DatabaseManager.getGameDatastore().delete(item));
DatabaseHelper.asyncOperation(() -> DatabaseManager.getGameDatastore().delete(item));
}
/**
@ -372,10 +383,10 @@ public final class DatabaseHelper {
*/
public static List<GameItem> getInventoryItems(Player player) {
return DatabaseManager.getGameDatastore()
.find(GameItem.class)
.filter(Filters.eq("ownerId", player.getUid()))
.stream()
.toList();
.find(GameItem.class)
.filter(Filters.eq("ownerId", player.getUid()))
.stream()
.toList();
}
public static List<Friendship> getFriends(Player player) {
@ -399,8 +410,7 @@ public final class DatabaseHelper {
}
public static void deleteFriendship(Friendship friendship) {
DatabaseHelper.asyncOperation(() ->
DatabaseManager.getGameDatastore().delete(friendship));
DatabaseHelper.asyncOperation(() -> DatabaseManager.getGameDatastore().delete(friendship));
}
public static Friendship getReverseFriendship(Friendship friendship) {
@ -460,8 +470,7 @@ public final class DatabaseHelper {
}
public static void deleteMail(Mail mail) {
DatabaseHelper.asyncOperation(() ->
DatabaseManager.getGameDatastore().delete(mail));
DatabaseHelper.asyncOperation(() -> DatabaseManager.getGameDatastore().delete(mail));
}
public static List<GameMainQuest> getAllQuests(Player player) {
@ -477,8 +486,7 @@ public final class DatabaseHelper {
}
public static void deleteQuest(GameMainQuest quest) {
DatabaseHelper.asyncOperation(() ->
DatabaseManager.getGameDatastore().delete(quest));
DatabaseHelper.asyncOperation(() -> DatabaseManager.getGameDatastore().delete(quest));
}
public static GameHome getHomeByUid(int id) {

View File

@ -1,5 +1,7 @@
package emu.grasscutter.database;
import static emu.grasscutter.config.Configuration.DATABASE;
import com.mongodb.MongoCommandException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
@ -16,8 +18,6 @@ import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.game.Account;
import org.reflections.Reflections;
import static emu.grasscutter.config.Configuration.DATABASE;
public final class DatabaseManager {
private static Datastore gameDatastore;
private static Datastore dispatchDatastore;

View File

@ -22,12 +22,13 @@ public abstract class AbilityActionHandler {
default -> throw new RuntimeException("Unknown target type: " + target);
case "Self" -> entity;
case "Team" -> ability.getPlayerOwner().getTeamManager().getEntity();
case "OriginOwner" -> ability.getPlayerOwner()
.getTeamManager().getCurrentAvatarEntity();
case "OriginOwner" -> ability.getPlayerOwner().getTeamManager().getCurrentAvatarEntity();
case "Owner" -> ability.getOwner();
case "Applier" -> entity; // TODO: Validate.
case "CurLocalAvatar" -> ability.getPlayerOwner()
.getTeamManager().getCurrentAvatarEntity(); // TODO: Validate.
case "CurLocalAvatar" -> ability
.getPlayerOwner()
.getTeamManager()
.getCurrentAvatarEntity(); // TODO: Validate.
case "CasterOriginOwner" -> null; // TODO: Figure out.
};
}

View File

@ -9,7 +9,8 @@ import emu.grasscutter.game.entity.GameEntity;
@AbilityAction(AbilityModifierAction.Type.CopyGlobalValue)
public final class ActionCopyGlobalValue extends AbilityActionHandler {
@Override
public boolean execute(Ability ability, AbilityModifierAction action, ByteString abilityData, GameEntity entity) {
public boolean execute(
Ability ability, AbilityModifierAction action, ByteString abilityData, GameEntity entity) {
// Get the entities referred to.
var source = this.getTarget(ability, entity, action.srcTarget);
var destination = this.getTarget(ability, entity, action.dstTarget);

View File

@ -1,5 +1,7 @@
package emu.grasscutter.game.inventory;
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
@ -20,14 +22,11 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
private final Long2ObjectMap<GameItem> store;
private final Int2ObjectMap<InventoryTab> inventoryTypes;

View File

@ -41,15 +41,14 @@ import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.packet.send.*;
import emu.grasscutter.utils.objects.KahnsSort;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import lombok.Getter;
import lombok.Setter;
import lombok.val;
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
import lombok.val;
public final class Scene {
@Getter private final World world;

View File

@ -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;
@ -11,12 +13,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;
@ -27,8 +23,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();

View File

@ -1,17 +1,13 @@
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;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.server.event.dispatch.ServerMessageEvent;
import emu.grasscutter.utils.Crypto;
import lombok.Getter;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
@ -20,8 +16,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.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
/* Internal communications server. */
public final class DispatchServer extends WebSocketServer implements IDispatcher {

View File

@ -1,5 +1,7 @@
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;
@ -7,9 +9,6 @@ import com.google.gson.JsonObject;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.JsonAdapters.ByteArrayAdapter;
import emu.grasscutter.utils.objects.JObject;
import org.java_websocket.WebSocket;
import org.slf4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.LinkedList;
@ -20,8 +19,8 @@ import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import org.java_websocket.WebSocket;
import org.slf4j.Logger;
public interface IDispatcher {
Gson JSON =
@ -255,17 +254,17 @@ public interface IDispatcher {
* @param binary Whether the data is binary.
*/
default void sendServerMessage(byte[] data, boolean binary) {
var message = new JObject()
.add("binary", binary)
.add("data", Base64.getEncoder().encodeToString(data))
.gson();
var message =
new JObject()
.add("binary", binary)
.add("data", Base64.getEncoder().encodeToString(data))
.gson();
this.sendMessage(PacketIds.ServerMessageNotify, message);
}
/**
* Sends a server message to the client.
* The data is sent as a string.
* Sends a server message to the client. The data is sent as a string.
*
* @param data The data to send.
*/
@ -274,8 +273,7 @@ public interface IDispatcher {
}
/**
* Sends a server message to the client.
* The data is sent as a byte array.
* Sends a server message to the client. The data is sent as a byte array.
*
* @param data The data to send.
*/
@ -284,8 +282,7 @@ public interface IDispatcher {
}
/**
* Sends a server message to the client.
* The data is sent as a JSON object.
* Sends a server message to the client. The data is sent as a JSON object.
*
* @param data The data to send.
*/

View File

@ -4,12 +4,11 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.server.dispatch.IDispatcher;
import emu.grasscutter.server.event.Event;
import java.util.Base64;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.java_websocket.WebSocket;
import java.util.Base64;
@Getter
@RequiredArgsConstructor
public final class ServerMessageEvent extends Event {
@ -22,8 +21,7 @@ public final class ServerMessageEvent extends Event {
public static void invoke(WebSocket client, JsonElement object) {
var message = IDispatcher.decode(object);
var isBinary = message.get("binary").getAsBoolean();
var data = Base64.getDecoder().decode(
message.get("data").getAsString());
var data = Base64.getDecoder().decode(message.get("data").getAsString());
// Create the event and invoke it.
new ServerMessageEvent(client, isBinary, data).call();
@ -46,16 +44,13 @@ public final class ServerMessageEvent extends Event {
* @return The message as a JSON object.
*/
public JsonObject asJson() {
return IDispatcher.JSON.fromJson(
this.asString(), JsonObject.class);
return IDispatcher.JSON.fromJson(this.asString(), JsonObject.class);
}
/**
* @return The message as a JSON object.
* The type is specified.
* @return The message as a JSON object. The type is specified.
*/
public <T> T asJson(Class<T> type) {
return IDispatcher.JSON.fromJson(
this.asString(), type);
return IDispatcher.JSON.fromJson(this.asString(), type);
}
}

View File

@ -1,5 +1,7 @@
package emu.grasscutter.server.http.documentation;
import static emu.grasscutter.config.Configuration.HANDBOOK;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
import emu.grasscutter.server.http.Router;
@ -11,8 +13,6 @@ import io.javalin.Javalin;
import io.javalin.http.ContentType;
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;
@ -102,8 +102,7 @@ public final class HandbookHandler implements Router {
} else {
ctx.status(result.getStatus())
.result(result.getBody())
.contentType(
result.isHtml() ? ContentType.TEXT_HTML : ContentType.TEXT_PLAIN);
.contentType(result.isHtml() ? ContentType.TEXT_HTML : ContentType.TEXT_PLAIN);
}
}
}

View File

@ -1,5 +1,7 @@
package emu.grasscutter.utils;
import static emu.grasscutter.config.Configuration.*;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import emu.grasscutter.BuildConfig;
@ -7,13 +9,10 @@ import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.net.packet.PacketOpcodesUtils;
import emu.grasscutter.tools.Dumpers;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import static emu.grasscutter.config.Configuration.*;
import org.slf4j.LoggerFactory;
/** A parser for start-up arguments. */
public interface StartupArguments {