Skip to content

Commit 12c6c9a

Browse files
authored
GH-1213 Remove usage of panda-utilities and panda expressible. (#1213)
* Remove usage of panda-utilities and panda expressible. * Simplify game mode parsing by removing Optional usage and directly handling exceptions * Refactor text capitalization method to use streams for improved readability * revert * Follow @noyzys review. * Fix.
1 parent aed16f7 commit 12c6c9a

File tree

12 files changed

+141
-66
lines changed

12 files changed

+141
-66
lines changed

eternalcore-api/src/main/java/com/eternalcode/core/feature/jail/JailService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import java.time.Duration;
44
import java.util.Collection;
55
import java.util.UUID;
6-
import javax.annotation.Nullable;
76
import org.bukkit.Location;
87

98
import java.util.Optional;
109
import org.bukkit.command.CommandSender;
1110
import org.bukkit.entity.Player;
1211
import org.jetbrains.annotations.Blocking;
12+
import org.jetbrains.annotations.Nullable;
1313

1414
public interface JailService {
1515

eternalcore-core/build.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,9 @@ eternalShadow {
9292
)
9393

9494
// common libraries
95-
library("org.panda-lang:expressible:${Versions.EXPRESSIBLE}")
96-
library("org.panda-lang:panda-utilities:${Versions.PANDA_UTILITIES}")
95+
9796
library("commons-io:commons-io:${Versions.APACHE_COMMONS}")
9897
libraryRelocate(
99-
"panda.std",
100-
"panda.utilities",
10198
"org.apache.commons.io",
10299
)
103100

eternalcore-core/src/main/java/com/eternalcode/core/feature/fullserverbypass/FullServerBypassController.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import org.bukkit.event.EventHandler;
1515
import org.bukkit.event.Listener;
1616
import org.bukkit.event.player.PlayerLoginEvent;
17-
import panda.utilities.text.Joiner;
1817

18+
import java.util.List;
1919
import java.util.Optional;
2020

2121
@PermissionDocs(
@@ -27,6 +27,7 @@
2727
class FullServerBypassController implements Listener {
2828

2929
static final String SLOT_BYPASS = "eternalcore.slot.bypass";
30+
private static final String LINE_SEPARATOR = "\n";
3031

3132
private final TranslationManager translationManager;
3233
private final UserManager userManager;
@@ -41,35 +42,30 @@ class FullServerBypassController implements Listener {
4142

4243
@EventHandler
4344
void onLogin(PlayerLoginEvent event) {
44-
if (event.getResult() == PlayerLoginEvent.Result.KICK_FULL) {
45-
Player player = event.getPlayer();
45+
if (event.getResult() != PlayerLoginEvent.Result.KICK_FULL) {
46+
return;
47+
}
4648

47-
if (player.hasPermission(SLOT_BYPASS)) {
48-
event.allow();
49+
Player player = event.getPlayer();
4950

50-
return;
51-
}
51+
if (player.hasPermission(SLOT_BYPASS)) {
52+
event.allow();
53+
return;
54+
}
5255

53-
String serverFullMessage = this.getServerFullMessage(player);
54-
Component serverFullMessageComponent = this.miniMessage.deserialize(serverFullMessage);
56+
String serverFullMessage = this.getServerFullMessage(player);
57+
Component serverFullMessageComponent = this.miniMessage.deserialize(serverFullMessage);
5558

56-
event.disallow(PlayerLoginEvent.Result.KICK_FULL, AdventureUtil.SECTION_SERIALIZER.serialize(serverFullMessageComponent));
57-
}
59+
event.disallow(PlayerLoginEvent.Result.KICK_FULL, AdventureUtil.SECTION_SERIALIZER.serialize(serverFullMessageComponent));
5860
}
5961

6062
private String getServerFullMessage(Player player) {
6163
Optional<User> userOption = this.userManager.getUser(player.getUniqueId());
6264

63-
if (userOption.isEmpty()) {
64-
return Joiner.on("\n")
65-
.join(this.translationManager.getMessages().online().serverFull())
66-
.toString();
67-
}
68-
69-
User user = userOption.get();
65+
List<String> messages = userOption
66+
.map(user -> this.translationManager.getMessages(user.getUniqueId()).online().serverFull())
67+
.orElseGet(() -> this.translationManager.getMessages(player.getUniqueId()).online().serverFull());
7068

71-
return Joiner.on("\n")
72-
.join(this.translationManager.getMessages(user.getUniqueId()).online().serverFull())
73-
.toString();
69+
return String.join(LINE_SEPARATOR, messages);
7470
}
7571
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/jail/JailServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020
import java.util.UUID;
21-
import javax.annotation.Nullable;
2221
import org.bukkit.Location;
2322
import org.bukkit.Server;
2423
import org.bukkit.command.CommandSender;
2524
import org.bukkit.entity.Player;
2625
import org.jetbrains.annotations.Blocking;
2726

2827
import java.util.Optional;
28+
import org.jetbrains.annotations.Nullable;
2929

3030
@Service
3131
class JailServiceImpl implements JailService {

eternalcore-core/src/main/java/com/eternalcode/core/feature/onlineplayers/OnlinePlayersListCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import dev.rollczi.litecommands.annotations.execute.Execute;
1212
import dev.rollczi.litecommands.annotations.permission.Permission;
1313
import java.util.Collection;
14+
import java.util.stream.Collectors;
1415
import org.bukkit.Server;
1516
import org.bukkit.entity.HumanEntity;
1617
import org.bukkit.entity.Player;
17-
import panda.utilities.text.Joiner;
1818

1919
@Command(name = "list")
2020
@Permission("eternalcore.list")
@@ -43,9 +43,9 @@ void execute(@Sender Viewer viewer) {
4343
.toList();
4444

4545
String onlineCount = String.valueOf(online.size());
46-
String players = Joiner.on(this.config.format.separator)
47-
.join(online, HumanEntity::getName)
48-
.toString();
46+
String players = online.stream()
47+
.map(HumanEntity::getName)
48+
.collect(Collectors.joining(this.config.format.separator));
4949

5050
this.noticeService.create()
5151
.notice(translation -> translation.online().onlinePlayersList())

eternalcore-core/src/main/java/com/eternalcode/core/feature/teleport/TeleportTask.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
import org.bukkit.Location;
1515
import org.bukkit.Server;
1616
import org.bukkit.entity.Player;
17-
import panda.utilities.StringUtils;
1817

1918
@Task(delay = 1L, period = 1L, unit = TimeUnit.SECONDS)
2019
class TeleportTask implements Runnable {
2120

2221
private static final int SECONDS_OFFSET = 1;
22+
private static final double MOVEMENT_THRESHOLD = 0.5;
23+
private static final String EMPTY_STRING = "";
2324

2425
private final NoticeService noticeService;
2526
private final TeleportTaskService teleportTaskService;
@@ -58,7 +59,7 @@ public void run() {
5859
teleport.completeResult(TeleportResult.MOVED_DURING_TELEPORT);
5960

6061
this.noticeService.create()
61-
.notice(translation -> Notice.actionbar(StringUtils.EMPTY))
62+
.notice(translation -> Notice.actionbar(EMPTY_STRING))
6263
.notice(translation -> translation.teleport().teleportTaskCanceled())
6364
.player(player.getUniqueId())
6465
.send();
@@ -102,11 +103,12 @@ private void completeTeleport(Player player, Teleport teleport) {
102103
private boolean hasPlayerMovedDuringTeleport(Player player, Teleport teleport) {
103104
Location startLocation = PositionAdapter.convert(teleport.getStartLocation());
104105
Location currentLocation = player.getLocation();
106+
105107
if (!currentLocation.getWorld().equals(startLocation.getWorld())) {
106-
return true;
108+
return true;
107109
}
108-
return currentLocation.distance(startLocation) > 0.5;
110+
111+
return currentLocation.distance(startLocation) > MOVEMENT_THRESHOLD;
109112
}
110113

111114
}
112-

eternalcore-core/src/main/java/com/eternalcode/core/injector/bean/BeanCandidateContainer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.eternalcode.core.injector.bean;
22

33
import dev.rollczi.litecommands.priority.MutablePrioritizedList;
4-
import dev.rollczi.litecommands.priority.Prioritized;
5-
import dev.rollczi.litecommands.priority.PrioritizedList;
64
import dev.rollczi.litecommands.priority.PriorityLevel;
7-
import java.util.HashSet;
8-
import java.util.Iterator;
9-
import java.util.Set;
105
import java.util.function.Function;
11-
import javax.annotation.Nullable;
6+
import org.jetbrains.annotations.Nullable;
127

138
class BeanCandidateContainer {
149

@@ -66,5 +61,4 @@ BeanCandidate nextCandidate() {
6661
return candidate;
6762
}
6863
}
69-
7064
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.eternalcode.core.litecommand.argument;
2+
3+
import dev.rollczi.litecommands.argument.parser.ParseResult;
4+
import java.util.function.Function;
5+
import java.util.function.Predicate;
6+
import java.util.function.Supplier;
7+
8+
final class ArgumentParser<INPUT, OUTPUT> {
9+
10+
private final Function<INPUT, ParseResult<OUTPUT>> parser;
11+
12+
private ArgumentParser(Function<INPUT, ParseResult<OUTPUT>> parser) {
13+
this.parser = parser;
14+
}
15+
16+
public static <INPUT, OUTPUT> ArgumentParser<INPUT, OUTPUT> of() {
17+
return new ArgumentParser<>(input -> ParseResult.failure(new Object()));
18+
}
19+
20+
public ArgumentParser<INPUT, OUTPUT> thenTry(Function<INPUT, ParseResult<OUTPUT>> nextParser) {
21+
return new ArgumentParser<>(input -> this.parser.apply(input)
22+
.mapFailure(ignored -> nextParser.apply(input)));
23+
}
24+
25+
public ArgumentParser<INPUT, OUTPUT> thenTryIf(
26+
Predicate<INPUT> condition,
27+
Function<INPUT, ParseResult<OUTPUT>> nextParser
28+
) {
29+
return new ArgumentParser<>(input -> this.parser.apply(input)
30+
.mapFailure(ignored -> {
31+
if (condition.test(input)) {
32+
return nextParser.apply(input);
33+
}
34+
return ParseResult.failure(new Object());
35+
}));
36+
}
37+
38+
public Function<INPUT, ParseResult<OUTPUT>> build() {
39+
return this.parser;
40+
}
41+
42+
public Function<INPUT, ParseResult<OUTPUT>> buildWithFinalFailure(Function<INPUT, ParseResult<OUTPUT>> finalFailure) {
43+
return input -> this.parser.apply(input)
44+
.mapFailure(ignored -> finalFailure.apply(input));
45+
}
46+
47+
public Function<INPUT, ParseResult<OUTPUT>> buildWithFinalFailure(Supplier<ParseResult<OUTPUT>> finalFailure) {
48+
return input -> this.parser.apply(input)
49+
.mapFailure(ignored -> finalFailure.get());
50+
}
51+
}
52+

eternalcore-core/src/main/java/com/eternalcode/core/litecommand/argument/GameModeArgument.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,64 @@
1010
import dev.rollczi.litecommands.invocation.Invocation;
1111
import dev.rollczi.litecommands.suggestion.SuggestionContext;
1212
import dev.rollczi.litecommands.suggestion.SuggestionResult;
13+
import java.util.Locale;
14+
import java.util.function.Function;
1315
import org.bukkit.GameMode;
1416
import org.bukkit.command.CommandSender;
15-
import panda.std.Option;
16-
17-
import java.util.Optional;
1817

1918
@LiteArgument(type = GameMode.class)
2019
class GameModeArgument extends AbstractViewerArgument<GameMode> {
2120

2221
private final GameModeArgumentSettings gameModeArgumentSettings;
22+
private final ArgumentParser<String, GameMode> gameModeArgumentParser;
2323

2424
@Inject
2525
GameModeArgument(TranslationManager translationManager, GameModeArgumentSettings gameModeArgumentSettings) {
2626
super(translationManager);
2727
this.gameModeArgumentSettings = gameModeArgumentSettings;
28+
this.gameModeArgumentParser = createGameModeArgumentParser();
2829
}
2930

3031
@Override
3132
public ParseResult<GameMode> parse(Invocation<CommandSender> invocation, String argument, Translation translation) {
32-
Option<GameMode> gameMode = Option.supplyThrowing(IllegalArgumentException.class, () -> GameMode.valueOf(argument.toUpperCase()));
33-
34-
if (gameMode.isPresent()) {
35-
return ParseResult.success(gameMode.get());
36-
}
33+
String normalizedInput = argument.trim().toUpperCase(Locale.ROOT);
3734

38-
Optional<GameMode> alias = this.gameModeArgumentSettings.getByAlias(argument);
35+
Function<String, ParseResult<GameMode>> finalParser = this.gameModeArgumentParser
36+
.buildWithFinalFailure(input -> ParseResult.failure(translation.gamemode().gamemodeTypeInvalid()));
3937

40-
return alias
41-
.map(parsed -> ParseResult.success(parsed))
42-
.orElseGet(() -> ParseResult.failure(translation.gamemode().gamemodeTypeInvalid()));
38+
return finalParser.apply(normalizedInput);
4339
}
4440

4541
@Override
46-
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<GameMode> argument, SuggestionContext context) {
42+
public SuggestionResult suggest(
43+
Invocation<CommandSender> invocation,
44+
Argument<GameMode> argument,
45+
SuggestionContext context
46+
) {
4747
return this.gameModeArgumentSettings.getAvailableAliases()
4848
.stream()
4949
.collect(SuggestionResult.collector());
5050
}
5151

52+
private ArgumentParser<String, GameMode> createGameModeArgumentParser() {
53+
return ArgumentParser.<String, GameMode>of()
54+
.thenTry(this::parseDirectGameMode)
55+
.thenTry(this::parseFromAliases);
56+
}
57+
58+
private ParseResult<GameMode> parseDirectGameMode(String normalizedInput) {
59+
try {
60+
GameMode gameMode = GameMode.valueOf(normalizedInput);
61+
return ParseResult.success(gameMode);
62+
}
63+
catch (IllegalArgumentException exception) {
64+
return ParseResult.failure(new Object());
65+
}
66+
}
67+
68+
private ParseResult<GameMode> parseFromAliases(String normalizedInput) {
69+
return this.gameModeArgumentSettings.getByAlias(normalizedInput)
70+
.map(ParseResult::success)
71+
.orElseGet(() -> ParseResult.failure(new Object()));
72+
}
5273
}

eternalcore-core/src/main/java/com/eternalcode/core/user/UserClientBukkitSettings.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import org.bukkit.Server;
44
import org.bukkit.entity.Player;
5-
import panda.std.Option;
65

76
import java.lang.ref.WeakReference;
8-
import java.util.Locale;
7+
import java.util.Optional;
98
import java.util.UUID;
109

1110
class UserClientBukkitSettings implements UserClientSettings {
@@ -25,21 +24,21 @@ public boolean isOnline() {
2524
return this.getPlayer().isPresent();
2625
}
2726

28-
private Option<Player> getPlayer() {
27+
private Optional<Player> getPlayer() {
2928
Player player = this.playerReference.get();
3029

3130
if (player == null) {
3231
Player playerFromServer = this.server.getPlayer(this.uuid);
3332

3433
if (playerFromServer == null) {
35-
return Option.none();
34+
return Optional.empty();
3635
}
3736

3837
this.playerReference = new WeakReference<>(playerFromServer);
39-
return Option.of(playerFromServer);
38+
return Optional.of(playerFromServer);
4039
}
4140

42-
return Option.of(player);
41+
return Optional.of(player);
4342
}
4443

4544
}

0 commit comments

Comments
 (0)