diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index d0ca62bf4..63659e9d8 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -154,15 +154,6 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { Console.setInstance(this); - File file = new File(getDataFolder(), ".skipversion"); - if (!file.exists()) { - String serverString = Bukkit.getServer().getClass().getName(); - for (int i = 1; i <= 7; i++) { - if (serverString.contains(".v1_" + i + "_R")) { - throw new Error("Outdated server. This version of Essentials will only work on Bukkit 1.8 or higher."); - } - } - } final PluginManager pm = getServer().getPluginManager(); for (Plugin plugin : pm.getPlugins()) { if (plugin.getDescription().getName().startsWith("Essentials") && !plugin.getDescription().getVersion().equals(this.getDescription().getVersion()) && !plugin.getDescription().getName().equals("EssentialsAntiCheat")) { diff --git a/Essentials/src/com/earth2me/essentials/UserMap.java b/Essentials/src/com/earth2me/essentials/UserMap.java index a3f32e7e9..b2888ef5e 100644 --- a/Essentials/src/com/earth2me/essentials/UserMap.java +++ b/Essentials/src/com/earth2me/essentials/UserMap.java @@ -2,15 +2,17 @@ package com.earth2me.essentials; import com.earth2me.essentials.utils.StringUtil; import com.google.common.base.Charsets; +import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.util.concurrent.UncheckedExecutionException; import net.ess3.api.IEssentials; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.*; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListSet; @@ -20,19 +22,34 @@ import java.util.regex.Pattern; public class UserMap extends CacheLoader implements IConf { private final transient IEssentials ess; - private final transient LoadingCache users; - private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); - private final transient ConcurrentSkipListMap names = new ConcurrentSkipListMap(); - private final transient ConcurrentSkipListMap> history = new ConcurrentSkipListMap>(); + private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet<>(); + private final transient ConcurrentSkipListMap names = new ConcurrentSkipListMap<>(); + private final transient ConcurrentSkipListMap> history = new ConcurrentSkipListMap<>(); private UUIDMap uuidMap; + private final transient Cache users; + public UserMap(final IEssentials ess) { super(); this.ess = ess; uuidMap = new UUIDMap(ess); //RemovalListener remListener = new UserMapRemovalListener(); //users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().removalListener(remListener).build(this); - users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().build(this); + CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); + int maxCount = ess.getSettings().getMaxUserCacheCount(); + boolean legacy = false; + try { + cacheBuilder.maximumSize(maxCount); + } catch (NoSuchMethodError nsme) { + legacy = true; + legacyMaximumSize(cacheBuilder, maxCount); + } + cacheBuilder.softValues(); + if (!legacy) { + users = cacheBuilder.build(this); + } else { + users = legacyBuild(cacheBuilder); + } } private void loadAllUsersAsync(final IEssentials ess) { @@ -90,7 +107,11 @@ public class UserMap extends CacheLoader implements IConf { public User getUser(final UUID uuid) { try { - return users.get(uuid.toString()); + try { + return ((LoadingCache) users).get(uuid.toString()); + } catch (SecurityException | ClassCastException e) { + return legacyCacheGet(uuid); + } } catch (ExecutionException ex) { return null; } catch (UncheckedExecutionException ex) { @@ -244,4 +265,52 @@ public class UserMap extends CacheLoader implements IConf { return getUser(uuid); } } + + private static Method getLegacy; + + private User legacyCacheGet(UUID uuid) { + if (getLegacy == null) { + Class usersClass = users.getClass(); + for (Method m : usersClass.getDeclaredMethods()) { + if (m.getName().equals("get")) { + getLegacy = m; + getLegacy.setAccessible(true); + break; + } + } + } + try { + return (User) getLegacy.invoke(users, uuid.toString()); + } catch (IllegalAccessException | InvocationTargetException e) { + return null; + } + } + + private void legacyMaximumSize(CacheBuilder builder, int maxCount) { + try { + Method maxSizeLegacy = builder.getClass().getDeclaredMethod("maximumSize", Integer.TYPE); + maxSizeLegacy.invoke(builder, maxCount); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + private Cache legacyBuild(CacheBuilder builder) { + Method build = null; + for (Method method : builder.getClass().getDeclaredMethods()) { + if (method.getName().equals("build")) { + build = method; + break; + } + } + Cache legacyUsers; + try { + assert build != null; + legacyUsers = (Cache) build.invoke(builder, this); + } catch (IllegalAccessException | InvocationTargetException e) { + legacyUsers = null; + } + return legacyUsers; + } }