From 5ebbc178a59c944f13e56f58aa7a306f1fcd4b9d Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 6 Nov 2016 14:56:13 +0000 Subject: [PATCH] Remove messenger system & more cleanup --- .../luckperms/bukkit/LPBukkitPlugin.java | 13 ++- .../luckperms/bungee/LPBungeePlugin.java | 1 - .../luckperms/common/caching/MetaCache.java | 1 - .../common/commands/AbstractSender.java | 81 ++++++++++++++++++ .../common/commands/SenderFactory.java | 84 +------------------ .../luckperms/sponge/LPSpongePlugin.java | 1 - 6 files changed, 90 insertions(+), 91 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/commands/AbstractSender.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index 80c1eaf7..0bc2f37b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -225,7 +225,6 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { updateTaskBuffer.requestDirectly(); // register tasks - getServer().getScheduler().runTaskTimer(this, BukkitSenderFactory.get(this), 1L, 1L); getServer().getScheduler().runTaskTimerAsynchronously(this, new ExpireTemporaryTask(this), 60L, 60L); getServer().getScheduler().runTaskTimerAsynchronously(this, consecutiveExecutor, 20L, 20L); @@ -382,12 +381,12 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { ); // Check for and include varying Vault config options - try { - assert getConfiguration().isVaultIncludingGlobal() == getConfiguration().isIncludingGlobalPerms(); - assert getConfiguration().isIncludingGlobalWorldPerms(); - assert getConfiguration().isApplyingGlobalGroups(); - assert getConfiguration().isApplyingGlobalWorldGroups(); - } catch (AssertionError e) { + boolean vaultDiff = getConfiguration().isVaultIncludingGlobal() != getConfiguration().isIncludingGlobalPerms() || + !getConfiguration().isIncludingGlobalWorldPerms() || + !getConfiguration().isApplyingGlobalGroups() || + !getConfiguration().isApplyingGlobalWorldGroups(); + + if (vaultDiff) { contexts.addAll(c.stream() .map(map -> new Contexts(map, getConfiguration().isVaultIncludingGlobal(), true, true, true, true, op)) .collect(Collectors.toSet()) diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 3128d4e0..9bc38631 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -173,7 +173,6 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { updateTaskBuffer.requestDirectly(); // register tasks - getProxy().getScheduler().schedule(this, BungeeSenderFactory.get(this), 50L, 50L, TimeUnit.MILLISECONDS); // 20 times per second (once per "tick") getProxy().getScheduler().schedule(this, new ExpireTemporaryTask(this), 3L, 3L, TimeUnit.SECONDS); getProxy().getScheduler().schedule(this, consecutiveExecutor, 1L, 1L, TimeUnit.SECONDS); diff --git a/common/src/main/java/me/lucko/luckperms/common/caching/MetaCache.java b/common/src/main/java/me/lucko/luckperms/common/caching/MetaCache.java index c4b39a3b..a3320ce6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/caching/MetaCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/caching/MetaCache.java @@ -104,7 +104,6 @@ public class MetaCache implements MetaData { this.meta.put(meta.getKey(), meta.getValue()); } } - this.meta.put(meta.getKey(), meta.getValue()); } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/AbstractSender.java b/common/src/main/java/me/lucko/luckperms/common/commands/AbstractSender.java new file mode 100644 index 00000000..25ceac57 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/AbstractSender.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands; + +import lombok.Getter; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.constants.Constants; +import me.lucko.luckperms.common.constants.Permission; + +import java.lang.ref.WeakReference; +import java.util.UUID; + +/** + * Simple implementation of {@link Sender} using a {@link SenderFactory} + * @param the command sender type + */ +@Getter +public class AbstractSender implements Sender { + private final LuckPermsPlugin platform; + private final SenderFactory factory; + private final WeakReference ref; + private final String name; + private final UUID uuid; + + AbstractSender(LuckPermsPlugin platform, SenderFactory factory, T t) { + this.platform = platform; + this.factory = factory; + this.ref = new WeakReference<>(t); + this.name = factory.getName(t); + this.uuid = factory.getUuid(t); + } + + @Override + public void sendMessage(String s) { + final T t = ref.get(); + if (t != null) { + factory.sendMessage(t, s); + } + } + + @Override + public boolean hasPermission(Permission permission) { + if (isConsole()) return true; + + T t = ref.get(); + if (t != null) { + for (String s : permission.getNodes()) { + if (factory.hasPermission(t, s)) { + return true; + } + } + } + + return false; + } + + private boolean isConsole() { + return this.uuid.equals(Constants.getConsoleUUID()) || this.uuid.equals(Constants.getImporterUUID()); + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/SenderFactory.java b/common/src/main/java/me/lucko/luckperms/common/commands/SenderFactory.java index 84e72f19..15a5ef7a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/SenderFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/SenderFactory.java @@ -22,26 +22,18 @@ package me.lucko.luckperms.common.commands; -import lombok.Getter; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.constants.Constants; -import me.lucko.luckperms.common.constants.Permission; -import java.lang.ref.WeakReference; -import java.util.*; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.UUID; /** * Factory class to make a thread-safe sender instance * @param the command sender type */ @RequiredArgsConstructor -public abstract class SenderFactory implements Runnable { +public abstract class SenderFactory { private final LuckPermsPlugin plugin; - private final Map> messages = new HashMap<>(); - private final AtomicBoolean shouldSend = new AtomicBoolean(false); - private final SenderFactory factory = this; protected abstract String getName(T t); protected abstract UUID getUuid(T t); @@ -49,76 +41,6 @@ public abstract class SenderFactory implements Runnable { protected abstract boolean hasPermission(T t, String node); public final Sender wrap(T t) { - return new SenderImp(plugin, t); - } - - @Override - public final void run() { - if (!shouldSend.getAndSet(false)) { - return; - } - - synchronized (messages) { - for (Map.Entry> e : messages.entrySet()) { - for (String s : e.getValue()) { - factory.sendMessage(e.getKey(), s); - } - } - - messages.clear(); - } - } - - private class SenderImp implements Sender { - private final WeakReference tRef; - - @Getter - private final String name; - - @Getter - private final UUID uuid; - - private final boolean console; - - @Getter - private final LuckPermsPlugin platform; - - private SenderImp(LuckPermsPlugin platform, T t) { - this.platform = platform; - this.tRef = new WeakReference<>(t); - this.name = factory.getName(t); - this.uuid = factory.getUuid(t); - this.console = this.uuid.equals(Constants.getConsoleUUID()) || this.uuid.equals(Constants.getImporterUUID()); - } - - @Override - public void sendMessage(String s) { - final T t = tRef.get(); - if (t != null) { - synchronized (messages) { - if (!messages.containsKey(t)) { - messages.put(t, new ArrayList<>()); - } - - messages.get(t).add(s); - } - shouldSend.set(true); - } - } - - @Override - public boolean hasPermission(Permission permission) { - if (console) return true; - - T t = tRef.get(); - if (t == null) return false; - - for (String s : permission.getNodes()) { - if (factory.hasPermission(t, s)) { - return true; - } - } - return false; - } + return new AbstractSender<>(plugin, this, t); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 4658f3d0..c74f4a62 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -215,7 +215,6 @@ public class LPSpongePlugin implements LuckPermsPlugin { updateTaskBuffer.requestDirectly(); // register tasks - scheduler.createTaskBuilder().intervalTicks(1L).execute(SpongeSenderFactory.get(this)).submit(this); scheduler.createTaskBuilder().async().intervalTicks(60L).execute(new ExpireTemporaryTask(this)).submit(this); scheduler.createTaskBuilder().async().intervalTicks(20L).execute(consecutiveExecutor).submit(this);