misc refactor for PermissionHolder & DummySender

This commit is contained in:
Luck 2018-01-12 23:51:59 +00:00
parent 3201d10bdd
commit 22006617d0
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
5 changed files with 43 additions and 27 deletions

View File

@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.common.commands.CommandManager; import me.lucko.luckperms.common.commands.CommandManager;
import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.sender.DummySender;
import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.commands.utils.CommandUtils;
import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.locale.Message;

View File

@ -23,11 +23,10 @@
* SOFTWARE. * SOFTWARE.
*/ */
package me.lucko.luckperms.common.backup; package me.lucko.luckperms.common.commands.sender;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.commands.CommandManager; import me.lucko.luckperms.common.commands.CommandManager;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.TextUtils; import me.lucko.luckperms.common.utils.TextUtils;

View File

@ -747,9 +747,11 @@ public abstract class PermissionHolder {
if (perms.putIfAbsent(perm, node.getValuePrimitive()) == null) { if (perms.putIfAbsent(perm, node.getValuePrimitive()) == null) {
if (applyShorthand) { if (applyShorthand) {
List<String> sh = node.resolveShorthand(); List<String> shorthand = node.resolveShorthand();
if (!sh.isEmpty()) { if (!shorthand.isEmpty()) {
sh.stream().map(s -> lowerCase ? s.toLowerCase() : s).forEach(s -> perms.putIfAbsent(s, node.getValuePrimitive())); for (String s : shorthand) {
perms.putIfAbsent(lowerCase ? s.toLowerCase() : s, node.getValuePrimitive());
}
} }
} }
} }

View File

@ -28,6 +28,7 @@ package me.lucko.luckperms.common.utils;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* A cycle of elements, backed by a list. All operations are thread safe. * A cycle of elements, backed by a list. All operations are thread safe.
@ -35,45 +36,58 @@ import java.util.List;
* @param <E> the element type * @param <E> the element type
*/ */
public class Cycle<E> { public class Cycle<E> {
protected final List<E> objects;
protected int index = 0; /**
* The list that backs this instance
*/
private final List<E> objects;
/**
* The number of elements in the cycle
*/
private final int size;
/**
* The current position of the cursor
*/
private AtomicInteger cursor = new AtomicInteger(0);
public Cycle(List<E> objects) { public Cycle(List<E> objects) {
if (objects == null || objects.isEmpty()) { if (objects == null || objects.isEmpty()) {
throw new IllegalArgumentException("List of objects cannot be null/empty."); throw new IllegalArgumentException("List of objects cannot be null/empty.");
} }
this.objects = ImmutableList.copyOf(objects); this.objects = ImmutableList.copyOf(objects);
this.size = this.objects.size();
} }
public int getIndex() { public int cursor() {
return this.index; return this.cursor.get();
} }
public E current() { public E current() {
synchronized (this) { return this.objects.get(cursor());
return this.objects.get(this.index);
}
} }
public E next() { public E next() {
synchronized (this) { return this.objects.get(this.cursor.updateAndGet(i -> {
this.index++; int n = i + 1;
this.index = this.index > this.objects.size() - 1 ? 0 : this.index; if (n >= this.size) {
return 0;
return this.objects.get(this.index); }
} return n;
}));
} }
public E back() { public E previous() {
synchronized (this) { return this.objects.get(this.cursor.updateAndGet(i -> {
this.index--; if (i == 0) {
this.index = this.index == -1 ? this.objects.size() - 1 : this.index; return this.size - 1;
}
return this.objects.get(this.index); return i - 1;
} }));
} }
public List<E> getBacking() { public List<E> getBacking() {
return this.objects; return this.objects;
} }
} }

View File

@ -33,13 +33,13 @@ import me.lucko.luckperms.api.platform.PlatformType;
import me.lucko.luckperms.common.actionlog.LogDispatcher; import me.lucko.luckperms.common.actionlog.LogDispatcher;
import me.lucko.luckperms.common.api.ApiRegistrationUtil; import me.lucko.luckperms.common.api.ApiRegistrationUtil;
import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.backup.DummySender;
import me.lucko.luckperms.common.buffers.BufferedRequest; import me.lucko.luckperms.common.buffers.BufferedRequest;
import me.lucko.luckperms.common.buffers.UpdateTaskBuffer; import me.lucko.luckperms.common.buffers.UpdateTaskBuffer;
import me.lucko.luckperms.common.caching.handlers.CachedStateManager; import me.lucko.luckperms.common.caching.handlers.CachedStateManager;
import me.lucko.luckperms.common.calculators.CalculatorFactory; import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.commands.CommandPermission; import me.lucko.luckperms.common.commands.CommandPermission;
import me.lucko.luckperms.common.commands.abstraction.Command; import me.lucko.luckperms.common.commands.abstraction.Command;
import me.lucko.luckperms.common.commands.sender.DummySender;
import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.config.AbstractConfiguration; import me.lucko.luckperms.common.config.AbstractConfiguration;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;