1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-25 16:11:05 +01:00

feat: OfflineUserArgument type

This commit is contained in:
Jozef Steinhübl 2023-10-12 18:56:16 +02:00
parent af006a7ebf
commit 42ee5da076
No known key found for this signature in database
GPG key ID: E944BC293F5FF7E7
6 changed files with 64 additions and 11 deletions

View file

@ -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.logger.PluginLogger;
import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter;
import lombok.Getter; import lombok.Getter;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -15,6 +16,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@Getter @Getter
@ -59,6 +61,11 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr
return this.loader.getDataFolder().toPath(); return this.loader.getDataFolder().toPath();
} }
@Override
public Optional<UUID> lookupUniqueId(String username) {
return Optional.ofNullable(this.loader.getServer().getOfflinePlayer(username)).map(OfflinePlayer::getUniqueId);
}
@Override @Override
public boolean isPlayerOnline(UUID uniqueId) { public boolean isPlayerOnline(UUID uniqueId) {
Player player = this.loader.getServer().getPlayer(uniqueId); Player player = this.loader.getServer().getPlayer(uniqueId);

View file

@ -18,9 +18,9 @@ public abstract class Command {
@Getter @Getter
@NonNull @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.name = name;
this.permission = permission; this.permission = permission;
this.args = List.of(args); this.args = List.of(args);

View file

@ -1,19 +1,20 @@
package dev.xhyrom.lighteco.common.command.argument; package dev.xhyrom.lighteco.common.command.argument;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import lombok.Getter; import lombok.Getter;
import net.kyori.adventure.text.Component;
public class Argument { public abstract class Argument<T> {
@Getter @Getter
private final String name; private final String name;
@Getter protected final LightEcoPlugin plugin;
private final boolean required;
@Getter
private final Component description;
public Argument(String name, boolean required, Component description) { protected Argument(LightEcoPlugin plugin, String name) {
this.plugin = plugin;
this.name = name; this.name = name;
this.required = required;
this.description = description;
} }
public abstract Class<T> getPrimitiveType();
public abstract ArgumentType getArgumentType();
public abstract T parse(String input);
} }

View file

@ -0,0 +1,7 @@
package dev.xhyrom.lighteco.common.command.argument;
public enum ArgumentType {
OFFLINE_USER,
INTEGER,
DOUBLE;
}

View file

@ -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();
}
}

View file

@ -6,6 +6,7 @@ import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public interface LightEcoBootstrap { public interface LightEcoBootstrap {
@ -13,7 +14,10 @@ public interface LightEcoBootstrap {
PluginLogger getLogger(); PluginLogger getLogger();
SchedulerAdapter getScheduler(); SchedulerAdapter getScheduler();
Path getDataDirectory(); Path getDataDirectory();
Optional<UUID> lookupUniqueId(String username);
boolean isPlayerOnline(UUID uniqueId); boolean isPlayerOnline(UUID uniqueId);
List<UUID> getOnlinePlayers(); List<UUID> getOnlinePlayers();
InputStream getResourceStream(String filename); InputStream getResourceStream(String filename);
} }