Implement suggestions from PR comments

This commit is contained in:
Alexander Söderberg 2020-07-24 12:15:07 +02:00 committed by Alexander Söderberg
parent d2f40612f4
commit 722361aedb
6 changed files with 41 additions and 8 deletions

View File

@ -94,7 +94,7 @@ public class Auto extends SubCommand {
if (diff - sizeX * sizeZ < 0) { if (diff - sizeX * sizeZ < 0) {
try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData( try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(
PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) { PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) {
if (metaDataAccess.has()) { if (metaDataAccess.isPresent()) {
int grantedPlots = metaDataAccess.get().orElse(0); int grantedPlots = metaDataAccess.get().orElse(0);
if (diff < 0 && grantedPlots < sizeX * sizeZ) { if (diff < 0 && grantedPlots < sizeX * sizeZ) {
MainUtil.sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS); MainUtil.sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS);

View File

@ -92,7 +92,7 @@ public class Claim extends SubCommand {
try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) { try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) {
int grants = 0; int grants = 0;
if (currentPlots >= player.getAllowedPlots() && !force) { if (currentPlots >= player.getAllowedPlots() && !force) {
if (metaDataAccess.has()) { if (metaDataAccess.isPresent()) {
grants = metaDataAccess.get().orElse(0); grants = metaDataAccess.get().orElse(0);
if (grants <= 0) { if (grants <= 0) {
metaDataAccess.remove(); metaDataAccess.remove();

View File

@ -34,7 +34,6 @@ import java.util.Optional;
/** /**
* Access to player meta data * Access to player meta data
* *
* @param <P> Player type
* @param <T> Meta data type * @param <T> Meta data type
*/ */
public abstract class MetaDataAccess<T> implements AutoCloseable { public abstract class MetaDataAccess<T> implements AutoCloseable {
@ -42,6 +41,7 @@ public abstract class MetaDataAccess<T> implements AutoCloseable {
private final PlotPlayer<?> player; private final PlotPlayer<?> player;
private final MetaDataKey<T> metaDataKey; private final MetaDataKey<T> metaDataKey;
private final LockRepository.LockAccess lockAccess; private final LockRepository.LockAccess lockAccess;
private boolean closed = false;
MetaDataAccess(@Nonnull final PlotPlayer<?> player, @Nonnull final MetaDataKey<T> metaDataKey, MetaDataAccess(@Nonnull final PlotPlayer<?> player, @Nonnull final MetaDataKey<T> metaDataKey,
@Nonnull final LockRepository.LockAccess lockAccess) { @Nonnull final LockRepository.LockAccess lockAccess) {
@ -56,12 +56,12 @@ public abstract class MetaDataAccess<T> implements AutoCloseable {
* @return {@code true} if player has meta data with this key, or * @return {@code true} if player has meta data with this key, or
* {@code false} * {@code false}
*/ */
public abstract boolean has(); public abstract boolean isPresent();
/** /**
* Remove the stored value meta data * Remove the stored value meta data
* *
* @return Old value, or {@link null} * @return Old value, or {@code null}
*/ */
@Nullable public abstract T remove(); @Nullable public abstract T remove();
@ -81,6 +81,7 @@ public abstract class MetaDataAccess<T> implements AutoCloseable {
@Override public final void close() { @Override public final void close() {
this.lockAccess.close(); this.lockAccess.close();
this.closed = true;
} }
/** /**
@ -101,4 +102,28 @@ public abstract class MetaDataAccess<T> implements AutoCloseable {
return this.metaDataKey; 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 <E extends Throwable> void sneakyThrow(final Throwable e) throws E {
throw (E) e;
}
} }

View File

@ -39,11 +39,13 @@ final class PersistentMetaDataAccess<T> extends MetaDataAccess<T> {
super(player, metaDataKey, lockAccess); super(player, metaDataKey, lockAccess);
} }
@Override public boolean has() { @Override public boolean isPresent() {
this.checkClosed();
return this.getPlayer().hasPersistentMeta(getMetaDataKey().toString()); return this.getPlayer().hasPersistentMeta(getMetaDataKey().toString());
} }
@Override @Nullable public T remove() { @Override @Nullable public T remove() {
this.checkClosed();
final Object old = this.getPlayer().removePersistentMeta(this.getMetaDataKey().toString()); final Object old = this.getPlayer().removePersistentMeta(this.getMetaDataKey().toString());
if (old == null) { if (old == null) {
return null; return null;
@ -52,10 +54,12 @@ final class PersistentMetaDataAccess<T> extends MetaDataAccess<T> {
} }
@Override public void set(@Nonnull T value) { @Override public void set(@Nonnull T value) {
this.checkClosed();
this.getPlayer().setPersistentMeta(this.getMetaDataKey(), value); this.getPlayer().setPersistentMeta(this.getMetaDataKey(), value);
} }
@Nonnull @Override public Optional<T> get() { @Nonnull @Override public Optional<T> get() {
this.checkClosed();
return Optional.ofNullable(this.getPlayer().getPersistentMeta(this.getMetaDataKey())); return Optional.ofNullable(this.getPlayer().getPersistentMeta(this.getMetaDataKey()));
} }

View File

@ -39,11 +39,13 @@ final class TemporaryMetaDataAccess<T> extends MetaDataAccess<T> {
super(player, metaDataKey, lockAccess); super(player, metaDataKey, lockAccess);
} }
@Override public boolean has() { @Override public boolean isPresent() {
this.checkClosed();
return this.getPlayer().getMeta(this.getMetaDataKey().toString()) != null; return this.getPlayer().getMeta(this.getMetaDataKey().toString()) != null;
} }
@Override @Nullable public T remove() { @Override @Nullable public T remove() {
this.checkClosed();
final Object old = getPlayer().deleteMeta(this.getMetaDataKey().toString()); final Object old = getPlayer().deleteMeta(this.getMetaDataKey().toString());
if (old == null) { if (old == null) {
return null; return null;
@ -52,10 +54,12 @@ final class TemporaryMetaDataAccess<T> extends MetaDataAccess<T> {
} }
@Override public void set(@Nonnull T value) { @Override public void set(@Nonnull T value) {
this.checkClosed();
this.getPlayer().setMeta(this.getMetaDataKey().toString(), null); this.getPlayer().setMeta(this.getMetaDataKey().toString(), null);
} }
@Nonnull @Override public Optional<T> get() { @Nonnull @Override public Optional<T> get() {
this.checkClosed();
return Optional.ofNullable(this.getPlayer().getMeta(this.getMetaDataKey().toString())); return Optional.ofNullable(this.getPlayer().getMeta(this.getMetaDataKey().toString()));
} }

View File

@ -142,7 +142,7 @@ public class ExpireManager {
public void confirmExpiry(final PlotPlayer<?> pp) { public void confirmExpiry(final PlotPlayer<?> pp) {
try (final MetaDataAccess<Boolean> metaDataAccess = pp.accessTemporaryMetaData( try (final MetaDataAccess<Boolean> metaDataAccess = pp.accessTemporaryMetaData(
PlayerMetaDataKeys.TEMPORARY_IGNORE_EXPIRE_TASK)) { PlayerMetaDataKeys.TEMPORARY_IGNORE_EXPIRE_TASK)) {
if (metaDataAccess.has()) { if (metaDataAccess.isPresent()) {
return; return;
} }
if (plotsToDelete != null && !plotsToDelete.isEmpty() && pp.hasPermission("plots.admin.command.autoclear")) { if (plotsToDelete != null && !plotsToDelete.isEmpty() && pp.hasPermission("plots.admin.command.autoclear")) {