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 51 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
  •  
  •  
  •  
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public final class SendMailCommand implements CommandHandler {
// the command system (again). For now this will do

// Key = User that is constructing the mail.
private static final HashMap<Integer, MailBuilder> mailBeingConstructed =
new HashMap<Integer, MailBuilder>();
private static final HashMap<Integer, MailBuilder> mailBeingConstructed = new HashMap<>();

// Yes this is awful and I hate it.
@Override
Expand Down Expand Up @@ -69,7 +68,7 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
} else {
MailBuilder mailBuilder = mailBeingConstructed.get(senderId);

if (args.size() >= 1) {
if (!args.isEmpty()) {
switch (args.get(0).toLowerCase()) {
case "stop" -> {
mailBeingConstructed.remove(senderId);
Expand Down Expand Up @@ -106,14 +105,12 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
getConstructionArgs(mailBuilder.constructionStage, sender)));
}
}
case "help" -> {
CommandHandler.sendMessage(
sender,
translate(
sender,
"commands.sendMail.please_use",
getConstructionArgs(mailBuilder.constructionStage, sender)));
}
case "help" -> CommandHandler.sendMessage(
sender,
translate(
sender,
"commands.sendMail.please_use",
getConstructionArgs(mailBuilder.constructionStage, sender)));
default -> {
switch (mailBuilder.constructionStage) {
case 0 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class SendMessageCommand implements CommandHandler {

@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() == 0) {
if (args.isEmpty()) {
sendUsageMessage(sender);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public final class SetConstCommand 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
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ enum PseudoProp {
}

static class Prop {
String name;
PlayerProperty prop;
PseudoProp pseudoProp;
final String name;
final PlayerProperty prop;
final PseudoProp pseudoProp;

public Prop(PlayerProperty prop) {
this(prop.toString(), prop, PseudoProp.NONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ private void unlockAllSceneTags(Player targetPlayer) {
// Only remove for big world as some other scenes only have defaults
.filter(sceneTag -> sceneTag.getSceneId() == 3)
.forEach(
sceneTag -> {
targetPlayer.getSceneTags().get(sceneTag.getSceneId()).remove(sceneTag.getId());
});
sceneTag ->
targetPlayer.getSceneTags().get(sceneTag.getSceneId()).remove(sceneTag.getId()));

this.setSceneTags(targetPlayer);
}
Expand Down
Loading