From bac4b0ea40517c38b10db2ae01a2c21feee9dfd6 Mon Sep 17 00:00:00 2001 From: Xiao-MoMi <70987828+Xiao-MoMi@users.noreply.github.com> Date: Mon, 16 Jan 2023 00:03:01 +0800 Subject: [PATCH] 1.2.18.2 --- build.gradle | 2 +- .../quest/NewBetonQuestCFQuest.java | 116 ++++++++++++++++++ ...CFQuest.java => OldBetonQuestCFQuest.java} | 17 ++- .../manager/IntegrationManager.java | 7 +- 4 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/momirealms/customfishing/integration/quest/NewBetonQuestCFQuest.java rename src/main/java/net/momirealms/customfishing/integration/quest/{BetonQuestCFQuest.java => OldBetonQuestCFQuest.java} (91%) diff --git a/build.gradle b/build.gradle index f0c3454b..ec7be263 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'net.momirealms' -version = '1.2.18' +version = '1.2.18.2' repositories { mavenCentral() diff --git a/src/main/java/net/momirealms/customfishing/integration/quest/NewBetonQuestCFQuest.java b/src/main/java/net/momirealms/customfishing/integration/quest/NewBetonQuestCFQuest.java new file mode 100644 index 00000000..f03e448e --- /dev/null +++ b/src/main/java/net/momirealms/customfishing/integration/quest/NewBetonQuestCFQuest.java @@ -0,0 +1,116 @@ +/* + * 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.integration.quest; + +import net.momirealms.customfishing.api.event.FishResultEvent; +import net.momirealms.customfishing.object.fishing.FishResult; +import net.momirealms.customfishing.util.AdventureUtil; +import org.betonquest.betonquest.BetonQuest; +import org.betonquest.betonquest.Instruction; +import org.betonquest.betonquest.VariableNumber; +import org.betonquest.betonquest.api.CountingObjective; +import org.betonquest.betonquest.api.profiles.OnlineProfile; +import org.betonquest.betonquest.api.profiles.Profile; +import org.betonquest.betonquest.exceptions.InstructionParseException; +import org.betonquest.betonquest.utils.PlayerConverter; +import org.betonquest.betonquest.utils.location.CompoundLocation; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; + +import java.util.Collections; +import java.util.HashSet; + +public class NewBetonQuestCFQuest extends CountingObjective implements Listener { + + private final CompoundLocation playerLocation; + private final VariableNumber rangeVar; + private final HashSet loot_ids; + + public NewBetonQuestCFQuest(Instruction instruction) throws InstructionParseException { + super(instruction, "loot_to_fish"); + loot_ids = new HashSet<>(); + Collections.addAll(loot_ids, instruction.getArray()); + targetAmount = instruction.getInt(); + + final String pack = instruction.getPackage().getQuestPath(); + final String loc = instruction.getOptional("playerLocation"); + final String range = instruction.getOptional("range"); + if (loc != null && range != null) { + playerLocation = new CompoundLocation(pack, loc); + rangeVar = new VariableNumber(pack, range); + } else { + playerLocation = null; + rangeVar = null; + } + + if (targetAmount <= 0) { + throw new InstructionParseException("Fish amount cannot be less than 0"); + } + } + + public static void register() { + BetonQuest.getInstance().registerObjectives("customfishing", NewBetonQuestCFQuest.class); + } + + @EventHandler + public void onFish(FishResultEvent event) { + if (event.getResult() != FishResult.FAILURE) { + OnlineProfile onlineProfile = PlayerConverter.getID(event.getPlayer()); + if (!containsPlayer(onlineProfile)) { + return; + } + if (isInvalidLocation(event, onlineProfile)) { + return; + } + if (this.loot_ids.contains(event.getLoot_id()) && this.checkConditions(onlineProfile)) { + getCountingData(onlineProfile).progress(event.isDouble() ? 1 : 2); + completeIfDoneOrNotify(onlineProfile); + } + } + } + + private boolean isInvalidLocation(FishResultEvent event, final Profile profile) { + if (playerLocation == null || rangeVar == null) { + return false; + } + + final Location targetLocation; + try { + targetLocation = playerLocation.getLocation(profile); + } catch (final org.betonquest.betonquest.exceptions.QuestRuntimeException e) { + AdventureUtil.consoleMessage(e.getMessage()); + return true; + } + final int range = rangeVar.getInt(profile); + final Location playerLoc = event.getPlayer().getLocation(); + return !playerLoc.getWorld().equals(targetLocation.getWorld()) || targetLocation.distanceSquared(playerLoc) > range * range; + } + + @Override + public void start() { + Bukkit.getPluginManager().registerEvents(this, BetonQuest.getInstance()); + } + + @Override + public void stop() { + HandlerList.unregisterAll(this); + } +} diff --git a/src/main/java/net/momirealms/customfishing/integration/quest/BetonQuestCFQuest.java b/src/main/java/net/momirealms/customfishing/integration/quest/OldBetonQuestCFQuest.java similarity index 91% rename from src/main/java/net/momirealms/customfishing/integration/quest/BetonQuestCFQuest.java rename to src/main/java/net/momirealms/customfishing/integration/quest/OldBetonQuestCFQuest.java index 9d57d0cb..8cf0873e 100644 --- a/src/main/java/net/momirealms/customfishing/integration/quest/BetonQuestCFQuest.java +++ b/src/main/java/net/momirealms/customfishing/integration/quest/OldBetonQuestCFQuest.java @@ -19,6 +19,7 @@ package net.momirealms.customfishing.integration.quest; import net.momirealms.customfishing.api.event.FishResultEvent; import net.momirealms.customfishing.object.fishing.FishResult; +import net.momirealms.customfishing.util.AdventureUtil; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -39,14 +40,14 @@ import java.util.HashSet; import java.util.Locale; import java.util.logging.Level; -public class BetonQuestCFQuest extends Objective implements Listener { +public class OldBetonQuestCFQuest extends Objective implements Listener { private final HashSet loot_ids = new HashSet<>(); private final int amount; private final boolean notify; private final int notifyInterval; - public BetonQuestCFQuest(Instruction instruction) throws InstructionParseException { + public OldBetonQuestCFQuest(Instruction instruction) throws InstructionParseException { super(instruction); this.template = FishData.class; this.notifyInterval = instruction.getInt(instruction.getOptional("notify"), 1); @@ -56,7 +57,7 @@ public class BetonQuestCFQuest extends Objective implements Listener { } public static void register() { - BetonQuest.getInstance().registerObjectives("customfishing", BetonQuestCFQuest.class); + BetonQuest.getInstance().registerObjectives("customfishing", OldBetonQuestCFQuest.class); } @Override @@ -71,7 +72,7 @@ public class BetonQuestCFQuest extends Objective implements Listener { @Override public String getDefaultDataInstruction() { - return null; + return Integer.toString(this.amount); } @Override @@ -130,7 +131,13 @@ public class BetonQuestCFQuest extends Objective implements Listener { public FishData(String instruction, String playerID, String objID) { super(instruction, playerID, objID); - this.amount = Integer.parseInt(instruction); + try { + this.amount = Integer.parseInt(instruction); + } + catch (NumberFormatException e) { + AdventureUtil.consoleMessage("<>"); + this.amount = 1; + } } public void catchFish(int caughtAmount) { diff --git a/src/main/java/net/momirealms/customfishing/manager/IntegrationManager.java b/src/main/java/net/momirealms/customfishing/manager/IntegrationManager.java index 9a47628e..77259008 100644 --- a/src/main/java/net/momirealms/customfishing/manager/IntegrationManager.java +++ b/src/main/java/net/momirealms/customfishing/manager/IntegrationManager.java @@ -28,7 +28,8 @@ import net.momirealms.customfishing.integration.item.*; import net.momirealms.customfishing.integration.mob.MythicMobsMobImpl; import net.momirealms.customfishing.integration.papi.PlaceholderManager; import net.momirealms.customfishing.integration.quest.BattlePassCFQuest; -import net.momirealms.customfishing.integration.quest.BetonQuestCFQuest; +import net.momirealms.customfishing.integration.quest.NewBetonQuestCFQuest; +import net.momirealms.customfishing.integration.quest.OldBetonQuestCFQuest; import net.momirealms.customfishing.integration.quest.ClueScrollCFQuest; import net.momirealms.customfishing.integration.season.CustomCropsSeasonImpl; import net.momirealms.customfishing.integration.season.RealisticSeasonsImpl; @@ -212,14 +213,14 @@ public class IntegrationManager extends Function { } public void registerQuests() { - if (Bukkit.getPluginManager().isPluginEnabled("ClueScrolls")) { ClueScrollCFQuest clueScrollCFQuest = new ClueScrollCFQuest(); Bukkit.getPluginManager().registerEvents(clueScrollCFQuest, CustomFishing.plugin); hookMessage("ClueScrolls"); } if (Bukkit.getPluginManager().isPluginEnabled("BetonQuest")) { - BetonQuestCFQuest.register(); + if (Bukkit.getPluginManager().getPlugin("BetonQuest").getDescription().getVersion().startsWith("2.")) NewBetonQuestCFQuest.register(); + else OldBetonQuestCFQuest.register(); hookMessage("BetonQuest"); } if (Bukkit.getPluginManager().isPluginEnabled("BattlePass")) {