From 6dba31b257170ceda73afac49fe28def7f4f9f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 21 Jul 2020 14:28:54 +0200 Subject: [PATCH] Start working on the new permission system --- .../plotsquared/bukkit/BukkitPlatform.java | 10 +- .../bukkit/inject/PermissionModule.java | 52 +++++++++ .../permissions/BukkitPermissionHandler.java | 84 +++++++++++++++ .../permissions/VaultPermissionHandler.java | 102 ++++++++++++++++++ .../bukkit/player/BukkitOfflinePlayer.java | 14 ++- .../bukkit/player/BukkitPlayer.java | 22 ++-- .../bukkit/player/BukkitPlayerManager.java | 14 ++- .../plotsquared/bukkit/util/BukkitUtil.java | 2 +- .../com/plotsquared/core/PlotPlatform.java | 10 ++ .../permissions/ConsolePermissionProfile.java | 37 +++++++ .../permissions/NullPermissionProfile.java | 37 +++++++ .../core/permissions/PermissionHandler.java | 95 ++++++++++++++++ .../core/permissions/PermissionHolder.java | 43 ++++++++ .../core/permissions/PermissionProfile.java | 43 ++++++++ .../core/player/ConsolePlayer.java | 14 ++- .../core/player/OfflinePlotPlayer.java | 5 +- .../plotsquared/core/player/PlotPlayer.java | 17 ++- .../com/plotsquared/core/util/ChunkUtil.java | 25 +++++ .../com/plotsquared/core/util/MainUtil.java | 47 ++++++++ 19 files changed, 638 insertions(+), 35 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java create mode 100644 Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java create mode 100644 Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java create mode 100644 Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java create mode 100644 Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java create mode 100644 Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index 01cf8114e..d83813d14 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -33,6 +33,7 @@ import com.google.inject.Stage; import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.plotsquared.bukkit.inject.BackupModule; import com.plotsquared.bukkit.inject.BukkitModule; +import com.plotsquared.bukkit.inject.PermissionModule; import com.plotsquared.bukkit.inject.WorldManagerModule; import com.plotsquared.bukkit.listener.ChunkListener; import com.plotsquared.bukkit.listener.EntitySpawnListener; @@ -245,8 +246,11 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; // We create the injector after PlotSquared has been initialized, so that we have access // to generated instances and settings - this.injector = Guice.createInjector(Stage.PRODUCTION, new WorldManagerModule(), new PlotSquaredModule(), - new BukkitModule(this), new BackupModule()); + this.injector = Guice.createInjector(Stage.PRODUCTION, new PermissionModule(), + new WorldManagerModule(), + new PlotSquaredModule(), + new BukkitModule(this), + new BackupModule()); this.injector.injectMembers(this); if (PremiumVerification.isPremium() && Settings.Enabled_Components.UPDATE_NOTIFICATIONS) { @@ -1140,7 +1144,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; return names; } - @Override public com.plotsquared.core.location.World getPlatformWorld(@Nonnull final String worldName) { + @Override @Nonnull public com.plotsquared.core.location.World getPlatformWorld(@Nonnull final String worldName) { return BukkitWorld.of(worldName); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java new file mode 100644 index 000000000..b22cb3648 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java @@ -0,0 +1,52 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.inject; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.Singleton; +import com.plotsquared.bukkit.permissions.BukkitPermissionHandler; +import com.plotsquared.bukkit.permissions.VaultPermissionHandler; +import com.plotsquared.core.permissions.PermissionHandler; +import net.milkbowl.vault.permission.Permission; +import org.bukkit.Bukkit; +import org.bukkit.plugin.RegisteredServiceProvider; + +public class PermissionModule extends AbstractModule { + + @Provides @Singleton PermissionHandler providePermissionHandler() { + try { + RegisteredServiceProvider permissionProvider = + Bukkit.getServer().getServicesManager().getRegistration(Permission.class); + if (permissionProvider != null) { + return new VaultPermissionHandler(); + } + } catch (final Exception ignored) { + } + return new BukkitPermissionHandler(); + } + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java new file mode 100644 index 000000000..bceecda3e --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.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.permissions; + +import com.plotsquared.bukkit.player.BukkitPlayer; +import com.plotsquared.core.permissions.ConsolePermissionProfile; +import com.plotsquared.core.permissions.PermissionHandler; +import com.plotsquared.core.permissions.PermissionProfile; +import com.plotsquared.core.player.ConsolePlayer; +import com.plotsquared.core.player.OfflinePlotPlayer; +import com.plotsquared.core.player.PlotPlayer; +import org.bukkit.entity.Player; + +import javax.annotation.Nonnull; +import java.lang.ref.WeakReference; +import java.util.EnumSet; +import java.util.Optional; +import java.util.Set; + +public class BukkitPermissionHandler implements PermissionHandler { + + @Override public void initialize() { + } + + @Nonnull @Override public Optional getPermissionProfile( + @Nonnull PlotPlayer playerPlotPlayer) { + if (playerPlotPlayer instanceof BukkitPlayer) { + final BukkitPlayer bukkitPlayer = (BukkitPlayer) playerPlotPlayer; + return Optional.of(new BukkitPermissionProfile(bukkitPlayer.getPlatformPlayer())); + } else if (playerPlotPlayer instanceof ConsolePlayer) { + return Optional.of(ConsolePermissionProfile.INSTANCE); + } + return Optional.empty(); + } + + @Nonnull @Override public Optional getPermissionProfile( + @Nonnull OfflinePlotPlayer offlinePlotPlayer) { + return Optional.empty(); + } + + @Nonnull @Override public Set getCapabilities() { + return EnumSet.of(PermissionHandlerCapability.ONLINE_PERMISSIONS); + } + + + private static final class BukkitPermissionProfile implements PermissionProfile { + + private final WeakReference playerReference; + + private BukkitPermissionProfile(@Nonnull final Player player) { + this.playerReference = new WeakReference<>(player); + } + + @Override public boolean hasPermission(@Nonnull final String permission) { + final Player player = this.playerReference.get(); + return player != null && player.hasPermission(permission); + } + + } + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java new file mode 100644 index 000000000..a7978e671 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java @@ -0,0 +1,102 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.permissions; + +import com.plotsquared.bukkit.player.BukkitOfflinePlayer; +import com.plotsquared.bukkit.player.BukkitPlayer; +import com.plotsquared.core.permissions.ConsolePermissionProfile; +import com.plotsquared.core.permissions.PermissionHandler; +import com.plotsquared.core.permissions.PermissionProfile; +import com.plotsquared.core.player.ConsolePlayer; +import com.plotsquared.core.player.OfflinePlotPlayer; +import com.plotsquared.core.player.PlotPlayer; +import net.milkbowl.vault.permission.Permission; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.plugin.RegisteredServiceProvider; + +import javax.annotation.Nonnull; +import java.util.EnumSet; +import java.util.Optional; +import java.util.Set; + +public class VaultPermissionHandler implements PermissionHandler { + + private Permission permissions; + + @Override public void initialize() { + if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) { + throw new IllegalStateException("Vault is not present on the server"); + } + RegisteredServiceProvider permissionProvider = + Bukkit.getServer().getServicesManager().getRegistration(Permission.class); + if (permissionProvider != null) { + this.permissions = permissionProvider.getProvider(); + } + } + + @Nonnull @Override public Optional getPermissionProfile( + @Nonnull PlotPlayer playerPlotPlayer) { + if (playerPlotPlayer instanceof BukkitPlayer) { + final BukkitPlayer bukkitPlayer = (BukkitPlayer) playerPlotPlayer; + return Optional.of(new VaultPermissionProfile(bukkitPlayer.getPlatformPlayer())); + } else if (playerPlotPlayer instanceof ConsolePlayer) { + return Optional.of(ConsolePermissionProfile.INSTANCE); + } + return Optional.empty(); + } + + @Nonnull @Override public Optional getPermissionProfile( + @Nonnull OfflinePlotPlayer offlinePlotPlayer) { + if (offlinePlotPlayer instanceof BukkitOfflinePlayer) { + return Optional.of(new VaultPermissionProfile(((BukkitOfflinePlayer) offlinePlotPlayer).player)); + } + return Optional.empty(); + } + + @Nonnull @Override public Set getCapabilities() { + return EnumSet.of(PermissionHandlerCapability.ONLINE_PERMISSIONS, PermissionHandlerCapability.OFFLINE_PERMISSIONS); + } + + + private final class VaultPermissionProfile implements PermissionProfile { + + private final OfflinePlayer offlinePlayer; + + private VaultPermissionProfile(@Nonnull final OfflinePlayer offlinePlayer) { + this.offlinePlayer = offlinePlayer; + } + + @Override public boolean hasPermission(@Nonnull final String permission) { + if (permissions == null) { + return false; + } + return permissions.playerHas(null, offlinePlayer, permission); + } + + } + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java index 3aa5d12c3..e9c74dfb9 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java @@ -25,6 +25,9 @@ */ package com.plotsquared.bukkit.player; +import com.plotsquared.core.permissions.NullPermissionProfile; +import com.plotsquared.core.permissions.PermissionHandler; +import com.plotsquared.core.permissions.PermissionProfile; import com.plotsquared.core.player.OfflinePlotPlayer; import org.bukkit.OfflinePlayer; import javax.annotation.Nonnull; @@ -34,6 +37,7 @@ import java.util.UUID; public class BukkitOfflinePlayer implements OfflinePlotPlayer { public final OfflinePlayer player; + private final PermissionProfile permissionProfile; /** * Please do not use this method. Instead use BukkitUtil.getPlayer(Player), @@ -41,8 +45,11 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer { * * @param player */ - public BukkitOfflinePlayer(OfflinePlayer player) { + public BukkitOfflinePlayer(@Nonnull final OfflinePlayer player, @Nonnull final + PermissionHandler permissionHandler) { this.player = player; + this.permissionProfile = permissionHandler.getPermissionProfile(this) + .orElse(NullPermissionProfile.INSTANCE); } @Nonnull @Override public UUID getUUID() { @@ -60,4 +67,9 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer { @Override public String getName() { return this.player.getName(); } + + @Override public boolean hasPermission(@Nonnull final String permission) { + return this.permissionProfile.hasPermission(permission); + } + } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java index 15d9bacf8..0fcc50fb0 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -32,6 +32,7 @@ import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.location.Location; +import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.PlotWeather; import com.plotsquared.core.plot.world.PlotAreaManager; @@ -83,19 +84,21 @@ public class BukkitPlayer extends PlotPlayer { * @param player Bukkit player instance */ public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, - @Nonnull final Player player, @Nullable final EconHandler econHandler) { - this(plotAreaManager, eventDispatcher, player, false, econHandler); + @Nonnull final Player player, @Nullable final EconHandler econHandler, @Nonnull final PermissionHandler permissionHandler) { + this(plotAreaManager, eventDispatcher, player, false, econHandler, permissionHandler); } public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, - @Nonnull final Player player, final boolean offline, @Nullable final EconHandler econHandler) { - this(plotAreaManager, eventDispatcher, player, offline, true, econHandler); + @Nonnull final Player player, final boolean offline, @Nullable final EconHandler econHandler, + @Nonnull final PermissionHandler permissionHandler) { + this(plotAreaManager, eventDispatcher, player, offline, true, econHandler, permissionHandler); } public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, @Nonnull final Player player, final boolean offline, - final boolean realPlayer, @Nullable final EconHandler econHandler) { - super(plotAreaManager, eventDispatcher, econHandler); + final boolean realPlayer, @Nullable final EconHandler econHandler, + @Nonnull final PermissionHandler permissionHandler) { + super(plotAreaManager, eventDispatcher, econHandler, permissionHandler); this.player = player; this.offline = offline; this.econHandler = econHandler; @@ -161,13 +164,6 @@ public class BukkitPlayer extends PlotPlayer { } } - @Override public boolean hasPermission(final String permission) { - if (this.offline && this.econHandler != null) { - return this.econHandler.hasPermission(getName(), permission); - } - return this.player.hasPermission(permission); - } - @Override public int hasPermissionRange(final String stub, final int range) { if (hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) { return Integer.MAX_VALUE; diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java index 7238709e3..d05fafd7a 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java @@ -27,6 +27,7 @@ package com.plotsquared.bukkit.player; import com.google.inject.Inject; import com.google.inject.Singleton; +import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.EventDispatcher; @@ -46,13 +47,16 @@ import java.util.UUID; private final PlotAreaManager plotAreaManager; private final EventDispatcher eventDispatcher; private final EconHandler econHandler; + private final PermissionHandler permissionHandler; @Inject public BukkitPlayerManager(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler) { + @Nullable final EconHandler econHandler, + @Nonnull final PermissionHandler permissionHandler) { this.plotAreaManager = plotAreaManager; this.eventDispatcher = eventDispatcher; this.econHandler = econHandler; + this.permissionHandler = permissionHandler; } @Nonnull @Override public BukkitPlayer getPlayer(@Nonnull final Player object) { @@ -60,7 +64,7 @@ import java.util.UUID; return getPlayer(object.getUniqueId()); } catch (final NoSuchPlayerException exception) { return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object, - object.isOnline(), false, this.econHandler); + object.isOnline(), false, this.econHandler, this.permissionHandler); } } @@ -69,18 +73,18 @@ import java.util.UUID; if (player == null || !player.isOnline()) { throw new NoSuchPlayerException(uuid); } - return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.econHandler); + return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.econHandler, this.permissionHandler); } @Nullable @Override public BukkitOfflinePlayer getOfflinePlayer(@Nullable final UUID uuid) { if (uuid == null) { return null; } - return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid)); + return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid), this.permissionHandler); } @Nonnull @Override public BukkitOfflinePlayer getOfflinePlayer(@Nonnull final String username) { - return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username)); + return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username), this.permissionHandler); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java index 5cbbf6cfc..1d88f9a6f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java @@ -131,7 +131,7 @@ import java.util.stream.Stream; final Player player = OfflinePlayerUtil.loadPlayer(op); player.loadData(); return new BukkitPlayer(PlotSquared.get().getPlotAreaManager(), - PlotSquared.get().getEventDispatcher(), player, true, PlotSquared.platform().getEconHandler()); + PlotSquared.get().getEventDispatcher(), player, true, PlotSquared.platform().getEconHandler(), PlotSquared.platform().getPermissionHandler()); } /** diff --git a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java index dc3136372..c84bce2b9 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java +++ b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java @@ -33,6 +33,7 @@ import com.plotsquared.core.generator.HybridUtils; import com.plotsquared.core.generator.IndependentPlotGenerator; import com.plotsquared.core.inject.annotations.DefaultGenerator; import com.plotsquared.core.location.World; +import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.queue.GlobalBlockQueue; import com.plotsquared.core.util.ChatManager; @@ -266,4 +267,13 @@ public interface PlotPlatform

