mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-19 15:09:24 +00:00
Added random loots action
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
package net.momirealms.customfishing.api.mechanic.context;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.competition.CompetitionGoal;
|
||||
import net.momirealms.customfishing.api.mechanic.effect.Effect;
|
||||
import net.momirealms.customfishing.api.mechanic.loot.LootType;
|
||||
import net.momirealms.customfishing.api.mechanic.totem.ActiveTotemList;
|
||||
import org.bukkit.Location;
|
||||
@@ -92,6 +93,7 @@ public class ContextKeys<T> {
|
||||
public static final ContextKeys<Double> BONUS = of("bonus", Double.class);
|
||||
public static final ContextKeys<Double> BASE = of("base", Double.class);
|
||||
public static final ContextKeys<Integer> LOOT_ORDER = of("loot_order", Integer.class);
|
||||
public static final ContextKeys<Effect> EFFECT = of("effect", Effect.class);
|
||||
|
||||
private final String key;
|
||||
private final Class<T> type;
|
||||
|
||||
@@ -216,6 +216,7 @@ public class CustomFishingHook {
|
||||
|
||||
// trigger event
|
||||
EventUtils.fireAndForget(new FishingEffectApplyEvent(this, tempEffect, FishingEffectApplyEvent.Stage.FISHING));
|
||||
context.arg(ContextKeys.EFFECT, tempEffect);
|
||||
|
||||
// start the mechanic
|
||||
plugin.debug("Final Effect:" + tempEffect);
|
||||
@@ -379,6 +380,7 @@ public class CustomFishingHook {
|
||||
*/
|
||||
public void onReelIn() {
|
||||
if (isPlayingGame() || !hook.isValid()) return;
|
||||
context.arg(ContextKeys.OTHER_LOCATION, hook.getLocation());
|
||||
if (hookMechanic != null) {
|
||||
if (!hookMechanic.isHooked()) {
|
||||
gears.trigger(ActionTrigger.REEL, context);
|
||||
@@ -405,6 +407,7 @@ public class CustomFishingHook {
|
||||
*/
|
||||
public void onBite() {
|
||||
if (isPlayingGame() || !hook.isValid()) return;
|
||||
context.arg(ContextKeys.OTHER_LOCATION, hook.getLocation());
|
||||
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.LOOT, ActionTrigger.BITE);
|
||||
gears.trigger(ActionTrigger.BITE, context);
|
||||
if (RequirementManager.isSatisfied(context, ConfigManager.autoFishingRequirements())) {
|
||||
@@ -424,6 +427,7 @@ public class CustomFishingHook {
|
||||
*/
|
||||
public void onLand() {
|
||||
if (!hook.isValid()) return;
|
||||
context.arg(ContextKeys.OTHER_LOCATION, hook.getLocation());
|
||||
gears.trigger(ActionTrigger.LAND, context);
|
||||
}
|
||||
|
||||
@@ -432,6 +436,7 @@ public class CustomFishingHook {
|
||||
*/
|
||||
public void onEscape() {
|
||||
if (isPlayingGame() || !hook.isValid()) return;
|
||||
context.arg(ContextKeys.OTHER_LOCATION, hook.getLocation());
|
||||
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.LOOT, ActionTrigger.ESCAPE);
|
||||
gears.trigger(ActionTrigger.ESCAPE, context);
|
||||
}
|
||||
@@ -441,6 +446,7 @@ public class CustomFishingHook {
|
||||
*/
|
||||
public void onLure() {
|
||||
if (isPlayingGame() || !hook.isValid()) return;
|
||||
context.arg(ContextKeys.OTHER_LOCATION, hook.getLocation());
|
||||
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.LOOT, ActionTrigger.LURE);
|
||||
gears.trigger(ActionTrigger.LURE, context);
|
||||
}
|
||||
|
||||
@@ -27,8 +27,11 @@ import net.momirealms.customfishing.api.mechanic.action.Action;
|
||||
import net.momirealms.customfishing.api.mechanic.action.ActionExpansion;
|
||||
import net.momirealms.customfishing.api.mechanic.action.ActionFactory;
|
||||
import net.momirealms.customfishing.api.mechanic.action.ActionManager;
|
||||
import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import net.momirealms.customfishing.api.mechanic.context.ContextKeys;
|
||||
import net.momirealms.customfishing.api.mechanic.effect.Effect;
|
||||
import net.momirealms.customfishing.api.mechanic.loot.Loot;
|
||||
import net.momirealms.customfishing.api.mechanic.loot.LootType;
|
||||
import net.momirealms.customfishing.api.mechanic.misc.placeholder.BukkitPlaceholderManager;
|
||||
import net.momirealms.customfishing.api.mechanic.misc.value.MathValue;
|
||||
import net.momirealms.customfishing.api.mechanic.misc.value.TextValue;
|
||||
@@ -53,6 +56,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.ExperienceOrb;
|
||||
import org.bukkit.entity.FishHook;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -173,6 +177,7 @@ public class BukkitActionManager implements ActionManager<Player> {
|
||||
this.registerFakeItemAction();
|
||||
this.registerTitleAction();
|
||||
this.registerInsertArgumentAction();
|
||||
this.registerDropRandomLootsAction();
|
||||
}
|
||||
|
||||
private void registerMessageAction() {
|
||||
@@ -386,6 +391,49 @@ public class BukkitActionManager implements ActionManager<Player> {
|
||||
}, "level");
|
||||
}
|
||||
|
||||
private void registerDropRandomLootsAction() {
|
||||
registerAction((args, chance) -> {
|
||||
if (args instanceof Section section) {
|
||||
boolean toInv = section.getBoolean("to-inventory");
|
||||
MathValue<Player> count = MathValue.auto(section.get("amount"));
|
||||
int extraAttempts = section.getInt("extra-attempts", 5);
|
||||
return context -> {
|
||||
if (Math.random() > chance.evaluate(context)) return;
|
||||
Effect effect = context.arg(ContextKeys.EFFECT);
|
||||
if (effect == null) effect = Effect.newInstance();
|
||||
int triesTimes = 0;
|
||||
int successTimes = 0;
|
||||
int requiredTimes = (int) count.evaluate(context);
|
||||
Player player = context.holder();
|
||||
ItemStack rod = player.getInventory().getItemInMainHand();
|
||||
if (rod.getType() != Material.FISHING_ROD) rod = player.getInventory().getItemInOffHand();
|
||||
if (rod.getType() != Material.FISHING_ROD) rod = new ItemStack(Material.FISHING_ROD);
|
||||
FishHook fishHook = context.arg(ContextKeys.HOOK_ENTITY);
|
||||
if (fishHook == null) return;
|
||||
|
||||
while (successTimes < requiredTimes && triesTimes < requiredTimes + extraAttempts) {
|
||||
Loot loot = plugin.getLootManager().getNextLoot(effect, context);
|
||||
Context<Player> newContext = Context.player(player).combine(context);
|
||||
if (loot != null && loot.type() == LootType.ITEM) {
|
||||
newContext.arg(ContextKeys.ID, loot.id());
|
||||
if (!toInv) {
|
||||
plugin.getItemManager().dropItemLoot(newContext, rod, fishHook);
|
||||
} else {
|
||||
ItemStack itemLoot = plugin.getItemManager().getItemLoot(newContext, rod, fishHook);
|
||||
PlayerUtils.giveItem(player, itemLoot, itemLoot.getAmount());
|
||||
}
|
||||
successTimes++;
|
||||
}
|
||||
triesTimes++;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
plugin.getPluginLogger().warn("Invalid value type: " + args.getClass().getSimpleName() + " found at actionbar-nearby action which should be Section");
|
||||
return Action.empty();
|
||||
}
|
||||
}, "drop-random-loots");
|
||||
}
|
||||
|
||||
private void registerFoodAction() {
|
||||
registerAction((args, chance) -> {
|
||||
MathValue<Player> value = MathValue.auto(args);
|
||||
|
||||
Reference in New Issue
Block a user