diff --git a/common/src/main/java/com/lishid/openinv/util/Cache.java b/common/src/main/java/com/lishid/openinv/util/Cache.java index f6dce83..dc627cc 100644 --- a/common/src/main/java/com/lishid/openinv/util/Cache.java +++ b/common/src/main/java/com/lishid/openinv/util/Cache.java @@ -19,11 +19,12 @@ package com.lishid.openinv.util; import com.google.common.collect.Multimap; import com.google.common.collect.TreeMultimap; import java.util.ArrayList; -import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.function.Function; /** * A minimal thread-safe time-based cache implementation backed by a HashMap and TreeMultimap. @@ -35,7 +36,7 @@ public class Cache { private final Map internal; private final Multimap expiry; private final long retention; - private final Function inUseCheck, postRemoval; + private final Function inUseCheck, postRemoval; /** * Constructs a Cache with the specified retention duration, in use function, and post-removal function. @@ -44,21 +45,10 @@ public class Cache { * @param inUseCheck Function used to check if a key is considered in use * @param postRemoval Function used to perform any operations required when a key is invalidated */ - public Cache(final long retention, final Function inUseCheck, final Function postRemoval) { - this.internal = new HashMap(); + public Cache(final long retention, final Function inUseCheck, final Function postRemoval) { + this.internal = new HashMap<>(); - this.expiry = TreeMultimap.create(new Comparator() { - @Override - public int compare(final Long long1, final Long long2) { - return long1.compareTo(long2); - } - }, - new Comparator() { - @Override - public int compare(final K k1, final K k2) { - return k1 == k2 || k1 != null && k1.equals(k2) ? 0 : 1; - } - }); + this.expiry = TreeMultimap.create(Long::compareTo, (k1, k2) -> Objects.equals(k1, k2) ? 0 : 1); this.retention = retention; this.inUseCheck = inUseCheck; @@ -146,7 +136,7 @@ public class Cache { public void invalidateAll() { synchronized (this.internal) { for (V value : this.internal.values()) { - this.postRemoval.run(value); + this.postRemoval.apply(value); } this.expiry.clear(); this.internal.clear(); @@ -160,7 +150,7 @@ public class Cache { private void lazyCheck() { long now = System.currentTimeMillis(); synchronized (this.internal) { - List inUse = new ArrayList(); + List inUse = new ArrayList<>(); for (Iterator> iterator = this.expiry.entries().iterator(); iterator .hasNext();) { Map.Entry entry = iterator.next(); @@ -171,7 +161,7 @@ public class Cache { iterator.remove(); - if (this.inUseCheck.run(this.internal.get(entry.getValue()))) { + if (this.inUseCheck.apply(this.internal.get(entry.getValue()))) { inUse.add(entry.getValue()); continue; } @@ -182,7 +172,7 @@ public class Cache { continue; } - this.postRemoval.run(value); + this.postRemoval.apply(value); } long nextExpiry = now + this.retention; diff --git a/common/src/main/java/com/lishid/openinv/util/Function.java b/common/src/main/java/com/lishid/openinv/util/Function.java deleted file mode 100644 index b650684..0000000 --- a/common/src/main/java/com/lishid/openinv/util/Function.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2011-2020 lishid. All rights reserved. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3. - * - * 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.lishid.openinv.util; - -/** - * Abstraction for some simple cache calls. - * - * @author Jikoo - */ -public abstract class Function { - - public abstract boolean run(V value); - -} diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index bc49b5c..9553332 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -70,47 +70,41 @@ public class OpenInv extends JavaPlugin implements IOpenInv { private final Multimap> pluginUsage = HashMultimap.create(); private final Cache playerCache = new Cache<>(300000L, - new Function() { - @Override - public boolean run(final Player value) { + value -> { + String key = OpenInv.this.getPlayerID(value); - String key = OpenInv.this.getPlayerID(value); - return OpenInv.this.inventories.containsKey(key) - && OpenInv.this.inventories.get(key).isInUse() - || OpenInv.this.enderChests.containsKey(key) - && OpenInv.this.enderChests.get(key).isInUse() - || OpenInv.this.pluginUsage.containsKey(key); + return OpenInv.this.inventories.containsKey(key) + && OpenInv.this.inventories.get(key).isInUse() + || OpenInv.this.enderChests.containsKey(key) + && OpenInv.this.enderChests.get(key).isInUse() + || OpenInv.this.pluginUsage.containsKey(key); + }, + value -> { + String key = OpenInv.this.getPlayerID(value); + + // Check if inventory is stored, and if it is, remove it and eject all viewers + if (OpenInv.this.inventories.containsKey(key)) { + Inventory inv = OpenInv.this.inventories.remove(key).getBukkitInventory(); + List viewers = inv.getViewers(); + for (HumanEntity entity : viewers.toArray(new HumanEntity[0])) { + entity.closeInventory(); + } } - }, new Function() { - @Override - public boolean run(final Player value) { - String key = OpenInv.this.getPlayerID(value); - - // Check if inventory is stored, and if it is, remove it and eject all viewers - if (OpenInv.this.inventories.containsKey(key)) { - Inventory inv = OpenInv.this.inventories.remove(key).getBukkitInventory(); - List viewers = inv.getViewers(); - for (HumanEntity entity : viewers.toArray(new HumanEntity[0])) { - entity.closeInventory(); + // Check if ender chest is stored, and if it is, remove it and eject all viewers + if (OpenInv.this.enderChests.containsKey(key)) { + Inventory inv = OpenInv.this.enderChests.remove(key).getBukkitInventory(); + List viewers = inv.getViewers(); + for (HumanEntity entity : viewers.toArray(new HumanEntity[0])) { + entity.closeInventory(); + } } - } - // Check if ender chest is stored, and if it is, remove it and eject all viewers - if (OpenInv.this.enderChests.containsKey(key)) { - Inventory inv = OpenInv.this.enderChests.remove(key).getBukkitInventory(); - List viewers = inv.getViewers(); - for (HumanEntity entity : viewers.toArray(new HumanEntity[0])) { - entity.closeInventory(); + if (!OpenInv.this.disableSaving() && !value.isOnline()) { + value.saveData(); } - } - - if (!OpenInv.this.disableSaving() && !value.isOnline()) { - value.saveData(); - } - return true; - } - }); + return true; + }); private InternalAccessor accessor; diff --git a/pom.xml b/pom.xml index e107501..959b6c3 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.0.0 + 3.2.2