Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code cleanup #2519

Draft
wants to merge 68 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
6bb349d
fix: make those fields final
brenoepics Jun 9, 2024
da61d30
refactor: make getGoodsLimit one liner
brenoepics Jun 9, 2024
ef58ab3
refactor: replace `size() == 0` with isEmpty()
brenoepics Jun 9, 2024
c3eafba
refactor: remove unnecessary toString call
brenoepics Jun 9, 2024
029ef82
refactor: remove unnecessary semicolons
brenoepics Jun 9, 2024
9fa0f2b
refactor: unwrap redundant code block
brenoepics Jun 9, 2024
7d3abb0
refactor: this can also be final
brenoepics Jun 9, 2024
ca6ddfd
refactor: replace protected with private for final class
brenoepics Jun 9, 2024
0161b4d
refactor: replace protected with private for final class
brenoepics Jun 9, 2024
427a1cf
fix: variable is assigned to itself
brenoepics Jun 9, 2024
9c946e9
chore: remove unused import
brenoepics Jun 9, 2024
8c35c89
refactor: replace Collections.sort with list.sort
brenoepics Jun 9, 2024
870085f
refactor: replace lambda with method reference
brenoepics Jun 9, 2024
3119e0f
refactor: replace lambda with method reference
brenoepics Jun 9, 2024
c7119aa
refactor: simplify map operations
brenoepics Jun 9, 2024
fcd4093
refactor: replace statement with expression lambda
brenoepics Jun 9, 2024
b5d0afd
refactor: we don't need explicit types here
brenoepics Jun 9, 2024
bb82e21
refactor: replace for loop with enhanced loop
brenoepics Jun 9, 2024
6d669bd
refactor: Integer.ParseInt is more appropriate here
brenoepics Jun 9, 2024
7db8b6f
refactor: Integer.ParseInt is more appropriate here
brenoepics Jun 9, 2024
c9b42a6
refactor: replace switch with enhanced switch
brenoepics Jun 9, 2024
77e2462
refactor: fix javadoc issues
brenoepics Jun 9, 2024
6e7418a
refactor: replace concatenation log with parameterized
brenoepics Jun 9, 2024
d5ad077
fix: integer division in floating point
brenoepics Jun 9, 2024
6e3b3b2
refactor: implicit cast from double to float may be lossy
brenoepics Jun 9, 2024
4f1ad7b
perf: Call to 'asList()' with only one argument
brenoepics Jun 9, 2024
e4123f4
perf: class initializer may be static
brenoepics Jun 9, 2024
b773529
perf: `count > 0` can be anyMatch
brenoepics Jun 9, 2024
33d5c52
perf: `findAny().isPresent()` is cleaner here
brenoepics Jun 9, 2024
1424405
perf: fix redundant addAll
brenoepics Jun 9, 2024
1e2ccc9
perf: we can chain append here
brenoepics Jun 9, 2024
aeadf6b
perf: equals "" can be isEmpty here
brenoepics Jun 9, 2024
7ee644c
perf: add Serial annotation here
brenoepics Jun 9, 2024
8f4120f
refactor(lombok): add Getter where possible
brenoepics Jun 9, 2024
dd9f59f
refactor(lombok): add Setter where possible
brenoepics Jun 9, 2024
4b03304
perf: this is just Math.min
brenoepics Jun 9, 2024
f01da54
perf: size cannot be negative
brenoepics Jun 9, 2024
8f8a6a7
perf: simplify optional chain
brenoepics Jun 9, 2024
85ca877
perf: remove redundant collection operation
brenoepics Jun 9, 2024
3d00abc
perf: we can just equalsIgnoreCase
brenoepics Jun 9, 2024
45bf5ed
perf: stream api can be simplified here
brenoepics Jun 9, 2024
809009f
refactor: null check is not necessary, we have instanceof
brenoepics Jun 9, 2024
db28038
chore: fix codestyle indentation
brenoepics Jun 9, 2024
3b68645
refactor: number comparison
brenoepics Jun 9, 2024
d4ce7aa
refactor: fix nullable params
brenoepics Jun 9, 2024
8c53f2b
Format code [skip actions]
github-actions[bot] Jun 9, 2024
bfc2c7c
refactor: fix lombok
brenoepics Jun 9, 2024
2bb2da4
Format code [skip actions]
github-actions[bot] Jun 9, 2024
dbb4f85
ci: bump actions
brenoepics Jun 9, 2024
c313939
Merge branch 'development' of https://github.com/brenoepics/Grasscutt…
brenoepics Jun 9, 2024
1234d18
ci: Create dependabot.yml
brenoepics Jun 9, 2024
62d53f3
chore(deps): bump com.google.protobuf:protoc from 3.18.1 to 4.27.1
dependabot[bot] Jun 9, 2024
52b7ca6
chore(deps): bump com.fasterxml.jackson:jackson-bom
dependabot[bot] Jun 9, 2024
bbec09d
chore(deps): bump org.jline:jline-terminal-jna from 3.21.0 to 3.26.1
dependabot[bot] Jun 9, 2024
ff2aeac
chore(deps): bump org.junit.jupiter:junit-jupiter-engine
dependabot[bot] Jun 9, 2024
63fbe66
security: add secure mode and padding scheme
brenoepics Jun 9, 2024
75d8ed0
Merge branch 'development' of https://github.com/brenoepics/Grasscutt…
brenoepics Jun 9, 2024
0736956
Format code [skip actions]
github-actions[bot] Jun 9, 2024
fa283d8
Merge pull request #1 from brenoepics/dependabot/gradle/com.google.pr…
brenoepics Jun 9, 2024
22c7f3c
Merge pull request #2 from brenoepics/dependabot/gradle/com.fasterxml…
brenoepics Jun 9, 2024
a05d6cd
Merge pull request #3 from brenoepics/dependabot/gradle/org.jline-jli…
brenoepics Jun 9, 2024
cd48328
Merge pull request #4 from brenoepics/dependabot/gradle/org.junit.jup…
brenoepics Jun 9, 2024
011549f
chore(deps): bump org.java-websocket:Java-WebSocket from 1.5.2 to 1.5.6
dependabot[bot] Jun 10, 2024
0819435
chore(deps): bump ch.qos.logback:logback-core from 1.4.7 to 1.5.6
dependabot[bot] Jun 10, 2024
a0922ff
Merge pull request #9 from brenoepics/dependabot/gradle/ch.qos.logbac…
brenoepics Jun 10, 2024
c77aa52
Merge pull request #7 from brenoepics/dependabot/gradle/org.java-webs…
brenoepics Jun 10, 2024
ab5824b
chore(deps): bump org.slf4j:slf4j-api from 2.0.7 to 2.0.13
dependabot[bot] Jun 10, 2024
0e7e165
Merge pull request #8 from brenoepics/dependabot/gradle/org.slf4j-slf…
brenoepics Jun 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "gradle" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
Expand All @@ -44,7 +44,7 @@ jobs:
- name: Run Gradle
run: ./gradlew -PskipHandbook=1 && ./gradlew jar -PskipHandbook=1
- name: Upload build
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Grasscutter
path: grasscutter-*.jar
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ dependencies {
implementation group: 'it.unimi.dsi', name: 'fastutil', version: '8.5.8'

// Logging libraries.
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.7'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.4.7'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.13'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.5.6'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.4.7'

// Line reading libraries.
implementation group: 'org.jline', name: 'jline', version: '3.21.0'
implementation group: 'org.jline', name: 'jline-terminal-jna', version: '3.21.0'
implementation group: 'org.jline', name: 'jline-terminal-jna', version: '3.26.1'
implementation group: 'net.java.dev.jna', name: 'jna', version: '5.10.0'

// Java Netty for networking.
Expand All @@ -96,7 +96,7 @@ dependencies {
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.19.6'

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.2'
implementation platform('com.fasterxml.jackson:jackson-bom:2.14.0')
implementation platform('com.fasterxml.jackson:jackson-bom:2.17.1')
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.2'

// Reflections libraries.
Expand All @@ -121,7 +121,7 @@ dependencies {
// Java HTTP server library.
implementation group: 'io.javalin', name: 'javalin', version: '5.5.0'
// Java WebSocket server & client library.
implementation group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.2'
implementation group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.6'

// Google Protocol Buffer definitions.
// These are the raw '.proto' files.
Expand All @@ -130,7 +130,7 @@ dependencies {
// Testing libraries.
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.2'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.8.2'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.2'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.2'

// HTTP client library for testing.
testImplementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'
Expand Down Expand Up @@ -261,7 +261,7 @@ clean {
protobuf {
protoc {
// The artifact spec for the Protobuf Compiler
artifact = 'com.google.protobuf:protoc:3.18.1'
artifact = 'com.google.protobuf:protoc:4.27.1'
}

generatedFilesBaseDir = "$projectDir/src/generated/"
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/emu/grasscutter/DebugConstants.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package emu.grasscutter;

public final class DebugConstants {
public static boolean LOG_ABILITIES = false;
public static boolean LOG_LUA_SCRIPTS = false;
public static boolean LOG_QUEST_START = false;
public static boolean LOG_MISSING_ABILITIES = false;
public static boolean LOG_MISSING_LUA_SCRIPTS = false;
public static boolean LOG_MISSING_ABILITY_HANDLERS = false;
public static final boolean LOG_ABILITIES = false;
public static final boolean LOG_LUA_SCRIPTS = false;
public static final boolean LOG_QUEST_START = false;
public static final boolean LOG_MISSING_ABILITIES = false;
public static final boolean LOG_MISSING_LUA_SCRIPTS = false;
public static final boolean LOG_MISSING_ABILITY_HANDLERS = false;

/**
* WARNING: THIS IS A DANGEROUS SETTING. DO NOT ENABLE UNLESS YOU KNOW WHAT YOU ARE DOING. This
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emu/grasscutter/GameConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.Arrays;

public final class GameConstants {
public static String VERSION = "4.0.0";
public static int[] VERSION_PARTS = {4, 0, 0};
public static final String VERSION = "4.0.0";
public static final int[] VERSION_PARTS = {4, 0, 0};
public static boolean DEBUG = false;

public static final int DEFAULT_TEAMS = 4;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emu/grasscutter/Grasscutter.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static LineReader getConsole() {
public static void updateDayOfWeek() {
Calendar calendar = Calendar.getInstance();
Grasscutter.currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
logger.debug("Set day of week to " + currentDayOfWeek);
logger.debug("Set day of week to {}", currentDayOfWeek);
}

public static void startConsole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public LoginResultJson authenticate(AuthenticationRequest request) {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey private_key = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");

cipher.init(Cipher.DECRYPT_MODE, private_key);

Expand Down
21 changes: 10 additions & 11 deletions src/main/java/emu/grasscutter/command/CommandMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static int getUidFromString(String input) {
* @return Instance chaining.
*/
public CommandMap registerCommand(String label, CommandHandler command) {
Grasscutter.getLogger().trace("Registered command: " + label);
Grasscutter.getLogger().trace("Registered command: {}", label);
label = label.toLowerCase();

// Get command data.
Expand All @@ -76,7 +76,7 @@ public CommandMap registerCommand(String label, CommandHandler command) {
* @return Instance chaining.
*/
public CommandMap unregisterCommand(String label) {
Grasscutter.getLogger().trace("Un-registered command: " + label);
Grasscutter.getLogger().trace("Un-registered command: {}", label);

CommandHandler handler = this.commands.get(label);
if (handler == null) return this;
Expand Down Expand Up @@ -231,14 +231,12 @@ public void invoke(Player player, Player targetPlayer, String rawMessage) {
if (player != null) {
Grasscutter.getLogger()
.info(
"Command used by ["
+ player.getAccount().getUsername()
+ " (Player UID: "
+ player.getUid()
+ ")]: "
+ rawMessage);
"Command used by [{} (Player UID: {})]: {}",
player.getAccount().getUsername(),
player.getUid(),
rawMessage);
} else {
Grasscutter.getLogger().info("Command used by server console: " + rawMessage);
Grasscutter.getLogger().info("Command used by server console: {}", rawMessage);
}
}

Expand Down Expand Up @@ -350,11 +348,12 @@ private void scan() {
this.registerCommand(cmdData.label(), (CommandHandler) object);
else
Grasscutter.getLogger()
.error("Class " + annotated.getName() + " is not a CommandHandler!");
.error("Class {} is not a CommandHandler!", annotated.getName());
} catch (Exception exception) {
Grasscutter.getLogger()
.error(
"Failed to register command handler for " + annotated.getSimpleName(),
"Failed to register command handler for {}",
annotated.getSimpleName(),
exception);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static void revokeAll(Player sender, Player targetPlayer, Achievements a

@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
if (args.isEmpty()) {
this.sendUsageMessage(sender);
return;
}
Expand All @@ -88,7 +88,7 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {

private void grant(
Player sender, Player targetPlayer, Achievements achievements, List<String> args) {
if (args.size() < 1) {
if (args.isEmpty()) {
this.sendUsageMessage(sender);
}

Expand All @@ -109,7 +109,7 @@ private void grant(

private void revoke(
Player sender, Player targetPlayer, Achievements achievements, List<String> args) {
if (args.size() < 1) {
if (args.isEmpty()) {
this.sendUsageMessage(sender);
}

Expand All @@ -136,20 +136,19 @@ private void progress(

parseInt(args.remove(0))
.ifPresentOrElse(
integer -> {
parseInt(args.remove(0))
.ifPresentOrElse(
progress -> {
var ret = achievements.progress(integer, progress);
switch (ret.getRet()) {
case SUCCESS -> sendSuccessMessage(
sender, "progress", targetPlayer.getNickname(), integer, progress);
case ACHIEVEMENT_NOT_FOUND -> CommandHandler.sendTranslatedMessage(
sender, ret.getRet().getKey());
}
},
() -> this.sendUsageMessage(sender));
},
integer ->
parseInt(args.remove(0))
.ifPresentOrElse(
progress -> {
var ret = achievements.progress(integer, progress);
switch (ret.getRet()) {
case SUCCESS -> sendSuccessMessage(
sender, "progress", targetPlayer.getNickname(), integer, progress);
case ACHIEVEMENT_NOT_FOUND -> CommandHandler.sendTranslatedMessage(
sender, ret.getRet().getKey());
}
},
() -> this.sendUsageMessage(sender)),
() -> this.sendUsageMessage(sender));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class AnnounceCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
var manager = Grasscutter.getGameServer().getAnnouncementSystem();
if (args.size() < 1) {
if (args.isEmpty()) {
sendUsageMessage(sender);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
// Extract any tagged int arguments (e.g. "lv90", "x100", "r5")
parseIntParameters(args, param, intCommandHandlers);

if (args.size() < 1) {
if (args.isEmpty()) {
sendUsageMessage(sender);
return;
}
Expand Down Expand Up @@ -93,9 +93,10 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
}
}

@Setter
private static class ClearItemParameters {
@Setter public int lvl = 1;
@Setter public int refinement = 1;
@Setter public int rank = 4;
public int lvl = 1;
public int refinement = 1;
public int rank = 4;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class EnterDungeonCommand implements CommandHandler {

@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
if (args.isEmpty()) {
sendUsageMessage(sender);
return;
}
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/emu/grasscutter/command/commands/GiveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
import static emu.grasscutter.GameConstants.*;
import static emu.grasscutter.command.CommandHelpers.*;

import emu.grasscutter.command.*;
import emu.grasscutter.data.*;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.GameDepot;
import emu.grasscutter.data.excels.ItemData;
import emu.grasscutter.data.excels.avatar.AvatarData;
import emu.grasscutter.data.excels.reliquary.*;
import emu.grasscutter.data.excels.reliquary.ReliquaryAffixData;
import emu.grasscutter.data.excels.reliquary.ReliquaryMainPropData;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.inventory.*;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.*;
import java.util.*;
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.FightProperty;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
import lombok.Setter;
Expand Down Expand Up @@ -279,7 +286,7 @@ private GiveItemParameters parseArgs(Player sender, List<String> args)
parseIntParameters(args, param, intCommandHandlers);

// At this point, first remaining argument MUST be itemId/avatarId
if (args.size() < 1) {
if (args.isEmpty()) {
sendUsageMessage(sender); // Reachable if someone does `/give lv90` or similar
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -362,7 +369,7 @@ else if ((param.id > 1000) && (param.id < 1100))

@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) { // *No args*
if (args.isEmpty()) { // *No args*
sendUsageMessage(sender);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {

CommandHandler.sendMessage(sender, translate(sender, "commands.group.refreshed", groupId));
}
default -> {
sendUsageMessage(sender);
}
default -> sendUsageMessage(sender);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
Map<Integer, Player> playersMap = Grasscutter.getGameServer().getPlayers();
boolean needUID = false;

if (args.size() > 0) {
if (!args.isEmpty()) {
needUID = args.get(0).equals("uid");
}

CommandHandler.sendMessage(
sender, translate(sender, "commands.list.success", playersMap.size()));

if (playersMap.size() != 0) {
if (!playersMap.isEmpty()) {
StringBuilder playerSet = new StringBuilder();
boolean finalNeedUID = needUID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class ResetConstCommand implements CommandHandler {

@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() > 0 && args.get(0).equalsIgnoreCase("all")) {
if (!args.isEmpty() && args.get(0).equalsIgnoreCase("all")) {
targetPlayer.getAvatars().forEach(this::resetConstellation);
CommandHandler.sendMessage(sender, translate(sender, "commands.resetConst.reset_all"));
} else {
Expand Down
Loading
Loading