From 2875b050c535ea795c0953866060a90dc15e3314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 23 May 2020 19:43:32 +0200 Subject: [PATCH] Add plugin UUID services. They probably work. --- Bukkit/build.gradle | 3 + .../com/plotsquared/bukkit/BukkitMain.java | 37 +++++++- .../bukkit/uuid/EssentialsUUIDService.java | 70 ++++++++++++++++ .../bukkit/uuid/LuckPermsUUIDService.java | 84 +++++++++++++++++++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/uuid/EssentialsUUIDService.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/uuid/LuckPermsUUIDService.java diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index 9b6b862a6..965928520 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -11,6 +11,7 @@ repositories { name = "papermc" url = "https://papermc.io/repo/repository/maven-public/" } + maven { url = "https://ci.ender.zone/plugin/repository/everything/" } maven { url = "https://mvn.intellectualsites.com/content/repositories/snapshots" } mavenLocal() } @@ -30,6 +31,8 @@ dependencies { exclude(module: "bukkit") } implementation("me.clip:placeholderapi:2.10.4") + implementation("net.luckperms:api:5.0") + implementation("net.ess3:EssentialsX:2.16.1") compile("se.hyperver.hyperverse:Core:0.6.0-SNAPSHOT"){ transitive = false } compile 'com.github.pavog:SquirrelID:0.6.1' } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 84ba2f591..588813d5f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -51,6 +51,8 @@ import com.plotsquared.bukkit.util.BukkitTaskManager; import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.bukkit.util.SetGenCB; import com.plotsquared.bukkit.util.UpdateUtility; +import com.plotsquared.bukkit.uuid.EssentialsUUIDService; +import com.plotsquared.bukkit.uuid.LuckPermsUUIDService; import com.plotsquared.bukkit.uuid.OfflinePlayerUUIDService; import com.plotsquared.bukkit.uuid.PaperUUIDService; import com.plotsquared.bukkit.uuid.SQLiteUUIDService; @@ -231,38 +233,71 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain PlotSquared.log(Captions.PREFIX + "&6Couldn't verify purchase :("); } - // TODO: Do we respect the UUID settings? final UUIDPipeline impromptuPipeline = PlotSquared.get().getImpromptuUUIDPipeline(); final UUIDPipeline backgroundPipeline = PlotSquared.get().getBackgroundUUIDPipeline(); + // Services are accessed in order final CacheUUIDService cacheUUIDService = new CacheUUIDService(Settings.UUID.UUID_CACHE_SIZE); impromptuPipeline.registerService(cacheUUIDService); backgroundPipeline.registerService(cacheUUIDService); impromptuPipeline.registerConsumer(cacheUUIDService); backgroundPipeline.registerConsumer(cacheUUIDService); + // Now, if the server is in offline mode we can only use profiles and direct UUID // access, and so we skip the player profile stuff as well as SquirrelID (Mojang lookups) if (Settings.UUID.OFFLINE) { final OfflineModeUUIDService offlineModeUUIDService = new OfflineModeUUIDService(); impromptuPipeline.registerService(offlineModeUUIDService); backgroundPipeline.registerService(offlineModeUUIDService); + PlotSquared.log(Captions.PREFIX + "(UUID) Using the offline mode UUID service"); } + final OfflinePlayerUUIDService offlinePlayerUUIDService = new OfflinePlayerUUIDService(); impromptuPipeline.registerService(offlinePlayerUUIDService); backgroundPipeline.registerService(offlinePlayerUUIDService); final SQLiteUUIDService sqLiteUUIDService = new SQLiteUUIDService(); + + final LuckPermsUUIDService luckPermsUUIDService; + if (Bukkit.getPluginManager().getPlugin("LuckPerms") != null) { + luckPermsUUIDService = new LuckPermsUUIDService(); + PlotSquared.log(Captions.PREFIX + "(UUID) Using LuckPerms as a complementary UUID service"); + } else { + luckPermsUUIDService = null; + } + + final EssentialsUUIDService essentialsUUIDService; + if (Bukkit.getPluginManager().getPlugin("Essentials") != null) { + essentialsUUIDService = new EssentialsUUIDService(); + PlotSquared.log(Captions.PREFIX + "(UUID) Using Essentials as a complementary UUID service"); + } else { + essentialsUUIDService = null; + } + if (!Settings.UUID.OFFLINE) { // If running Paper we'll also try to use their profiles if (PaperLib.isPaper()) { final PaperUUIDService paperUUIDService = new PaperUUIDService(); impromptuPipeline.registerService(paperUUIDService); backgroundPipeline.registerService(paperUUIDService); + PlotSquared.log(Captions.PREFIX + "(UUID) Using Paper as a complementary UUID service"); } + impromptuPipeline.registerService(sqLiteUUIDService); backgroundPipeline.registerService(sqLiteUUIDService); impromptuPipeline.registerConsumer(sqLiteUUIDService); backgroundPipeline.registerConsumer(sqLiteUUIDService); + + // Plugin providers + if (luckPermsUUIDService != null) { + impromptuPipeline.registerService(luckPermsUUIDService); + backgroundPipeline.registerService(luckPermsUUIDService); + } + if (essentialsUUIDService != null) { + impromptuPipeline.registerService(essentialsUUIDService); + backgroundPipeline.registerService(essentialsUUIDService); + } + final SquirrelIdUUIDService impromptuMojangService = new SquirrelIdUUIDService(Settings.UUID.IMPROMPTU_LIMIT); impromptuPipeline.registerService(impromptuMojangService); final SquirrelIdUUIDService backgroundMojangService = new SquirrelIdUUIDService(Settings.UUID.BACKGROUND_LIMIT); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/EssentialsUUIDService.java b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/EssentialsUUIDService.java new file mode 100644 index 000000000..eb473d67e --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/EssentialsUUIDService.java @@ -0,0 +1,70 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.uuid; + +import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.User; +import com.plotsquared.core.uuid.UUIDMapping; +import com.plotsquared.core.uuid.UUIDService; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +/** + * UUID service using the EssentialsX API + */ +public class EssentialsUUIDService implements UUIDService { + + private final Essentials essentials; + + public EssentialsUUIDService() { + this.essentials = Essentials.getPlugin(Essentials.class); + } + + @Override @NotNull public List getNames(@NotNull final List uuids) { + return Collections.emptyList(); + } + + @Override @NotNull public List getUUIDs(@NotNull final List usernames) { + final List mappings = new ArrayList<>(usernames.size()); + for (final String username : usernames) { + try { + final User user = essentials.getUser(username); + if (user != null) { + final UUID uuid = user.getConfigUUID(); + if (uuid != null) { + mappings.add(new UUIDMapping(uuid, username)); + } + } + } catch (final Exception ignored){} + } + return mappings; + } + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/LuckPermsUUIDService.java b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/LuckPermsUUIDService.java new file mode 100644 index 000000000..47bcc9bb3 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/LuckPermsUUIDService.java @@ -0,0 +1,84 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.uuid; + +import com.plotsquared.core.uuid.UUIDMapping; +import com.plotsquared.core.uuid.UUIDService; +import net.luckperms.api.LuckPerms; +import net.luckperms.api.model.user.UserManager; +import org.bukkit.Bukkit; +import org.bukkit.plugin.RegisteredServiceProvider; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * UUID service that uses the LuckPerms API + */ +public class LuckPermsUUIDService implements UUIDService { + + private final LuckPerms luckPerms; + + public LuckPermsUUIDService() { + final RegisteredServiceProvider provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class); + if (provider != null) { + this.luckPerms = provider.getProvider(); + } else { + throw new IllegalStateException("LuckPerms not available"); + } + } + + @Override @NotNull public List getNames(@NotNull final List uuids) { + final List mappings = new ArrayList<>(uuids.size()); + final UserManager userManager = this.luckPerms.getUserManager(); + for (final UUID uuid : uuids) { + try { + final String username = userManager.lookupUsername(uuid).get(); + if (username != null) { + mappings.add(new UUIDMapping(uuid, username)); + } + } catch (final Exception ignored) {} + } + return mappings; + } + + @Override @NotNull public List getUUIDs(@NotNull final List usernames) { + final List mappings = new ArrayList<>(usernames.size()); + final UserManager userManager = this.luckPerms.getUserManager(); + for (final String username : usernames) { + try { + final UUID uuid = userManager.lookupUniqueId(username).get(); + if (username != null) { + mappings.add(new UUIDMapping(uuid, username)); + } + } catch (final Exception ignored) {} + } + return mappings; + } + +}