Remove messenger system & more cleanup

This commit is contained in:
Luck
2016-11-06 14:56:13 +00:00
Unverified
parent 4f6e229943
commit 5ebbc178a5
6 changed files with 90 additions and 91 deletions
@@ -104,7 +104,6 @@ public class MetaCache implements MetaData {
this.meta.put(meta.getKey(), meta.getValue());
}
}
this.meta.put(meta.getKey(), meta.getValue());
}
}
}
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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 <T> the command sender type
*/
@Getter
public class AbstractSender<T> implements Sender {
private final LuckPermsPlugin platform;
private final SenderFactory<T> factory;
private final WeakReference<T> ref;
private final String name;
private final UUID uuid;
AbstractSender(LuckPermsPlugin platform, SenderFactory<T> 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());
}
}
@@ -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 <T> the command sender type
*/
@RequiredArgsConstructor
public abstract class SenderFactory<T> implements Runnable {
public abstract class SenderFactory<T> {
private final LuckPermsPlugin plugin;
private final Map<T, List<String>> messages = new HashMap<>();
private final AtomicBoolean shouldSend = new AtomicBoolean(false);
private final SenderFactory<T> factory = this;
protected abstract String getName(T t);
protected abstract UUID getUuid(T t);
@@ -49,76 +41,6 @@ public abstract class SenderFactory<T> 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<T, List<String>> e : messages.entrySet()) {
for (String s : e.getValue()) {
factory.sendMessage(e.getKey(), s);
}
}
messages.clear();
}
}
private class SenderImp implements Sender {
private final WeakReference<T> 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);
}
}