Moved some logic out of StatRequest to keep API and core separated

This commit is contained in:
Artemis-the-gr8 2023-02-10 12:53:04 +01:00
parent 1fe07176ef
commit 4a67dfef10
4 changed files with 38 additions and 28 deletions

View File

@ -1,8 +1,6 @@
package com.artemis.the.gr8.playerstats.api;
import com.artemis.the.gr8.playerstats.api.enums.Target;
import com.artemis.the.gr8.playerstats.core.config.ConfigHandler;
import com.artemis.the.gr8.playerstats.core.utils.OfflinePlayerHandler;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
@ -24,6 +22,8 @@ public abstract class StatRequest<T> {
settings = new Settings(requester);
}
public abstract boolean isValid();
/**
* Use this method to view the settings that have
* been configured for this StatRequest.
@ -32,15 +32,6 @@ public abstract class StatRequest<T> {
return settings;
}
public boolean isValid() {
if (settings.statistic == null) {
return false;
} else if (!hasValidTarget()) {
return false;
}
return hasMatchingSubStat();
}
protected void configureForPlayer(String playerName) {
this.settings.target = Target.PLAYER;
this.settings.playerName = playerName;
@ -86,25 +77,11 @@ public abstract class StatRequest<T> {
this.settings.subStatEntryName = entityType.toString();
}
private boolean hasValidTarget() {
if (settings.target == null) {
protected boolean hasMatchingSubStat() {
if (settings.statistic == null) {
return false;
}
else if (settings.target == Target.PLAYER) {
OfflinePlayerHandler offlinePlayerHandler = OfflinePlayerHandler.getInstance();
if (settings.playerName == null) {
return false;
} else if (offlinePlayerHandler.isExcludedPlayer(settings.playerName)) {
return ConfigHandler.getInstance().allowPlayerLookupsForExcludedPlayers();
} else {
return (offlinePlayerHandler.isIncludedPlayer(settings.playerName));
}
}
return true;
}
private boolean hasMatchingSubStat() {
switch (settings.statistic.getType()) {
case BLOCK -> {
return settings.block != null;
@ -141,7 +118,6 @@ public abstract class StatRequest<T> {
this.sender = sender;
}
public @NotNull CommandSender getCommandSender() {
return sender;
}

View File

@ -2,6 +2,8 @@ package com.artemis.the.gr8.playerstats.core.statrequest;
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.artemis.the.gr8.playerstats.api.StatRequest;
import com.artemis.the.gr8.playerstats.core.config.ConfigHandler;
import com.artemis.the.gr8.playerstats.core.utils.OfflinePlayerHandler;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Statistic;
@ -20,6 +22,28 @@ public final class PlayerStatRequest extends StatRequest<Integer> implements Req
super.configureForPlayer(playerName);
}
@Override
public boolean isValid() {
if (!hasValidTarget()) {
return false;
}
return super.hasMatchingSubStat();
}
private boolean hasValidTarget() {
StatRequest.Settings settings = super.getSettings();
if (settings.getPlayerName() == null) {
return false;
}
OfflinePlayerHandler offlinePlayerHandler = OfflinePlayerHandler.getInstance();
if (offlinePlayerHandler.isExcludedPlayer(settings.getPlayerName())) {
return ConfigHandler.getInstance().allowPlayerLookupsForExcludedPlayers();
} else {
return offlinePlayerHandler.isIncludedPlayer(settings.getPlayerName());
}
}
@Override
public StatRequest<Integer> untyped(@NotNull Statistic statistic) {
super.configureUntyped(statistic);

View File

@ -21,6 +21,11 @@ public final class ServerStatRequest extends StatRequest<Long> implements Reques
super.configureForServer();
}
@Override
public boolean isValid() {
return super.hasMatchingSubStat();
}
@Override
public StatRequest<Long> untyped(@NotNull Statistic statistic) {
super.configureUntyped(statistic);

View File

@ -22,6 +22,11 @@ public final class TopStatRequest extends StatRequest<LinkedHashMap<String, Inte
super.configureForTop(topListSize);
}
@Override
public boolean isValid() {
return super.hasMatchingSubStat();
}
@Override
public StatRequest<LinkedHashMap<String, Integer>> untyped(@NotNull Statistic statistic) {
super.configureUntyped(statistic);