diff --git a/eco-api/src/main/java/com/willfp/eco/core/math/MathContext.java b/eco-api/src/main/java/com/willfp/eco/core/math/MathContext.java index b629e35d..c4da6670 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/math/MathContext.java +++ b/eco-api/src/main/java/com/willfp/eco/core/math/MathContext.java @@ -10,21 +10,11 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Collections; +import java.util.Objects; -/** - * Represents a parseContext to do math in. - * - * @param injectableContext The PlaceholderInjectable parseContext. - * @param player The player. - * @param additionalPlayers The additional players. - */ -public record MathContext( - @NotNull PlaceholderInjectable injectableContext, - @Nullable Player player, - @NotNull Collection additionalPlayers -) { +public class MathContext { /** - * Empty math parseContext. + * Returns an empty math parseContext. */ public static final MathContext EMPTY = new MathContext( PlaceholderManager.EMPTY_INJECTABLE, @@ -32,6 +22,150 @@ public record MathContext( Collections.emptyList() ); + /** + * The PlaceholderInjectable parse context. + */ + @NotNull + private final PlaceholderInjectable injectableContext; + + /** + * The player. + */ + @Nullable + private final Player player; + + /** + * The additional players. + */ + @NotNull + private final Collection additionalPlayers; + + /** + * Constructs a new MathContext with the given parameters. + * + * @param injectableContext The PlaceholderInjectable parseContext. + * @param player The player. + * @param additionalPlayers The additional players. + */ + public MathContext(@NotNull PlaceholderInjectable injectableContext, + @Nullable Player player, + @NotNull Collection additionalPlayers) { + this.injectableContext = injectableContext; + this.player = player; + this.additionalPlayers = additionalPlayers; + } + + /** + * Returns the PlaceholderInjectable parse context. + *

+ * Duplicate method because MathContext used to be a record. + * + * @return The injectable context. + */ + @NotNull + public PlaceholderInjectable injectableContext() { + return injectableContext; + } + + /** + * Returns the PlaceholderInjectable parse context. + * + * @return The injectable context. + */ + @NotNull + public PlaceholderInjectable getInjectableContext() { + return injectableContext; + } + + /** + * Returns the player. + *

+ * Duplicate method because MathContext used to be a record. + * + * @return The player. + */ + @Nullable + public Player player() { + return player; + } + + /** + * Returns the player. + * + * @return The player. + */ + @Nullable + public Player getPlayer() { + return player; + } + + /** + * Returns the additional players. + *

+ * Duplicate method because MathContext used to be a record. + * + * @return The additional players. + */ + @NotNull + public Collection additionalPlayers() { + return additionalPlayers; + } + + /** + * Returns the additional players. + * + * @return The additional players. + */ + @NotNull + public Collection getAdditionalPlayers() { + return additionalPlayers; + } + + /** + * Convert to PlaceholderContext. + * + * @return The PlaceholderContext. + */ + @NotNull + public PlaceholderContext toPlaceholderContext() { + return new PlaceholderContext( + this.player, + null, + this.injectableContext, + this.additionalPlayers + ); + } + + @Override + public String toString() { + return "MathContext{" + + "injectableContext=" + injectableContext + + ", player=" + player + + ", additionalPlayers=" + additionalPlayers + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + + if (!(o instanceof MathContext)) { + return false; + } + MathContext that = (MathContext) o; + + return injectableContext.equals(that.injectableContext) && + Objects.equals(player, that.player) && + additionalPlayers.equals(that.additionalPlayers); + } + + @Override + public int hashCode() { + return Objects.hash(injectableContext, player, additionalPlayers); + } + /** * Create MathContext of a PlaceholderInjectable parseContext. * @@ -56,23 +190,9 @@ public record MathContext( public static MathContext copyWithPlayer(@NotNull final MathContext context, @Nullable final Player player) { return new MathContext( - context.injectableContext(), + context.injectableContext, player, - context.additionalPlayers() - ); - } - - /** - * Convert to PlaceholderContext. - * - * @return The PlaceholderContext. - */ - public PlaceholderContext toPlaceholderContext() { - return new PlaceholderContext( - this.player, - null, - this.injectableContext, - this.additionalPlayers + context.additionalPlayers ); } } diff --git a/eco-api/src/main/java/com/willfp/eco/core/placeholder/parsing/PlaceholderContext.java b/eco-api/src/main/java/com/willfp/eco/core/placeholder/parsing/PlaceholderContext.java index 90d1d515..b6c68568 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/placeholder/parsing/PlaceholderContext.java +++ b/eco-api/src/main/java/com/willfp/eco/core/placeholder/parsing/PlaceholderContext.java @@ -1,6 +1,7 @@ package com.willfp.eco.core.placeholder.parsing; import com.willfp.eco.core.integrations.placeholder.PlaceholderManager; +import com.willfp.eco.core.math.MathContext; import com.willfp.eco.core.placeholder.AdditionalPlayer; import com.willfp.eco.core.placeholder.PlaceholderInjectable; import org.bukkit.entity.Player; @@ -13,18 +14,8 @@ import java.util.Collections; /** * Represents a context to translate placeholders in. - * - * @param player The player. - * @param itemStack The ItemStack. - * @param injectableContext The injectable context. - * @param additionalPlayers The additional players. */ -public record PlaceholderContext( - @Nullable Player player, - @Nullable ItemStack itemStack, - @NotNull PlaceholderInjectable injectableContext, - @NotNull Collection additionalPlayers -) { +public class PlaceholderContext extends MathContext { /** * An empty context. */ @@ -35,6 +26,39 @@ public record PlaceholderContext( Collections.emptyList() ); + /** + * The ItemStack. + */ + @Nullable + private final ItemStack itemStack; + + /** + * Constructs a new PlaceholderContext with the given parameters. + * + * @param player The player. + * @param itemStack The ItemStack. + * @param injectableContext The PlaceholderInjectable parseContext. + * @param additionalPlayers The additional players. + */ + public PlaceholderContext(@Nullable final Player player, + @Nullable final ItemStack itemStack, + @NotNull final PlaceholderInjectable injectableContext, + @NotNull final Collection additionalPlayers) { + super(injectableContext, player, additionalPlayers); + + this.itemStack = itemStack; + } + + /** + * Get the ItemStack. + * + * @return The ItemStack. + */ + @Nullable + public ItemStack getItemStack() { + return itemStack; + } + /** * Create MathContext of a PlaceholderInjectable parseContext. * @@ -59,9 +83,9 @@ public record PlaceholderContext( public PlaceholderContext copyWithPlayer(@Nullable final Player player) { return new PlaceholderContext( player, - this.itemStack(), - this.injectableContext(), - this.additionalPlayers() + this.getItemStack(), + this.getInjectableContext(), + this.getAdditionalPlayers() ); } }