Skip to content

Commit 233d4db

Browse files
committed
Increase code quality according to sonarqube
1 parent 7681f9f commit 233d4db

32 files changed

+332
-251
lines changed

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ repositories {
2020
name = "Terraformers"
2121
url = "https://maven.terraformersmc.com/"
2222
}
23+
24+
// Because me lazy
25+
maven { url 'https://projectlombok.org/edge-releases' }
2326
}
2427

2528
dependencies {
@@ -33,6 +36,9 @@ dependencies {
3336

3437
// Mod Menu
3538
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}"
39+
40+
// Because Iam lazy
41+
compileOnly "org.projectlombok:lombok:${project.lombok_version}"
3642
}
3743

3844
processResources {

gradle.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ maven_group=net.jasper.mod
2020
archives_base_name=playerautoma
2121

2222
# Dependencies
23-
fabric_version=0.119.9+1.21.5
23+
fabric_version=0.119.9+1.21.5
24+
25+
# Other dependencies
26+
lombok_version=0.11.0

src/main/java/net/jasper/mod/automation/Commands.java

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.mojang.brigadier.arguments.IntegerArgumentType;
44
import com.mojang.brigadier.arguments.StringArgumentType;
55
import com.mojang.brigadier.context.CommandContext;
6+
import lombok.AccessLevel;
7+
import lombok.NoArgsConstructor;
68
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
79
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
810
import net.jasper.mod.gui.PlayerAutomaMenuScreen;
@@ -17,70 +19,92 @@
1719

1820
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
1921
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
20-
import static net.jasper.mod.PlayerAutomaClient.LOGGER;
2122
import static net.jasper.mod.PlayerAutomaClient.PLAYERAUTOMA_RECORDING_PATH;
2223

2324
/**
2425
* Class to register all commands associated with playerautoma
2526
*/
27+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2628
public class Commands {
29+
30+
private static final String START = "start";
31+
private static final String STOP = "stop";
32+
private static final String CLEAR = "clear";
33+
private static final String RECORD = "record";
34+
private static final String QUICKSLOT = "quickslot";
35+
private static final String LOAD = "load";
36+
private static final String STORE = "store";
37+
private static final String SLOT = "slot";
38+
private static final String NAME = "name";
39+
private static final String PLAYERAUTOMA = "playerautoma";
40+
private static final String OPEN = "open";
41+
private static final String LOADSCREEN = "loadscreen";
42+
private static final String STORESCREEN = "storescreen";
43+
private static final String MENU = "menu";
44+
private static final String JSON = "json";
45+
private static final String REC = "rec";
46+
private static final String TOGGLEPAUSE = "togglepause";
47+
private static final String REPLAY = "replay";
48+
private static final String LOOP = "loop";
49+
50+
2751
public static void register() {
2852
// Register /record <start|stop|clear>
2953
// /record quickslot <load|store> <slot>
3054
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
3155
dispatcher.register(
32-
literal("record")
33-
.then(literal("start")
56+
literal(RECORD)
57+
.then(literal(START)
3458
.executes(context -> {
3559
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startRecord);
3660
return 1;
3761
})
3862
)
39-
.then(literal("stop")
63+
.then(literal(STOP)
4064
.executes(context -> {
4165
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopRecord);
4266
return 1;
4367
})
4468
)
45-
.then(literal("clear")
69+
.then(literal(CLEAR)
4670
.executes(context -> {
4771
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::clearRecord);
4872
return 1;
4973
})
5074
)
51-
.then(literal("quickslot")
52-
.then(literal("load")
53-
.then(argument("slot", IntegerArgumentType.integer())
54-
.executes(context -> handleQuickSlotCommand(context, "load"))
75+
.then(literal(QUICKSLOT)
76+
.then(literal(LOAD)
77+
.then(argument(SLOT, IntegerArgumentType.integer())
78+
.executes(context -> handleQuickSlotCommand(context, LOAD))
5579
)
5680
)
57-
.then(literal("store")
58-
.then(argument("slot", IntegerArgumentType.integer())
59-
.executes(context -> handleQuickSlotCommand(context, "store"))
81+
.then(literal(STORE)
82+
.then(argument(SLOT, IntegerArgumentType.integer())
83+
.executes(context -> handleQuickSlotCommand(context, STORE))
6084
)
6185
)
62-
.then(literal("clear")
86+
.then(literal(CLEAR)
6387
.executes(context -> { QuickSlots.clearQuickSlot(); return 1; })
64-
.then(argument("slot", IntegerArgumentType.integer())
65-
.executes(context -> handleQuickSlotCommand(context, "clear")))
88+
.then(argument(SLOT, IntegerArgumentType.integer())
89+
.executes(context -> handleQuickSlotCommand(context, CLEAR)))
6690
)
6791
)
68-
.then(literal("store")
69-
.then(argument("name", StringArgumentType.string())
70-
.then(literal("json")
71-
.executes(context -> handleStoreFileCommand(context, "json"))
92+
.then(literal(STORE)
93+
.then(argument(NAME, StringArgumentType.string())
94+
.then(literal(JSON)
95+
.executes(context -> handleStoreFileCommand(context, JSON))
7296
)
73-
.then(literal("rec")
74-
.executes(context -> handleStoreFileCommand(context, "rec"))
97+
.then(literal(REC)
98+
.executes(context -> handleStoreFileCommand(context, REC))
7599
)
76100
)
77101
)
78-
.then(literal("load")
79-
.then(argument("name", StringArgumentType.string())
102+
.then(literal(LOAD)
103+
.then(argument(NAME, StringArgumentType.string())
80104
.suggests((context, builder) -> {
81105
String current = "";
82106
try {
83-
current = StringArgumentType.getString(context, "name");
107+
current = StringArgumentType.getString(context, NAME);
84108
} catch (IllegalArgumentException e) {
85109
// Empty argument therefore keep startsWith as ""
86110
}
@@ -98,7 +122,7 @@ public static void register() {
98122
return builder.buildFuture();
99123
})
100124
.executes(context -> {
101-
String name = StringArgumentType.getString(context, "name");
125+
String name = StringArgumentType.getString(context, NAME);
102126
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.loadRecord(name));
103127
return 1;
104128
})
@@ -109,25 +133,25 @@ public static void register() {
109133

110134
// Register /replay <start|stop|loop|togglepause>
111135
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
112-
dispatcher.register(literal("replay")
113-
.then(literal("start")
136+
dispatcher.register(literal(REPLAY)
137+
.then(literal(START)
114138
.executes(context -> {
115139
PlayerAutomaExceptionHandler.callSafe(() -> PlayerRecorder.startReplay(false));
116140
return 1;
117141
})
118142
)
119-
.then(literal("stop")
143+
.then(literal(STOP)
120144
.executes(context -> {
121145
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::stopReplay);
122146
return 1;
123147
})
124148
)
125-
.then(literal("togglepause")
149+
.then(literal(TOGGLEPAUSE)
126150
.executes(context -> {
127151
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::togglePauseReplay);
128152
return 1;
129153
})
130-
).then(literal("loop")
154+
).then(literal(LOOP)
131155
.executes(context -> {
132156
PlayerAutomaExceptionHandler.callSafe(PlayerRecorder::startReplay);
133157
return 1;
@@ -138,20 +162,17 @@ public static void register() {
138162

139163
// Register /playerautoma open <loadscreen|storescreen|menu>
140164
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
141-
dispatcher.register(literal("playerautoma")
142-
.then(literal("open")
143-
.then(literal("loadscreen").executes(context -> {
144-
LOGGER.info("12341234");
165+
dispatcher.register(literal(PLAYERAUTOMA)
166+
.then(literal(OPEN)
167+
.then(literal(LOADSCREEN).executes(context -> {
145168
MinecraftClient.getInstance().execute(RecordingSelectorScreen::open);
146169
return 1;
147170
})
148-
).then(literal("storescreen").executes(context -> {
149-
LOGGER.info("qwerqwer");
171+
).then(literal(STORESCREEN).executes(context -> {
150172
MinecraftClient.getInstance().execute(RecordingStorerScreen::open);
151173
return 1;
152174
})
153-
).then(literal("menu").executes(context -> {
154-
LOGGER.info("asdfasdf");
175+
).then(literal(MENU).executes(context -> {
155176
MinecraftClient.getInstance().execute(PlayerAutomaMenuScreen::open);
156177
return 1;
157178
}))
@@ -161,9 +182,9 @@ public static void register() {
161182
}
162183

163184
private static int handleStoreFileCommand(CommandContext<FabricClientCommandSource> context, String fileType) {
164-
String fileName = StringArgumentType.getString(context, "name");
185+
String fileName = StringArgumentType.getString(context, NAME);
165186

166-
boolean callNext = RecordingStorerScreen.useJSON.getValue() && fileType.equals("rec") || !RecordingStorerScreen.useJSON.getValue() && fileType.equals("json");
187+
boolean callNext = RecordingStorerScreen.useJSON.getValue() && fileType.equals(REC) || !RecordingStorerScreen.useJSON.getValue() && fileType.equals(JSON);
167188

168189
// Initialize button element to allow calling next
169190
Screen currentScreen = RecordingStorerScreen.open();
@@ -185,19 +206,19 @@ private static int handleStoreFileCommand(CommandContext<FabricClientCommandSour
185206
}
186207

187208
private static int handleQuickSlotCommand(CommandContext<FabricClientCommandSource> context, String command) {
188-
final int slot = IntegerArgumentType.getInteger(context, "slot");
209+
final int slot = IntegerArgumentType.getInteger(context, SLOT);
189210
if (slot < 1 || 9 < slot) {
190211
context.getSource().sendFeedback(Text.literal("Slot Index out of range"));
191212
return 0;
192213
}
193214
switch (command) {
194-
case "load":
215+
case LOAD:
195216
QuickSlots.loadRecording(slot - 1);
196217
break;
197-
case "store":
218+
case STORE:
198219
QuickSlots.storeRecording(slot - 1);
199220
break;
200-
case "clear":
221+
case CLEAR:
201222
QuickSlots.clearQuickSlot(slot - 1);
202223
}
203224
return 1;

src/main/java/net/jasper/mod/automation/InventoryAutomation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.jasper.mod.automation;
22

3+
import lombok.AccessLevel;
4+
import lombok.NoArgsConstructor;
35
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
46
import net.jasper.mod.gui.option.PlayerAutomaOptionsScreen;
57
import net.jasper.mod.util.data.TaskQueue;
@@ -17,10 +19,13 @@
1719
/**
1820
* This class is responsible for re-stacking items in the inventory when replaying
1921
*/
22+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2023
public class InventoryAutomation {
2124
private static boolean doAutomation = true;
2225

2326
public static final TaskQueue inventoryTasks = new TaskQueue(TaskQueue.HIGH_PRIORITY);
27+
28+
@SuppressWarnings("java:S3776")
2429
public static void register() {
2530
inventoryTasks.register("inventoryTasks");
2631
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
@@ -29,7 +34,7 @@ public static void register() {
2934
}
3035

3136
// If is disabled in settings do nothing
32-
if (!PlayerAutomaOptionsScreen.restackBlocksOption.getValue()) {
37+
if (Boolean.FALSE.equals(PlayerAutomaOptionsScreen.restackBlocksOption.getValue())) {
3338
return ActionResult.PASS;
3439
}
3540

src/main/java/net/jasper/mod/automation/MenuPrevention.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.jasper.mod.automation;
22

3+
import lombok.AccessLevel;
4+
import lombok.NoArgsConstructor;
35
import net.jasper.mod.gui.option.PlayerAutomaOptionsScreen;
46
import net.jasper.mod.util.ClientHelpers;
57
import net.minecraft.client.MinecraftClient;
@@ -14,6 +16,7 @@
1416
/**
1517
* Class to track state and implement menu prevention that allows for replays to work in background
1618
*/
19+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1720
public class MenuPrevention {
1821

1922
public static boolean preventToBackground = false;
@@ -55,7 +58,7 @@ public static void toggleBackgroundPrevention() {
5558

5659
preventToBackground = !preventToBackground;
5760

58-
if (PlayerAutomaOptionsScreen.writeStateToActionBarOption.getValue()) {
61+
if (Boolean.TRUE.equals(PlayerAutomaOptionsScreen.writeStateToActionBarOption.getValue())) {
5962
ClientHelpers.writeToActionBar(Text.translatable("playerautoma.messages.menuPreventionToggle").append(preventToBackground ? ScreenTexts.ON : ScreenTexts.OFF));
6063
}
6164

src/main/java/net/jasper/mod/automation/PlayerRecorder.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class PlayerRecorder {
6969
// Click on Enchantment (EnchantmentScreen)
7070
public static final Queue<Integer> lastEnchantmentMade = new ConcurrentLinkedDeque<>();
7171

72+
@SuppressWarnings("java:S3776")
7273
public static void register() {
7374
// Always reset the PlayerRecorder if a Join event happens could be breaking the mod. Therefor leave it out now.
7475
// ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> PlayerRecorder.reset());
@@ -149,7 +150,7 @@ public static void startRecord() {
149150
ClientHelpers.writeToActionBar(Text.translatable("playerautoma.messages.startRecording"));
150151
clearRecord();
151152

152-
if (PlayerAutomaOptionsScreen.saveThumbnailsWithRecording.getValue()) {
153+
if (Boolean.TRUE.equals(PlayerAutomaOptionsScreen.saveThumbnailsWithRecording.getValue())) {
153154
ThumbnailHelpers.create((thumbnail, nativeImage) -> {
154155
if (thumbnail != null) {
155156
record.thumbnail = thumbnail;
@@ -162,7 +163,7 @@ public static void startRecord() {
162163
});
163164
}
164165

165-
if (PlayerAutomaOptionsScreen.resetKeyBindingsOnRecordingOption.getValue()) {
166+
if (Boolean.TRUE.equals(PlayerAutomaOptionsScreen.resetKeyBindingsOnRecordingOption.getValue())) {
166167
KeyBinding.unpressAll();
167168
}
168169

@@ -222,6 +223,7 @@ public static void startReplay(int loopCount) {
222223
* if looped is true and count is negative loop indefinitely
223224
* if looped is true and count is positive loop for that amount
224225
*/
226+
@SuppressWarnings("java:S3776")
225227
private static void startReplay(boolean looped, int loopCount) {
226228
if (state.isRecording() || state.isPausedRecording()) {
227229
ClientHelpers.writeToActionBar(Text.translatable("playerautoma.messages.error.cannotStartReplayWhileRecording"));
@@ -242,7 +244,7 @@ private static void startReplay(boolean looped, int loopCount) {
242244
}
243245

244246
// If menu prevention is activated enable it by default if not already enabled
245-
if (PlayerAutomaOptionsScreen.alwaysPreventMenuOption.getValue() && !MenuPrevention.preventToBackground) {
247+
if (Boolean.TRUE.equals(PlayerAutomaOptionsScreen.alwaysPreventMenuOption.getValue()) && !MenuPrevention.preventToBackground) {
246248
MenuPrevention.toggleBackgroundPrevention();
247249
}
248250

@@ -297,8 +299,8 @@ private static void startReplay(boolean looped, int loopCount) {
297299
}
298300

299301
// Update keys pressed count
300-
for (String translationKey : timesPressed.keySet()) {
301-
((KeyBindingAccessor) keysByID.get(translationKey)).setTimesPressed(timesPressed.get(translationKey));
302+
for (Map.Entry<String, Integer> keyPressed : timesPressed.entrySet()) {
303+
((KeyBindingAccessor) keysByID.get(keyPressed.getKey())).setTimesPressed(keyPressed.getValue());
302304
}
303305

304306
// Toggle modifiers accordingly
@@ -356,9 +358,9 @@ private static void startReplay(boolean looped, int loopCount) {
356358
}
357359

358360
// Click enchantment if possible
359-
if (enchantmentMade != null && client.currentScreen instanceof EnchantmentScreen) {
361+
if (enchantmentMade != null && client.currentScreen instanceof EnchantmentScreen enchantmentScreen) {
360362
try {
361-
client.interactionManager.clickButton(((EnchantmentScreen) client.currentScreen).getScreenHandler().syncId, enchantmentMade);
363+
client.interactionManager.clickButton(enchantmentScreen.getScreenHandler().syncId, enchantmentMade);
362364
} catch (Exception e) {
363365
PlayerAutomaClient.LOGGER.warn("Enchantment Click resulted in unexpected exception", e);
364366
}
@@ -473,7 +475,7 @@ public static void stopReplay() {
473475
KeyBinding.unpressAll();
474476

475477
// If default menu prevention is enabled it needs to be disabled here if enabled
476-
if (PlayerAutomaOptionsScreen.alwaysPreventMenuOption.getValue() && MenuPrevention.preventToBackground) {
478+
if (Boolean.TRUE.equals(PlayerAutomaOptionsScreen.alwaysPreventMenuOption.getValue()) && MenuPrevention.preventToBackground) {
477479
MenuPrevention.toggleBackgroundPrevention();
478480
}
479481

0 commit comments

Comments
 (0)