9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-29 03:49:07 +00:00

events and actions

This commit is contained in:
XiaoMoMi
2023-09-09 03:29:37 +08:00
parent f56169c56e
commit 7f9cc4f2c0
31 changed files with 713 additions and 307 deletions

View File

@@ -0,0 +1,36 @@
package net.momirealms.customfishing.api.event;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
public class FishHookLandEvent extends PlayerEvent {
private static final HandlerList handlerList = new HandlerList();
private final Target target;
public FishHookLandEvent(@NotNull Player who, Target target) {
super(who);
this.target = target;
}
public Target getTarget() {
return target;
}
public static HandlerList getHandlerList() {
return handlerList;
}
@NotNull
@Override
public HandlerList getHandlers() {
return getHandlerList();
}
public enum Target {
LAVA,
WATER
}
}

View File

@@ -17,6 +17,7 @@
package net.momirealms.customfishing.api.event;
import net.momirealms.customfishing.api.mechanic.condition.FishingPreparation;
import net.momirealms.customfishing.api.mechanic.effect.Effect;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
@@ -29,12 +30,14 @@ public class RodCastEvent extends PlayerEvent implements Cancellable {
private final Effect effect;
private boolean isCancelled;
private final PlayerFishEvent event;
private final FishingPreparation preparation;
private static final HandlerList handlerList = new HandlerList();
public RodCastEvent(PlayerFishEvent event, Effect effect) {
public RodCastEvent(PlayerFishEvent event, FishingPreparation fishingPreparation, Effect effect) {
super(event.getPlayer());
this.effect = effect;
this.event = event;
this.preparation = fishingPreparation;
}
@Override
@@ -51,6 +54,10 @@ public class RodCastEvent extends PlayerEvent implements Cancellable {
return handlerList;
}
public FishingPreparation getPreparation() {
return preparation;
}
@NotNull
@Override
public HandlerList getHandlers() {

View File

@@ -19,8 +19,11 @@ package net.momirealms.customfishing.api.manager;
import net.momirealms.customfishing.api.mechanic.action.Action;
import net.momirealms.customfishing.api.mechanic.action.ActionFactory;
import net.momirealms.customfishing.api.mechanic.action.ActionTrigger;
import org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap;
public interface ActionManager {
boolean registerAction(String type, ActionFactory actionFactory);
@@ -29,6 +32,8 @@ public interface ActionManager {
Action getAction(ConfigurationSection section);
HashMap<ActionTrigger, Action[]> getActionMap(ConfigurationSection section);
Action[] getActions(ConfigurationSection section);
ActionFactory getActionBuilder(String type);

View File

@@ -18,6 +18,7 @@
package net.momirealms.customfishing.api.manager;
import net.momirealms.customfishing.api.mechanic.TempFishingState;
import net.momirealms.customfishing.api.mechanic.condition.FishingPreparation;
import net.momirealms.customfishing.api.mechanic.effect.Effect;
import net.momirealms.customfishing.api.mechanic.game.GameInstance;
import net.momirealms.customfishing.api.mechanic.game.GameSettings;
@@ -25,7 +26,10 @@ import net.momirealms.customfishing.api.mechanic.game.GamingPlayer;
import net.momirealms.customfishing.api.mechanic.loot.Loot;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
@@ -42,6 +46,12 @@ public interface FishingManager {
void processGameResult(GamingPlayer gamingPlayer);
Collection<String> getPossibleLootKeys(FishingPreparation fishingPreparation);
@NotNull Map<String, Double> getPossibleLootKeysWithWeight(Effect initialEffect, FishingPreparation fishingPreparation);
Loot getNextLoot(Effect initialEffect, FishingPreparation fishingPreparation);
void startFishingGame(Player player, Loot loot, Effect effect);
void startFishingGame(Player player, GameSettings settings, GameInstance gameInstance);

View File

@@ -23,7 +23,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
public interface LootManager {
Loot getGlobalLootProperties();
@Nullable List<String> getLootGroup(String key);

View File

@@ -0,0 +1,64 @@
package net.momirealms.customfishing.api.mechanic;
import net.momirealms.customfishing.api.CustomFishingPlugin;
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 org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap;
import java.util.Map;
public class GlobalSettings {
public static HashMap<ActionTrigger, Action[]> lootActions = new HashMap<>();
public static HashMap<ActionTrigger, Action[]> rodActions = new HashMap<>();
public static HashMap<ActionTrigger, Action[]> baitActions = new HashMap<>();
public static void load(ConfigurationSection section) {
if (section == null) return;
for (Map.Entry<String, Object> entry : section.getValues(false).entrySet()) {
if (entry.getValue() instanceof ConfigurationSection inner) {
HashMap<ActionTrigger, Action[]> map = CustomFishingPlugin.get().getActionManager().getActionMap(inner);
switch (entry.getKey()) {
case "loot" -> lootActions = map;
case "rod" -> rodActions = map;
case "bait" -> baitActions = map;
}
}
}
}
public static void unload() {
lootActions.clear();
rodActions.clear();
baitActions.clear();
}
public static void triggerLootActions(ActionTrigger trigger, Condition condition) {
Action[] actions = lootActions.get(trigger);
if (actions != null) {
for (Action action : actions) {
action.trigger(condition);
}
}
}
public static void triggerRodActions(ActionTrigger trigger, Condition condition) {
Action[] actions = rodActions.get(trigger);
if (actions != null) {
for (Action action : actions) {
action.trigger(condition);
}
}
}
public static void triggerBaitActions(ActionTrigger trigger, Condition condition) {
Action[] actions = baitActions.get(trigger);
if (actions != null) {
for (Action action : actions) {
action.trigger(condition);
}
}
}
}

View File

@@ -23,5 +23,6 @@ public enum ActionTrigger {
FAILURE,
HOOK,
CONSUME,
CAST
CAST,
BITE, LAND
}

View File

@@ -27,20 +27,9 @@ import java.util.Map;
public class Condition {
@Nullable
protected final Location location;
@Nullable
protected final Player player;
@NotNull
protected final Map<String, String> args;
public Condition() {
this(null, null, new HashMap<>());
}
public Condition(HashMap<String, String> args) {
this(null, null, args);
}
protected @NotNull Location location;
protected final @NotNull Player player;
protected final @NotNull Map<String, String> args;
public Condition(Player player) {
this(player.getLocation(), player, new HashMap<>());
@@ -50,26 +39,31 @@ public class Condition {
this(player.getLocation(), player, args);
}
public Condition(@Nullable Location location, @Nullable Player player, @NotNull Map<String, String> args) {
public Condition(@NotNull Location location, @NotNull Player player, @NotNull Map<String, String> args) {
this.location = location;
this.player = player;
this.args = args;
if (player != null)
this.args.put("{player}", player.getName());
if (location != null) {
this.args.put("{x}", String.valueOf(location.getX()));
this.args.put("{y}", String.valueOf(location.getY()));
this.args.put("{z}", String.valueOf(location.getZ()));
this.args.put("{world}", location.getWorld().getName());
}
this.args.put("{player}", player.getName());
this.args.put("{x}", String.valueOf(location.getX()));
this.args.put("{y}", String.valueOf(location.getY()));
this.args.put("{z}", String.valueOf(location.getZ()));
this.args.put("{world}", location.getWorld().getName());
}
@Nullable
public void setLocation(@NotNull Location location) {
this.location = location;
this.args.put("{x}", String.valueOf(location.getX()));
this.args.put("{y}", String.valueOf(location.getY()));
this.args.put("{z}", String.valueOf(location.getZ()));
this.args.put("{world}", location.getWorld().getName());
}
@NotNull
public Location getLocation() {
return location;
}
@Nullable
@NotNull
public Player getPlayer() {
return player;
}

View File

@@ -18,6 +18,9 @@
package net.momirealms.customfishing.api.mechanic.condition;
import net.momirealms.customfishing.api.CustomFishingPlugin;
import net.momirealms.customfishing.api.mechanic.GlobalSettings;
import net.momirealms.customfishing.api.mechanic.action.Action;
import net.momirealms.customfishing.api.mechanic.action.ActionTrigger;
import net.momirealms.customfishing.api.mechanic.effect.Effect;
import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier;
import org.bukkit.Material;
@@ -34,14 +37,12 @@ import java.util.List;
public class FishingPreparation extends Condition {
private final boolean rodOnMainHand;
private final @NotNull ItemStack rodItemStack;
private final @NotNull String rodItemID;
private @Nullable EffectCarrier baitEffect;
private final @Nullable EffectCarrier rodEffect;
private @Nullable ItemStack baitItemStack;
private @Nullable String baitItemID;
private @Nullable EffectCarrier baitEffect;
private final @NotNull ItemStack rodItemStack;
private final List<EffectCarrier> utilEffects;
private final Effect enchantEffect;
private boolean canFish = true;
public FishingPreparation(Player player, CustomFishingPlugin plugin) {
@@ -52,18 +53,18 @@ public class FishingPreparation extends Condition {
ItemStack offHandItem = playerInventory.getItemInOffHand();
this.utilEffects = new ArrayList<>();
this.rodOnMainHand = mainHandItem.getType() == Material.FISHING_ROD;
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);
this.enchantEffect = plugin.getEffectManager().getInitialEffect();
boolean rodOnMainHand = mainHandItem.getType() == Material.FISHING_ROD;
this.rodItemStack = rodOnMainHand ? mainHandItem : offHandItem;
String rodItemID = plugin.getItemManager().getAnyItemID(this.rodItemStack);
this.rodEffect = plugin.getEffectManager().getEffect("rod", rodItemID);
super.insertArg("{rod}", rodItemID);
String baitItemID = plugin.getItemManager().getAnyItemID(this.rodOnMainHand ? offHandItem : mainHandItem);
String baitItemID = plugin.getItemManager().getAnyItemID(rodOnMainHand ? offHandItem : mainHandItem);
EffectCarrier baitEffect = plugin.getEffectManager().getEffect("bait", baitItemID);
if (baitEffect != null) {
this.baitItemID = baitItemID;
this.baitItemStack = this.rodOnMainHand ? offHandItem : mainHandItem;
this.baitItemStack = rodOnMainHand ? offHandItem : mainHandItem;
this.baitEffect = baitEffect;
}
@@ -75,10 +76,9 @@ public class FishingPreparation extends Condition {
ItemStack itemInBag = fishingBag.getItem(i);
String bagItemID = plugin.getItemManager().getItemID(itemInBag);
if (bagItemID == null) continue;
if (this.baitItemID == null) {
if (this.baitEffect == null) {
EffectCarrier effect = plugin.getEffectManager().getEffect("bait", bagItemID);
if (effect != null) {
this.baitItemID = bagItemID;
this.baitItemStack = itemInBag;
this.baitEffect = effect;
continue;
@@ -98,7 +98,7 @@ public class FishingPreparation extends Condition {
this.canFish = false;
return;
}
super.insertArg("{bait}", this.baitItemID);
super.insertArg("{bait}", this.baitEffect.getKey().value());
}
if (this.rodEffect != null) {
@@ -107,10 +107,13 @@ public class FishingPreparation extends Condition {
return;
}
}
}
public boolean isRodOnMainHand() {
return rodOnMainHand;
for (String enchant : plugin.getIntegrationManager().getEnchantments(rodItemStack)) {
EffectCarrier enchantEffect = plugin.getEffectManager().getEffect("enchant", enchant);
if (enchantEffect != null && enchantEffect.isConditionMet(this)) {
this.enchantEffect.merge(enchantEffect.getEffect());
}
}
}
@NotNull
@@ -118,39 +121,54 @@ public class FishingPreparation extends Condition {
return rodItemStack;
}
@NotNull
public String getRodItemID() {
return rodItemID;
}
@Nullable
public ItemStack getBaitItemStack() {
return baitItemStack;
}
@Nullable
public String getBaitItemID() {
return baitItemID;
public EffectCarrier getBaitEffect() {
return baitEffect;
}
@Nullable
public EffectCarrier getRodEffect() {
return rodEffect;
}
public boolean canFish() {
return this.canFish;
}
@NotNull
@Override
public Player getPlayer() {
return super.player;
}
public Effect mergeEffect(Effect effect) {
public void mergeEffect(Effect effect) {
if (this.rodEffect != null)
effect.merge(this.rodEffect.getEffect());
if (this.enchantEffect != null)
effect.merge(this.enchantEffect);
if (this.baitEffect != null)
effect.merge(this.baitEffect.getEffect());
for (EffectCarrier util : utilEffects) {
effect.merge(util.getEffect());
}
return effect;
}
public void triggerActions(ActionTrigger actionTrigger) {
GlobalSettings.triggerRodActions(actionTrigger, this);
if (rodEffect != null) {
Action[] actions = rodEffect.getActions(actionTrigger);
if (actions != null)
for (Action action : actions) {
action.trigger(this);
}
}
if (baitEffect != null) {
GlobalSettings.triggerBaitActions(actionTrigger, this);
Action[] actions = baitEffect.getActions(actionTrigger);
if (actions != null)
for (Action action : actions) {
action.trigger(this);
}
}
}
}

View File

@@ -5,6 +5,7 @@ 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 org.jetbrains.annotations.Nullable;
import java.util.Map;
@@ -70,6 +71,11 @@ public class EffectCarrier {
return actionMap;
}
@Nullable
public Action[] getActions(ActionTrigger trigger) {
return actionMap.get(trigger);
}
public boolean isPersist() {
return persist;
}

View File

@@ -3,6 +3,7 @@ package net.momirealms.customfishing.api.mechanic.loot;
import net.momirealms.customfishing.api.CustomFishingPlugin;
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.game.GameConfig;
import org.jetbrains.annotations.NotNull;
@@ -167,6 +168,16 @@ public class CFLoot implements Loot {
return actionMap.get(actionTrigger);
}
@Override
public void triggerActions(ActionTrigger actionTrigger, Condition condition) {
Action[] actions = getActions(actionTrigger);
if (actions != null) {
for (Action action : actions) {
action.trigger(condition);
}
}
}
@Override
public Action[] getSuccessTimesActions(int times) {
return successTimesActionMap.get(times);

View File

@@ -19,6 +19,7 @@ package net.momirealms.customfishing.api.mechanic.loot;
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.game.GameConfig;
import org.jetbrains.annotations.NotNull;
@@ -87,6 +88,8 @@ public interface Loot {
*/
Action[] getActions(ActionTrigger actionTrigger);
void triggerActions(ActionTrigger actionTrigger, Condition condition);
/**
* get actions when succeeding in fishing for certain times
* @param times times

View File

@@ -22,38 +22,27 @@ import net.momirealms.customfishing.api.data.StatisticData;
import net.momirealms.customfishing.api.mechanic.action.Action;
import net.momirealms.customfishing.api.mechanic.condition.FishingPreparation;
import net.momirealms.customfishing.api.mechanic.loot.Loot;
import org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Statistics {
@SerializedName("statistic_map")
private final HashMap<String, Integer> statisticMap;
private final ConcurrentHashMap<String, Integer> statisticMap;
private int total;
public Statistics() {
this.statisticMap = new HashMap<>();
this.total = 0;
}
public Statistics(ConfigurationSection section) {
this.statisticMap = new HashMap<>();
this.total = 0;
for (String key : section.getKeys(false)) {
int amount = section.getInt(key);
total += amount;
statisticMap.put(key, amount);
}
}
public Statistics(StatisticData statisticData) {
this.statisticMap = new HashMap<>(statisticData.statisticMap);
this.statisticMap = new ConcurrentHashMap<>(statisticData.statisticMap);
this.total = statisticMap.values().stream().mapToInt(Integer::intValue).sum();
}
public void addLootAmount(Loot loot, FishingPreparation fishingPreparation, int amount) {
public synchronized void addLootAmount(Loot loot, FishingPreparation fishingPreparation, int amount) {
if (amount == 1) {
addSingleLootAmount(loot, fishingPreparation);
return;
}
Integer previous = statisticMap.get(loot.getID());
if (previous == null) previous = 0;
int after = previous + amount;
@@ -75,6 +64,19 @@ public class Statistics {
}
}
private void addSingleLootAmount(Loot loot, FishingPreparation fishingPreparation) {
Integer previous = statisticMap.get(loot.getID());
if (previous == null) previous = 0;
int after = previous + 1;
statisticMap.put(loot.getID(), after);
total += 1;
Action[] actions = loot.getSuccessTimesActionMap().get(after);
if (actions != null)
for (Action action : actions) {
action.trigger(fishingPreparation);
}
}
public int getLootAmount(String key) {
Integer amount = statisticMap.get(key);
return amount == null ? 0 : amount;
@@ -88,7 +90,7 @@ public class Statistics {
statisticMap.clear();
}
public HashMap<String, Integer> getStatisticMap() {
public Map<String, Integer> getStatisticMap() {
return statisticMap;
}

View File

@@ -19,7 +19,10 @@ package net.momirealms.customfishing.api.util;
import net.momirealms.customfishing.api.common.Pair;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class WeightUtils {
@@ -37,7 +40,7 @@ public class WeightUtils {
return getRandom(weights, available, index);
}
public static <T> T getRandom(HashMap<T, Double> map) {
public static <T> T getRandom(Map<T, Double> map) {
List<T> available = new ArrayList<>();
double[] weights = new double[map.size()];
int index = 0;