Small changes

This commit is contained in:
MattBDev
2019-06-26 20:14:00 -04:00
parent 1932c96d4f
commit d0a31691e1
61 changed files with 382 additions and 577 deletions

View File

@ -22,13 +22,14 @@ package com.sk89q.worldedit.util;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import javax.annotation.Nullable;
/**
* A collection of cardinal, ordinal, and secondary-ordinal directions.
*/

View File

@ -38,7 +38,8 @@ import javax.annotation.Nullable;
*/
public class TargetBlock {
private World world;
private final World world;
private int maxDistance;
private double checkDistance, curDistance;
private BlockVector3 targetPos = BlockVector3.ZERO;
@ -121,7 +122,7 @@ public class TargetBlock {
this.checkDistance = checkDistance;
this.curDistance = 0;
xRotation = (xRotation + 90) % 360;
yRotation = yRotation * -1;
yRotation *= -1;
double h = (checkDistance * Math.cos(Math.toRadians(yRotation)));
@ -144,15 +145,15 @@ public class TargetBlock {
boolean searchForLastBlock = true;
Location lastBlock = null;
while (getNextBlock() != null) {
if (!stopMask.test(targetPos)) {
if (stopMask.test(targetPos)) {
break;
} else {
if (searchForLastBlock) {
lastBlock = getCurrentBlock();
if (lastBlock.getBlockY() <= 0 || lastBlock.getBlockY() >= world.getMaxY()) {
searchForLastBlock = false;
}
}
} else {
break;
}
}
Location currentBlock = getCurrentBlock();

View File

@ -19,11 +19,10 @@
package com.sk89q.worldedit.util.eventbus;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.eventbus.DeadEvent;
import com.sk89q.worldedit.internal.annotation.RequiresNewerGuava;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,13 +30,11 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Dispatches events to listeners, and provides ways for listeners to register
@ -46,17 +43,15 @@ import static com.google.common.base.Preconditions.checkNotNull;
* <p>This class is based on Guava's {@link EventBus} but priority is supported
* and events are dispatched at the time of call, rather than being queued up.
* This does allow dispatching during an in-progress dispatch.</p>
*
* <p>This implementation utilizes naive synchronization on all getter and
* setter methods. Dispatch does not occur when a lock has been acquired,
* however.</p>
*/
public class EventBus {
public final class EventBus {
private final Logger logger = LoggerFactory.getLogger(EventBus.class);
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final SetMultimap<Class<?>, EventHandler> handlersByType =
Multimaps.newSetMultimap(new HashMap<>(), this::newHandlerSet);
HashMultimap.create();
/**
* Strategy for finding handler methods in registered objects. Currently,
@ -65,7 +60,6 @@ public class EventBus {
*/
private final SubscriberFindingStrategy finder = new AnnotatedSubscriberFinder();
@RequiresNewerGuava
private HierarchyCache flattenHierarchyCache = new HierarchyCache();
/**
@ -74,10 +68,15 @@ public class EventBus {
* @param clazz the event class to register
* @param handler the handler to register
*/
public synchronized void subscribe(Class<?> clazz, EventHandler handler) {
public void subscribe(Class<?> clazz, EventHandler handler) {
checkNotNull(clazz);
checkNotNull(handler);
handlersByType.put(clazz, handler);
lock.writeLock().lock();
try {
handlersByType.put(clazz, handler);
} finally {
lock.writeLock().unlock();
}
}
/**
@ -85,9 +84,14 @@ public class EventBus {
*
* @param handlers a map of handlers
*/
public synchronized void subscribeAll(Multimap<Class<?>, EventHandler> handlers) {
public void subscribeAll(Multimap<Class<?>, EventHandler> handlers) {
checkNotNull(handlers);
handlersByType.putAll(handlers);
lock.writeLock().lock();
try {
handlersByType.putAll(handlers);
} finally {
lock.writeLock().unlock();
}
}
/**
@ -96,10 +100,15 @@ public class EventBus {
* @param clazz the class
* @param handler the handler
*/
public synchronized void unsubscribe(Class<?> clazz, EventHandler handler) {
public void unsubscribe(Class<?> clazz, EventHandler handler) {
checkNotNull(clazz);
checkNotNull(handler);
handlersByType.remove(clazz, handler);
lock.writeLock().lock();
try {
handlersByType.remove(clazz, handler);
} finally {
lock.writeLock().unlock();
}
}
/**
@ -107,15 +116,15 @@ public class EventBus {
*
* @param handlers a map of handlers
*/
public synchronized void unsubscribeAll(Multimap<Class<?>, EventHandler> handlers) {
public void unsubscribeAll(Multimap<Class<?>, EventHandler> handlers) {
checkNotNull(handlers);
for (Map.Entry<Class<?>, Collection<EventHandler>> entry : handlers.asMap().entrySet()) {
Set<EventHandler> currentHandlers = getHandlersForEventType(entry.getKey());
Collection<EventHandler> eventMethodsInListener = entry.getValue();
if (currentHandlers != null &&!currentHandlers.containsAll(entry.getValue())) {
currentHandlers.removeAll(eventMethodsInListener);
lock.writeLock().lock();
try {
for (Map.Entry<Class<?>, Collection<EventHandler>> entry : handlers.asMap().entrySet()) {
handlersByType.get(entry.getKey()).removeAll(entry.getValue());
}
} finally {
lock.writeLock().unlock();
}
}
@ -146,25 +155,23 @@ public class EventBus {
* successfully after the event has been posted to all handlers, and
* regardless of any exceptions thrown by handlers.
*
* <p>If no handlers have been subscribed for {@code event}'s class, and
* {@code event} is not already a {@link DeadEvent}, it will be wrapped in a
* DeadEvent and reposted.
*
* @param event event to post.
*/
public void post(Object event) {
List<EventHandler> dispatching = new ArrayList<>();
synchronized (this) {
Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());
Set<Class<?>> dispatchTypes = flattenHierarchyCache.get(event.getClass());
lock.readLock().lock();
try {
for (Class<?> eventType : dispatchTypes) {
Set<EventHandler> wrappers = getHandlersForEventType(eventType);
Set<EventHandler> wrappers = handlersByType.get(eventType);
if (wrappers != null && !wrappers.isEmpty()) {
dispatching.addAll(wrappers);
}
}
} finally {
lock.readLock().unlock();
}
Collections.sort(dispatching);
@ -175,14 +182,12 @@ public class EventBus {
}
/**
* Dispatches {@code event} to the handler in {@code handler}. This method
* is an appropriate override point for subclasses that wish to make
* event delivery asynchronous.
* Dispatches {@code event} to the handler in {@code handler}.
*
* @param event event to dispatch.
* @param handler handler that will call the handler.
*/
protected void dispatch(Object event, EventHandler handler) {
private void dispatch(Object event, EventHandler handler) {
try {
handler.handleEvent(event);
} catch (InvocationTargetException e) {
@ -190,39 +195,4 @@ public class EventBus {
}
}
/**
* Retrieves a mutable set of the currently registered handlers for
* {@code type}. If no handlers are currently registered for {@code type},
* this method may either return {@code null} or an empty set.
*
* @param type type of handlers to retrieve.
* @return currently registered handlers, or {@code null}.
*/
synchronized Set<EventHandler> getHandlersForEventType(Class<?> type) {
return handlersByType.get(type);
}
/**
* Creates a new Set for insertion into the handler map. This is provided
* as an override point for subclasses. The returned set should support
* concurrent access.
*
* @return a new, mutable set for handlers.
*/
protected synchronized Set<EventHandler> newHandlerSet() {
return new HashSet<>();
}
/**
* Flattens a class's type hierarchy into a set of Class objects. The set
* will include all superclasses (transitively), and all interfaces
* implemented by these superclasses.
*
* @param concreteClass class whose type hierarchy will be retrieved.
* @return {@code clazz}'s complete type hierarchy, flattened and uniqued.
*/
Set<Class<?>> flattenHierarchy(Class<?> concreteClass) {
return flattenHierarchyCache.get(concreteClass);
}
}

View File

@ -19,34 +19,27 @@
package com.sk89q.worldedit.util.eventbus;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sk89q.worldedit.internal.annotation.RequiresNewerGuava;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
/**
* Holds a cache of class hierarchy.
*
* <p>This exists because Bukkit has an ancient version of Guava and the cache
* library in Guava has since changed.</>
*/
@RequiresNewerGuava
class HierarchyCache {
private final Map<Class<?>, Set<Class<?>>> cache = new WeakHashMap<>();
private final LoadingCache<Class<?>, Set<Class<?>>> cache = CacheBuilder.newBuilder()
.weakKeys()
.build(CacheLoader.from(this::build));
public Set<Class<?>> get(Class<?> concreteClass) {
Set<Class<?>> ret = cache.get(concreteClass);
if (ret == null) {
ret = build(concreteClass);
cache.put(concreteClass, ret);
}
return ret;
return cache.getUnchecked(concreteClass);
}
protected Set<Class<?>> build(Class<?> concreteClass) {

View File

@ -1,60 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.logging;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* Adds a WorldEdit prefix to WorldEdit's logger messages using a handler.
*/
public final class WorldEditPrefixHandler extends Handler {
private WorldEditPrefixHandler() {
}
@Override
public void publish(LogRecord record) {
String message = record.getMessage();
if (!message.startsWith("WorldEdit: ") && !message.startsWith("[WorldEdit] ")) {
record.setMessage("[WorldEdit] " + message);
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
/**
* Add the handler to the following logger name.
*
* @param name the logger name
*/
public static void register(String name) {
//todo fix this
//Logger.getLogger(name).addHandler(new WorldEditPrefixHandler());
}
}