diff --git a/api/src/main/java/net/momirealms/customfishing/api/event/FishingResultEvent.java b/api/src/main/java/net/momirealms/customfishing/api/event/FishingResultEvent.java new file mode 100644 index 00000000..bfe1a771 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/event/FishingResultEvent.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + +package net.momirealms.customfishing.api.event; + +import net.momirealms.customfishing.api.mechanic.loot.Loot; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class FishingResultEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList handlerList = new HandlerList(); + private boolean isCancelled; + private final Result result; + private final Loot loot; + private final Map args; + + public FishingResultEvent(@NotNull Player who, Result result, Loot loot, Map args) { + super(who); + this.result = result; + this.loot = loot; + this.args = args; + } + + public static HandlerList getHandlerList() { + return handlerList; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + @Override + public boolean isCancelled() { + return isCancelled; + } + + @Override + public void setCancelled(boolean cancel) { + isCancelled = cancel; + } + + public String getArg(String key) { + return args.get("{" + key + "}"); + } + + public Result getResult() { + return result; + } + + public Loot getLoot() { + return loot; + } + + public enum Result { + SUCCESS, + FAILURE + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/manager/EffectManager.java b/api/src/main/java/net/momirealms/customfishing/api/manager/EffectManager.java index ddd74e00..0612efef 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/manager/EffectManager.java +++ b/api/src/main/java/net/momirealms/customfishing/api/manager/EffectManager.java @@ -19,14 +19,16 @@ package net.momirealms.customfishing.api.manager; import net.momirealms.customfishing.api.common.Key; import net.momirealms.customfishing.api.mechanic.effect.Effect; +import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier; import org.jetbrains.annotations.Nullable; public interface EffectManager { - boolean registerEffect(Key key, Effect effect); - boolean unregisterEffect(Key key); + boolean registerEffectItem(Key key, EffectCarrier effect); - @Nullable Effect getEffect(String namespace, String id); + boolean unregisterEffectItem(Key key); + + @Nullable EffectCarrier getEffect(String namespace, String id); Effect getInitialEffect(); } diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/action/ActionExpansion.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/action/ActionExpansion.java index 39ab1ba0..bd48cfd1 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/action/ActionExpansion.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/action/ActionExpansion.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + package net.momirealms.customfishing.api.mechanic.action; public abstract class ActionExpansion { diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/condition/FishingPreparation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/condition/FishingPreparation.java index ca98cbbd..9a2a5844 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/condition/FishingPreparation.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/condition/FishingPreparation.java @@ -19,6 +19,7 @@ package net.momirealms.customfishing.api.mechanic.condition; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.mechanic.effect.Effect; +import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -36,11 +37,11 @@ public class FishingPreparation extends Condition { private final boolean rodOnMainHand; private final @NotNull ItemStack rodItemStack; private final @NotNull String rodItemID; - private final @Nullable Effect rodEffect; + private final @Nullable EffectCarrier rodEffect; private @Nullable ItemStack baitItemStack; private @Nullable String baitItemID; - private @Nullable Effect baitEffect; - private final List utilEffects; + private @Nullable EffectCarrier baitEffect; + private final List utilEffects; private boolean canFish = true; public FishingPreparation(Player player, CustomFishingPlugin plugin) { @@ -55,15 +56,18 @@ public class FishingPreparation extends Condition { this.rodItemStack = this.rodOnMainHand ? mainHandItem : offHandItem; this.rodItemID = plugin.getItemManager().getAnyItemID(this.rodItemStack); this.rodEffect = plugin.getEffectManager().getEffect("rod", this.rodItemID); - super.insertArg("rod", this.rodItemID); + super.insertArg("{rod}", this.rodItemID); String baitItemID = plugin.getItemManager().getAnyItemID(this.rodOnMainHand ? offHandItem : mainHandItem); - Effect baitEffect = plugin.getEffectManager().getEffect("bait", baitItemID); + EffectCarrier baitEffect = plugin.getEffectManager().getEffect("bait", baitItemID); + if (baitEffect != null) { this.baitItemID = baitItemID; this.baitItemStack = this.rodOnMainHand ? offHandItem : mainHandItem; this.baitEffect = baitEffect; - } else if (plugin.getBagManager().isBagEnabled()) { + } + + if (plugin.getBagManager().isBagEnabled()) { Inventory fishingBag = plugin.getBagManager().getOnlineBagInventory(player.getUniqueId()); HashSet uniqueUtils = new HashSet<>(4); if (fishingBag != null) { @@ -71,8 +75,8 @@ public class FishingPreparation extends Condition { ItemStack itemInBag = fishingBag.getItem(i); String bagItemID = plugin.getItemManager().getItemID(itemInBag); if (bagItemID == null) continue; - if (this.baitEffect == null) { - Effect effect = plugin.getEffectManager().getEffect("bait", bagItemID); + if (this.baitItemID == null) { + EffectCarrier effect = plugin.getEffectManager().getEffect("bait", bagItemID); if (effect != null) { this.baitItemID = bagItemID; this.baitItemStack = itemInBag; @@ -80,32 +84,27 @@ public class FishingPreparation extends Condition { continue; } } - Effect utilEffect = plugin.getEffectManager().getEffect("util", bagItemID); - if (utilEffect != null - && !uniqueUtils.contains(bagItemID) - && utilEffect.canMerge(this)) { + EffectCarrier utilEffect = plugin.getEffectManager().getEffect("util", bagItemID); + if (utilEffect != null && !uniqueUtils.contains(bagItemID) && utilEffect.isConditionMet(this)) { utilEffects.add(utilEffect); uniqueUtils.add(bagItemID); } } } - } else { - this.baitItemID = null; - this.baitItemStack = null; - this.baitEffect = null; } if (this.baitEffect != null) { - if (!this.baitEffect.canMerge(this)) { + if (!this.baitEffect.isConditionMet(this)) { this.canFish = false; return; } - super.insertArg("bait", this.baitItemID); + super.insertArg("{bait}", this.baitItemID); } if (this.rodEffect != null) { - if (!this.rodEffect.canMerge(this)) { + if (!this.rodEffect.isConditionMet(this)) { this.canFish = false; + return; } } } @@ -134,27 +133,24 @@ public class FishingPreparation extends Condition { return baitItemID; } - @Nullable - public Effect getRodEffect() { - return rodEffect; - } - - @Nullable - public Effect getBaitEffect() { - return baitEffect; - } - public boolean canFish() { return this.canFish; } + @NotNull @Override - public @NotNull Player getPlayer() { - assert super.player != null; + public Player getPlayer() { return super.player; } - public List getUtilEffects() { - return utilEffects; + public Effect mergeEffect(Effect effect) { + if (this.rodEffect != null) + effect.merge(this.rodEffect.getEffect()); + if (this.baitEffect != null) + effect.merge(this.baitEffect.getEffect()); + for (EffectCarrier util : utilEffects) { + effect.merge(util.getEffect()); + } + return effect; } } diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/AbstractEffect.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/AbstractEffect.java index 33ff7807..f8ddd7ef 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/AbstractEffect.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/AbstractEffect.java @@ -34,19 +34,8 @@ public class AbstractEffect implements Effect { protected double timeModifier = 1; protected double difficultyModifier = 0; protected double gameTimeModifier = 0; - protected Requirement[] requirements; protected List> lootWeightModifier = new ArrayList<>(); - @Override - public boolean persist() { - return false; - } - - @Override - public Requirement[] getRequirements() { - return requirements; - } - @Override public boolean canLavaFishing() { return lavaFishing; @@ -99,15 +88,4 @@ public class AbstractEffect implements Effect { public List> getLootWeightModifier() { return lootWeightModifier; } - - @Override - public boolean canMerge(Condition condition) { - if (this.requirements == null) return true; - for (Requirement requirement : requirements) { - if (!requirement.isConditionMet(condition)) { - return false; - } - } - return true; - } } diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java index 2344a899..06ea1cb8 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java @@ -26,10 +26,6 @@ import java.util.List; public interface Effect { - boolean persist(); - - Requirement[] getRequirements(); - boolean canLavaFishing(); double getMultipleLootChance(); @@ -47,6 +43,4 @@ public interface Effect { Effect merge(Effect another); List> getLootWeightModifier(); - - boolean canMerge(Condition condition); } diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectCarrier.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectCarrier.java new file mode 100644 index 00000000..24cb1f49 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectCarrier.java @@ -0,0 +1,86 @@ +package net.momirealms.customfishing.api.mechanic.effect; + +import net.momirealms.customfishing.api.common.Key; +import net.momirealms.customfishing.api.mechanic.action.Action; +import net.momirealms.customfishing.api.mechanic.action.ActionTrigger; +import net.momirealms.customfishing.api.mechanic.condition.Condition; +import net.momirealms.customfishing.api.mechanic.requirement.Requirement; + +import java.util.Map; + +public class EffectCarrier { + + private Key key; + private Requirement[] requirements; + private Effect effect; + private Map actionMap; + private boolean persist; + + public static class Builder { + + private final EffectCarrier item; + + public Builder() { + this.item = new EffectCarrier(); + } + + public Builder persist(boolean persist) { + item.persist = persist; + return this; + } + + public Builder key(Key key) { + item.key = key; + return this; + } + + public Builder requirements(Requirement[] requirements) { + item.requirements = requirements; + return this; + } + + public Builder effect(Effect effect) { + item.effect = effect; + return this; + } + + public Builder actionMap(Map actionMap) { + item.actionMap = actionMap; + return this; + } + + public EffectCarrier build() { + return item; + } + } + + public Key getKey() { + return key; + } + + public Requirement[] getRequirements() { + return requirements; + } + + public Effect getEffect() { + return effect; + } + + public Map getActionMap() { + return actionMap; + } + + public boolean isPersist() { + return persist; + } + + public boolean isConditionMet(Condition condition) { + if (requirements == null) return true; + for (Requirement requirement : requirements) { + if (!requirement.isConditionMet(condition)) { + return false; + } + } + return true; + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/FishingEffect.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/FishingEffect.java index a7c75071..c6096387 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/FishingEffect.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/FishingEffect.java @@ -73,11 +73,6 @@ public class FishingEffect extends AbstractEffect { return this; } - public Builder requirements(Requirement[] requirements) { - effect.requirements = requirements; - return this; - } - public FishingEffect build() { return effect; } diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameExpansion.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameExpansion.java index 082c1e1e..1ac43f95 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameExpansion.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameExpansion.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + package net.momirealms.customfishing.api.mechanic.game; public abstract class GameExpansion { diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameFactory.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameFactory.java index bd5c02b8..aad604a8 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameFactory.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/GameFactory.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + package net.momirealms.customfishing.api.mechanic.game; import org.bukkit.configuration.ConfigurationSection; diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/requirement/RequirementExpansion.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/requirement/RequirementExpansion.java index c4120512..145abc37 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/requirement/RequirementExpansion.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/requirement/RequirementExpansion.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + package net.momirealms.customfishing.api.mechanic.requirement; public abstract class RequirementExpansion { diff --git a/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java b/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java index b3e0477c..9a7796f8 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java +++ b/plugin/src/main/java/net/momirealms/customfishing/command/sub/ItemCommand.java @@ -18,24 +18,26 @@ package net.momirealms.customfishing.command.sub; import dev.jorel.commandapi.CommandAPICommand; -import dev.jorel.commandapi.arguments.ArgumentSuggestions; -import dev.jorel.commandapi.arguments.EntitySelectorArgument; -import dev.jorel.commandapi.arguments.IntegerArgument; -import dev.jorel.commandapi.arguments.TextArgument; +import dev.jorel.commandapi.arguments.*; import net.momirealms.customfishing.adventure.AdventureManagerImpl; import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Key; +import net.momirealms.customfishing.api.mechanic.condition.Condition; +import net.momirealms.customfishing.api.mechanic.item.BuildableItem; import net.momirealms.customfishing.mechanic.item.ItemManagerImpl; import net.momirealms.customfishing.setting.Locale; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.util.Collection; +import java.util.HashMap; public class ItemCommand { public static ItemCommand INSTANCE = new ItemCommand(); + private final HashMap completionMap = new HashMap<>(); + public CommandAPICommand getItemCommand() { return new CommandAPICommand("items") .withPermission("customfishing.command.items") @@ -48,28 +50,31 @@ public class ItemCommand { } private CommandAPICommand getSubCommand(String namespace) { - Collection items = CustomFishingPlugin.get() + completionMap.put(namespace, CustomFishingPlugin.get() .getItemManager() .getAllItemsKey() .stream() .filter(it -> it.namespace().equals(namespace)) .map(Key::value) - .toList(); + .toList().toArray(new String[0])); return new CommandAPICommand(namespace) .withSubcommands( - getCommand(namespace, items), - giveCommand(namespace, items) + getCommand(namespace), + giveCommand(namespace) ); } - private CommandAPICommand getCommand(String namespace, Collection items) { + private CommandAPICommand getCommand(String namespace) { return new CommandAPICommand("get") - .withArguments(new TextArgument("id").replaceSuggestions(ArgumentSuggestions.strings(items))) + .withArguments(new StringArgument("id") + .replaceSuggestions(ArgumentSuggestions.strings( + info -> completionMap.get(namespace) + ))) .withOptionalArguments(new IntegerArgument("amount", 1)) .executesPlayer((player, args) -> { String id = (String) args.get("id"); int amount = (int) args.getOrDefault("amount", 1); - ItemStack item = CustomFishingPlugin.get().getItemManager().build(player, namespace, id); + ItemStack item = CustomFishingPlugin.get().getItemManager().build(player, namespace, id, new Condition(player).getArgs()); if (item != null) { int actual = ItemManagerImpl.giveCertainAmountOfItem(player, item, amount); AdventureManagerImpl.getInstance().sendMessageWithPrefix(player, Locale.MSG_Get_Item.replace("{item}", id).replace("{amount}", String.valueOf(actual))); @@ -79,18 +84,22 @@ public class ItemCommand { }); } - private CommandAPICommand giveCommand(String namespace, Collection items) { + private CommandAPICommand giveCommand(String namespace) { return new CommandAPICommand("give") .withArguments(new EntitySelectorArgument.ManyPlayers("player")) - .withArguments(new TextArgument("id").replaceSuggestions(ArgumentSuggestions.strings(items))) + .withArguments(new StringArgument("id") + .replaceSuggestions(ArgumentSuggestions.strings( + info -> completionMap.get(namespace) + ))) .withOptionalArguments(new IntegerArgument("amount", 1)) .executes((sender, args) -> { Collection players = (Collection) args.get("player"); String id = (String) args.get("id"); int amount = (int) args.getOrDefault("amount", 1); - ItemStack item = CustomFishingPlugin.get().getItemManager().build(players.stream().findAny().get(), namespace, id); - if (item != null) { + BuildableItem buildableItem = CustomFishingPlugin.get().getItemManager().getBuildableItem(namespace, id); + if (buildableItem != null) { for (Player player : players) { + ItemStack item = CustomFishingPlugin.get().getItemManager().build(player, namespace, id, new Condition(player).getArgs()); int actual = ItemManagerImpl.giveCertainAmountOfItem(player, item, amount); AdventureManagerImpl.getInstance().sendMessageWithPrefix(sender, Locale.MSG_Give_Item.replace("{item}", id).replace("{amount}", String.valueOf(actual)).replace("{player}", player.getName())); } diff --git a/plugin/src/main/java/net/momirealms/customfishing/command/sub/StatisticsCommand.java b/plugin/src/main/java/net/momirealms/customfishing/command/sub/StatisticsCommand.java new file mode 100644 index 00000000..8f24f203 --- /dev/null +++ b/plugin/src/main/java/net/momirealms/customfishing/command/sub/StatisticsCommand.java @@ -0,0 +1,4 @@ +package net.momirealms.customfishing.command.sub; + +public class StatisticsCommand { +} diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/effect/EffectManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/effect/EffectManagerImpl.java index f02e3f01..6e2ae1c9 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/effect/EffectManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/effect/EffectManagerImpl.java @@ -21,6 +21,7 @@ import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Key; import net.momirealms.customfishing.api.manager.EffectManager; import net.momirealms.customfishing.api.mechanic.effect.Effect; +import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier; import net.momirealms.customfishing.api.mechanic.effect.FishingEffect; import net.momirealms.customfishing.util.ConfigUtils; import org.apache.commons.lang3.StringUtils; @@ -35,7 +36,7 @@ public class EffectManagerImpl implements EffectManager { private final CustomFishingPlugin plugin; - private final HashMap effectMap; + private final HashMap effectMap; public EffectManagerImpl(CustomFishingPlugin plugin) { this.plugin = plugin; @@ -43,20 +44,20 @@ public class EffectManagerImpl implements EffectManager { } @Override - public boolean registerEffect(Key key, Effect effect) { + public boolean registerEffectItem(Key key, EffectCarrier effect) { if (effectMap.containsKey(key)) return false; this.effectMap.put(key, effect); return true; } @Override - public boolean unregisterEffect(Key key) { + public boolean unregisterEffectItem(Key key) { return this.effectMap.remove(key) != null; } @Nullable @Override - public Effect getEffect(String namespace, String id) { + public EffectCarrier getEffect(String namespace, String id) { return effectMap.get(Key.of(namespace, id)); } @@ -90,31 +91,42 @@ public class EffectManagerImpl implements EffectManager { for (Map.Entry entry : yaml.getValues(false).entrySet()) { String value = entry.getKey(); if (entry.getValue() instanceof ConfigurationSection section) { - effectMap.put(Key.of(namespace, value), getFishingEffectFromSection(section)); + Key key = Key.of(namespace, value); + EffectCarrier item = getEffectItemFromSection(key, section); + if (item != null) + effectMap.put(key, item); } } } - private Effect getFishingEffectFromSection(ConfigurationSection section) { + private EffectCarrier getEffectItemFromSection(Key key, ConfigurationSection section) { + if (section == null) return null; + return new EffectCarrier.Builder() + .key(key) + .requirements(plugin.getRequirementManager().getRequirements(section.getConfigurationSection("requirements"), true)) + .effect(getEffectFromSection(section.getConfigurationSection("effects"))) + .build(); + } + + public Effect getEffectFromSection(ConfigurationSection section) { if (section == null) return getInitialEffect(); return new FishingEffect.Builder() .lootWeightModifier(ConfigUtils.getModifiers(section.getStringList("weight"))) .timeModifier(section.getDouble("hook-time", 1)) .difficultyModifier(section.getDouble("difficulty", 0)) - .multipleLootChance(section.getDouble("multiple-loot")) + .multipleLootChance(section.getDouble("multiple-loot", 0)) .lavaFishing(section.getBoolean("lava-fishing", false)) .scoreMultiplier(section.getDouble("score-bonus", 1)) .sizeMultiplier(section.getDouble("size-bonus", 1)) .gameTimeModifier(section.getDouble("game-time", 0)) - .requirements(plugin.getRequirementManager().getRequirements(section.getConfigurationSection("requirements"), true)) .build(); } public void unload() { - HashMap temp = new HashMap<>(effectMap); + HashMap temp = new HashMap<>(effectMap); effectMap.clear(); - for (Map.Entry entry : temp.entrySet()) { - if (entry.getValue().persist()) { + for (Map.Entry entry : temp.entrySet()) { + if (entry.getValue().isPersist()) { effectMap.put(entry.getKey(), entry.getValue()); } } diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java index af1450ca..70a6b691 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java @@ -21,6 +21,7 @@ import com.destroystokyo.paper.event.player.PlayerJumpEvent; import de.tr7zw.changeme.nbtapi.NBTItem; import net.momirealms.customfishing.CustomFishingPluginImpl; import net.momirealms.customfishing.api.common.Pair; +import net.momirealms.customfishing.api.event.FishingResultEvent; import net.momirealms.customfishing.api.event.LavaFishingEvent; import net.momirealms.customfishing.api.event.RodCastEvent; import net.momirealms.customfishing.api.manager.FishingManager; @@ -31,6 +32,7 @@ import net.momirealms.customfishing.api.mechanic.action.ActionTrigger; import net.momirealms.customfishing.api.mechanic.competition.FishingCompetition; import net.momirealms.customfishing.api.mechanic.condition.FishingPreparation; import net.momirealms.customfishing.api.mechanic.effect.Effect; +import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier; import net.momirealms.customfishing.api.mechanic.game.GameConfig; import net.momirealms.customfishing.api.mechanic.game.GameInstance; import net.momirealms.customfishing.api.mechanic.game.GameSettings; @@ -243,19 +245,13 @@ public class FishingManagerImpl implements Listener, FishingManager { } // Merge rod/bait/util effects Effect initialEffect = plugin.getEffectManager().getInitialEffect(); - initialEffect - .merge(fishingPreparation.getRodEffect()) - .merge(fishingPreparation.getBaitEffect()); - - for (Effect utilEffect : fishingPreparation.getUtilEffects()) { - initialEffect.merge(utilEffect); - } + fishingPreparation.mergeEffect(initialEffect); // Apply enchants for (String enchant : plugin.getIntegrationManager().getEnchantments(fishingPreparation.getRodItemStack())) { - Effect enchantEffect = plugin.getEffectManager().getEffect("enchant", enchant); - if (enchantEffect != null && enchantEffect.canMerge(fishingPreparation)) { - initialEffect.merge(enchantEffect); + EffectCarrier enchantEffect = plugin.getEffectManager().getEffect("enchant", enchant); + if (enchantEffect != null && enchantEffect.isConditionMet(fishingPreparation)) { + initialEffect.merge(enchantEffect.getEffect()); } } //TODO Apply totem effects @@ -454,14 +450,14 @@ public class FishingManagerImpl implements Listener, FishingManager { if (gamingPlayer.isSuccessful()) success(tempFishingState, fishHook); else - fail(tempFishingState); + fail(tempFishingState, fishHook); gamingPlayer.cancel(); gamingPlayerMap.remove(uuid); plugin.getScheduler().runTaskSync(fishHook::remove, fishHook.getLocation()); } - public void fail(TempFishingState state) { + public void fail(TempFishingState state, FishHook hook) { var loot = state.getLoot(); var fishingPreparation = state.getPreparation(); @@ -473,15 +469,29 @@ public class FishingManagerImpl implements Listener, FishingManager { } } - Action[] globalActions = LootManagerImpl.globalLootProperties.getActions(ActionTrigger.FAILURE); - if (globalActions != null) - for (Action action : globalActions) - action.trigger(fishingPreparation); + plugin.getScheduler().runTaskSync(() -> { + // call event + FishingResultEvent fishingResultEvent = new FishingResultEvent( + fishingPreparation.getPlayer(), + FishingResultEvent.Result.FAILURE, + loot, + fishingPreparation.getArgs() + ); + Bukkit.getPluginManager().callEvent(fishingResultEvent); + if (fishingResultEvent.isCancelled()) { + return; + } - Action[] actions = loot.getActions(ActionTrigger.FAILURE); - if (actions != null) - for (Action action : actions) - action.trigger(fishingPreparation); + Action[] globalActions = LootManagerImpl.globalLootProperties.getActions(ActionTrigger.FAILURE); + if (globalActions != null) + for (Action action : globalActions) + action.trigger(fishingPreparation); + + Action[] actions = loot.getActions(ActionTrigger.FAILURE); + if (actions != null) + for (Action action : actions) + action.trigger(fishingPreparation); + }, hook.getLocation()); } public void success(TempFishingState state, FishHook hook) { @@ -496,6 +506,19 @@ public class FishingManagerImpl implements Listener, FishingManager { fishingPreparation.insertArg("{z}", String.valueOf(hook.getLocation().getBlockZ())); plugin.getScheduler().runTaskSync(() -> { + + // call event + FishingResultEvent fishingResultEvent = new FishingResultEvent( + player, + FishingResultEvent.Result.SUCCESS, + loot, + fishingPreparation.getArgs() + ); + Bukkit.getPluginManager().callEvent(fishingResultEvent); + if (fishingResultEvent.isCancelled()) { + return; + } + switch (loot.getType()) { case LOOT -> { int amount = (int) effect.getMultipleLootChance(); diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java index caccd963..63567006 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java @@ -209,7 +209,7 @@ public class ItemManagerImpl implements ItemManager { @Override public CFBuilder getItemBuilder(ConfigurationSection section, String type, String id) { if (section == null) return null; - String material = section.getString("material", "PAPER"); + String material = section.getString("material", type.equals("rod") ? "FISHING_ROD" : "PAPER"); CFBuilder itemCFBuilder; if (material.contains(":")) { String[] split = material.split(":", 2); diff --git a/plugin/src/main/java/net/momirealms/customfishing/util/ClassUtils.java b/plugin/src/main/java/net/momirealms/customfishing/util/ClassUtils.java index b98e9fc0..51cfbc15 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/util/ClassUtils.java +++ b/plugin/src/main/java/net/momirealms/customfishing/util/ClassUtils.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + package net.momirealms.customfishing.util; import org.jetbrains.annotations.NotNull; diff --git a/plugin/src/main/resources/contents/rods/default.yml b/plugin/src/main/resources/contents/rods/default.yml index 83b1e797..205d31e8 100644 --- a/plugin/src/main/resources/contents/rods/default.yml +++ b/plugin/src/main/resources/contents/rods/default.yml @@ -16,7 +16,7 @@ nature_fishing_cane: - 'The wild power makes it easier to be hooked' - 'But also increase the difficulty' custom-model-data: 50001 - effect: + effects: hook-time: 0.9 difficulty: 1 game-time: 3 @@ -28,10 +28,6 @@ silver_fishing_rod: lore: - 'Increase the chance of getting silver quality fish' custom-model-data: 50002 - effect: - weight-add: - silver: 20 - gold: -10 golden_fishing_rod: material: fishing_rod @@ -40,17 +36,10 @@ golden_fishing_rod: lore: - 'Increase the chance of getting golden quality fish' custom-model-data: 50003 - effect: - weight-add: - silver: -20 - gold: 20 star_fishing_rod: material: fishing_rod - effect: - weight-add: - silver: 20 - gold: 10 + effects: lava-fishing: true # https://mo-mi.gitbook.io/xiaomomi-plugins/plugin-wiki/customfishing/requirements requirements: