Merged different StatResult classes into one typed record

This commit is contained in:
Artemis-the-gr8 2022-10-07 19:48:37 +02:00
parent e4fca5a0c8
commit b46a25d23f
9 changed files with 36 additions and 114 deletions

View File

@ -18,7 +18,7 @@ import java.util.Arrays;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class InternalStatRequest extends StatRequest<Object> { public final class InternalStatRequest extends StatRequest<Object> {
private final OfflinePlayerHandler offlinePlayerHandler; private final OfflinePlayerHandler offlinePlayerHandler;
private final EnumHandler enumHandler; private final EnumHandler enumHandler;

View File

@ -1,9 +1,9 @@
package com.artemis.the.gr8.playerstats.statistic.request; package com.artemis.the.gr8.playerstats.statistic.request;
import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.Main;
import com.artemis.the.gr8.playerstats.statistic.result.PlayerStatResult;
import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -39,11 +39,7 @@ public final class PlayerStatRequest extends StatRequest<Integer> implements Req
} }
@Override @Override
public PlayerStatResult execute() { public @NotNull StatResult<Integer> execute() {
return getStatResult();
}
private PlayerStatResult getStatResult() {
int stat = Main int stat = Main
.getStatCalculator() .getStatCalculator()
.getPlayerStat(settings); .getPlayerStat(settings);
@ -56,6 +52,6 @@ public final class PlayerStatRequest extends StatRequest<Integer> implements Req
.getTranslatableComponentSerializer() .getTranslatableComponentSerializer()
.serialize(prettyComponent); .serialize(prettyComponent);
return new PlayerStatResult(stat, prettyComponent, prettyString); return new StatResult<>(stat, prettyComponent, prettyString);
} }
} }

View File

@ -1,9 +1,9 @@
package com.artemis.the.gr8.playerstats.statistic.request; package com.artemis.the.gr8.playerstats.statistic.request;
import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.Main;
import com.artemis.the.gr8.playerstats.statistic.result.ServerStatResult;
import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -39,11 +39,7 @@ public final class ServerStatRequest extends StatRequest<Long> implements Reques
} }
@Override @Override
public ServerStatResult execute() { public @NotNull StatResult<Long> execute() {
return getStatResult();
}
private ServerStatResult getStatResult() {
long stat = Main long stat = Main
.getStatCalculator() .getStatCalculator()
.getServerStat(settings); .getServerStat(settings);
@ -56,6 +52,6 @@ public final class ServerStatRequest extends StatRequest<Long> implements Reques
.getTranslatableComponentSerializer() .getTranslatableComponentSerializer()
.serialize(prettyComponent); .serialize(prettyComponent);
return new ServerStatResult(stat, prettyComponent, prettyString); return new StatResult<>(stat, prettyComponent, prettyString);
} }
} }

View File

@ -1,7 +1,7 @@
package com.artemis.the.gr8.playerstats.statistic.request; package com.artemis.the.gr8.playerstats.statistic.request;
import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.Main;
import com.artemis.the.gr8.playerstats.statistic.result.TopStatResult; import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
@ -41,11 +41,7 @@ public final class TopStatRequest extends StatRequest<LinkedHashMap<String, Inte
} }
@Override @Override
public TopStatResult execute() { public @NotNull StatResult<LinkedHashMap<String, Integer>> execute() {
return getStatResult();
}
private TopStatResult getStatResult() {
LinkedHashMap<String, Integer> stat = Main LinkedHashMap<String, Integer> stat = Main
.getStatCalculator() .getStatCalculator()
.getTopStats(settings); .getTopStats(settings);
@ -58,6 +54,6 @@ public final class TopStatRequest extends StatRequest<LinkedHashMap<String, Inte
.getTranslatableComponentSerializer() .getTranslatableComponentSerializer()
.serialize(prettyComponent); .serialize(prettyComponent);
return new TopStatResult(stat, prettyComponent, prettyString); return new StatResult<>(stat, prettyComponent, prettyString);
} }
} }

View File

