From e41bb8753bdfada2f06f9a8b478aa45266c581a4 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sat, 4 May 2019 01:45:41 -0400 Subject: [PATCH] Backport some fixes, fix version in plugin yml Still need to get automatic filtering fixed --- build.gradle | 2 +- .../concurrency/ConcurrentPlayerMap.java | 63 +++++++++---------- .../protocol/wrappers/WrappedBlockData.java | 5 +- .../protocol/wrappers/WrappedStatistic.java | 31 +++++---- src/main/resources/plugin.yml | 5 +- 5 files changed, 52 insertions(+), 54 deletions(-) diff --git a/build.gradle b/build.gradle index a0fcffed..65d17770 100644 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,7 @@ processResources { with copySpec { from 'src/main/resources' - filter(ReplaceTokens, tokens: ['fullVersion': fullVersion]) + filter(ReplaceTokens, tokens: ['$fullVersion': fullVersion]) } } diff --git a/src/main/java/com/comphenix/protocol/concurrency/ConcurrentPlayerMap.java b/src/main/java/com/comphenix/protocol/concurrency/ConcurrentPlayerMap.java index db7e7032..00b0261f 100644 --- a/src/main/java/com/comphenix/protocol/concurrency/ConcurrentPlayerMap.java +++ b/src/main/java/com/comphenix/protocol/concurrency/ConcurrentPlayerMap.java @@ -1,5 +1,16 @@ package com.comphenix.protocol.concurrency; +import com.comphenix.protocol.utility.SafeCacheBuilder; +import com.comphenix.protocol.utility.Util; +import com.google.common.base.Function; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.RemovalListener; +import com.google.common.collect.AbstractIterator; +import com.google.common.collect.Maps; +import com.google.common.util.concurrent.UncheckedExecutionException; + +import org.bukkit.entity.Player; + import java.util.AbstractMap; import java.util.AbstractSet; import java.util.Iterator; @@ -7,18 +18,6 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import org.bukkit.entity.Player; - -import com.comphenix.protocol.utility.SafeCacheBuilder; -import com.comphenix.protocol.utility.Util; -import com.google.common.base.Function; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; -import com.google.common.collect.AbstractIterator; -import com.google.common.collect.Maps; -import com.google.common.util.concurrent.UncheckedExecutionException; - /** * Represents a concurrent player map. *

@@ -37,7 +36,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp ADDRESS { @Override public Object apply(Player player) { - return player.getAddress(); + return player == null ? null : player.getAddress(); } }, @@ -47,7 +46,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp NAME { @Override public Object apply(Player player) { - return player.getName(); + return player == null ? null : player.getName(); } }, } @@ -55,17 +54,17 @@ public class ConcurrentPlayerMap extends AbstractMap imp /** * An internal map of player keys to values. */ - protected ConcurrentMap valueLookup = createValueMap(); + private ConcurrentMap valueLookup = createValueMap(); /** * A cache of the associated keys for each player. */ - - protected ConcurrentMap keyLookup = createKeyCache(); + + private ConcurrentMap keyLookup = createKeyCache(); /** * The method used to retrieve a unique key for a player. */ - protected final Function keyMethod; + private final Function keyMethod; /** * Construct a new concurrent player map that uses each player's address as key. @@ -89,7 +88,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp * Construct a new concurrent player map using the given standard key method. * @param standardMethod - the standard key method. */ - public ConcurrentPlayerMap(PlayerKey standardMethod) { + private ConcurrentPlayerMap(PlayerKey standardMethod) { this.keyMethod = standardMethod; } @@ -107,7 +106,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp * The default implementation uses a {@link ConcurrentHashMap}. * @return The value map. */ - protected ConcurrentMap createValueMap() { + private ConcurrentMap createValueMap() { return Maps.newConcurrentMap(); } @@ -115,19 +114,16 @@ public class ConcurrentPlayerMap extends AbstractMap imp * Construct a cache of keys and the associated player. * @return The key map. */ - protected ConcurrentMap createKeyCache() { + private ConcurrentMap createKeyCache() { return SafeCacheBuilder.newBuilder(). weakValues(). removalListener( - new RemovalListener() { - @Override - public void onRemoval(RemovalNotification removed) { - // We ignore explicit removal - if (removed.wasEvicted()) { - onCacheEvicted(removed.getKey()); - } - } - }). + (RemovalListener) removed -> { + // We ignore explicit removal + if (removed.wasEvicted()) { + onCacheEvicted(removed.getKey()); + } + }). build( new CacheLoader() { @Override @@ -147,7 +143,6 @@ public class ConcurrentPlayerMap extends AbstractMap imp /** * Invoked when an entry in the cache has been evicted, typically by the garbage collector. * @param key - the key. - * @param player - the value that was evicted or collected. */ private void onCacheEvicted(Object key) { Player newPlayer = findOnlinePlayer(key); @@ -165,7 +160,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp * @param key - a non-null key. * @return The player with the given key, or NULL if not found. */ - protected Player findOnlinePlayer(Object key) { + private Player findOnlinePlayer(Object key) { for (Player player : Util.getOnlinePlayers()) { if (key.equals(keyMethod.apply(player))) { return player; @@ -179,7 +174,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp * @param key - the key of the player we are locating. * @return The player, or NULL if not found. */ - protected Player lookupPlayer(Object key) { + private Player lookupPlayer(Object key) { try { return keyLookup.get(key); } catch (UncheckedExecutionException e) { @@ -192,7 +187,7 @@ public class ConcurrentPlayerMap extends AbstractMap imp * @param player - the player whose key we want to retrieve. * @return The key. */ - protected Object cachePlayerKey(Player player) { + private Object cachePlayerKey(Player player) { Object key = keyMethod.apply(player); keyLookup.put(key, player); diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java index 2a584df3..6485093a 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java @@ -26,7 +26,6 @@ import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; import org.bukkit.Material; -import org.bukkit.block.data.BlockData; /** * Represents a wrapper around IBlockData. @@ -157,7 +156,7 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab return new NewBlockData(FROM_LEGACY_DATA.invoke(null, material, (byte) data)); } - private static WrappedBlockData createNewData(BlockData data) { + private static WrappedBlockData createNewData(Object data) { return new NewBlockData(GET_HANDLE.invoke(data)); } } @@ -324,7 +323,7 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab * @param data Spigot block data * @return The new Wrapped Block Data */ - public static WrappedBlockData createData(BlockData data) { + public static WrappedBlockData createData(Object data) { return NewBlockData.createNewData(data); } diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedStatistic.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedStatistic.java index a4296df4..bcc3b426 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedStatistic.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedStatistic.java @@ -1,6 +1,7 @@ package com.comphenix.protocol.wrappers; import java.util.Map; +import java.util.stream.Collectors; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.accessors.Accessors; @@ -18,13 +19,22 @@ public class WrappedStatistic extends AbstractWrapper { private static final Class STATISTIC = MinecraftReflection.getStatisticClass(); private static final Class STATISTIC_LIST = MinecraftReflection.getStatisticListClass(); - private static final MethodAccessor FIND_STATISTICS = Accessors.getMethodAccessor( - FuzzyReflection.fromClass(STATISTIC_LIST).getMethodByParameters( - "findStatistic", STATISTIC, new Class[] { String.class } - ) - ); - private static final FieldAccessor MAP_ACCESSOR = Accessors.getFieldAccessor(STATISTIC_LIST, Map.class, true); - private static final FieldAccessor GET_NAME = Accessors.getFieldAccessor(STATISTIC, String.class, true); + + static { + try { + FIND_STATISTICS = Accessors.getMethodAccessor( + FuzzyReflection.fromClass(STATISTIC_LIST).getMethodByParameters( + "findStatistic", STATISTIC, new Class[]{String.class})); + MAP_ACCESSOR = Accessors.getFieldAccessor(STATISTIC_LIST, Map.class, true); + GET_NAME = Accessors.getFieldAccessor(STATISTIC, String.class, true); + } catch (Exception ex) { + // TODO - find an alternative + } + } + + private static MethodAccessor FIND_STATISTICS; + private static FieldAccessor MAP_ACCESSOR; + private static FieldAccessor GET_NAME; private final String name; @@ -62,12 +72,7 @@ public class WrappedStatistic extends AbstractWrapper { @SuppressWarnings("unchecked") Map map = (Map) MAP_ACCESSOR.get(null); - return Iterables.transform(map.values(), new Function() { - @Override - public WrappedStatistic apply(Object handle) { - return fromHandle(handle); - }; - }); + return map.values().stream().map(WrappedStatistic::fromHandle).collect(Collectors.toList()); } /** diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 92db9b6c..bb3457d8 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,11 +1,10 @@ name: ProtocolLib -#version: ${project.fullVersion} -version: 4.4.0 +version: 4.5.0-SNAPSHOT description: Provides read/write access to the Minecraft protocol. authors: [dmulloy2, comphenix] main: com.comphenix.protocol.ProtocolLib -load: startup +load: STARTUP database: false commands: