Skip to content

Commit

Permalink
conditions recode
Browse files Browse the repository at this point in the history
  • Loading branch information
Oribuin committed Jan 10, 2025
1 parent 91ef6fc commit f5be61e
Show file tree
Hide file tree
Showing 21 changed files with 1,012 additions and 293 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package dev.oribuin.fishing.api.condition;

import dev.oribuin.fishing.api.config.Configurable;
import dev.oribuin.fishing.api.event.impl.ConditionCheckEvent;
import dev.oribuin.fishing.fish.Fish;
import dev.rosewood.rosegarden.config.CommentedConfigurationSection;
import dev.rosewood.rosegarden.utils.StringPlaceholders;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import dev.oribuin.fishing.api.event.impl.ConditionCheckEvent;
import dev.oribuin.fishing.fish.Fish;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
* A condition that is checked when a player is trying to catch a fish
* <p>
* First, {@link #shouldRun(Fish)} is called to check if the fish has the condition type
* If the fish has the condition type, {@link #check(Fish, Player, ItemStack, FishHook)} is called to check if the player meets the condition to catch the fish
*
* @see dev.oribuin.fishing.fish.condition.ConditionRegistry#check(Fish, Player, ItemStack, FishHook) to see how this is used
* @see dev.oribuin.fishing.fish.condition.ConditionRegistry#check(List, Fish, Player, ItemStack, FishHook) to see how this is used
*/
public interface CatchCondition {
public abstract class CatchCondition implements Configurable {

/**
* Decides whether the condition should be checked in the first place,
Expand All @@ -25,12 +31,12 @@ public interface CatchCondition {
*
* @return true if the fish has the condition applied. @see {@link #check(Fish, Player, ItemStack, FishHook)} for the actual condition check
*/
boolean shouldRun(Fish fish);
public abstract boolean shouldRun(Fish fish);

/**
* Check if the player meets the condition to catch the fish or not, Requires {@link #shouldRun(Fish)} to return true before running
* <p>
* To see how this is used, check {@link dev.oribuin.fishing.fish.condition.ConditionRegistry#check(Fish, Player, ItemStack, FishHook)}
* To see how this is used, check {@link dev.oribuin.fishing.fish.condition.ConditionRegistry#check(List, Fish, Player, ItemStack, FishHook)}
* <p>
* All conditions are passed through {@link ConditionCheckEvent} to overwrite the result if needed
*
Expand All @@ -41,6 +47,63 @@ public interface CatchCondition {
*
* @return Results in true if the player can catch the fish
*/
boolean check(Fish fish, Player player, ItemStack rod, FishHook hook);
public abstract boolean check(Fish fish, Player player, ItemStack rod, FishHook hook);

/**
* All the placeholders that can be used in the configuration file for this configurable class
*
* @return The placeholders
*/
public StringPlaceholders placeholders() {
return StringPlaceholders.empty();
}

/**
* Initialize a {@link CommentedConfigurationSection} from a configuration file to establish the settings
* for the configurable class, will be automatically called when the configuration file is loaded using {@link #reload()}
* <p>
* If your class inherits from another configurable class, make sure to call super.loadSettings(config)
* to save the settings from the parent class
* <p>
* A class must be initialized before settings are loaded, If you wish to have a configurable data class style, its best to create a
* static method that will create a new instance and call this method on the new instance
* <p>
* The {@link CommentedConfigurationSection} should never be null, when creating a new section,
* use {@link #pullSection(CommentedConfigurationSection, String)} to establish new section if it doesn't exist
*
* @param config The {@link CommentedConfigurationSection} to load the settings from, this cannot be null.
*/
public abstract void loadSettings(@NotNull CommentedConfigurationSection config);

/**
* Create a new condition object. This is used to create a new condition object from a configuration section.
*
* @param clazz The class of the condition
* @param base The configuration section to load the settings from
* @param <T> The type of the condition
*
* @return The new condition object
*/
public static <T extends CatchCondition> T create(Class<T> clazz, CommentedConfigurationSection base) {
try {
T condition = clazz.getDeclaredConstructor().newInstance();
if (base != null) condition.loadSettings(base);
return condition;
} catch (Exception e) {
return null;
}
}

/**
* Create a new condition object. This will be an empty condition object with no settings loaded.
*
* @param clazz The class of the condition
* @param <T> The type of the condition
*
* @return The new condition object
*/
public static <T extends CatchCondition> T create(Class<T> clazz) {
return create(clazz, null);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.oribuin.fishing.api.event.impl;

import org.bukkit.Bukkit;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand All @@ -9,6 +10,8 @@
import org.jetbrains.annotations.NotNull;
import dev.oribuin.fishing.api.condition.CatchCondition;

import java.nio.Buffer;

/**
* This event determines the result of a condition check, which can be modified by other conditions.
*/
Expand All @@ -31,7 +34,7 @@ public class ConditionCheckEvent extends PlayerEvent implements Cancellable {
* @param result The result of the condition check, True if the player meets the condition
*/
public ConditionCheckEvent(@NotNull Player who, @NotNull ItemStack rod, @NotNull FishHook hook, @NotNull CatchCondition condition, boolean result) {
super(who, false);
super(who, !Bukkit.isPrimaryThread());

this.rod = rod;
this.hook = hook;
Expand Down
70 changes: 19 additions & 51 deletions src/main/java/dev/oribuin/fishing/fish/Fish.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package dev.oribuin.fishing.fish;

import dev.oribuin.fishing.FishingPlugin;
import dev.oribuin.fishing.api.condition.CatchCondition;
import dev.oribuin.fishing.api.config.Configurable;
import dev.oribuin.fishing.augment.Augment;
import dev.oribuin.fishing.fish.condition.ConditionRegistry;
import dev.oribuin.fishing.manager.TierManager;
import dev.oribuin.fishing.storage.util.PersistKeys;
import dev.rosewood.rosegarden.config.CommentedConfigurationSection;
import dev.rosewood.rosegarden.config.CommentedFileConfiguration;
import dev.rosewood.rosegarden.utils.StringPlaceholders;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import dev.oribuin.fishing.FishingPlugin;
import dev.oribuin.fishing.api.config.Configurable;
import dev.oribuin.fishing.augment.Augment;
import dev.oribuin.fishing.fish.condition.Condition;
import dev.oribuin.fishing.fish.condition.Time;
import dev.oribuin.fishing.fish.condition.Weather;
import dev.oribuin.fishing.manager.TierManager;
import dev.oribuin.fishing.storage.util.PersistKeys;
import dev.oribuin.fishing.util.FishUtils;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;

public class Fish implements Configurable {

Expand All @@ -32,7 +30,7 @@ public class Fish implements Configurable {
private List<String> description; // The description of the fish
private int modelData; // The model data of the fish
private ItemStack itemStack; // The item stack of the fish
private Condition condition; // The requirements to catch the fish
private List<CatchCondition> conditions; // The conditions to catch the fish

/**
* Create a new name of fish with a name and quality
Expand All @@ -43,10 +41,10 @@ public class Fish implements Configurable {
public Fish(@NotNull String name, @NotNull String tier) {
this.name = name;
this.tier = tier;
this.condition = new Condition();
this.displayName = StringUtils.capitalize(name.toLowerCase().replace("_", " "));
this.description = new ArrayList<>();
this.modelData = -1;
this.conditions = new ArrayList<>();
}

/**
Expand All @@ -70,17 +68,8 @@ public void loadSettings(@NotNull CommentedConfigurationSection config) {
this.description = config.getStringList("description");
this.modelData = config.getInt("model-data", -1);

// Catch Conditions
this.condition.biomes(config.getStringList("biomes"));
this.condition.weather(FishUtils.getEnum(Weather.class, config.getString("weather")));
this.condition.time(FishUtils.getEnum(Time.class, config.getString("time")));
this.condition.worlds(config.getStringList("worlds"));
this.condition.environment(FishUtils.getEnum(World.Environment.class, config.getString("environment")));
this.condition.waterDepth((Integer) config.get("water-depth"));
this.condition.iceFishing(config.getBoolean("ice-fishing"));
this.condition.lightLevel((Integer) config.get("light-level"));
this.condition.height(FishUtils.getHeight(config.getString("height")));
this.condition.boatFishing(config.getBoolean("boat-fishing"));
// Catch Conditions for the fish
this.conditions = ConditionRegistry.loadConditions(this, this.pullSection(config, "conditions"));
}

/**
Expand All @@ -99,20 +88,6 @@ public void saveSettings(@NotNull CommentedConfigurationSection config) {
config.set(this.name + ".display-name", this.displayName);
config.set(this.name + ".description", this.description);
config.set(this.name + ".model-data", this.modelData);

// Conditions for the fish
config.set(this.name + ".biomes", this.condition.biomes());
config.set(this.name + ".worlds", this.condition.worlds());
config.set(this.name + ".ice-fishing", this.condition.iceFishing());
config.set(this.name + "boat-fishing", this.condition.boatFishing());

// ugly :)
if (this.condition.weather() != null) config.set(this.name + ".weather", this.condition.weather().name());
if (this.condition.time() != null) config.set(this.name + ".time", this.condition.time().name());
if (this.condition.environment() != null) config.set(this.name + ".environment", this.condition.environment().name());
if (this.condition.waterDepth() != null) config.set(this.name + ".water-depth", this.condition.waterDepth());
if (this.condition.lightLevel() != null) config.set(this.name + ".light-level", this.condition.lightLevel());
if (this.condition.height() != null) config.set(this.name + ".height", this.condition.height().getLeft() + "-" + condition.height().getRight());
}

/**
Expand Down Expand Up @@ -161,23 +136,16 @@ public ItemStack createItemStack() {
return this.itemStack.clone(); // Clone the item stack to prevent any changes
}

private StringPlaceholders placeholders() {
return StringPlaceholders.builder()
public StringPlaceholders placeholders() {
StringPlaceholders.Builder builder = StringPlaceholders.builder()
.add("id", this.name)
.add("name", this.displayName)
.add("tier", this.tier)
.add("description", String.join("\n", this.description))
.add("biomes", this.condition.biomes().isEmpty() ? "Any Biome" : String.join(", ", this.condition.biomes()))
.add("weather", FishUtils.niceify(this.condition.weather(), "Any Weather"))
.add("time", FishUtils.niceify(this.condition.time(), "Any Time"))
.add("worlds", this.condition.worlds().isEmpty() ? "Any World" : String.join(", ", this.condition.worlds()))
.add("environment", FishUtils.niceify(this.condition.environment(), "Any Environment"))
.add("water-depth", this.condition.waterDepth() == null ? "Any Depth" : this.condition.waterDepth().toString())
.add("ice-fishing", this.condition.iceFishing() ? "Yes" : "No")
.add("height", this.condition.height() == null ? "All" : this.condition.height().getLeft() + " - " + this.condition.height().getRight())
.add("light-level", this.condition.lightLevel() == null ? "All" : this.condition.lightLevel().toString())
.add("boat-fishing", this.condition.boatFishing() ? "Yes" : "No")
.build();
.add("description", String.join("\n", this.description));

// Add all the placeholders from the conditions
this.conditions.forEach(condition -> builder.addAll(condition.placeholders()));
return builder.build();
}

/**
Expand Down
127 changes: 0 additions & 127 deletions src/main/java/dev/oribuin/fishing/fish/condition/Condition.java

This file was deleted.

Loading

0 comments on commit f5be61e

Please sign in to comment.