From c3bf1e60fc068afad0d6d222ef825ca71f170cc3 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Thu, 28 Jul 2022 19:44:21 +0200 Subject: [PATCH] Started attempt to make the API more user friendly --- .../the/gr8/playerstats/api/APIRequest.java | 15 ++++++ .../gr8/playerstats/api/PlayerRequest.java | 10 ++++ .../playerstats/api/PlayerStatRequest.java | 48 +++++++++++++++++++ .../the/gr8/playerstats/api/PlayerStats.java | 2 +- .../gr8/playerstats/api/PlayerStatsAPI.java | 28 +++++++++-- .../playerstats/api/ServerStatRequest.java | 4 ++ 6 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/api/APIRequest.java create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerRequest.java create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatRequest.java create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ServerStatRequest.java diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/APIRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/APIRequest.java new file mode 100644 index 0000000..7d7869a --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/APIRequest.java @@ -0,0 +1,15 @@ +package com.gmail.artemis.the.gr8.playerstats.api; + +import com.gmail.artemis.the.gr8.playerstats.models.StatRequest; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; + +public interface APIRequest { + + StatRequest untyped(Statistic statistic); + + StatRequest blockOrItemType(Statistic statistic, Material material); + + StatRequest entityType(Statistic statistic, EntityType entityType); +} diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerRequest.java new file mode 100644 index 0000000..d1798d9 --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerRequest.java @@ -0,0 +1,10 @@ +package com.gmail.artemis.the.gr8.playerstats.api; + +public abstract class PlayerRequest implements APIRequest { + + protected final String playerName; + + public PlayerRequest(String playerName) { + this.playerName = playerName; + } +} diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatRequest.java new file mode 100644 index 0000000..085bdbe --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatRequest.java @@ -0,0 +1,48 @@ +package com.gmail.artemis.the.gr8.playerstats.api; + +import com.gmail.artemis.the.gr8.playerstats.enums.Target; +import com.gmail.artemis.the.gr8.playerstats.models.StatRequest; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; +import org.jetbrains.annotations.NotNull; + +public class PlayerStatRequest extends PlayerRequest { + + public PlayerStatRequest(String playerName) { + super(playerName); + } + + @Override + public StatRequest untyped(Statistic statistic) { + StatRequest request = getBasePlayerRequest(); + request.setStatistic(statistic); + return request; + } + + @Override + public StatRequest blockOrItemType(@NotNull Statistic statistic, Material material) { + StatRequest request = getBasePlayerRequest(statistic); + request.setSubStatEntryName(material.toString()); + if (statistic.getType() == Statistic.Type.BLOCK) { + request.setBlock(material); + } else { + request.setItem(material); + } + return request; + } + + @Override + public StatRequest entityType(Statistic statistic, EntityType entityType) { + return null; + } + + private StatRequest getBasePlayerRequest(@NotNull Statistic statistic) { + StatRequest request = new StatRequest(Bukkit.getConsoleSender(), true); + request.setStatistic(statistic); + request.setTarget(Target.PLAYER); + request.setPlayerName(super.playerName); + return request; + } +} diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java index 8d1db2d..e4792db 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java @@ -26,7 +26,7 @@ public interface PlayerStats { return Main.getPlayerStatsAPI(); } - StatResult getPlayerStat(String playerName, Statistic statistic, Material material, EntityType entity); + PlayerRequest getPlayerStat(String playerName); StatResult getServerStat(Statistic statistic, Material material, EntityType entity); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java index bd91d08..aff6bfa 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java @@ -27,12 +27,23 @@ public final class PlayerStatsAPI implements PlayerStats { statFormatter = format; } + public PlayerRequest getPlayerStat(String playerName) { + return ; + } + @Override - public StatResult getPlayerStat(String playerName, Statistic statistic, Material material, EntityType entity) { - StatRequest request = getStatRequest(Target.PLAYER, statistic, material, entity, playerName); - int stat = statCalculator.getPlayerStat(request); - TextComponent prettyStat = statFormatter.formatPlayerStat(request, stat); - return new PlayerStatResult(stat, prettyStat); + public StatResult getPlayerStat(String playerName, Statistic statistic) { + return getPlayerStat(playerName, statistic, null, null); + } + + @Override + public StatResult getPlayerStat(String playerName, Statistic statistic, Material material) { + return getPlayerStat(playerName, statistic, material, null); + } + + @Override + public StatResult getPlayerStat(String playerName, Statistic statistic, EntityType entityType) { + return getPlayerStat(playerName, statistic, null, entityType); } @Override @@ -51,6 +62,13 @@ public final class PlayerStatsAPI implements PlayerStats { return new TopStatResult(stat, prettyStat); } + private PlayerStatResult getPlayerStat(String playerName, Statistic statistic, Material material, EntityType entityType) { + StatRequest request = getStatRequest(Target.PLAYER, statistic, material, entityType, playerName); + int stat = statCalculator.getPlayerStat(request); + TextComponent prettyStat = statFormatter.formatPlayerStat(request, stat); + return new PlayerStatResult(stat, prettyStat); + } + private StatRequest getStatRequest(Target target, Statistic statistic, Material material, EntityType entity, String playerName) throws NullPointerException { StatRequest request = requestGenerator.generateAPIRequest(target, statistic, material, entity, playerName); if (requestGenerator.validateAPIRequest(request)) { diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ServerStatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ServerStatRequest.java new file mode 100644 index 0000000..53b912b --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ServerStatRequest.java @@ -0,0 +1,4 @@ +package com.gmail.artemis.the.gr8.playerstats.api; + +public interface ServerStatRequest extends APIRequest { +}