LuckPerms-DBA/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java

152 lines
5.4 KiB
Java
Raw Normal View History

2016-05-22 08:57:10 +08:00
package me.lucko.luckperms;
import lombok.Getter;
import me.lucko.luckperms.data.Datastore;
2016-06-22 23:47:54 +08:00
import me.lucko.luckperms.data.MySQLConfiguration;
2016-06-23 23:37:29 +08:00
import me.lucko.luckperms.data.methods.FlatfileDatastore;
2016-06-22 23:47:54 +08:00
import me.lucko.luckperms.data.methods.MySQLDatastore;
import me.lucko.luckperms.data.methods.SQLiteDatastore;
2016-05-22 08:57:10 +08:00
import me.lucko.luckperms.groups.GroupManager;
import me.lucko.luckperms.listeners.PlayerListener;
import me.lucko.luckperms.runnables.UpdateTask;
2016-07-16 05:44:25 +08:00
import me.lucko.luckperms.tracks.TrackManager;
2016-05-22 08:57:10 +08:00
import me.lucko.luckperms.users.BukkitUserManager;
import me.lucko.luckperms.users.UserManager;
import me.lucko.luckperms.utils.LPConfiguration;
import me.lucko.luckperms.vaulthooks.VaultHook;
2016-05-22 08:57:10 +08:00
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
2016-07-17 20:18:43 +08:00
import org.bukkit.entity.Player;
2016-05-22 08:57:10 +08:00
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
2016-06-22 23:47:54 +08:00
import java.io.File;
2016-05-22 08:57:10 +08:00
import java.util.Arrays;
2016-07-17 20:18:43 +08:00
import java.util.List;
2016-05-22 08:57:10 +08:00
import java.util.UUID;
2016-07-17 20:18:43 +08:00
import java.util.stream.Collectors;
2016-05-22 08:57:10 +08:00
@Getter
public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
2016-07-19 20:01:58 +08:00
public static final String VERSION = "v1.2";
2016-05-22 08:57:10 +08:00
private LPConfiguration configuration;
private UserManager userManager;
private GroupManager groupManager;
2016-07-16 05:44:25 +08:00
private TrackManager trackManager;
2016-05-22 08:57:10 +08:00
private Datastore datastore;
@Override
public void onEnable() {
2016-07-21 21:15:14 +08:00
getLogger().info("Loading configuration...");
2016-05-22 08:57:10 +08:00
configuration = new BukkitConfig(this);
// register events
PluginManager pm = Bukkit.getPluginManager();
pm.registerEvents(new PlayerListener(this), this);
// register commands
2016-07-21 21:15:14 +08:00
getLogger().info("Registering commands...");
2016-05-22 08:57:10 +08:00
CommandManagerBukkit commandManager = new CommandManagerBukkit(this);
PluginCommand main = getServer().getPluginCommand("luckperms");
main.setExecutor(commandManager);
2016-07-17 20:18:43 +08:00
main.setTabCompleter(commandManager);
2016-05-22 08:57:10 +08:00
main.setAliases(Arrays.asList("perms", "lp", "permissions", "p", "perm"));
2016-07-21 21:15:14 +08:00
getLogger().info("Detecting storage method...");
2016-06-22 23:47:54 +08:00
final String storageMethod = configuration.getStorageMethod();
if (storageMethod.equalsIgnoreCase("mysql")) {
getLogger().info("Using MySQL as storage method.");
datastore = new MySQLDatastore(this, new MySQLConfiguration(
configuration.getDatabaseValue("address"),
configuration.getDatabaseValue("database"),
configuration.getDatabaseValue("username"),
configuration.getDatabaseValue("password")
));
} else if (storageMethod.equalsIgnoreCase("sqlite")) {
getLogger().info("Using SQLite as storage method.");
datastore = new SQLiteDatastore(this, new File(getDataFolder(), "luckperms.sqlite"));
2016-06-23 23:37:29 +08:00
} else if (storageMethod.equalsIgnoreCase("flatfile")) {
getLogger().info("Using Flatfile (JSON) as storage method.");
datastore = new FlatfileDatastore(this, getDataFolder());
2016-06-22 23:47:54 +08:00
} else {
2016-07-21 21:15:14 +08:00
getLogger().severe("Storage method '" + storageMethod + "' was not recognised. Using SQLite as fallback.");
2016-06-22 23:47:54 +08:00
datastore = new SQLiteDatastore(this, new File(getDataFolder(), "luckperms.sqlite"));
}
2016-07-21 21:15:14 +08:00
getLogger().info("Initialising datastore...");
2016-06-22 23:47:54 +08:00
datastore.init();
2016-05-22 08:57:10 +08:00
2016-07-21 21:15:14 +08:00
getLogger().info("Loading internal permission managers...");
2016-05-22 08:57:10 +08:00
userManager = new BukkitUserManager(this);
groupManager = new GroupManager(this);
2016-07-21 21:15:14 +08:00
trackManager = new TrackManager();
2016-05-22 08:57:10 +08:00
// Run update task to refresh any online users
2016-07-21 21:15:14 +08:00
getLogger().info("Scheduling Update Task to refresh any online users.");
runUpdateTask();
2016-05-22 08:57:10 +08:00
int mins = getConfiguration().getSyncTime();
if (mins > 0) {
long ticks = mins * 60 * 20;
getServer().getScheduler().runTaskTimerAsynchronously(this, new UpdateTask(this), ticks, ticks);
2016-05-22 08:57:10 +08:00
}
// Provide vault support
2016-07-21 21:15:14 +08:00
getLogger().info("Attempting to hook into Vault...");
try {
if (getServer().getPluginManager().isPluginEnabled("Vault")) {
VaultHook.hook(this);
getLogger().info("Registered Vault permission & chat hook.");
} else {
getLogger().info("Vault not found.");
}
} catch (Exception e) {
getLogger().severe("Error occurred whilst hooking into Vault.");
e.printStackTrace();
2016-05-22 08:57:10 +08:00
}
2016-07-21 21:15:14 +08:00
getLogger().info("Successfully loaded.");
2016-05-22 08:57:10 +08:00
}
@Override
public void onDisable() {
datastore.shutdown();
}
2016-05-22 08:57:10 +08:00
@Override
public void doAsync(Runnable r) {
Bukkit.getScheduler().runTaskAsynchronously(this, r);
}
@Override
public void doSync(Runnable r) {
Bukkit.getScheduler().runTask(this, r);
}
@Override
public String getVersion() {
return VERSION;
}
@Override
public String getPlayerStatus(UUID uuid) {
2016-06-29 22:35:57 +08:00
return getServer().getPlayer(uuid) != null ? "&aOnline" : "&cOffline";
2016-05-22 08:57:10 +08:00
}
@Override
public int getPlayerCount() {
return getServer().getOnlinePlayers().size();
}
2016-07-17 20:18:43 +08:00
@Override
public List<String> getPlayerList() {
return getServer().getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
}
2016-05-22 08:57:10 +08:00
@Override
public void runUpdateTask() {
getServer().getScheduler().runTaskAsynchronously(this, new UpdateTask(this));
2016-05-22 08:57:10 +08:00
}
}