PlaceholderContext now extends MathContext

This commit is contained in:
Auxilor
2023-04-25 19:11:15 +01:00
parent 36d47c55a1
commit f566aec00e
2 changed files with 187 additions and 43 deletions

View File

@@ -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<AdditionalPlayer> 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<AdditionalPlayer> 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<AdditionalPlayer> additionalPlayers) {
this.injectableContext = injectableContext;
this.player = player;
this.additionalPlayers = additionalPlayers;
}
/**
* Returns the PlaceholderInjectable parse context.
* <p>
* 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.
* <p>
* 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.
* <p>
* Duplicate method because MathContext used to be a record.
*
* @return The additional players.
*/
@NotNull
public Collection<AdditionalPlayer> additionalPlayers() {
return additionalPlayers;
}
/**
* Returns the additional players.
*
* @return The additional players.
*/
@NotNull
public Collection<AdditionalPlayer> 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
);
}
}

View File

@@ -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<AdditionalPlayer> 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<AdditionalPlayer> 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()
);
}
}