Improve various javadocs, add LogNotifyEvent

This commit is contained in:
Luck
2017-12-27 17:00:54 +00:00
Unverified
parent ab115c4a6b
commit 3d6aa69ca1
38 changed files with 990 additions and 225 deletions
@@ -42,6 +42,21 @@ import java.util.Optional;
public class LogDispatcher {
private final LuckPermsPlugin plugin;
private void broadcast(ExtendedLogEntry entry, LogBroadcastEvent.Origin origin, Sender sender) {
plugin.getOnlineSenders()
.filter(CommandPermission.LOG_NOTIFY::isAuthorized)
.filter(s -> {
boolean shouldCancel = LogNotify.isIgnoring(plugin, s.getUuid()) || (sender != null && s.getUuid().equals(sender.getUuid()));
return !plugin.getApiProvider().getEventFactory().handleLogNotify(shouldCancel, entry, origin, s);
})
.forEach(s -> Message.LOG.send(s,
entry.getActorFriendlyString(),
Character.toString(entry.getType().getCode()),
entry.getActedFriendlyString(),
entry.getAction()
));
}
public void dispatch(ExtendedLogEntry entry, Sender sender) {
// set the event to cancelled if the sender is import
if (!plugin.getApiProvider().getEventFactory().handleLogPublish(sender.isImport(), entry)) {
@@ -58,35 +73,37 @@ public class LogDispatcher {
messagingService.get().pushLog(entry);
}
if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(!plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY), entry, LogBroadcastEvent.Origin.LOCAL)) {
plugin.getOnlineSenders()
.filter(CommandPermission.LOG_NOTIFY::isAuthorized)
.filter(s -> !LogNotify.isIgnoring(plugin, s.getUuid()))
.filter(s -> !s.getUuid().equals(sender.getUuid()))
.forEach(s -> Message.LOG.send(s,
entry.getActorFriendlyString(),
Character.toString(entry.getType().getCode()),
entry.getActedFriendlyString(),
entry.getAction()
));
boolean shouldCancel = !plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY);
if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL)) {
broadcast(entry, LogBroadcastEvent.Origin.LOCAL, sender);
}
}
public void dispatchFromApi(ExtendedLogEntry entry) {
if (!plugin.getApiProvider().getEventFactory().handleLogPublish(false, entry)) {
try {
plugin.getStorage().logAction(entry).get();
} catch (Exception e) {
e.printStackTrace();
}
}
broadcastFromApi(entry);
}
public void broadcastFromApi(ExtendedLogEntry entry) {
plugin.getMessagingService().ifPresent(extendedMessagingService -> extendedMessagingService.pushLog(entry));
boolean shouldCancel = !plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY);
if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL_API)) {
broadcast(entry, LogBroadcastEvent.Origin.LOCAL_API, null);
}
}
public void dispatchFromRemote(ExtendedLogEntry entry) {
if (!plugin.getConfiguration().get(ConfigKeys.BROADCAST_RECEIVED_LOG_ENTRIES)) {
return;
}
if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(!plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY), entry, LogBroadcastEvent.Origin.REMOTE)) {
plugin.getOnlineSenders()
.filter(CommandPermission.LOG_NOTIFY::isAuthorized)
.filter(s -> !LogNotify.isIgnoring(plugin, s.getUuid()))
.forEach(s -> Message.LOG.send(s,
entry.getActorFriendlyString(),
Character.toString(entry.getType().getCode()),
entry.getActedFriendlyString(),
entry.getAction()
));
boolean shouldCancel = !plugin.getConfiguration().get(ConfigKeys.BROADCAST_RECEIVED_LOG_ENTRIES) || !plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY);
if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.REMOTE)) {
broadcast(entry, LogBroadcastEvent.Origin.LOCAL_API, null);
}
}
}
@@ -28,8 +28,8 @@ package me.lucko.luckperms.common.api;
import lombok.AccessLevel;
import lombok.Getter;
import me.lucko.luckperms.api.ActionLogger;
import me.lucko.luckperms.api.LPConfiguration;
import me.lucko.luckperms.api.LogEntry;
import me.lucko.luckperms.api.LuckPermsApi;
import me.lucko.luckperms.api.MessagingService;
import me.lucko.luckperms.api.NodeFactory;
@@ -42,11 +42,11 @@ import me.lucko.luckperms.api.manager.TrackManager;
import me.lucko.luckperms.api.manager.UserManager;
import me.lucko.luckperms.api.metastacking.MetaStackFactory;
import me.lucko.luckperms.api.platform.PlatformInfo;
import me.lucko.luckperms.common.actionlog.ExtendedLogEntry;
import me.lucko.luckperms.common.api.delegates.manager.ApiContextManager;
import me.lucko.luckperms.common.api.delegates.manager.ApiGroupManager;
import me.lucko.luckperms.common.api.delegates.manager.ApiTrackManager;
import me.lucko.luckperms.common.api.delegates.manager.ApiUserManager;
import me.lucko.luckperms.common.api.delegates.misc.ApiActionLogger;
import me.lucko.luckperms.common.api.delegates.misc.ApiMetaStackFactory;
import me.lucko.luckperms.common.api.delegates.misc.ApiNodeFactory;
import me.lucko.luckperms.common.api.delegates.misc.ApiPlatformInfo;
@@ -71,6 +71,7 @@ public class ApiProvider implements LuckPermsApi {
private final GroupManager groupManager;
private final TrackManager trackManager;
private final LuckPermsEventBus eventBus;
private final ActionLogger actionLogger;
private final ContextManager contextManager;
private final MetaStackFactory metaStackFactory;
private final EventFactory eventFactory;
@@ -83,6 +84,7 @@ public class ApiProvider implements LuckPermsApi {
this.groupManager = new ApiGroupManager(plugin.getGroupManager());
this.trackManager = new ApiTrackManager(plugin.getTrackManager());
this.eventBus = new LuckPermsEventBus(plugin);
this.actionLogger = new ApiActionLogger(plugin);
this.contextManager = new ApiContextManager(plugin, plugin.getContextManager());
this.metaStackFactory = new ApiMetaStackFactory(plugin);
this.eventFactory = new EventFactory(eventBus);
@@ -137,6 +139,11 @@ public class ApiProvider implements LuckPermsApi {
return plugin.getMessagingService().map(Function.identity());
}
@Override
public ActionLogger getActionLogger() {
return actionLogger;
}
@Override
public UuidCache getUuidCache() {
return plugin.getUuidCache().getDelegate();
@@ -157,8 +164,4 @@ public class ApiProvider implements LuckPermsApi {
return metaStackFactory;
}
@Override
public LogEntry.Builder newLogEntryBuilder() {
return ExtendedLogEntry.build();
}
}
@@ -0,0 +1,67 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* 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.api.delegates.misc;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.api.ActionLogger;
import me.lucko.luckperms.api.Log;
import me.lucko.luckperms.api.LogEntry;
import me.lucko.luckperms.common.actionlog.ExtendedLogEntry;
import me.lucko.luckperms.common.api.delegates.model.ApiLog;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor
public class ApiActionLogger implements ActionLogger {
private final LuckPermsPlugin plugin;
@Override
public LogEntry.Builder newEntryBuilder() {
return ExtendedLogEntry.build();
}
@Override
public CompletableFuture<Log> getLog() {
return plugin.getStorage().noBuffer().getLog().thenApply(log -> log == null ? null : new ApiLog(log));
}
@Override
public CompletableFuture<Void> submit(LogEntry entry) {
return CompletableFuture.runAsync(() -> plugin.getLogDispatcher().dispatchFromApi((ExtendedLogEntry) entry), plugin.getScheduler().async());
}
@Override
public CompletableFuture<Void> submitToStorage(LogEntry entry) {
return plugin.getStorage().noBuffer().logAction(entry);
}
@Override
public CompletableFuture<Void> broadcastAction(LogEntry entry) {
return CompletableFuture.runAsync(() -> plugin.getLogDispatcher().broadcastFromApi((ExtendedLogEntry) entry), plugin.getScheduler().async());
}
}
@@ -108,7 +108,7 @@ public abstract class HolderCachedData<T extends PermissionHolder> implements Ca
*/
private MetaCache calculateMeta(@NonNull MetaContexts contexts, MetaCache data) {
if (data == null) {
data = new MetaCache();
data = new MetaCache(contexts);
}
if (contexts.getContexts() == Contexts.allowAll()) {
@@ -27,13 +27,15 @@ package me.lucko.luckperms.common.caching.type;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ListMultimap;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.caching.MetaContexts;
import me.lucko.luckperms.api.caching.MetaData;
import me.lucko.luckperms.api.metastacking.MetaStackDefinition;
import me.lucko.luckperms.common.metastacking.MetaStack;
@@ -48,11 +50,16 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
* Holds cached meta for a given context
*/
@Getter
@NoArgsConstructor
@RequiredArgsConstructor
public class MetaCache implements MetaData {
@Getter(AccessLevel.NONE)
private final ReadWriteLock lock = new ReentrantReadWriteLock();
/**
* The contexts this container is holding data for
*/
private final MetaContexts metaContexts;
private ListMultimap<String, String> metaMultimap = ImmutableListMultimap.of();
private Map<String, String> meta = ImmutableMap.of();
private SortedMap<Integer, String> prefixes = ImmutableSortedMap.of();
@@ -118,4 +125,9 @@ public class MetaCache implements MetaData {
return suffixStack.getDefinition();
}
@Override
public Contexts getContexts() {
return metaContexts.getContexts();
}
}
@@ -25,6 +25,7 @@
package me.lucko.luckperms.common.caching.type;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.api.Contexts;
@@ -44,6 +45,12 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class PermissionCache implements PermissionData {
/**
* The contexts this container is holding data for
*/
@Getter
private final Contexts contexts;
/**
* The raw set of permission strings.
*/
@@ -62,6 +69,7 @@ public class PermissionCache implements PermissionData {
private final PermissionCalculator calculator;
public PermissionCache(Contexts contexts, PermissionCalculatorMetadata metadata, CalculatorFactory calculatorFactory) {
this.contexts = contexts;
permissions = new ConcurrentHashMap<>();
permissionsUnmodifiable = Collections.unmodifiableMap(permissions);
@@ -38,6 +38,7 @@ import me.lucko.luckperms.api.event.LuckPermsEvent;
import me.lucko.luckperms.api.event.cause.CreationCause;
import me.lucko.luckperms.api.event.cause.DeletionCause;
import me.lucko.luckperms.api.event.log.LogBroadcastEvent;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.event.impl.EventConfigReload;
import me.lucko.luckperms.common.event.impl.EventGroupCacheLoad;
import me.lucko.luckperms.common.event.impl.EventGroupCreate;
@@ -47,6 +48,7 @@ import me.lucko.luckperms.common.event.impl.EventGroupLoad;
import me.lucko.luckperms.common.event.impl.EventGroupLoadAll;
import me.lucko.luckperms.common.event.impl.EventLogBroadcast;
import me.lucko.luckperms.common.event.impl.EventLogNetworkPublish;
import me.lucko.luckperms.common.event.impl.EventLogNotify;
import me.lucko.luckperms.common.event.impl.EventLogPublish;
import me.lucko.luckperms.common.event.impl.EventLogReceive;
import me.lucko.luckperms.common.event.impl.EventNodeAdd;
@@ -142,6 +144,13 @@ public final class EventFactory {
return cancel.get();
}
public boolean handleLogNotify(boolean initialState, LogEntry entry, LogBroadcastEvent.Origin origin, Sender sender) {
AtomicBoolean cancel = new AtomicBoolean(initialState);
EventLogNotify event = new EventLogNotify(cancel, entry, origin, sender);
fireEvent(event);
return cancel.get();
}
public void handleLogReceive(UUID id, LogEntry entry) {
EventLogReceive event = new EventLogReceive(id, entry);
fireEventAsync(event);
@@ -0,0 +1,93 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* 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.event.impl;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import me.lucko.luckperms.api.LogEntry;
import me.lucko.luckperms.api.event.log.LogBroadcastEvent;
import me.lucko.luckperms.api.event.log.LogNotifyEvent;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.event.AbstractEvent;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
@Getter
@ToString
@RequiredArgsConstructor
public class EventLogNotify extends AbstractEvent implements LogNotifyEvent {
private final AtomicBoolean cancellationState;
private final LogEntry entry;
private final LogBroadcastEvent.Origin origin;
private final Sender sender;
@Getter(AccessLevel.NONE)
private Notifiable notifiable;
@Override
public synchronized Notifiable getNotifiable() {
if (notifiable == null) {
notifiable = new SenderNotifiable(sender);
}
return notifiable;
}
@AllArgsConstructor
private static final class SenderNotifiable implements Notifiable {
private final Sender sender;
@Override
public Optional<UUID> getUuid() {
if (sender.isConsole()) {
return Optional.empty();
}
return Optional.of(sender.getUuid());
}
@Override
public String getName() {
return sender.getName();
}
@Override
public boolean isConsole() {
return sender.isConsole();
}
@Override
public boolean isPlayer() {
return !sender.isConsole();
}
}
}