extends ILogger { return getInjector().getInstance(ChunkManager.class); } + /** + * Get the {@link PermissionHandler} implementation for the platform + * + * @return Permission handler + */ + @Nonnull default PermissionHandler getPermissionHandler() { + return getInjector().getInstance(PermissionHandler.class); + } + } diff --git a/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java b/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java new file mode 100644 index 000000000..09857628c --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java @@ -0,0 +1,37 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.core.permissions; + +import javax.annotation.Nonnull; + +public enum ConsolePermissionProfile implements PermissionProfile { + INSTANCE; + + @Override public boolean hasPermission(@Nonnull final String permission) { + return true; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java b/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java new file mode 100644 index 000000000..b59b5a78d --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java @@ -0,0 +1,37 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.core.permissions; + +import javax.annotation.Nonnull; + +public enum NullPermissionProfile implements PermissionProfile { + INSTANCE; + + @Override public boolean hasPermission(@Nonnull final String permission) { + return false; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java new file mode 100644 index 000000000..c88537f69 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java @@ -0,0 +1,95 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.core.permissions; + +import com.plotsquared.core.player.OfflinePlotPlayer; +import com.plotsquared.core.player.PlotPlayer; + +import javax.annotation.Nonnull; +import java.util.Optional; +import java.util.Set; + +/** + * Permission handler + */ +public interface PermissionHandler { + + /** + * Initialize the permission handler + */ + void initialize(); + + /** + * Attempt to construct a permission profile for a plot player + * + * @param playerPlotPlayer Plot player + * @return Permission profile, if one was able to be constructed + */ + @Nonnull Optional getPermissionProfile( + @Nonnull PlotPlayer playerPlotPlayer); + + /** + * Attempt to construct a permission profile for an offline plot player + * + * @param offlinePlotPlayer Offline player + * @return Permission profile, if one was able to be constructed + */ + @Nonnull Optional getPermissionProfile( + @Nonnull OfflinePlotPlayer offlinePlotPlayer); + + /** + * Get all capabilities that the permission handler has + * + * @return Immutable set of capabilities + */ + @Nonnull Set getCapabilities(); + + /** + * Check whether or not the permission handler has a given capability + * + * @param capability Capability + * @return {@code true} if the handler has the capability, else {@code false} + */ + default boolean hasCapability(@Nonnull final PermissionHandlerCapability capability) { + return this.getCapabilities().contains(capability); + } + + + /** + * Permission handler capabilities + */ + enum PermissionHandlerCapability { + /** + * The ability to check for online (player) permissions + */ + ONLINE_PERMISSIONS, + /** + * The ability to check for offline (player) permissions + */ + OFFLINE_PERMISSIONS + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java new file mode 100644 index 000000000..a94bd0b39 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java @@ -0,0 +1,43 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.core.permissions; + +import javax.annotation.Nonnull; + +/** + * Any object which can hold permissions + */ +public interface PermissionHolder { + + /** + * Check if the permission holder has the given permission node + * + * @param permission Permission node + * @return {@code true} if the holder has the given permission node, else {@code false} + */ + boolean hasPermission(@Nonnull String permission); + +} diff --git a/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java b/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java new file mode 100644 index 000000000..912147980 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java @@ -0,0 +1,43 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.core.permissions; + +import javax.annotation.Nonnull; + +/** + * A permission profile that can be used to check for permissions + */ +public interface PermissionProfile { + + /** + * Check if the owner of the profile has a given permission + * + * @param permission Permission + * @return {@code true} if the owner has the given permission, else {@code false} + */ + boolean hasPermission(@Nonnull String permission); + +} diff --git a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java index c6efe33cd..2c1e67c8f 100644 --- a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java @@ -32,6 +32,7 @@ import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.inject.annotations.ConsoleActor; import com.plotsquared.core.location.Location; +import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotWeather; import com.plotsquared.core.plot.world.PlotAreaManager; @@ -42,11 +43,11 @@ import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.item.ItemType; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.UUID; public class ConsolePlayer extends PlotPlayer { @@ -59,8 +60,9 @@ public class ConsolePlayer extends PlotPlayer { @Inject private ConsolePlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, @ConsoleActor @Nonnull final Actor actor, - @Nullable final EconHandler econHandler) { - super(plotAreaManager, eventDispatcher, econHandler); + @Nullable final EconHandler econHandler, + @Nonnull final PermissionHandler permissionHandler) { + super(plotAreaManager, eventDispatcher, econHandler, permissionHandler); this.actor = actor; final PlotArea[] areas = plotAreaManager.getAllPlotAreas(); final PlotArea area; @@ -121,10 +123,6 @@ public class ConsolePlayer extends PlotPlayer { return 0; } - @Override public boolean hasPermission(String permission) { - return true; - } - @Override public boolean isPermissionSet(String permission) { return true; } diff --git a/Core/src/main/java/com/plotsquared/core/player/OfflinePlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/OfflinePlotPlayer.java index f48079f67..92a0e6536 100644 --- a/Core/src/main/java/com/plotsquared/core/player/OfflinePlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/OfflinePlotPlayer.java @@ -25,9 +25,11 @@ */ package com.plotsquared.core.player; +import com.plotsquared.core.permissions.PermissionHolder; + import java.util.UUID; -public interface OfflinePlotPlayer { +public interface OfflinePlotPlayer extends PermissionHolder { /** * Gets the {@code UUID} of this player @@ -57,4 +59,5 @@ public interface OfflinePlotPlayer { * @return the player name */ String getName(); + } diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index 6d2428d05..d3c4fd369 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -35,6 +35,9 @@ import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.location.Location; +import com.plotsquared.core.permissions.NullPermissionProfile; +import com.plotsquared.core.permissions.PermissionHandler; +import com.plotsquared.core.permissions.PermissionProfile; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotCluster; @@ -54,11 +57,11 @@ import com.plotsquared.core.util.task.TaskManager; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.item.ItemType; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.nio.ByteBuffer; import java.util.Collection; import java.util.Collections; @@ -95,11 +98,15 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer private final PlotAreaManager plotAreaManager; private final EventDispatcher eventDispatcher; private final EconHandler econHandler; + private final PermissionProfile permissionProfile; - public PlotPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, @Nullable final EconHandler econHandler) { + public PlotPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, @Nullable final EconHandler econHandler, + @Nonnull final PermissionHandler permissionHandler) { this.plotAreaManager = plotAreaManager; this.eventDispatcher = eventDispatcher; this.econHandler = econHandler; + this.permissionProfile = permissionHandler.getPermissionProfile(this).orElse( + NullPermissionProfile.INSTANCE); } public static PlotPlayer from(@Nonnull final T object) { @@ -147,6 +154,10 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer return PlotSquared.platform().wrapPlayer(player); } + @Override public final boolean hasPermission(@Nonnull final String permission) { + return this.permissionProfile.hasPermission(permission); + } + public abstract Actor toActor(); public abstract P getPlatformPlayer(); diff --git a/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java b/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java index 3e89c630a..6c1a1415d 100644 --- a/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * 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.core.util; import lombok.experimental.UtilityClass; diff --git a/Core/src/main/java/com/plotsquared/core/util/MainUtil.java b/Core/src/main/java/com/plotsquared/core/util/MainUtil.java index 2a96b7120..010622813 100644 --- a/Core/src/main/java/com/plotsquared/core/util/MainUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/MainUtil.java @@ -44,6 +44,53 @@ import javax.annotation.Nonnull; private static final Logger logger = LoggerFactory.getLogger("P2/" + MainUtil.class.getSimpleName()); + /** + * Cache of mapping x,y,z coordinates to the chunk array
+ * - Used for efficient world generation
+ */ + public static short[][] x_loc; + public static short[][] y_loc; + public static short[][] z_loc; + public static short[][][] CACHE_I = null; + public static short[][][] CACHE_J = null; + + /** + * This cache is used for world generation and just saves a bit of calculation time when checking if something is in the plot area. + */ + public static void initCache() { + if (x_loc == null) { + x_loc = new short[16][4096]; + y_loc = new short[16][4096]; + z_loc = new short[16][4096]; + for (int i = 0; i < 16; i++) { + int i4 = i << 4; + for (int j = 0; j < 4096; j++) { + int y = i4 + (j >> 8); + int a = j - ((y & 0xF) << 8); + int z1 = a >> 4; + int x1 = a - (z1 << 4); + x_loc[i][j] = (short) x1; + y_loc[i][j] = (short) y; + z_loc[i][j] = (short) z1; + } + } + } + if (CACHE_I == null) { + CACHE_I = new short[256][16][16]; + CACHE_J = new short[256][16][16]; + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + for (int y = 0; y < 256; y++) { + short i = (short) (y >> 4); + short j = (short) ((y & 0xF) << 8 | z << 4 | x); + CACHE_I[y][x][z] = i; + CACHE_J[y][x][z] = j; + } + } + } + } + } + /** * Send a message to the player. *