mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-24 01:19:32 +00:00
checkpoint - 3
This commit is contained in:
@@ -17,16 +17,33 @@
|
||||
|
||||
package net.momirealms.customfishing.api;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.action.ActionManager;
|
||||
import net.momirealms.customfishing.api.mechanic.config.ConfigManager;
|
||||
import net.momirealms.customfishing.api.mechanic.event.EventManager;
|
||||
import net.momirealms.customfishing.api.mechanic.misc.placeholder.PlaceholderManager;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.RequirementManager;
|
||||
import net.momirealms.customfishing.common.plugin.CustomFishingPlugin;
|
||||
import net.momirealms.customfishing.common.sender.SenderFactory;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public abstract class BukkitCustomFishingPlugin implements CustomFishingPlugin {
|
||||
|
||||
private static BukkitCustomFishingPlugin instance;
|
||||
private final Plugin boostrap = requireNonNull(Bukkit.getPluginManager().getPlugin("CustomFishing"));
|
||||
|
||||
protected EventManager eventManager;
|
||||
protected ConfigManager configManager;
|
||||
protected RequirementManager<Player> requirementManager;
|
||||
protected ActionManager<Player> actionManager;
|
||||
protected SenderFactory<BukkitCustomFishingPlugin, CommandSender> senderFactory;
|
||||
protected PlaceholderManager placeholderManager;
|
||||
|
||||
public BukkitCustomFishingPlugin() {
|
||||
instance = this;
|
||||
@@ -47,4 +64,28 @@ public abstract class BukkitCustomFishingPlugin implements CustomFishingPlugin {
|
||||
public ConfigManager getConfigManager() {
|
||||
return configManager;
|
||||
}
|
||||
|
||||
public RequirementManager<Player> getRequirementManager() {
|
||||
return requirementManager;
|
||||
}
|
||||
|
||||
public ActionManager<Player> getActionManager() {
|
||||
return actionManager;
|
||||
}
|
||||
|
||||
public SenderFactory<BukkitCustomFishingPlugin, CommandSender> getSenderFactory() {
|
||||
return senderFactory;
|
||||
}
|
||||
|
||||
public File getDataFolder() {
|
||||
return boostrap.getDataFolder();
|
||||
}
|
||||
|
||||
public PlaceholderManager getPlaceholderManager() {
|
||||
return placeholderManager;
|
||||
}
|
||||
|
||||
public Plugin getBoostrap() {
|
||||
return boostrap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) <2022> <XiaoMoMi>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.api.manager;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface PlaceholderManager {
|
||||
|
||||
/**
|
||||
* Register a custom placeholder
|
||||
*
|
||||
* @param placeholder for instance {level}
|
||||
* @param original for instance %player_level%
|
||||
* @return success or not, it would fail if the placeholder has been registered
|
||||
*/
|
||||
boolean registerCustomPlaceholder(String placeholder, String original);
|
||||
|
||||
/**
|
||||
* Set placeholders in a text string for a player.
|
||||
*
|
||||
* @param player The player for whom the placeholders should be set.
|
||||
* @param text The text string containing placeholders.
|
||||
* @return The text string with placeholders replaced if PlaceholderAPI is available; otherwise, the original text.
|
||||
*/
|
||||
String setPlaceholders(Player player, String text);
|
||||
|
||||
/**
|
||||
* Set placeholders in a text string for an offline player.
|
||||
*
|
||||
* @param player The offline player for whom the placeholders should be set.
|
||||
* @param text The text string containing placeholders.
|
||||
* @return The text string with placeholders replaced if PlaceholderAPI is available; otherwise, the original text.
|
||||
*/
|
||||
String setPlaceholders(OfflinePlayer player, String text);
|
||||
|
||||
/**
|
||||
* Detect and extract placeholders from a text string.
|
||||
*
|
||||
* @param text The text string to search for placeholders.
|
||||
* @return A list of detected placeholders in the text.
|
||||
*/
|
||||
List<String> detectPlaceholders(String text);
|
||||
|
||||
/**
|
||||
* Get the value associated with a single placeholder.
|
||||
*
|
||||
* @param player The player for whom the placeholders are being resolved (nullable).
|
||||
* @param placeholder The placeholder to look up.
|
||||
* @param placeholders A map of placeholders to their corresponding values.
|
||||
* @return The value associated with the placeholder, or the original placeholder if not found.
|
||||
*/
|
||||
String getSingleValue(@Nullable Player player, String placeholder, Map<String, String> placeholders);
|
||||
|
||||
/**
|
||||
* Parse a text string by replacing placeholders with their corresponding values.
|
||||
*
|
||||
* @param player The offline player for whom the placeholders are being resolved (nullable).
|
||||
* @param text The text string containing placeholders.
|
||||
* @param placeholders A map of placeholders to their corresponding values.
|
||||
* @return The text string with placeholders replaced by their values.
|
||||
*/
|
||||
String parse(@Nullable OfflinePlayer player, String text, Map<String, String> placeholders);
|
||||
|
||||
/**
|
||||
* Parse a list of text strings by replacing placeholders with their corresponding values.
|
||||
*
|
||||
* @param player The player for whom the placeholders are being resolved (can be null for offline players).
|
||||
* @param list The list of text strings containing placeholders.
|
||||
* @param replacements A map of custom replacements for placeholders.
|
||||
* @return The list of text strings with placeholders replaced by their values.
|
||||
*/
|
||||
List<String> parse(@Nullable OfflinePlayer player, List<String> list, Map<String, String> replacements);
|
||||
|
||||
/**
|
||||
* Get an expression's value
|
||||
* @param player player
|
||||
* @param formula formula
|
||||
* @param vars vars
|
||||
* @return result
|
||||
*/
|
||||
double getExpressionValue(Player player, String formula, Map<String, String> vars);
|
||||
|
||||
/**
|
||||
* Get an expression's value
|
||||
* @param formula formula
|
||||
* @return result
|
||||
*/
|
||||
double getExpressionValue(String formula);
|
||||
}
|
||||
@@ -30,5 +30,5 @@ public interface ActionFactory<T> {
|
||||
* @param args the args containing the arguments needed to build the action
|
||||
* @return the constructed action
|
||||
*/
|
||||
Action<T> process(Object args);
|
||||
Action<T> process(Object args, double chance);
|
||||
}
|
||||
|
||||
@@ -22,106 +22,81 @@ import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The ActionManager interface manages custom action types and provides methods for handling actions.
|
||||
*
|
||||
* @param <T> the type of the context in which the actions are triggered.
|
||||
*/
|
||||
public interface ActionManager<T> {
|
||||
|
||||
/**
|
||||
* Registers an ActionFactory for a specific action type.
|
||||
* This method allows you to associate an ActionFactory with a custom action type.
|
||||
* Registers a custom action type with its corresponding factory.
|
||||
*
|
||||
* @param type The custom action type to register.
|
||||
* @param actionFactory The ActionFactory responsible for creating actions of the specified type.
|
||||
* @return True if the registration was successful (the action type was not already registered), false otherwise.
|
||||
* @param type The type identifier of the action.
|
||||
* @param actionFactory The factory responsible for creating instances of the action.
|
||||
* @return True if registration was successful, false if the type is already registered.
|
||||
*/
|
||||
boolean registerAction(String type, ActionFactory<T> actionFactory);
|
||||
|
||||
/**
|
||||
* Unregisters an ActionFactory for a specific action type.
|
||||
* This method allows you to remove the association between an action type and its ActionFactory.
|
||||
* Unregisters a custom action type.
|
||||
*
|
||||
* @param type The custom action type to unregister.
|
||||
* @return True if the action type was successfully unregistered, false if it was not found.
|
||||
* @param type The type identifier of the action to unregister.
|
||||
* @return True if unregistration was successful, false if the type is not registered.
|
||||
*/
|
||||
boolean unregisterAction(String type);
|
||||
|
||||
/**
|
||||
* Retrieves an Action object based on the configuration provided in a ConfigurationSection.
|
||||
* This method reads the type of action from the section, obtains the corresponding ActionFactory,
|
||||
* and builds an Action object using the specified values and chance.
|
||||
* <p>
|
||||
* events:
|
||||
* success:
|
||||
* action_1: <- section
|
||||
* ...
|
||||
* Checks if an action type is registered.
|
||||
*
|
||||
* @param section The ConfigurationSection containing the action configuration.
|
||||
* @return An Action object created based on the configuration, or an EmptyAction instance if the action type is invalid.
|
||||
* @param type The type identifier of the action.
|
||||
* @return True if the action type is registered, otherwise false.
|
||||
*/
|
||||
Action<T> getAction(Section section);
|
||||
boolean hasAction(@NotNull String type);
|
||||
|
||||
/**
|
||||
* Retrieves a mapping of ActionTriggers to arrays of Actions from a ConfigurationSection.
|
||||
* This method iterates through the provided ConfigurationSection to extract action triggers
|
||||
* and their associated arrays of Actions.
|
||||
* <p>
|
||||
* events: <- section
|
||||
* success:
|
||||
* action_1:
|
||||
* ...
|
||||
* Retrieves the action factory for the specified action type.
|
||||
*
|
||||
* @param section The ConfigurationSection containing action mappings.
|
||||
* @return A HashMap where keys are ActionTriggers and values are arrays of Action objects.
|
||||
*/
|
||||
HashMap<ActionTrigger, Action<T>[]> getActionMap(Section section);
|
||||
|
||||
/**
|
||||
* Retrieves an array of Action objects from a ConfigurationSection.
|
||||
* This method iterates through the provided ConfigurationSection to extract Action configurations
|
||||
* and build an array of Action objects.
|
||||
* <p>
|
||||
* events:
|
||||
* success: <- section
|
||||
* action_1:
|
||||
* ...
|
||||
*
|
||||
* @param section The ConfigurationSection containing action configurations.
|
||||
* @return An array of Action objects created based on the configurations in the section.
|
||||
*/
|
||||
@NotNull
|
||||
Action<T>[] getActions(@NotNull Section section);
|
||||
|
||||
/**
|
||||
* Retrieves an ActionFactory associated with a specific action type.
|
||||
*
|
||||
* @param type The action type for which to retrieve the ActionFactory.
|
||||
* @return The ActionFactory associated with the specified action type, or null if not found.
|
||||
* @param type The type identifier of the action.
|
||||
* @return The action factory for the specified type, or null if no factory is found.
|
||||
*/
|
||||
@Nullable
|
||||
ActionFactory<T> getActionFactory(@NotNull String type);
|
||||
|
||||
/**
|
||||
* Retrieves a mapping of success times to corresponding arrays of actions from a ConfigurationSection.
|
||||
* <p>
|
||||
* events:
|
||||
* success-times: <- section
|
||||
* 1:
|
||||
* action_1:
|
||||
* ...
|
||||
* Parses an action from a configuration section.
|
||||
*
|
||||
* @param section The ConfigurationSection containing success times actions.
|
||||
* @return A HashMap where success times associated with actions.
|
||||
* @param section The configuration section containing the action definition.
|
||||
* @return The parsed action.
|
||||
*/
|
||||
@NotNull
|
||||
HashMap<Integer, Action<T>[]> getTimesActionMap(@NotNull Section section);
|
||||
Action<T> parseAction(Section section);
|
||||
|
||||
/**
|
||||
* Triggers a list of actions with the given condition.
|
||||
* Parses an array of actions from a configuration section.
|
||||
*
|
||||
* @param section The configuration section containing the action definitions.
|
||||
* @return An array of parsed actions.
|
||||
*/
|
||||
@NotNull
|
||||
Action<T>[] parseActions(@NotNull Section section);
|
||||
|
||||
/**
|
||||
* Parses an action from the given type and arguments.
|
||||
*
|
||||
* @param type The type identifier of the action.
|
||||
* @param args The arguments for the action.
|
||||
* @return The parsed action.
|
||||
*/
|
||||
Action<T> parseAction(@NotNull String type, @NotNull Object args);
|
||||
|
||||
/**
|
||||
* Triggers a list of actions with the given context.
|
||||
* If the list of actions is not null, each action in the list is triggered.
|
||||
*
|
||||
* @param actions The list of actions to trigger.
|
||||
* @param context The context associated with the actions.
|
||||
* @param actions The list of actions to trigger.
|
||||
*/
|
||||
static <T> void trigger(@NotNull Context<T> context, @Nullable List<Action<T>> actions) {
|
||||
if (actions != null)
|
||||
@@ -129,6 +104,13 @@ public interface ActionManager<T> {
|
||||
action.trigger(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers an array of actions with the given context.
|
||||
* If the array of actions is not null, each action in the array is triggered.
|
||||
*
|
||||
* @param context The context associated with the actions.
|
||||
* @param actions The array of actions to trigger.
|
||||
*/
|
||||
static <T> void trigger(@NotNull Context<T> context, @Nullable Action<T>[] actions) {
|
||||
if (actions != null)
|
||||
for (Action<T> action : actions)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package net.momirealms.customfishing.api.mechanic.action;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* An implementation of the Action interface that represents an empty action with no behavior.
|
||||
* This class serves as a default action to prevent NPE.
|
||||
*/
|
||||
public class EmptyAction implements Action<Player> {
|
||||
|
||||
public static final EmptyAction INSTANCE = new EmptyAction();
|
||||
|
||||
@Override
|
||||
public void trigger(Context<Player> context) {
|
||||
}
|
||||
}
|
||||
@@ -1,197 +1,227 @@
|
||||
/*
|
||||
* Copyright (C) <2022> <XiaoMoMi>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.api.mechanic.competition;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.action.Action;
|
||||
import net.momirealms.customfishing.api.mechanic.competition.info.ActionBarConfigImpl;
|
||||
import net.momirealms.customfishing.api.mechanic.competition.info.BossBarConfigImpl;
|
||||
import net.momirealms.customfishing.api.mechanic.competition.info.ActionBarConfig;
|
||||
import net.momirealms.customfishing.api.mechanic.competition.info.BossBarConfig;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CompetitionConfig {
|
||||
/**
|
||||
* Interface representing the configuration for a fishing competition.
|
||||
*/
|
||||
public interface CompetitionConfig {
|
||||
|
||||
private final String key;
|
||||
private int duration;
|
||||
private int minPlayers;
|
||||
private BossBarConfigImpl bossBarConfigImpl;
|
||||
private ActionBarConfigImpl actionBarConfigImpl;
|
||||
private Action[] skipActions;
|
||||
private Action[] startActions;
|
||||
private Action[] endActions;
|
||||
private Action[] joinActions;
|
||||
private Requirement[] requirements;
|
||||
private CompetitionGoal goal;
|
||||
private HashMap<String, Action[]> rewards;
|
||||
CompetitionGoal DEFAULT_GOAL = CompetitionGoal.CATCH_AMOUNT;
|
||||
int DEFAULT_DURATION = 300;
|
||||
int DEFAULT_MIN_PLAYERS = 0;
|
||||
Requirement<Player>[] DEFAULT_REQUIREMENTS = null;
|
||||
Action<Player>[] DEFAULT_SKIP_ACTIONS = null;
|
||||
Action<Player>[] DEFAULT_START_ACTIONS = null;
|
||||
Action<Player>[] DEFAULT_END_ACTIONS = null;
|
||||
Action<Player>[] DEFAULT_JOIN_ACTIONS = null;
|
||||
HashMap<String, Action<Player>[]> DEFAULT_REWARDS = new HashMap<>();
|
||||
|
||||
public CompetitionConfig(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
/**
|
||||
* Gets the unique key for the competition.
|
||||
*
|
||||
* @return the key for the competition.
|
||||
*/
|
||||
String key();
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
/**
|
||||
* Gets the duration of the competition in seconds.
|
||||
*
|
||||
* @return the duration in seconds.
|
||||
*/
|
||||
int durationInSeconds();
|
||||
|
||||
public int getDurationInSeconds() {
|
||||
return duration;
|
||||
}
|
||||
/**
|
||||
* Gets the minimum number of players required to start the competition.
|
||||
*
|
||||
* @return the minimum number of players.
|
||||
*/
|
||||
int minPlayersToStart();
|
||||
|
||||
public int getMinPlayersToStart() {
|
||||
return minPlayers;
|
||||
}
|
||||
/**
|
||||
* Gets the actions to be performed when the competition starts.
|
||||
*
|
||||
* @return an array of start actions.
|
||||
*/
|
||||
Action<Player>[] startActions();
|
||||
|
||||
@Nullable
|
||||
public Action[] getStartActions() {
|
||||
return startActions;
|
||||
}
|
||||
/**
|
||||
* Gets the actions to be performed when the competition ends.
|
||||
*
|
||||
* @return an array of end actions.
|
||||
*/
|
||||
Action<Player>[] endActions();
|
||||
|
||||
@Nullable
|
||||
public Action[] getEndActions() {
|
||||
return endActions;
|
||||
/**
|
||||
* Gets the actions to be performed when a player joins the competition.
|
||||
*
|
||||
* @return an array of join actions.
|
||||
*/
|
||||
Action<Player>[] joinActions();
|
||||
|
||||
/**
|
||||
* Gets the actions to be performed when a player skips the competition.
|
||||
*
|
||||
* @return an array of skip actions.
|
||||
*/
|
||||
Action<Player>[] skipActions();
|
||||
|
||||
/**
|
||||
* Gets the requirements that players must meet to join the competition.
|
||||
*
|
||||
* @return an array of join requirements.
|
||||
*/
|
||||
Requirement<Player>[] joinRequirements();
|
||||
|
||||
/**
|
||||
* Gets the goal of the competition.
|
||||
*
|
||||
* @return the competition goal.
|
||||
*/
|
||||
CompetitionGoal goal();
|
||||
|
||||
/**
|
||||
* Gets the rewards for the competition.
|
||||
*
|
||||
* @return a hashmap where the key is a string identifier and the value is an array of actions.
|
||||
*/
|
||||
HashMap<String, Action<Player>[]> rewards();
|
||||
|
||||
/**
|
||||
* Gets the configuration for the boss bar during the competition.
|
||||
*
|
||||
* @return the boss bar configuration.
|
||||
*/
|
||||
BossBarConfig bossBarConfig();
|
||||
|
||||
/**
|
||||
* Gets the configuration for the action bar during the competition.
|
||||
*
|
||||
* @return the action bar configuration.
|
||||
*/
|
||||
ActionBarConfig actionBarConfig();
|
||||
|
||||
/**
|
||||
* Creates a new builder for the competition configuration.
|
||||
*
|
||||
* @return a new builder instance.
|
||||
*/
|
||||
static Builder builder() {
|
||||
return new CompetitionConfigImpl.BuilderImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actions to perform if player joined the competition
|
||||
*
|
||||
* @return actions
|
||||
* Builder interface for constructing a CompetitionConfig instance.
|
||||
*/
|
||||
@Nullable
|
||||
public Action[] getJoinActions() {
|
||||
return joinActions;
|
||||
}
|
||||
interface Builder {
|
||||
|
||||
/**
|
||||
* Get the actions to perform if the amount of players doesn't meet the requirement
|
||||
*
|
||||
* @return actions
|
||||
*/
|
||||
@Nullable
|
||||
public Action[] getSkipActions() {
|
||||
return skipActions;
|
||||
}
|
||||
/**
|
||||
* Sets the unique key for the competition.
|
||||
*
|
||||
* @param key the key for the competition.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder key(String key);
|
||||
|
||||
/**
|
||||
* Get the requirements for participating the competition
|
||||
*
|
||||
* @return requirements
|
||||
*/
|
||||
@Nullable
|
||||
public Requirement[] getRequirements() {
|
||||
return requirements;
|
||||
}
|
||||
/**
|
||||
* Sets the goal of the competition.
|
||||
*
|
||||
* @param goal the competition goal.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder goal(CompetitionGoal goal);
|
||||
|
||||
@NotNull
|
||||
public CompetitionGoal getGoal() {
|
||||
return goal;
|
||||
}
|
||||
/**
|
||||
* Sets the duration of the competition.
|
||||
*
|
||||
* @param duration the duration in seconds.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder duration(int duration);
|
||||
|
||||
/**
|
||||
* Get the reward map
|
||||
*
|
||||
* @return reward map
|
||||
*/
|
||||
public HashMap<String, Action[]> getRewards() {
|
||||
return rewards;
|
||||
}
|
||||
/**
|
||||
* Sets the minimum number of players required to start the competition.
|
||||
*
|
||||
* @param minPlayers the minimum number of players.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder minPlayers(int minPlayers);
|
||||
|
||||
@Nullable
|
||||
public BossBarConfigImpl getBossBarConfig() {
|
||||
return bossBarConfigImpl;
|
||||
}
|
||||
/**
|
||||
* Sets the requirements that players must meet to join the competition.
|
||||
*
|
||||
* @param joinRequirements an array of join requirements.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder joinRequirements(Requirement<Player>[] joinRequirements);
|
||||
|
||||
@Nullable
|
||||
public ActionBarConfigImpl getActionBarConfig() {
|
||||
return actionBarConfigImpl;
|
||||
}
|
||||
/**
|
||||
* Sets the actions to be performed when a player skips the competition.
|
||||
*
|
||||
* @param skipActions an array of skip actions.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder skipActions(Action<Player>[] skipActions);
|
||||
|
||||
public static Builder builder(String key) {
|
||||
return new Builder(key);
|
||||
}
|
||||
/**
|
||||
* Sets the actions to be performed when the competition starts.
|
||||
*
|
||||
* @param startActions an array of start actions.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder startActions(Action<Player>[] startActions);
|
||||
|
||||
public static class Builder {
|
||||
/**
|
||||
* Sets the actions to be performed when the competition ends.
|
||||
*
|
||||
* @param endActions an array of end actions.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder endActions(Action<Player>[] endActions);
|
||||
|
||||
private final CompetitionConfig config;
|
||||
/**
|
||||
* Sets the actions to be performed when a player joins the competition.
|
||||
*
|
||||
* @param joinActions an array of join actions.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder joinActions(Action<Player>[] joinActions);
|
||||
|
||||
public Builder(String key) {
|
||||
this.config = new CompetitionConfig(key);
|
||||
}
|
||||
/**
|
||||
* Sets the rewards for the competition.
|
||||
*
|
||||
* @param rewards a hashmap where the key is a string identifier and the value is an array of actions.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder rewards(HashMap<String, Action<Player>[]> rewards);
|
||||
|
||||
public Builder duration(int duration) {
|
||||
config.duration = duration;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Sets the configuration for the boss bar during the competition.
|
||||
*
|
||||
* @param bossBarConfig the boss bar configuration.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder bossBarConfig(BossBarConfig bossBarConfig);
|
||||
|
||||
public Builder minPlayers(int min) {
|
||||
config.minPlayers = min;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Sets the configuration for the action bar during the competition.
|
||||
*
|
||||
* @param actionBarConfig the action bar configuration.
|
||||
* @return the builder instance.
|
||||
*/
|
||||
Builder actionBarConfig(ActionBarConfig actionBarConfig);
|
||||
|
||||
public Builder startActions(Action[] startActions) {
|
||||
config.startActions = startActions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder endActions(Action[] endActions) {
|
||||
config.endActions = endActions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder skipActions(Action[] skipActions) {
|
||||
config.skipActions = skipActions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder joinActions(Action[] joinActions) {
|
||||
config.joinActions = joinActions;
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public Builder actionbar(ActionBarConfigImpl actionBarConfigImpl) {
|
||||
config.actionBarConfigImpl = actionBarConfigImpl;
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public Builder bossbar(BossBarConfigImpl bossBarConfigImpl) {
|
||||
config.bossBarConfigImpl = bossBarConfigImpl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder requirements(Requirement[] requirements) {
|
||||
config.requirements = requirements;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder goal(CompetitionGoal goal) {
|
||||
config.goal = goal;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder rewards(HashMap<String, Action[]> rewards) {
|
||||
config.rewards = rewards;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompetitionConfig build() {
|
||||
return config;
|
||||
}
|
||||
/**
|
||||
* Builds and returns the CompetitionConfig instance.
|
||||
*
|
||||
* @return the constructed CompetitionConfig instance.
|
||||
*/
|
||||
CompetitionConfig build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (C) <2022> <XiaoMoMi>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.api.mechanic.competition;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.action.Action;
|
||||
import net.momirealms.customfishing.api.mechanic.competition.info.ActionBarConfig;
|
||||
import net.momirealms.customfishing.api.mechanic.competition.info.BossBarConfig;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CompetitionConfigImpl implements CompetitionConfig {
|
||||
|
||||
private final String key;
|
||||
private final CompetitionGoal goal;
|
||||
private final int duration;
|
||||
private final int minPlayers;
|
||||
private final Requirement<Player>[] joinRequirements;
|
||||
private final Action<Player>[] skipActions;
|
||||
private final Action<Player>[] startActions;
|
||||
private final Action<Player>[] endActions;
|
||||
private final Action<Player>[] joinActions;
|
||||
private final HashMap<String, Action<Player>[]> rewards;
|
||||
private final BossBarConfig bossBarConfig;
|
||||
private final ActionBarConfig actionBarConfig;
|
||||
|
||||
public CompetitionConfigImpl(String key, CompetitionGoal goal, int duration, int minPlayers, Requirement<Player>[] joinRequirements, Action<Player>[] skipActions, Action<Player>[] startActions, Action<Player>[] endActions, Action<Player>[] joinActions, HashMap<String, Action<Player>[]> rewards, BossBarConfig bossBarConfig, ActionBarConfig actionBarConfig) {
|
||||
this.key = key;
|
||||
this.goal = goal;
|
||||
this.duration = duration;
|
||||
this.minPlayers = minPlayers;
|
||||
this.joinRequirements = joinRequirements;
|
||||
this.skipActions = skipActions;
|
||||
this.startActions = startActions;
|
||||
this.endActions = endActions;
|
||||
this.joinActions = joinActions;
|
||||
this.rewards = rewards;
|
||||
this.bossBarConfig = bossBarConfig;
|
||||
this.actionBarConfig = actionBarConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int durationInSeconds() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int minPlayersToStart() {
|
||||
return minPlayers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action<Player>[] startActions() {
|
||||
return startActions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action<Player>[] endActions() {
|
||||
return endActions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action<Player>[] joinActions() {
|
||||
return joinActions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action<Player>[] skipActions() {
|
||||
return skipActions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement<Player>[] joinRequirements() {
|
||||
return joinRequirements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompetitionGoal goal() {
|
||||
return goal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<String, Action<Player>[]> rewards() {
|
||||
return rewards;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BossBarConfig bossBarConfig() {
|
||||
return bossBarConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionBarConfig actionBarConfig() {
|
||||
return actionBarConfig;
|
||||
}
|
||||
|
||||
public static class BuilderImpl implements Builder {
|
||||
private String key;
|
||||
private CompetitionGoal goal = DEFAULT_GOAL;
|
||||
private int duration = DEFAULT_DURATION;
|
||||
private int minPlayers = DEFAULT_MIN_PLAYERS;
|
||||
private Requirement<Player>[] joinRequirements = DEFAULT_REQUIREMENTS;
|
||||
private Action<Player>[] skipActions = DEFAULT_SKIP_ACTIONS;
|
||||
private Action<Player>[] startActions = DEFAULT_START_ACTIONS;
|
||||
private Action<Player>[] endActions = DEFAULT_END_ACTIONS;
|
||||
private Action<Player>[] joinActions = DEFAULT_JOIN_ACTIONS;
|
||||
private HashMap<String, Action<Player>[]> rewards = DEFAULT_REWARDS;
|
||||
private BossBarConfig bossBarConfig;
|
||||
private ActionBarConfig actionBarConfig;
|
||||
@Override
|
||||
public Builder key(String key) {
|
||||
this.key = key;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder goal(CompetitionGoal goal) {
|
||||
this.goal = goal;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder duration(int duration) {
|
||||
this.duration = duration;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder minPlayers(int minPlayers) {
|
||||
this.minPlayers = minPlayers;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder joinRequirements(Requirement<Player>[] joinRequirements) {
|
||||
this.joinRequirements = joinRequirements;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder skipActions(Action<Player>[] skipActions) {
|
||||
this.skipActions = skipActions;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder startActions(Action<Player>[] startActions) {
|
||||
this.startActions = startActions;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder endActions(Action<Player>[] endActions) {
|
||||
this.endActions = endActions;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder joinActions(Action<Player>[] joinActions) {
|
||||
this.joinActions = joinActions;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder rewards(HashMap<String, Action<Player>[]> rewards) {
|
||||
this.rewards = rewards;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder bossBarConfig(BossBarConfig bossBarConfig) {
|
||||
this.bossBarConfig = bossBarConfig;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Builder actionBarConfig(ActionBarConfig actionBarConfig) {
|
||||
this.actionBarConfig = actionBarConfig;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public CompetitionConfig build() {
|
||||
return new CompetitionConfigImpl(key, goal, duration, minPlayers, joinRequirements, skipActions, startActions, endActions, joinActions, rewards, bossBarConfig, actionBarConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ package net.momirealms.customfishing.api.mechanic.competition;
|
||||
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.util.Index;
|
||||
import net.momirealms.customfishing.common.util.RandomUtils;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
@@ -60,7 +61,7 @@ public final class CompetitionGoal {
|
||||
* @return A randomly selected competition goal.
|
||||
*/
|
||||
public static CompetitionGoal getRandom() {
|
||||
return CompetitionGoal.values()[ThreadLocalRandom.current().nextInt(CompetitionGoal.values().length - 1)];
|
||||
return CompetitionGoal.values()[RandomUtils.generateRandomInt(0, values.length - 1)];
|
||||
}
|
||||
|
||||
private final Key key;
|
||||
|
||||
@@ -64,7 +64,7 @@ public interface CompetitionManager {
|
||||
* @param serverGroup Whether the competition should start across all servers that connected to Redis
|
||||
* @return True if the competition was started successfully, false otherwise.
|
||||
*/
|
||||
boolean startCompetition(CompetitionConfig config, boolean force, @Nullable String serverGroup);
|
||||
boolean startCompetition(CompetitionConfigImpl config, boolean force, @Nullable String serverGroup);
|
||||
|
||||
/**
|
||||
* Gets the number of seconds until the next competition.
|
||||
@@ -77,7 +77,8 @@ public interface CompetitionManager {
|
||||
* Retrieves the configuration for a competition based on its key.
|
||||
*
|
||||
* @param key The key of the competition configuration to retrieve.
|
||||
* @return The {@link CompetitionConfig} for the specified key, or {@code null} if no configuration exists with that key.
|
||||
* @return The {@link CompetitionConfigImpl} for the specified key, or {@code null} if no configuration exists with that key.
|
||||
*/
|
||||
@Nullable CompetitionConfig getConfig(String key);
|
||||
@Nullable
|
||||
CompetitionConfigImpl getConfig(String key);
|
||||
}
|
||||
|
||||
@@ -19,53 +19,98 @@ package net.momirealms.customfishing.api.mechanic.competition;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CompetitionPlayer implements Comparable<CompetitionPlayer>{
|
||||
/**
|
||||
* Represents a player participating in a fishing competition.
|
||||
*/
|
||||
public class CompetitionPlayer implements Comparable<CompetitionPlayer> {
|
||||
|
||||
private static CompetitionPlayer empty = new CompetitionPlayer("", 0);
|
||||
private long time;
|
||||
private final String player;
|
||||
private long time;
|
||||
private double score;
|
||||
|
||||
/**
|
||||
* Constructs a new CompetitionPlayer with the specified player name and initial score.
|
||||
*
|
||||
* @param player the name of the player.
|
||||
* @param score the initial score of the player.
|
||||
*/
|
||||
public CompetitionPlayer(String player, double score) {
|
||||
this.player = player;
|
||||
this.score = score;
|
||||
this.time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified score to the player's current score.
|
||||
* If the added score is positive, updates the player's time to the current time.
|
||||
*
|
||||
* @param score the score to add.
|
||||
*/
|
||||
public void addScore(double score) {
|
||||
this.score += score;
|
||||
if (score <= 0) return;
|
||||
this.time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the player's score to the specified value and updates the player's time to the current time.
|
||||
*
|
||||
* @param score the new score for the player.
|
||||
*/
|
||||
public void setScore(double score) {
|
||||
this.score = score;
|
||||
this.time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the time when the player's score was last updated.
|
||||
*
|
||||
* @return the last update time in milliseconds.
|
||||
*/
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player's current score.
|
||||
*
|
||||
* @return the current score.
|
||||
*/
|
||||
public double getScore() {
|
||||
return this.score;
|
||||
}
|
||||
|
||||
public String getPlayer(){
|
||||
/**
|
||||
* Gets the name of the player.
|
||||
*
|
||||
* @return the player's name.
|
||||
*/
|
||||
public String getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this player to another CompetitionPlayer for ordering.
|
||||
* Players are compared first by score, then by time if scores are equal.
|
||||
*
|
||||
* @param another the other player to compare to.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(@NotNull CompetitionPlayer competitionPlayer) {
|
||||
if (competitionPlayer.getScore() != this.score) {
|
||||
return (competitionPlayer.getScore() > this.score) ? 1 : -1;
|
||||
} else if (competitionPlayer.getTime() != this.time) {
|
||||
return (competitionPlayer.getTime() > this.time) ? 1 : -1;
|
||||
public int compareTo(@NotNull CompetitionPlayer another) {
|
||||
if (another.getScore() != this.score) {
|
||||
return (another.getScore() > this.score) ? 1 : -1;
|
||||
} else if (another.getTime() != this.time) {
|
||||
return (another.getTime() > this.time) ? 1 : -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the CompetitionPlayer.
|
||||
*
|
||||
* @return a string containing the player's name, score, and last update time.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CompetitionPlayer[" +
|
||||
|
||||
@@ -91,7 +91,8 @@ public interface FishingCompetition {
|
||||
*
|
||||
* @return The configuration of the fishing competition.
|
||||
*/
|
||||
@NotNull CompetitionConfig getConfig();
|
||||
@NotNull
|
||||
CompetitionConfigImpl getConfig();
|
||||
|
||||
/**
|
||||
* Gets the goal of the fishing competition.
|
||||
@@ -105,7 +106,8 @@ public interface FishingCompetition {
|
||||
*
|
||||
* @return The ranking data for the fishing competition.
|
||||
*/
|
||||
@NotNull Ranking getRanking();
|
||||
@NotNull
|
||||
RankingProvider getRanking();
|
||||
|
||||
/**
|
||||
* Gets the cached placeholders for the fishing competition.
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public interface Ranking {
|
||||
public interface RankingProvider {
|
||||
|
||||
/**
|
||||
* Clears the list of competition players.
|
||||
@@ -27,12 +27,14 @@ public abstract class AbstractCompetitionInfo {
|
||||
protected int switchInterval;
|
||||
protected boolean showToAll;
|
||||
protected String[] texts;
|
||||
protected boolean enabled;
|
||||
|
||||
protected AbstractCompetitionInfo(int refreshRate, int switchInterval, boolean showToAll, String[] texts) {
|
||||
protected AbstractCompetitionInfo(boolean enabled, int refreshRate, int switchInterval, boolean showToAll, String[] texts) {
|
||||
this.refreshRate = refreshRate;
|
||||
this.switchInterval = switchInterval;
|
||||
this.showToAll = showToAll;
|
||||
this.texts = texts;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,4 +72,13 @@ public abstract class AbstractCompetitionInfo {
|
||||
public String[] texts() {
|
||||
return texts;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the feature is enabled.
|
||||
*
|
||||
* @return enabled or not.
|
||||
*/
|
||||
public boolean enabled() {
|
||||
return enabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,13 @@ public interface ActionBarConfig {
|
||||
*/
|
||||
String[] texts();
|
||||
|
||||
/**
|
||||
* Is action bar enabled
|
||||
*
|
||||
* @return enabled or not
|
||||
*/
|
||||
boolean enabled();
|
||||
|
||||
/**
|
||||
* Creates a new builder instance for constructing {@code ActionBarConfig} objects.
|
||||
*
|
||||
@@ -81,6 +88,8 @@ public interface ActionBarConfig {
|
||||
*/
|
||||
Builder text(String[] texts);
|
||||
|
||||
Builder enable(boolean enable);
|
||||
|
||||
/**
|
||||
* Builds the {@code ActionBarConfig} object with the configured settings.
|
||||
*
|
||||
|
||||
@@ -19,8 +19,8 @@ package net.momirealms.customfishing.api.mechanic.competition.info;
|
||||
|
||||
public class ActionBarConfigImpl extends AbstractCompetitionInfo implements ActionBarConfig {
|
||||
|
||||
public ActionBarConfigImpl(int refreshRate, int switchInterval, boolean showToAll, String[] texts) {
|
||||
super(refreshRate, switchInterval, showToAll, texts);
|
||||
public ActionBarConfigImpl(boolean enable, int refreshRate, int switchInterval, boolean showToAll, String[] texts) {
|
||||
super(enable, refreshRate, switchInterval, showToAll, texts);
|
||||
}
|
||||
|
||||
public static class BuilderImpl implements Builder {
|
||||
@@ -28,29 +28,35 @@ public class ActionBarConfigImpl extends AbstractCompetitionInfo implements Acti
|
||||
private int switchInterval = DEFAULT_SWITCH_INTERVAL;
|
||||
private boolean showToAll = DEFAULT_VISIBILITY;
|
||||
private String[] texts = DEFAULT_TEXTS;
|
||||
private boolean enable = true;
|
||||
@Override
|
||||
public BuilderImpl showToAll(boolean showToAll) {
|
||||
public Builder showToAll(boolean showToAll) {
|
||||
this.showToAll = showToAll;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl refreshRate(int rate) {
|
||||
public Builder refreshRate(int rate) {
|
||||
this.refreshRate = rate;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl switchInterval(int interval) {
|
||||
public Builder switchInterval(int interval) {
|
||||
this.switchInterval = interval;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl text(String[] texts) {
|
||||
public Builder text(String[] texts) {
|
||||
this.texts = texts;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ActionBarConfigImpl build() {
|
||||
return new ActionBarConfigImpl(refreshRate, switchInterval, showToAll, texts);
|
||||
public Builder enable(boolean enable) {
|
||||
this.enable = enable;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ActionBarConfig build() {
|
||||
return new ActionBarConfigImpl(enable, refreshRate, switchInterval, showToAll, texts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,13 @@ public interface BossBarConfig {
|
||||
*/
|
||||
BossBar.Overlay overlay();
|
||||
|
||||
/**
|
||||
* Is boss bar enabled
|
||||
*
|
||||
* @return enabled or not
|
||||
*/
|
||||
boolean enabled();
|
||||
|
||||
/**
|
||||
* Creates a new builder instance for constructing {@code BossBarConfig} objects.
|
||||
*
|
||||
@@ -115,6 +122,8 @@ public interface BossBarConfig {
|
||||
*/
|
||||
Builder overlay(BossBar.Overlay overlay);
|
||||
|
||||
Builder enable(boolean enable);
|
||||
|
||||
/**
|
||||
* Builds the {@code BossBarConfig} object with the configured settings.
|
||||
*
|
||||
|
||||
@@ -24,8 +24,8 @@ public class BossBarConfigImpl extends AbstractCompetitionInfo implements BossBa
|
||||
private final BossBar.Color color;
|
||||
private final BossBar.Overlay overlay;
|
||||
|
||||
public BossBarConfigImpl(int refreshRate, int switchInterval, boolean showToAll, String[] texts, BossBar.Color color, BossBar.Overlay overlay) {
|
||||
super(refreshRate, switchInterval, showToAll, texts);
|
||||
public BossBarConfigImpl(boolean enable, int refreshRate, int switchInterval, boolean showToAll, String[] texts, BossBar.Color color, BossBar.Overlay overlay) {
|
||||
super(enable, refreshRate, switchInterval, showToAll, texts);
|
||||
this.color = color;
|
||||
this.overlay = overlay;
|
||||
}
|
||||
@@ -46,40 +46,46 @@ public class BossBarConfigImpl extends AbstractCompetitionInfo implements BossBa
|
||||
private boolean showToAll = DEFAULT_VISIBILITY;
|
||||
private String[] texts = DEFAULT_TEXTS;
|
||||
private BossBar.Overlay overlay = DEFAULT_OVERLAY;
|
||||
public BossBar.Color color = DEFAULT_COLOR;
|
||||
private BossBar.Color color = DEFAULT_COLOR;
|
||||
private boolean enable = true;
|
||||
@Override
|
||||
public BuilderImpl showToAll(boolean showToAll) {
|
||||
public Builder showToAll(boolean showToAll) {
|
||||
this.showToAll = showToAll;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl refreshRate(int rate) {
|
||||
public Builder refreshRate(int rate) {
|
||||
this.refreshRate = rate;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl switchInterval(int interval) {
|
||||
public Builder switchInterval(int interval) {
|
||||
this.switchInterval = interval;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl text(String[] texts) {
|
||||
public Builder text(String[] texts) {
|
||||
this.texts = texts;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl color(BossBar.Color color) {
|
||||
public Builder color(BossBar.Color color) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuilderImpl overlay(BossBar.Overlay overlay) {
|
||||
public Builder overlay(BossBar.Overlay overlay) {
|
||||
this.overlay = overlay;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BossBarConfigImpl build() {
|
||||
return new BossBarConfigImpl(refreshRate, switchInterval, showToAll, texts, color, overlay);
|
||||
public Builder enable(boolean enable) {
|
||||
this.enable = enable;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BossBarConfig build() {
|
||||
return new BossBarConfigImpl(enable, refreshRate, switchInterval, showToAll, texts, color, overlay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package net.momirealms.customfishing.api.mechanic.context;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -40,6 +39,13 @@ public interface Context<T> {
|
||||
*/
|
||||
Map<ContextKeys<?>, Object> args();
|
||||
|
||||
/**
|
||||
* Converts the context to a map of placeholders
|
||||
*
|
||||
* @return a map of placeholders
|
||||
*/
|
||||
Map<String, String> toPlaceholderMap();
|
||||
|
||||
/**
|
||||
* Adds or updates an argument in the context.
|
||||
* This method allows adding a new argument or updating the value of an existing argument.
|
||||
@@ -75,7 +81,7 @@ public interface Context<T> {
|
||||
* @param player the player to be used as the holder of the context.
|
||||
* @return a new Context instance with the specified player as the holder.
|
||||
*/
|
||||
static Context<Player> player(@NotNull Player player) {
|
||||
static Context<Player> player(@Nullable Player player) {
|
||||
return new PlayerContextImpl(player);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class ContextKeys<T> {
|
||||
public static final ContextKeys<String> TYPE = of("type", String.class);
|
||||
public static final ContextKeys<Float> SIZE = of("size", Float.class);
|
||||
public static final ContextKeys<Double> PRICE = of("price", Double.class);
|
||||
public static final ContextKeys<String> SURROUNDING = of("surrounding", String.class);
|
||||
|
||||
private final String key;
|
||||
private final Class<T> type;
|
||||
|
||||
@@ -19,7 +19,7 @@ package net.momirealms.customfishing.api.mechanic.context;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -39,8 +39,9 @@ public final class PlayerContextImpl implements Context<Player> {
|
||||
*
|
||||
* @param player the player to be associated with this context.
|
||||
*/
|
||||
public PlayerContextImpl(@NotNull Player player) {
|
||||
public PlayerContextImpl(@Nullable Player player) {
|
||||
this.player = player;
|
||||
if (player == null) return;
|
||||
final Location location = player.getLocation();
|
||||
arg(ContextKeys.LOCATION, location)
|
||||
.arg(ContextKeys.X, location.getBlockX())
|
||||
@@ -49,47 +50,32 @@ public final class PlayerContextImpl implements Context<Player> {
|
||||
.arg(ContextKeys.WORLD, location.getWorld().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the map of arguments associated with this context.
|
||||
*
|
||||
* @return a map where the keys are argument names and the values are argument values.
|
||||
*/
|
||||
@Override
|
||||
public Map<ContextKeys<?>, Object> args() {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an argument to the context and returns the context itself
|
||||
* to allow for method chaining.
|
||||
*
|
||||
* @param key the name of the argument to add.
|
||||
* @param value the value of the argument to add.
|
||||
* @return the PlayerContextImpl instance to allow for method chaining.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> toPlaceholderMap() {
|
||||
HashMap<String, String> placeholders = new HashMap<>();
|
||||
for (Map.Entry<ContextKeys<?>, Object> entry : args.entrySet()) {
|
||||
placeholders.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
}
|
||||
return placeholders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C> PlayerContextImpl arg(ContextKeys<C> key, C value) {
|
||||
args.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the value of a specific argument from the context.
|
||||
*
|
||||
* @param key the name of the argument to retrieve.
|
||||
* @return the value of the argument, or null if no argument with the given key exists.
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <C> C arg(ContextKeys<C> key) {
|
||||
return (C) args.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player associated with this context.
|
||||
*
|
||||
* @return the player object associated with this context.
|
||||
*/
|
||||
@Override
|
||||
public Player getHolder() {
|
||||
return player;
|
||||
|
||||
@@ -23,6 +23,8 @@ public class EffectProperties<T> {
|
||||
|
||||
public static final EffectProperties<Boolean> LAVA_FISHING = of("lava", Boolean.class);
|
||||
public static final EffectProperties<Boolean> VOID_FISHING = of("void", Boolean.class);
|
||||
// It's not actually used because it's a vanilla mechanic
|
||||
public static final EffectProperties<Boolean> WATER_FISHING = of("water", Boolean.class);
|
||||
|
||||
private final String key;
|
||||
private final Class<T> type;
|
||||
|
||||
@@ -17,95 +17,7 @@
|
||||
|
||||
package net.momirealms.customfishing.api.mechanic.fishing;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.effect.Effect;
|
||||
import net.momirealms.customfishing.api.mechanic.game.GameInstance;
|
||||
import net.momirealms.customfishing.api.mechanic.game.GameSettings;
|
||||
import net.momirealms.customfishing.api.mechanic.game.GamingPlayer;
|
||||
import org.bukkit.entity.FishHook;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface FishingManager {
|
||||
|
||||
/**
|
||||
* Removes a fishing hook entity associated with a given player's UUID.
|
||||
*
|
||||
* @param uuid The UUID of the player
|
||||
* @return {@code true} if the fishing hook was successfully removed, {@code false} otherwise.
|
||||
*/
|
||||
boolean removeHook(UUID uuid);
|
||||
|
||||
/**
|
||||
* Retrieves a FishHook object associated with the provided player's UUID
|
||||
*
|
||||
* @param uuid The UUID of the player
|
||||
* @return fishhook entity, null if not exists
|
||||
*/
|
||||
@Nullable FishHook getHook(UUID uuid);
|
||||
|
||||
/**
|
||||
* Sets the temporary fishing state for a player.
|
||||
*
|
||||
* @param player The player for whom to set the temporary fishing state.
|
||||
* @param tempFishingState The temporary fishing state to set for the player.
|
||||
*/
|
||||
void setTempFishingState(Player player, TempFishingState tempFishingState);
|
||||
|
||||
/**
|
||||
* Gets the {@link TempFishingState} object associated with the given UUID.
|
||||
*
|
||||
* @param uuid The UUID of the player.
|
||||
* @return The {@link TempFishingState} object if found, or {@code null} if not found.
|
||||
*/
|
||||
@Nullable TempFishingState getTempFishingState(UUID uuid);
|
||||
|
||||
/**
|
||||
* Removes the temporary fishing state associated with a player.
|
||||
*
|
||||
* @param player The player whose temporary fishing state should be removed.
|
||||
*/
|
||||
@Nullable TempFishingState removeTempFishingState(Player player);
|
||||
|
||||
/**
|
||||
* Processes the game result for a gaming player
|
||||
*
|
||||
* @param gamingPlayer The gaming player whose game result should be processed.
|
||||
*/
|
||||
void processGameResult(GamingPlayer gamingPlayer);
|
||||
|
||||
/**
|
||||
* Starts a fishing game for the specified player with the given condition and effect.
|
||||
*
|
||||
* @param player The player starting the fishing game.
|
||||
* @param playerContext The condition used to determine the game.
|
||||
* @param effect The effect applied to the game.
|
||||
*/
|
||||
boolean startFishingGame(Player player, PlayerContext playerContext, Effect effect);
|
||||
|
||||
/**
|
||||
* Starts a fishing game for the specified player with the given settings and game instance.
|
||||
*
|
||||
* @param player The player starting the fishing game.
|
||||
* @param settings The game settings for the fishing game.
|
||||
* @param gameInstance The instance of the fishing game to start.
|
||||
*/
|
||||
boolean startFishingGame(Player player, GameSettings settings, GameInstance gameInstance);
|
||||
|
||||
/**
|
||||
* Checks if a player with the given UUID has cast their fishing hook.
|
||||
*
|
||||
* @param uuid The UUID of the player to check.
|
||||
* @return {@code true} if the player has cast their fishing hook, {@code false} otherwise.
|
||||
*/
|
||||
boolean hasPlayerCastHook(UUID uuid);
|
||||
|
||||
/**
|
||||
* Gets the {@link GamingPlayer} object associated with the given UUID.
|
||||
*
|
||||
* @param uuid The UUID of the player.
|
||||
* @return The {@link GamingPlayer} object if found, or {@code null} if not found.
|
||||
*/
|
||||
@Nullable GamingPlayer getGamingPlayer(UUID uuid);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (C) <2022> <XiaoMoMi>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.api.mechanic.misc.placeholder;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public interface PlaceholderManager {
|
||||
|
||||
Pattern PATTERN = Pattern.compile("\\{[^{}]+}");
|
||||
|
||||
/**
|
||||
* Registers a custom placeholder with its corresponding original string.
|
||||
*
|
||||
* @param placeholder the placeholder to register.
|
||||
* @param original the original string corresponding to the placeholder.
|
||||
* @return true if the placeholder was successfully registered, false if it already exists.
|
||||
*/
|
||||
boolean registerCustomPlaceholder(String placeholder, String original);
|
||||
|
||||
/**
|
||||
* Resolves all placeholders within a given text.
|
||||
*
|
||||
* @param text the text to resolve placeholders in.
|
||||
* @return a list of found placeholders.
|
||||
*/
|
||||
List<String> resolvePlaceholders(String text);
|
||||
|
||||
/**
|
||||
* Parses a single placeholder for a specified player, optionally using a map of replacements.
|
||||
*
|
||||
* @param player the player for whom the placeholder should be parsed.
|
||||
* @param placeholder the placeholder to parse.
|
||||
* @param replacements a map of replacement strings for placeholders.
|
||||
* @return the parsed placeholder string.
|
||||
*/
|
||||
String parseSingle(@Nullable OfflinePlayer player, String placeholder, Map<String, String> replacements);
|
||||
|
||||
/**
|
||||
* Parses all placeholders in the given text for a specified player, optionally using a map of replacements.
|
||||
*
|
||||
* @param player the player for whom the placeholders should be parsed.
|
||||
* @param text the text containing placeholders.
|
||||
* @param replacements a map of replacement strings for placeholders.
|
||||
* @return the text with parsed placeholders.
|
||||
*/
|
||||
String parse(@Nullable OfflinePlayer player, String text, Map<String, String> replacements);
|
||||
|
||||
/**
|
||||
* Parses all placeholders in a list of strings for a specified player, optionally using a map of replacements.
|
||||
*
|
||||
* @param player the player for whom the placeholders should be parsed.
|
||||
* @param list the list of strings containing placeholders.
|
||||
* @param replacements a map of replacement strings for placeholders.
|
||||
* @return the list of strings with parsed placeholders.
|
||||
*/
|
||||
List<String> parse(@Nullable OfflinePlayer player, List<String> list, Map<String, String> replacements);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) <2022> <XiaoMoMi>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.api.mechanic.requirement;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Represents an empty requirement that always returns true when checking conditions.
|
||||
*/
|
||||
public class EmptyRequirement implements Requirement<Player> {
|
||||
|
||||
public static final EmptyRequirement INSTANCE = new EmptyRequirement();
|
||||
|
||||
@Override
|
||||
public boolean isSatisfied(Context<Player> context) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,13 @@ import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The RequirementManager interface manages custom requirement types and provides methods for handling requirements.
|
||||
*
|
||||
* @param <T> the type of the context in which the requirements are evaluated.
|
||||
*/
|
||||
public interface RequirementManager<T> {
|
||||
|
||||
/**
|
||||
@@ -42,52 +49,13 @@ public interface RequirementManager<T> {
|
||||
boolean unregisterRequirement(@NotNull String type);
|
||||
|
||||
/**
|
||||
* Retrieves an array of requirements based on a configuration section.
|
||||
* Checks if a requirement type is registered.
|
||||
*
|
||||
* @param section The configuration section containing requirement definitions.
|
||||
* @param runActions A flag indicating whether to use advanced requirements.
|
||||
* @return An array of Requirement objects based on the configuration section
|
||||
*/
|
||||
@Nullable
|
||||
Requirement<T>[] getRequirements(@NotNull Section section, boolean runActions);
|
||||
|
||||
/**
|
||||
* If a requirement type exists
|
||||
*
|
||||
* @param type type
|
||||
* @return exists or not
|
||||
* @param type The type identifier of the requirement.
|
||||
* @return True if the requirement type is registered, otherwise false.
|
||||
*/
|
||||
boolean hasRequirement(@NotNull String type);
|
||||
|
||||
/**
|
||||
* Retrieves a Requirement object based on a configuration section and advanced flag.
|
||||
* <p>
|
||||
* requirement_1: <- section
|
||||
* type: xxx
|
||||
* value: xxx
|
||||
*
|
||||
* @param section The configuration section containing requirement definitions.
|
||||
* @param runActions A flag indicating whether to use advanced requirements.
|
||||
* @return A Requirement object based on the configuration section, or an EmptyRequirement if the section is null or invalid.
|
||||
*/
|
||||
@NotNull
|
||||
Requirement<T> getRequirement(@NotNull Section section, boolean runActions);
|
||||
|
||||
/**
|
||||
* Gets a requirement based on the provided type and value.
|
||||
* If a valid RequirementFactory is found for the type, it is used to create the requirement.
|
||||
* If no factory is found, a warning is logged, and an empty requirement instance is returned.
|
||||
* <p>
|
||||
* world: <- type
|
||||
* - world <- value
|
||||
*
|
||||
* @param type The type representing the requirement type.
|
||||
* @param value The value associated with the requirement.
|
||||
* @return A Requirement instance based on the type and value, or an EmptyRequirement if the type is invalid.
|
||||
*/
|
||||
@NotNull
|
||||
Requirement<T> getRequirement(@NotNull String type, @NotNull Object value);
|
||||
|
||||
/**
|
||||
* Retrieves a RequirementFactory based on the specified requirement type.
|
||||
*
|
||||
@@ -97,6 +65,44 @@ public interface RequirementManager<T> {
|
||||
@Nullable
|
||||
RequirementFactory<T> getRequirementFactory(@NotNull String type);
|
||||
|
||||
/**
|
||||
* Retrieves an array of requirements based on a configuration section.
|
||||
*
|
||||
* @param section The configuration section containing requirement definitions.
|
||||
* @param runActions A flag indicating whether to use advanced requirements.
|
||||
* @return An array of Requirement objects based on the configuration section.
|
||||
*/
|
||||
@NotNull
|
||||
Requirement<T>[] parseRequirements(@NotNull Section section, boolean runActions);
|
||||
|
||||
/**
|
||||
* Retrieves a Requirement object based on a configuration section and advanced flag.
|
||||
*
|
||||
* @param section The configuration section containing requirement definitions.
|
||||
* @param runActions A flag indicating whether to use advanced requirements.
|
||||
* @return A Requirement object based on the configuration section, or an EmptyRequirement if the section is null or invalid.
|
||||
*/
|
||||
@NotNull
|
||||
Requirement<T> parseRequirement(@NotNull Section section, boolean runActions);
|
||||
|
||||
/**
|
||||
* Gets a requirement based on the provided type and value.
|
||||
* If a valid RequirementFactory is found for the type, it is used to create the requirement.
|
||||
*
|
||||
* @param type The type representing the requirement type.
|
||||
* @param value The value associated with the requirement.
|
||||
* @return A Requirement instance based on the type and value, or an EmptyRequirement if the type is invalid.
|
||||
*/
|
||||
@NotNull
|
||||
Requirement<T> parseRequirement(@NotNull String type, @NotNull Object value);
|
||||
|
||||
/**
|
||||
* Checks if all requirements in the provided array are satisfied within the given context.
|
||||
*
|
||||
* @param context The context in which the requirements are evaluated.
|
||||
* @param requirements An array of requirements to check.
|
||||
* @return True if all requirements are satisfied, otherwise false.
|
||||
*/
|
||||
static <T> boolean isSatisfied(Context<T> context, @Nullable Requirement<T>[] requirements) {
|
||||
if (requirements == null) return true;
|
||||
for (Requirement<T> requirement : requirements) {
|
||||
@@ -106,4 +112,14 @@ public interface RequirementManager<T> {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static <T> boolean isSatisfied(Context<T> context, @Nullable List<Requirement<T>> requirements) {
|
||||
if (requirements == null) return true;
|
||||
for (Requirement<T> requirement : requirements) {
|
||||
if (!requirement.isSatisfied(context)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user