From 722361aedb0b363e3830367d8bd0ebccce271183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 24 Jul 2020 12:15:07 +0200 Subject: [PATCH] Implement suggestions from PR comments --- .../com/plotsquared/core/command/Auto.java | 2 +- .../com/plotsquared/core/command/Claim.java | 2 +- .../core/player/MetaDataAccess.java | 31 +++++++++++++++++-- .../core/player/PersistentMetaDataAccess.java | 6 +++- .../core/player/TemporaryMetaDataAccess.java | 6 +++- .../core/plot/expiration/ExpireManager.java | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Auto.java b/Core/src/main/java/com/plotsquared/core/command/Auto.java index 2cd105ffa..aadb763c8 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -94,7 +94,7 @@ public class Auto extends SubCommand { if (diff - sizeX * sizeZ < 0) { try (final MetaDataAccess metaDataAccess = player.accessPersistentMetaData( PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) { - if (metaDataAccess.has()) { + if (metaDataAccess.isPresent()) { int grantedPlots = metaDataAccess.get().orElse(0); if (diff < 0 && grantedPlots < sizeX * sizeZ) { MainUtil.sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS); diff --git a/Core/src/main/java/com/plotsquared/core/command/Claim.java b/Core/src/main/java/com/plotsquared/core/command/Claim.java index fbbd25013..d2285e7ba 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Claim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Claim.java @@ -92,7 +92,7 @@ public class Claim extends SubCommand { try (final MetaDataAccess metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) { int grants = 0; if (currentPlots >= player.getAllowedPlots() && !force) { - if (metaDataAccess.has()) { + if (metaDataAccess.isPresent()) { grants = metaDataAccess.get().orElse(0); if (grants <= 0) { metaDataAccess.remove(); diff --git a/Core/src/main/java/com/plotsquared/core/player/MetaDataAccess.java b/Core/src/main/java/com/plotsquared/core/player/MetaDataAccess.java index c38111b73..800f1c080 100644 --- a/Core/src/main/java/com/plotsquared/core/player/MetaDataAccess.java +++ b/Core/src/main/java/com/plotsquared/core/player/MetaDataAccess.java @@ -34,7 +34,6 @@ import java.util.Optional; /** * Access to player meta data * - * @param

Player type * @param Meta data type */ public abstract class MetaDataAccess implements AutoCloseable { @@ -42,6 +41,7 @@ public abstract class MetaDataAccess implements AutoCloseable { private final PlotPlayer player; private final MetaDataKey metaDataKey; private final LockRepository.LockAccess lockAccess; + private boolean closed = false; MetaDataAccess(@Nonnull final PlotPlayer player, @Nonnull final MetaDataKey metaDataKey, @Nonnull final LockRepository.LockAccess lockAccess) { @@ -56,12 +56,12 @@ public abstract class MetaDataAccess implements AutoCloseable { * @return {@code true} if player has meta data with this key, or * {@code false} */ - public abstract boolean has(); + public abstract boolean isPresent(); /** * Remove the stored value meta data * - * @return Old value, or {@link null} + * @return Old value, or {@code null} */ @Nullable public abstract T remove(); @@ -81,6 +81,7 @@ public abstract class MetaDataAccess implements AutoCloseable { @Override public final void close() { this.lockAccess.close(); + this.closed = true; } /** @@ -101,4 +102,28 @@ public abstract class MetaDataAccess implements AutoCloseable { return this.metaDataKey; } + /** + * Check whether or not the meta data access has been closed. + * After being closed, all attempts to access the meta data + * through the instance, will lead to {@link IllegalAccessException} + * being thrown + * + * @return {@code true} if the access has been closed + */ + public boolean isClosed() { + return this.closed; + } + + protected void checkClosed() { + if (this.closed) { + sneakyThrow(new IllegalAccessException("The meta data access instance has been closed")); + } + } + + @SuppressWarnings("ALL") + private static void sneakyThrow(final Throwable e) throws E { + throw (E) e; + } + + } diff --git a/Core/src/main/java/com/plotsquared/core/player/PersistentMetaDataAccess.java b/Core/src/main/java/com/plotsquared/core/player/PersistentMetaDataAccess.java index e7bb67103..1e3767c7d 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PersistentMetaDataAccess.java +++ b/Core/src/main/java/com/plotsquared/core/player/PersistentMetaDataAccess.java @@ -39,11 +39,13 @@ final class PersistentMetaDataAccess extends MetaDataAccess { super(player, metaDataKey, lockAccess); } - @Override public boolean has() { + @Override public boolean isPresent() { + this.checkClosed(); return this.getPlayer().hasPersistentMeta(getMetaDataKey().toString()); } @Override @Nullable public T remove() { + this.checkClosed(); final Object old = this.getPlayer().removePersistentMeta(this.getMetaDataKey().toString()); if (old == null) { return null; @@ -52,10 +54,12 @@ final class PersistentMetaDataAccess extends MetaDataAccess { } @Override public void set(@Nonnull T value) { + this.checkClosed(); this.getPlayer().setPersistentMeta(this.getMetaDataKey(), value); } @Nonnull @Override public Optional get() { + this.checkClosed(); return Optional.ofNullable(this.getPlayer().getPersistentMeta(this.getMetaDataKey())); } diff --git a/Core/src/main/java/com/plotsquared/core/player/TemporaryMetaDataAccess.java b/Core/src/main/java/com/plotsquared/core/player/TemporaryMetaDataAccess.java index b301f5be7..22c5dd631 100644 --- a/Core/src/main/java/com/plotsquared/core/player/TemporaryMetaDataAccess.java +++ b/Core/src/main/java/com/plotsquared/core/player/TemporaryMetaDataAccess.java @@ -39,11 +39,13 @@ final class TemporaryMetaDataAccess extends MetaDataAccess { super(player, metaDataKey, lockAccess); } - @Override public boolean has() { + @Override public boolean isPresent() { + this.checkClosed(); return this.getPlayer().getMeta(this.getMetaDataKey().toString()) != null; } @Override @Nullable public T remove() { + this.checkClosed(); final Object old = getPlayer().deleteMeta(this.getMetaDataKey().toString()); if (old == null) { return null; @@ -52,10 +54,12 @@ final class TemporaryMetaDataAccess extends MetaDataAccess { } @Override public void set(@Nonnull T value) { + this.checkClosed(); this.getPlayer().setMeta(this.getMetaDataKey().toString(), null); } @Nonnull @Override public Optional get() { + this.checkClosed(); return Optional.ofNullable(this.getPlayer().getMeta(this.getMetaDataKey().toString())); } diff --git a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java index 6b14d0866..e0fb65d26 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java @@ -142,7 +142,7 @@ public class ExpireManager { public void confirmExpiry(final PlotPlayer pp) { try (final MetaDataAccess metaDataAccess = pp.accessTemporaryMetaData( PlayerMetaDataKeys.TEMPORARY_IGNORE_EXPIRE_TASK)) { - if (metaDataAccess.has()) { + if (metaDataAccess.isPresent()) { return; } if (plotsToDelete != null && !plotsToDelete.isEmpty() && pp.hasPermission("plots.admin.command.autoclear")) {