Move stuff into commons, rename PermissionObject, add more javadocs to api
This commit is contained in:
@@ -4,12 +4,12 @@ import com.google.inject.Inject;
|
||||
import lombok.Getter;
|
||||
import me.lucko.luckperms.api.LuckPermsApi;
|
||||
import me.lucko.luckperms.api.implementation.ApiProvider;
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.data.Datastore;
|
||||
import me.lucko.luckperms.data.methods.FlatfileDatastore;
|
||||
import me.lucko.luckperms.data.methods.MySQLDatastore;
|
||||
import me.lucko.luckperms.data.methods.SQLiteDatastore;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
import me.lucko.luckperms.listeners.PlayerListener;
|
||||
import me.lucko.luckperms.runnables.UpdateTask;
|
||||
import me.lucko.luckperms.tracks.TrackManager;
|
||||
import me.lucko.luckperms.users.SpongeUserManager;
|
||||
@@ -37,7 +37,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Getter
|
||||
@Plugin(id = "luckperms", name = "LuckPerms", version = LPSpongePlugin.VERSION)
|
||||
@Plugin(id = "luckperms", name = "LuckPerms", version = LPSpongePlugin.VERSION, authors = {"Luck"}, description = "A permissions plugin")
|
||||
public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
static final String VERSION = "1.5"; // TODO load this from pom
|
||||
|
||||
@@ -59,14 +59,17 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
private TrackManager trackManager;
|
||||
private Datastore datastore;
|
||||
private UuidCache uuidCache;
|
||||
private me.lucko.luckperms.api.Logger log;
|
||||
|
||||
@Listener
|
||||
public void onEnable(GamePreInitializationEvent event) {
|
||||
log = LogUtil.wrap(logger);
|
||||
|
||||
getLog().info("Loading configuration...");
|
||||
configuration = new SpongeConfig(this);
|
||||
|
||||
// register events
|
||||
Sponge.getEventManager().registerListeners(this, new PlayerListener(this));
|
||||
Sponge.getEventManager().registerListeners(this, new SpongeListener(this));
|
||||
|
||||
// register commands
|
||||
getLog().info("Registering commands...");
|
||||
@@ -129,16 +132,12 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
LuckPerms.unregisterProvider();
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private File getStorageDir() {
|
||||
File base = configDir.toFile().getParentFile().getParentFile();
|
||||
File luckperms = new File(base, "luckperms");
|
||||
luckperms.mkdirs();
|
||||
return luckperms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public me.lucko.luckperms.api.Logger getLog() {
|
||||
return LogUtil.wrap(getLogger());
|
||||
File luckPermsDir = new File(base, "luckperms");
|
||||
luckPermsDir.mkdirs();
|
||||
return luckPermsDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -147,8 +146,8 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPlayerStatus(UUID uuid) {
|
||||
return game.getServer().getPlayer(getUuidCache().getExternalUUID(uuid)).isPresent() ? "&aOnline" : "&cOffline";
|
||||
public Message getPlayerStatus(UUID uuid) {
|
||||
return game.getServer().getPlayer(getUuidCache().getExternalUUID(uuid)).isPresent() ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import me.lucko.luckperms.commands.CommandManager;
|
||||
import me.lucko.luckperms.commands.Sender;
|
||||
import me.lucko.luckperms.commands.SenderFactory;
|
||||
import me.lucko.luckperms.utils.Patterns;
|
||||
import org.spongepowered.api.command.CommandCallable;
|
||||
import org.spongepowered.api.command.CommandException;
|
||||
@@ -10,65 +10,59 @@ import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.text.Text;
|
||||
import org.spongepowered.api.text.serializer.TextSerializers;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SpongeCommand extends CommandManager implements CommandCallable {
|
||||
public SpongeCommand(LuckPermsPlugin plugin) {
|
||||
class SpongeCommand extends CommandManager implements CommandCallable {
|
||||
private static final Factory FACTORY = new Factory();
|
||||
|
||||
SpongeCommand(LuckPermsPlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult process(CommandSource source, String s) throws CommandException {
|
||||
onCommand(makeSender(source), "perms", Arrays.asList(Patterns.SPACE_SPLIT.split(s)));
|
||||
onCommand(FACTORY.wrap(source), "perms", Arrays.asList(Patterns.SPACE.split(s)));
|
||||
return CommandResult.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(CommandSource source, String s) throws CommandException {
|
||||
return onTabComplete(makeSender(source), Arrays.asList(Patterns.SPACE_SPLIT.split(s)));
|
||||
return onTabComplete(FACTORY.wrap(source), Arrays.asList(Patterns.SPACE.split(s)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testPermission(CommandSource commandSource) {
|
||||
public boolean testPermission(CommandSource source) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<? extends Text> getShortDescription(CommandSource commandSource) {
|
||||
public Optional<? extends Text> getShortDescription(CommandSource source) {
|
||||
return Optional.of(Text.of("LuckPerms main command."));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<? extends Text> getHelp(CommandSource commandSource) {
|
||||
public Optional<? extends Text> getHelp(CommandSource source) {
|
||||
return Optional.of(Text.of("Type /perms for help."));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Text getUsage(CommandSource commandSource) {
|
||||
public Text getUsage(CommandSource source) {
|
||||
return Text.of("/perms");
|
||||
}
|
||||
|
||||
private static Sender makeSender(CommandSource source) {
|
||||
return new Sender() {
|
||||
final WeakReference<CommandSource> cs = new WeakReference<>(source);
|
||||
private static class Factory extends SenderFactory<CommandSource> {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void sendMessage(String s) {
|
||||
final CommandSource c = cs.get();
|
||||
if (c != null) {
|
||||
c.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(s));
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void sendMessage(CommandSource source, String s) {
|
||||
source.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String node) {
|
||||
final CommandSource c = cs.get();
|
||||
return c != null && c.hasPermission(node);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
protected boolean hasPermission(CommandSource source, String node) {
|
||||
return source.hasPermission(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import me.lucko.luckperms.utils.LPConfiguration;
|
||||
import me.lucko.luckperms.utils.Patterns;
|
||||
import ninja.leaping.configurate.ConfigurationNode;
|
||||
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
|
||||
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
|
||||
@@ -47,7 +48,7 @@ class SpongeConfig extends LPConfiguration<LPSpongePlugin> {
|
||||
}
|
||||
|
||||
private ConfigurationNode getNode(String path) {
|
||||
String[] paths = path.split("\\.");
|
||||
String[] paths = Patterns.DOT.split(path);
|
||||
ConfigurationNode node = root;
|
||||
|
||||
for (String s : paths) {
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.users.User;
|
||||
import me.lucko.luckperms.utils.AbstractListener;
|
||||
import org.spongepowered.api.entity.Entity;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.entity.DisplaceEntityEvent;
|
||||
import org.spongepowered.api.event.network.ClientConnectionEvent;
|
||||
import org.spongepowered.api.profile.GameProfile;
|
||||
import org.spongepowered.api.text.serializer.TextSerializers;
|
||||
|
||||
class SpongeListener extends AbstractListener {
|
||||
private final LPSpongePlugin plugin;
|
||||
|
||||
SpongeListener(LPSpongePlugin plugin) {
|
||||
super(plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onClientAuth(ClientConnectionEvent.Auth e) {
|
||||
if (!plugin.getDatastore().isAcceptingLogins()) {
|
||||
// Datastore is disabled, prevent players from joining the server
|
||||
// Just don't load their data, they will be kicked at login
|
||||
return;
|
||||
}
|
||||
|
||||
final GameProfile p = e.getProfile();
|
||||
onAsyncLogin(p.getUniqueId(), p.getName().get());
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Listener
|
||||
public void onClientLogin(ClientConnectionEvent.Login e) {
|
||||
final GameProfile player = e.getProfile();
|
||||
final User user = plugin.getUserManager().getUser(plugin.getUuidCache().getUUID(player.getUniqueId()));
|
||||
|
||||
if (user == null) {
|
||||
e.setCancelled(true);
|
||||
e.setMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(Message.LOADING_ERROR.toString()));
|
||||
return;
|
||||
}
|
||||
|
||||
user.refreshPermissions();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onClientJoin(ClientConnectionEvent.Join e) {
|
||||
// Refresh permissions again
|
||||
refreshPlayer(e.getTargetEntity().getUniqueId());
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerTeleport(DisplaceEntityEvent.Teleport e) {
|
||||
final Entity entity = e.getTargetEntity();
|
||||
if (!(entity instanceof Player)){
|
||||
return;
|
||||
}
|
||||
|
||||
refreshPlayer(entity.getUniqueId());
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onClientLeave(ClientConnectionEvent.Disconnect e) {
|
||||
onLeave(e.getTargetEntity().getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package me.lucko.luckperms.listeners;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.LPSpongePlugin;
|
||||
import me.lucko.luckperms.commands.Util;
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.users.User;
|
||||
import me.lucko.luckperms.utils.UuidCache;
|
||||
import org.spongepowered.api.entity.Entity;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.entity.DisplaceEntityEvent;
|
||||
import org.spongepowered.api.event.network.ClientConnectionEvent;
|
||||
import org.spongepowered.api.profile.GameProfile;
|
||||
import org.spongepowered.api.text.serializer.TextSerializers;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class PlayerListener {
|
||||
private static final String KICK_MESSAGE = Util.color(Message.PREFIX + "User data could not be loaded. Please contact an administrator.");
|
||||
private final LPSpongePlugin plugin;
|
||||
|
||||
@Listener
|
||||
public void onClientAuth(ClientConnectionEvent.Auth e) {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
if (!plugin.getDatastore().isAcceptingLogins()) {
|
||||
// Datastore is disabled, prevent players from joining the server
|
||||
// Just don't load their data, they will be kickec at login
|
||||
return;
|
||||
}
|
||||
|
||||
final UuidCache cache = plugin.getUuidCache();
|
||||
final GameProfile p = e.getProfile();
|
||||
final String name = p.getName().get();
|
||||
|
||||
if (!cache.isOnlineMode()) {
|
||||
UUID uuid = plugin.getDatastore().getUUID(name);
|
||||
if (uuid != null) {
|
||||
cache.addToCache(p.getUniqueId(), uuid);
|
||||
} else {
|
||||
// No previous data for this player
|
||||
cache.addToCache(p.getUniqueId(), p.getUniqueId());
|
||||
plugin.getDatastore().saveUUIDData(name, p.getUniqueId(), b -> {});
|
||||
}
|
||||
} else {
|
||||
// Online mode, no cache needed. This is just for name -> uuid lookup.
|
||||
plugin.getDatastore().saveUUIDData(name, p.getUniqueId(), b -> {});
|
||||
}
|
||||
|
||||
plugin.getDatastore().loadOrCreateUser(cache.getUUID(p.getUniqueId()), name);
|
||||
final long time = System.currentTimeMillis() - startTime;
|
||||
if (time >= 1000) {
|
||||
plugin.getLog().warn("Processing login for " + p.getName() + " took " + time + "ms.");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Listener
|
||||
public void onClientLogin(ClientConnectionEvent.Login e) {
|
||||
final GameProfile player = e.getProfile();
|
||||
final User user = plugin.getUserManager().getUser(plugin.getUuidCache().getUUID(player.getUniqueId()));
|
||||
|
||||
if (user == null) {
|
||||
e.setCancelled(true);
|
||||
e.setMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(KICK_MESSAGE));
|
||||
return;
|
||||
}
|
||||
|
||||
user.refreshPermissions();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onClientJoin(ClientConnectionEvent.Join e) {
|
||||
// Refresh permissions again
|
||||
refreshPlayer(e.getTargetEntity());
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerTeleport(DisplaceEntityEvent.Teleport e) {
|
||||
final Entity entity = e.getTargetEntity();
|
||||
if (!(entity instanceof Player)){
|
||||
return;
|
||||
}
|
||||
|
||||
refreshPlayer((Player) entity);
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onClientLeave(ClientConnectionEvent.Disconnect e) {
|
||||
final Player player = e.getTargetEntity();
|
||||
final UuidCache cache = plugin.getUuidCache();
|
||||
|
||||
// Unload the user from memory when they disconnect;
|
||||
cache.clearCache(player.getUniqueId());
|
||||
|
||||
final User user = plugin.getUserManager().getUser(cache.getUUID(player.getUniqueId()));
|
||||
plugin.getUserManager().unloadUser(user);
|
||||
}
|
||||
|
||||
private void refreshPlayer(Player p) {
|
||||
final User user = plugin.getUserManager().getUser(plugin.getUuidCache().getUUID(p.getUniqueId()));
|
||||
if (user != null) {
|
||||
user.refreshPermissions();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user