@ -7,7 +7,7 @@ import net.kyori.adventure.text.TextComponent;
* This Record is used to store stat-results internally, * This Record is used to store stat-results internally,
* so Players can share them by clicking a share-button. * so Players can share them by clicking a share-button.
*/ */
public record InternalStatResult(String executorName, TextComponent formattedValue, int ID) implements StatResult<Integer> { public record InternalStatResult(String executorName, TextComponent formattedValue, int ID) {
/** /**
* Gets the ID number for this StatResult. Unlike for the * Gets the ID number for this StatResult. Unlike for the
@ -19,17 +19,14 @@ public record InternalStatResult(String executorName, TextComponent formattedVal
* *
@return Integer that represents this StatResult's ID number @return Integer that represents this StatResult's ID number
*/ */
@Override
public Integer getNumericalValue() { public Integer getNumericalValue() {
return ID; return ID;
} }
@Override
public TextComponent getFormattedTextComponent() { public TextComponent getFormattedTextComponent() {
return formattedValue; return formattedValue;
} }
@Override
public String getFormattedString() { public String getFormattedString() {
return ComponentUtils.getTranslatableComponentSerializer() return ComponentUtils.getTranslatableComponentSerializer()
.serialize(formattedValue); .serialize(formattedValue);

View File

@ -1,21 +0,0 @@
package com.artemis.the.gr8.playerstats.statistic.result;
import net.kyori.adventure.text.TextComponent;
public record PlayerStatResult(int value, TextComponent formattedComponent, String formattedString) implements StatResult<Integer> {
@Override
public Integer getNumericalValue() {
return value;
}
@Override
public TextComponent getFormattedTextComponent() {
return formattedComponent;
}
@Override
public String getFormattedString() {
return formattedString;
}
}

View File

@ -1,21 +0,0 @@
package com.artemis.the.gr8.playerstats.statistic.result;
import net.kyori.adventure.text.TextComponent;
public record ServerStatResult(long value, TextComponent formattedComponent, String formattedString) implements StatResult<Long> {
@Override
public Long getNumericalValue() {
return value;
}
@Override
public TextComponent getFormattedTextComponent() {
return formattedComponent;
}
@Override
public String getFormattedString() {
return formattedString;
}
}

View File

@ -25,7 +25,7 @@ import net.kyori.adventure.text.TextComponent;
* <br> [2.] [player-name] [.....] [formatted-number] * <br> [2.] [player-name] [.....] [formatted-number]
* <br> [3.] etc... * <br> [3.] etc...
* </ul> * </ul>
* <p>
* By default, the resulting message is a {@link TextComponent}, which can be * By default, the resulting message is a {@link TextComponent}, which can be
* sent directly to a Minecraft client or console with the Adventure library. * sent directly to a Minecraft client or console with the Adventure library.
* To send a Component, you need to get a {@link BukkitAudiences} object, * To send a Component, you need to get a {@link BukkitAudiences} object,
@ -35,44 +35,46 @@ import net.kyori.adventure.text.TextComponent;
* on how to get and use the BukkitAudiences object can be found on * on how to get and use the BukkitAudiences object can be found on
* <a href="https://docs.adventure.kyori.net/platform/bukkit.html">Adventure's website</a>. * <a href="https://docs.adventure.kyori.net/platform/bukkit.html">Adventure's website</a>.
* *
* <p>You can also use the provided {@link #getFormattedString()} method to get the * <p>You can also use the provided {@link #formattedString ()} method to get the
* same information in String-format. Don't use Adventure's <code>#content()</code> * same information in String-format. Don't use Adventure's <code>#content()</code>
* or <code>#toString()</code> methods on the Components - those won't get the actual * or <code>#toString()</code> methods on the Components - those won't get the actual
* message. And finally, if you want the results to be formatted differently, * message. And finally, if you want the results to be formatted differently,
* you can get an instance of the {@link ApiFormatter}. * you can get an instance of the {@link ApiFormatter}.
*/ */
public interface StatResult<T> { public record StatResult<T>(T value, TextComponent formattedComponent, String formattedString) {
/** /**
* Gets the raw number for the completed stat-lookup this {@link StatResult} * Gets the raw number for the completed stat-lookup this {@link StatResult} stores.
* stores.
* *
* @return {@code Integer} for playerStat, {@code Long} for serverStat, * @return {@code Integer} for playerStat, {@code Long} for serverStat, and {@code LinkedHashMap<String, Integer>}
* and {@code LinkedHashMap<String, Integer>} for topStat * for topStat
*/ */
T getNumericalValue(); T getNumericalValue() {
return value;
}
/** /**
* Gets the formatted message for the completed stat-lookup this * Gets the formatted message for the completed stat-lookup this StatResult stores.
* StatResult stores. *
* @return a {@code TextComponent} message containing the formatted number. This message follows the same
* @return a {@code TextComponent} message containing the formatted number. * style/color/language settings that are specified in the PlayerStats config. See class description for more
* This message follows the same style/color/language settings that are
* specified in the PlayerStats config. See class description for more
* information. * information.
* @see StatResult * @see StatResult
*/ */
TextComponent getFormattedTextComponent(); TextComponent getFormattedTextComponent() {
return formattedComponent;
}
/** /**
* Gets the formatted message for the completed stat-lookup this * Gets the formatted message for the completed stat-lookup this StatResult stores.
* StatResult stores. *
* @return a String message containing the formatted number. This message follows the same style and color settings
* @return a String message containing the formatted number. This message * that are specified in the PlayerStats config, but it is not translatable (it is always plain English). See class
* follows the same style and color settings that are specified in the * description for more information.
* PlayerStats config, but it is not translatable (it is always plain English).
* See class description for more information.
* @see StatResult * @see StatResult
*/ */
String getFormattedString(); @Override
public String formattedString() {
return formattedString;
}
} }

View File

@ -1,23 +0,0 @@
package com.artemis.the.gr8.playerstats.statistic.result;
import net.kyori.adventure.text.TextComponent;
import java.util.LinkedHashMap;
public record TopStatResult(LinkedHashMap<String, Integer> value, TextComponent formattedComponent, String formattedString) implements StatResult<LinkedHashMap<String,Integer>> {
@Override
public LinkedHashMap<String, Integer> getNumericalValue() {
return value;
}
@Override
public TextComponent getFormattedTextComponent() {
return formattedComponent;
}
@Override
public String getFormattedString() {
return formattedString;
}
}