Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.time.Duration;
import java.util.Collection;
import java.util.UUID;
import javax.annotation.Nullable;
import org.bukkit.Location;

import java.util.Optional;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Nullable;

public interface JailService {

Expand Down
5 changes: 1 addition & 4 deletions eternalcore-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@ eternalShadow {
)

// common libraries
library("org.panda-lang:expressible:${Versions.EXPRESSIBLE}")
library("org.panda-lang:panda-utilities:${Versions.PANDA_UTILITIES}")

library("commons-io:commons-io:${Versions.APACHE_COMMONS}")
libraryRelocate(
"panda.std",
"panda.utilities",
"org.apache.commons.io",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import panda.utilities.text.Joiner;

import java.util.List;
import java.util.Optional;

@PermissionDocs(
Expand All @@ -27,6 +27,7 @@
class FullServerBypassController implements Listener {

static final String SLOT_BYPASS = "eternalcore.slot.bypass";
private static final String LINE_SEPARATOR = "\n";

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

@EventHandler
void onLogin(PlayerLoginEvent event) {
if (event.getResult() == PlayerLoginEvent.Result.KICK_FULL) {
Player player = event.getPlayer();
if (event.getResult() != PlayerLoginEvent.Result.KICK_FULL) {
return;
}

if (player.hasPermission(SLOT_BYPASS)) {
event.allow();
Player player = event.getPlayer();

return;
}
if (player.hasPermission(SLOT_BYPASS)) {
event.allow();
return;
}

String serverFullMessage = this.getServerFullMessage(player);
Component serverFullMessageComponent = this.miniMessage.deserialize(serverFullMessage);
String serverFullMessage = this.getServerFullMessage(player);
Component serverFullMessageComponent = this.miniMessage.deserialize(serverFullMessage);

event.disallow(PlayerLoginEvent.Result.KICK_FULL, AdventureUtil.SECTION_SERIALIZER.serialize(serverFullMessageComponent));
}
event.disallow(PlayerLoginEvent.Result.KICK_FULL, AdventureUtil.SECTION_SERIALIZER.serialize(serverFullMessageComponent));
}

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

if (userOption.isEmpty()) {
return Joiner.on("\n")
.join(this.translationManager.getMessages().online().serverFull())
.toString();
}

User user = userOption.get();
List<String> messages = userOption
.map(user -> this.translationManager.getMessages(user.getUniqueId()).online().serverFull())
.orElseGet(() -> this.translationManager.getMessages(player.getUniqueId()).online().serverFull());

return Joiner.on("\n")
.join(this.translationManager.getMessages(user.getUniqueId()).online().serverFull())
.toString();
return String.join(LINE_SEPARATOR, messages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Nullable;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Blocking;

import java.util.Optional;
import org.jetbrains.annotations.Nullable;

@Service
class JailServiceImpl implements JailService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import java.util.Collection;
import java.util.stream.Collectors;
import org.bukkit.Server;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import panda.utilities.text.Joiner;

@Command(name = "list")
@Permission("eternalcore.list")
Expand Down Expand Up @@ -43,9 +43,9 @@ void execute(@Sender Viewer viewer) {
.toList();

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

this.noticeService.create()
.notice(translation -> translation.online().onlinePlayersList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import panda.utilities.StringUtils;

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

private static final int SECONDS_OFFSET = 1;
private static final double MOVEMENT_THRESHOLD = 0.5;
private static final String EMPTY_STRING = "";

private final NoticeService noticeService;
private final TeleportTaskService teleportTaskService;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void run() {
teleport.completeResult(TeleportResult.MOVED_DURING_TELEPORT);

this.noticeService.create()
.notice(translation -> Notice.actionbar(StringUtils.EMPTY))
.notice(translation -> Notice.actionbar(EMPTY_STRING))
.notice(translation -> translation.teleport().teleportTaskCanceled())
.player(player.getUniqueId())
.send();
Expand Down Expand Up @@ -102,11 +103,12 @@ private void completeTeleport(Player player, Teleport teleport) {
private boolean hasPlayerMovedDuringTeleport(Player player, Teleport teleport) {
Location startLocation = PositionAdapter.convert(teleport.getStartLocation());
Location currentLocation = player.getLocation();

if (!currentLocation.getWorld().equals(startLocation.getWorld())) {
return true;
return true;
}
return currentLocation.distance(startLocation) > 0.5;

return currentLocation.distance(startLocation) > MOVEMENT_THRESHOLD;
}

}

Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.eternalcode.core.injector.bean;

import dev.rollczi.litecommands.priority.MutablePrioritizedList;
import dev.rollczi.litecommands.priority.Prioritized;
import dev.rollczi.litecommands.priority.PrioritizedList;
import dev.rollczi.litecommands.priority.PriorityLevel;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

class BeanCandidateContainer {

Expand Down Expand Up @@ -66,5 +61,4 @@ BeanCandidate nextCandidate() {
return candidate;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.eternalcode.core.litecommand.argument;

import dev.rollczi.litecommands.argument.parser.ParseResult;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

final class ArgumentParser<INPUT, OUTPUT> {

private final Function<INPUT, ParseResult<OUTPUT>> parser;

private ArgumentParser(Function<INPUT, ParseResult<OUTPUT>> parser) {
this.parser = parser;
}

public static <INPUT, OUTPUT> ArgumentParser<INPUT, OUTPUT> of() {
return new ArgumentParser<>(input -> ParseResult.failure(new Object()));
}

public ArgumentParser<INPUT, OUTPUT> thenTry(Function<INPUT, ParseResult<OUTPUT>> nextParser) {
return new ArgumentParser<>(input -> this.parser.apply(input)
.mapFailure(ignored -> nextParser.apply(input)));
}

public ArgumentParser<INPUT, OUTPUT> thenTryIf(
Predicate<INPUT> condition,
Function<INPUT, ParseResult<OUTPUT>> nextParser
) {
return new ArgumentParser<>(input -> this.parser.apply(input)
.mapFailure(ignored -> {
if (condition.test(input)) {
return nextParser.apply(input);
}
return ParseResult.failure(new Object());
}));
}

public Function<INPUT, ParseResult<OUTPUT>> build() {
return this.parser;
}

public Function<INPUT, ParseResult<OUTPUT>> buildWithFinalFailure(Function<INPUT, ParseResult<OUTPUT>> finalFailure) {
return input -> this.parser.apply(input)
.mapFailure(ignored -> finalFailure.apply(input));
}

public Function<INPUT, ParseResult<OUTPUT>> buildWithFinalFailure(Supplier<ParseResult<OUTPUT>> finalFailure) {
return input -> this.parser.apply(input)
.mapFailure(ignored -> finalFailure.get());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,64 @@
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import java.util.Locale;
import java.util.function.Function;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import panda.std.Option;

import java.util.Optional;

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

private final GameModeArgumentSettings gameModeArgumentSettings;
private final ArgumentParser<String, GameMode> gameModeArgumentParser;

@Inject
GameModeArgument(TranslationManager translationManager, GameModeArgumentSettings gameModeArgumentSettings) {
super(translationManager);
this.gameModeArgumentSettings = gameModeArgumentSettings;
this.gameModeArgumentParser = createGameModeArgumentParser();
}

@Override
public ParseResult<GameMode> parse(Invocation<CommandSender> invocation, String argument, Translation translation) {
Option<GameMode> gameMode = Option.supplyThrowing(IllegalArgumentException.class, () -> GameMode.valueOf(argument.toUpperCase()));

if (gameMode.isPresent()) {
return ParseResult.success(gameMode.get());
}
String normalizedInput = argument.trim().toUpperCase(Locale.ROOT);

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

return alias
.map(parsed -> ParseResult.success(parsed))
.orElseGet(() -> ParseResult.failure(translation.gamemode().gamemodeTypeInvalid()));
return finalParser.apply(normalizedInput);
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<GameMode> argument, SuggestionContext context) {
public SuggestionResult suggest(
Invocation<CommandSender> invocation,
Argument<GameMode> argument,
SuggestionContext context
) {
return this.gameModeArgumentSettings.getAvailableAliases()
.stream()
.collect(SuggestionResult.collector());
}

private ArgumentParser<String, GameMode> createGameModeArgumentParser() {
return ArgumentParser.<String, GameMode>of()
.thenTry(this::parseDirectGameMode)
.thenTry(this::parseFromAliases);
}

private ParseResult<GameMode> parseDirectGameMode(String normalizedInput) {
try {
GameMode gameMode = GameMode.valueOf(normalizedInput);
return ParseResult.success(gameMode);
}
catch (IllegalArgumentException exception) {
return ParseResult.failure(new Object());
}
}

private ParseResult<GameMode> parseFromAliases(String normalizedInput) {
return this.gameModeArgumentSettings.getByAlias(normalizedInput)
.map(ParseResult::success)
.orElseGet(() -> ParseResult.failure(new Object()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import org.bukkit.Server;
import org.bukkit.entity.Player;
import panda.std.Option;

import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;

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

private Option<Player> getPlayer() {
private Optional<Player> getPlayer() {
Player player = this.playerReference.get();

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

if (playerFromServer == null) {
return Option.none();
return Optional.empty();
}

this.playerReference = new WeakReference<>(playerFromServer);
return Option.of(playerFromServer);
return Optional.of(playerFromServer);
}

return Option.of(player);
return Optional.of(player);
}

}
Loading