9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-31 04:46:36 +00:00

checkpoint - 28

This commit is contained in:
XiaoMoMi
2024-07-07 03:53:04 +08:00
parent 05ac427750
commit 8a0b969f23
24 changed files with 287 additions and 100 deletions

View File

@@ -17,53 +17,77 @@
package net.momirealms.customfishing.api.event;
import net.momirealms.customfishing.api.mechanic.context.Context;
import net.momirealms.customfishing.api.mechanic.loot.Loot;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
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 org.jetbrains.annotations.Nullable;
public class FishingLootSpawnEvent extends PlayerEvent implements Cancellable {
public class FishingLootSpawnEvent extends PlayerEvent {
private static final HandlerList handlerList = new HandlerList();
private final Location location;
private final Item item;
private final Entity entity;
private final Loot loot;
private boolean isCancelled;
private final Context<Player> context;
private boolean skipActions;
private boolean summonEntity;
public FishingLootSpawnEvent(@NotNull Player who, Location location, Loot loot, Item item) {
super(who);
this.item = item;
public FishingLootSpawnEvent(@NotNull Context<Player> context, Location location, Loot loot, @Nullable Entity entity) {
super(context.getHolder());
this.entity = entity;
this.loot = loot;
this.location = location;
this.isCancelled = false;
this.skipActions = false;
this.summonEntity = true;
this.context = context;
}
@Override
public boolean isCancelled() {
return isCancelled;
}
@Override
public void setCancelled(boolean cancel) {
isCancelled = cancel;
public Context<Player> getContext() {
return context;
}
public Location getLocation() {
return location;
}
public Item getItem() {
return item;
/**
* Get the loot entity
*
* @return entity
*/
@Nullable
public Entity getEntity() {
return entity;
}
@NotNull
public Loot getLoot() {
return loot;
}
public boolean summonEntity() {
return summonEntity;
}
public void summonEntity(boolean summonEntity) {
this.summonEntity = summonEntity;
}
public boolean skipActions() {
return skipActions;
}
public void skipActions(boolean skipActions) {
this.skipActions = skipActions;
}
@Override
public @NotNull HandlerList getHandlers() {
return handlerList;

View File

@@ -29,7 +29,7 @@ public interface BlockManager extends Reloadable {
boolean registerBlock(@NotNull BlockConfig block);
@Nullable
@NotNull
FallingBlock summonBlockLoot(@NotNull Context<Player> context);
@NotNull

View File

@@ -34,11 +34,10 @@ public class ContextKeys<T> {
public static final ContextKeys<LootType> LOOT = of("loot", LootType.class);
public static final ContextKeys<String> NICK = of("nick", String.class);
public static final ContextKeys<Boolean> OPEN_WATER = of("open_water", Boolean.class);
public static final ContextKeys<String> TYPE = of("type", String.class);
public static final ContextKeys<Float> SIZE = of("SIZE", Float.class);
public static final ContextKeys<String> SIZE_FORMATTED = of("size", String.class);
public static final ContextKeys<Double> PRICE = of("PRICE", Double.class);
public static final ContextKeys<String> PRICE_FORMATTED = of("price", String.class);
public static final ContextKeys<Float> SIZE = of("size", Float.class);
public static final ContextKeys<String> SIZE_FORMATTED = of("size_formatted", String.class);
public static final ContextKeys<Double> PRICE = of("price", Double.class);
public static final ContextKeys<String> PRICE_FORMATTED = of("price_formatted", String.class);
public static final ContextKeys<String> SURROUNDING = of("surrounding", String.class);
public static final ContextKeys<String> TEMP_NEAR_PLAYER = of("near", String.class);
public static final ContextKeys<String> ROD = of("rod", String.class);
@@ -67,8 +66,9 @@ public class ContextKeys<T> {
public static final ContextKeys<Integer> AMOUNT = of("amount", Integer.class);
public static final ContextKeys<Double> WEIGHT = of("0", Double.class);
public static final ContextKeys<String> TIME_LEFT = of("time_left", String.class);
public static final ContextKeys<String> REASON = of("reason", String.class);
public static final ContextKeys<String> PROGRESS = of("progress", String.class);
public static final ContextKeys<Float> RECORD = of("record", Float.class);
public static final ContextKeys<String> RECORD_FORMATTED = of("record_formatted", String.class);
public static final ContextKeys<Integer> CLICKS_LEFT = of("left_clicks", Integer.class);
public static final ContextKeys<Integer> REQUIRED_TIMES = of("clicks", Integer.class);

View File

@@ -21,6 +21,7 @@ import net.momirealms.customfishing.api.mechanic.context.Context;
import net.momirealms.customfishing.common.plugin.feature.Reloadable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
@@ -40,6 +41,6 @@ public interface EntityManager extends Reloadable {
boolean registerEntity(EntityConfig entity);
@Nullable
@NotNull
Entity summonEntityLoot(Context<Player> context);
}

View File

@@ -51,7 +51,6 @@ public interface EventManager extends Reloadable {
*
* @param context The context in which the event is triggered.
* @param id The unique identifier of the event carrier.
* @param type
* @param trigger The trigger that initiates the event.
*/
default void trigger(Context<Player> context, String id, MechanicType type, ActionTrigger trigger) {
@@ -63,7 +62,6 @@ public interface EventManager extends Reloadable {
*
* @param context The context in which the event is triggered.
* @param id The unique identifier of the event carrier.
* @param type
* @param trigger The trigger that initiates the event.
* @param previousTimes The previous times count for the event.
* @param afterTimes The after times count for the event.

View File

@@ -18,6 +18,7 @@
package net.momirealms.customfishing.api.mechanic.fishing;
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
import net.momirealms.customfishing.api.event.FishingLootSpawnEvent;
import net.momirealms.customfishing.api.event.FishingResultEvent;
import net.momirealms.customfishing.api.mechanic.action.ActionTrigger;
import net.momirealms.customfishing.api.mechanic.competition.CompetitionGoal;
@@ -44,13 +45,14 @@ import net.momirealms.customfishing.common.util.TriConsumer;
import net.momirealms.customfishing.common.util.TriFunction;
import net.momirealms.sparrow.heart.SparrowHeart;
import net.momirealms.sparrow.heart.feature.inventory.HandSlot;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Statistic;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -218,7 +220,7 @@ public class CustomFishingHook {
}
}
private void end() {
public void end() {
plugin.getFishingManager().destroy(context.getHolder().getUniqueId());
}
@@ -242,18 +244,6 @@ public class CustomFishingHook {
return gamingPlayer != null && gamingPlayer.isValid();
}
public void handleGameResult() {
if (gamingPlayer == null || !gamingPlayer.isValid()) {
throw new RuntimeException("You can't call this method if the player is not playing the game");
}
if (gamingPlayer.isSuccessful()) {
handleSuccessfulFishing();
} else {
handleFailedFishing();
}
end();
}
public void cancelCurrentGame() {
if (gamingPlayer == null || !gamingPlayer.isValid()) {
throw new RuntimeException("You can't call this method if the player is not playing the game");
@@ -270,11 +260,13 @@ public class CustomFishingHook {
return;
Game nextGame = plugin.getGameManager().getNextGame(tempFinalEffect, context);
if (nextGame != null) {
plugin.debug("Next game: " + nextGame.id());
gamingPlayer = nextGame.start(this, tempFinalEffect);
if (this.hookMechanic != null) {
this.hookMechanic.freeze();
}
} else {
plugin.debug("Next game: " + "`null`");
handleSuccessfulFishing();
end();
}
@@ -302,16 +294,16 @@ public class CustomFishingHook {
}
public void onEscape() {
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.getTypeByID(nextLoot.id()), ActionTrigger.ESCAPE);
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.LOOT, ActionTrigger.ESCAPE);
gears.trigger(ActionTrigger.ESCAPE, context);
}
public void onLure() {
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.getTypeByID(nextLoot.id()), ActionTrigger.LURE);
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.LOOT, ActionTrigger.LURE);
gears.trigger(ActionTrigger.LURE, context);
}
private void handleFailedFishing() {
public void handleFailedFishing() {
// update the hook location
context.arg(ContextKeys.HOOK_LOCATION, hook.getLocation());
@@ -320,10 +312,10 @@ public class CustomFishingHook {
context.arg(ContextKeys.HOOK_Z, hook.getLocation().getBlockZ());
gears.trigger(ActionTrigger.FAILURE, context);
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.getTypeByID(nextLoot.id()), ActionTrigger.FAILURE);
plugin.getEventManager().trigger(context, nextLoot.id(), MechanicType.LOOT, ActionTrigger.FAILURE);
}
private void handleSuccessfulFishing() {
public void handleSuccessfulFishing() {
// update the hook location
context.arg(ContextKeys.HOOK_LOCATION, hook.getLocation());
@@ -350,6 +342,8 @@ public class CustomFishingHook {
return;
}
gears.trigger(ActionTrigger.SUCCESS, context);
switch (lootType) {
case ITEM -> {
for (int i = 0; i < amount; i++) {
@@ -364,21 +358,41 @@ public class CustomFishingHook {
context.arg(ContextKeys.NICK, "<lang:" + stack.getType().translationKey() + ">");
}
}
FishingLootSpawnEvent spawnEvent = new FishingLootSpawnEvent(context, hook.getLocation(), nextLoot, item);
Bukkit.getPluginManager().callEvent(spawnEvent);
if (item != null && !spawnEvent.summonEntity())
item.remove();
if (spawnEvent.skipActions())
return;
if (item != null && item.isValid() && nextLoot.preventGrabbing()) {
item.getPersistentDataContainer().set(Objects.requireNonNull(NamespacedKey.fromString("owner", plugin.getBoostrap())), PersistentDataType.STRING, context.getHolder().getName());
}
doSuccessActions();
}, (long) ConfigManager.multipleLootSpawnDelay() * i, hook.getLocation());
}
}
case BLOCK -> {
plugin.getBlockManager().summonBlockLoot(context);
FallingBlock fallingBlock = plugin.getBlockManager().summonBlockLoot(context);
FishingLootSpawnEvent spawnEvent = new FishingLootSpawnEvent(context, hook.getLocation(), nextLoot, fallingBlock);
Bukkit.getPluginManager().callEvent(spawnEvent);
if (!spawnEvent.summonEntity())
fallingBlock.remove();
if (spawnEvent.skipActions())
return;
doSuccessActions();
}
case ENTITY -> {
plugin.getEntityManager().summonEntityLoot(context);
Entity entity = plugin.getEntityManager().summonEntityLoot(context);
FishingLootSpawnEvent spawnEvent = new FishingLootSpawnEvent(context, hook.getLocation(), nextLoot, entity);
Bukkit.getPluginManager().callEvent(spawnEvent);
if (!spawnEvent.summonEntity())
entity.remove();
if (spawnEvent.skipActions())
return;
doSuccessActions();
}
}
gears.trigger(ActionTrigger.SUCCESS, context);
}
private void doSuccessActions() {
@@ -416,13 +430,15 @@ public class CustomFishingHook {
String id = context.arg(ContextKeys.ID);
Player player = context.getHolder();
MechanicType type = MechanicType.getTypeByID(id);
plugin.getEventManager().trigger(context, id, type, ActionTrigger.SUCCESS);
player.setStatistic(Statistic.FISH_CAUGHT, player.getStatistic(Statistic.FISH_CAUGHT) + 1);
if (!nextLoot.disableStats()) {
plugin.getStorageManager().getOnlineUser(player.getUniqueId()).ifPresent(
userData -> {
userData.statistics().addAmount(id, 1);
Optional.ofNullable(context.arg(ContextKeys.SIZE)).ifPresent(size -> {
float max = Math.max(0, userData.statistics().getMaxSize(id));
context.arg(ContextKeys.RECORD, max);
context.arg(ContextKeys.RECORD_FORMATTED, String.format("%.2f", max));
if (userData.statistics().updateSize(id, size)) {
plugin.getEventManager().trigger(context, id, type, ActionTrigger.NEW_SIZE_RECORD);
}
@@ -430,5 +446,8 @@ public class CustomFishingHook {
}
);
}
plugin.getEventManager().trigger(context, id, type, ActionTrigger.SUCCESS);
player.setStatistic(Statistic.FISH_CAUGHT, player.getStatistic(Statistic.FISH_CAUGHT) + 1);
}
}

View File

@@ -28,7 +28,6 @@ import java.util.concurrent.TimeUnit;
public abstract class AbstractGamingPlayer implements GamingPlayer, Runnable {
// private final FishingManager manager;
protected long deadline;
protected boolean success;
protected SchedulerTask task;
@@ -130,8 +129,16 @@ public abstract class AbstractGamingPlayer implements GamingPlayer, Runnable {
protected abstract void tick();
protected void endGame() {
hook.handleGameResult();
valid = false;
destroy();
boolean success = isSuccessful();
BukkitCustomFishingPlugin.getInstance().getScheduler().sync().run(() -> {
if (success) {
hook.handleSuccessfulFishing();
} else {
hook.handleFailedFishing();
}
hook.end();
}, hook.getHookEntity().getLocation());
}
protected void setGameResult(boolean success) {

View File

@@ -63,6 +63,8 @@ public interface Loot {
*/
boolean showInFinder();
boolean preventGrabbing();
/**
* Get the unique identifier for this loot.
*
@@ -151,6 +153,8 @@ public interface Loot {
*/
Builder disableGame(boolean disableGame);
Builder preventGrabbing(boolean preventGrabbing);
/**
* Specify whether statistics recording is disabled for this loot.
*

View File

@@ -34,6 +34,7 @@ public class LootImpl implements Loot {
private final boolean disableGame;
private final boolean disableStatistics;
private final boolean showInFinder;
private final boolean preventGrabbing;
private final String id;
private final String nick;
private final StatisticsKeys statisticsKeys;
@@ -41,7 +42,7 @@ public class LootImpl implements Loot {
private final String[] groups;
private final LootBaseEffect lootBaseEffect;
public LootImpl(LootType type, boolean instantGame, boolean disableGame, boolean disableStatistics, boolean showInFinder, String id, String nick, StatisticsKeys statisticsKeys, MathValue<Player> score, String[] groups, LootBaseEffect lootBaseEffect) {
public LootImpl(LootType type, boolean instantGame, boolean disableGame, boolean disableStatistics, boolean showInFinder, boolean preventGrabbing, String id, String nick, StatisticsKeys statisticsKeys, MathValue<Player> score, String[] groups, LootBaseEffect lootBaseEffect) {
this.type = type;
this.instantGame = instantGame;
this.disableGame = disableGame;
@@ -53,6 +54,7 @@ public class LootImpl implements Loot {
this.score = score;
this.groups = groups;
this.lootBaseEffect = lootBaseEffect;
this.preventGrabbing = preventGrabbing;
}
@Override
@@ -86,6 +88,11 @@ public class LootImpl implements Loot {
return showInFinder;
}
@Override
public boolean preventGrabbing() {
return preventGrabbing;
}
@Override
public MathValue<Player> score() {
return score;
@@ -118,6 +125,7 @@ public class LootImpl implements Loot {
private boolean disableGame = Loot.DefaultProperties.DEFAULT_DISABLE_GAME;
private boolean disableStatistics = Loot.DefaultProperties.DEFAULT_DISABLE_STATS;
private boolean showInFinder = Loot.DefaultProperties.DEFAULT_SHOW_IN_FINDER;
private boolean preventGrabbing = false;
private String id = null;
private String nick = "UNDEFINED";
private StatisticsKeys statisticsKeys = null;
@@ -141,6 +149,11 @@ public class LootImpl implements Loot {
return this;
}
@Override
public Builder preventGrabbing(boolean preventGrabbing) {
this.preventGrabbing = preventGrabbing;
return this;
}
@Override
public Builder disableStatistics(boolean disableStatistics) {
this.disableStatistics = disableStatistics;
return this;
@@ -188,6 +201,7 @@ public class LootImpl implements Loot {
disableGame,
disableStatistics,
showInFinder,
preventGrabbing,
requireNonNull(id),
Optional.ofNullable(nick).orElse(id),
Optional.ofNullable(statisticsKeys).orElse(new StatisticsKeys(id, id)),

View File

@@ -85,6 +85,7 @@ public class FishingStatisticsImpl implements FishingStatistics {
float previous = sizeMap.getOrDefault(id, 0f);
if (previous >= newSize) return false;
sizeMap.put(id, newSize);
System.out.println(sizeMap);
return true;
}