mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-10 01:18:07 +01:00
feat: OfflineUserArgument type
This commit is contained in:
parent
af006a7ebf
commit
42ee5da076
6 changed files with 64 additions and 11 deletions
|
@ -8,6 +8,7 @@ import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap;
|
|||
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
|
||||
import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
@ -15,6 +16,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
|
@ -59,6 +61,11 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr
|
|||
return this.loader.getDataFolder().toPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UUID> lookupUniqueId(String username) {
|
||||
return Optional.ofNullable(this.loader.getServer().getOfflinePlayer(username)).map(OfflinePlayer::getUniqueId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerOnline(UUID uniqueId) {
|
||||
Player player = this.loader.getServer().getPlayer(uniqueId);
|
||||
|
|
|
@ -18,9 +18,9 @@ public abstract class Command {
|
|||
|
||||
@Getter
|
||||
@NonNull
|
||||
private final List<Argument> args;
|
||||
private final List<Argument<?>> args;
|
||||
|
||||
public Command(@NonNull String name, @Nullable String permission, @NonNull Argument... args) {
|
||||
public Command(@NonNull String name, @Nullable String permission, @NonNull Argument<?>... args) {
|
||||
this.name = name;
|
||||
this.permission = permission;
|
||||
this.args = List.of(args);
|
||||
|
|
|
@ -1,19 +1,20 @@
|
|||
package dev.xhyrom.lighteco.common.command.argument;
|
||||
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class Argument {
|
||||
public abstract class Argument<T> {
|
||||
@Getter
|
||||
private final String name;
|
||||
@Getter
|
||||
private final boolean required;
|
||||
@Getter
|
||||
private final Component description;
|
||||
protected final LightEcoPlugin plugin;
|
||||
|
||||
public Argument(String name, boolean required, Component description) {
|
||||
protected Argument(LightEcoPlugin plugin, String name) {
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
this.required = required;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public abstract Class<T> getPrimitiveType();
|
||||
public abstract ArgumentType getArgumentType();
|
||||
|
||||
public abstract T parse(String input);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package dev.xhyrom.lighteco.common.command.argument;
|
||||
|
||||
public enum ArgumentType {
|
||||
OFFLINE_USER,
|
||||
INTEGER,
|
||||
DOUBLE;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package dev.xhyrom.lighteco.common.command.argument.type;
|
||||
|
||||
import dev.xhyrom.lighteco.common.command.argument.Argument;
|
||||
import dev.xhyrom.lighteco.common.command.argument.ArgumentType;
|
||||
import dev.xhyrom.lighteco.common.model.user.User;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class OfflineUserArgument extends Argument<User> {
|
||||
public OfflineUserArgument(LightEcoPlugin plugin, String name) {
|
||||
super(plugin, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<User> getPrimitiveType() {
|
||||
return User.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getArgumentType() {
|
||||
return ArgumentType.OFFLINE_USER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User parse(String input) {
|
||||
UUID uniqueId = this.plugin.getBootstrap().lookupUniqueId(input).orElse(null);
|
||||
if (uniqueId == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.plugin.getUserManager().loadUser(uniqueId).join();
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter;
|
|||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface LightEcoBootstrap {
|
||||
|
@ -13,7 +14,10 @@ public interface LightEcoBootstrap {
|
|||
PluginLogger getLogger();
|
||||
SchedulerAdapter getScheduler();
|
||||
Path getDataDirectory();
|
||||
|
||||
Optional<UUID> lookupUniqueId(String username);
|
||||
boolean isPlayerOnline(UUID uniqueId);
|
||||
List<UUID> getOnlinePlayers();
|
||||
|
||||
InputStream getResourceStream(String filename);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue