From e3b21885075ca6a1cf2dd8bffda34dbe27f85311 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Sat, 27 Apr 2024 22:24:48 +0800 Subject: [PATCH] [Compatibility] Readd quest compatibilities --- build.gradle.kts | 2 +- .../compatibility/IntegrationManagerImpl.java | 17 ++ .../compatibility/quest/BattlePassHook.java | 87 ++++++++ .../compatibility/quest/BetonQuestHook.java | 190 ++++++++++++++++++ .../compatibility/quest/ClueScrollsHook.java | 67 ++++++ 5 files changed, 362 insertions(+), 1 deletion(-) create mode 100644 plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BattlePassHook.java create mode 100644 plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BetonQuestHook.java create mode 100644 plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/ClueScrollsHook.java diff --git a/build.gradle.kts b/build.gradle.kts index 57e31e5..c3c9cc2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { project.group = "net.momirealms" - project.version = "3.4.5.2" + project.version = "3.4.6" apply() apply(plugin = "java") diff --git a/plugin/src/main/java/net/momirealms/customcrops/compatibility/IntegrationManagerImpl.java b/plugin/src/main/java/net/momirealms/customcrops/compatibility/IntegrationManagerImpl.java index 1243e73..fefb4ed 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/compatibility/IntegrationManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/compatibility/IntegrationManagerImpl.java @@ -27,6 +27,9 @@ import net.momirealms.customcrops.compatibility.item.MythicMobsItemImpl; import net.momirealms.customcrops.compatibility.item.NeigeItemsItemImpl; import net.momirealms.customcrops.compatibility.item.ZaphkielItemImpl; import net.momirealms.customcrops.compatibility.level.*; +import net.momirealms.customcrops.compatibility.quest.BattlePassHook; +import net.momirealms.customcrops.compatibility.quest.BetonQuestHook; +import net.momirealms.customcrops.compatibility.quest.ClueScrollsHook; import net.momirealms.customcrops.compatibility.season.AdvancedSeasonsImpl; import net.momirealms.customcrops.compatibility.season.InBuiltSeason; import net.momirealms.customcrops.compatibility.season.RealisticSeasonsImpl; @@ -91,6 +94,20 @@ public class IntegrationManagerImpl implements IntegrationManager { registerLevelPlugin("AuraSkills", new AuraSkillsImpl()); hookMessage("AuraSkills"); } + if (plugin.isHookedPluginEnabled("BattlePass")){ + BattlePassHook battlePassHook = new BattlePassHook(); + battlePassHook.register(); + hookMessage("BattlePass"); + } + if (plugin.isHookedPluginEnabled("ClueScrolls")) { + ClueScrollsHook clueScrollsHook = new ClueScrollsHook(); + clueScrollsHook.register(); + hookMessage("ClueScrolls"); + } + if (plugin.isHookedPluginEnabled("BetonQuest")) { + BetonQuestHook.register(); + hookMessage("BetonQuest"); + } if (plugin.isHookedPluginEnabled("RealisticSeasons")) { this.seasonInterface = new RealisticSeasonsImpl(); hookMessage("RealisticSeasons"); diff --git a/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BattlePassHook.java b/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BattlePassHook.java new file mode 100644 index 0000000..b0b6c90 --- /dev/null +++ b/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BattlePassHook.java @@ -0,0 +1,87 @@ +/* + * 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.customcrops.compatibility.quest; + +import io.github.battlepass.BattlePlugin; +import io.github.battlepass.api.events.server.PluginReloadEvent; +import net.advancedplugins.bp.impl.actions.ActionRegistry; +import net.advancedplugins.bp.impl.actions.external.executor.ActionQuestExecutor; +import net.momirealms.customcrops.api.CustomCropsPlugin; +import net.momirealms.customcrops.api.event.CropBreakEvent; +import net.momirealms.customcrops.api.event.CropPlantEvent; +import net.momirealms.customcrops.api.mechanic.item.Crop; +import net.momirealms.customcrops.api.mechanic.world.level.WorldCrop; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +public class BattlePassHook implements Listener { + + public BattlePassHook() { + Bukkit.getPluginManager().registerEvents(this, CustomCropsPlugin.get()); + } + + public void register() { + ActionRegistry actionRegistry = BattlePlugin.getPlugin().getActionRegistry(); + actionRegistry.hook("customcrops", BPHarvestCropsQuest::new); + } + + @EventHandler(ignoreCancelled = true) + public void onBattlePassReload(PluginReloadEvent event){ + register(); + } + + private static class BPHarvestCropsQuest extends ActionQuestExecutor { + public BPHarvestCropsQuest(JavaPlugin plugin) { + super(plugin, "customcrops"); + } + + @EventHandler (ignoreCancelled = true) + public void onBreakCrop(CropBreakEvent event){ + Player player = event.getPlayer(); + if (player == null) return; + + WorldCrop worldCrop = event.getWorldCrop(); + if (worldCrop == null) return; + String id = worldCrop.getConfig().getStageItemByPoint(worldCrop.getPoint()); + + // Harvest crops + this.executionBuilder("harvest") + .player(player) + .root(id) + .progress(1) + .buildAndExecute(); + } + + @EventHandler (ignoreCancelled = true) + public void onPlantCrop(CropPlantEvent event){ + Player player = event.getPlayer(); + + Crop crop = event.getCrop(); + + // Harvest crops + this.executionBuilder("plant") + .player(player) + .root(crop.getKey()) + .progress(1) + .buildAndExecute(); + } + } +} diff --git a/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BetonQuestHook.java b/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BetonQuestHook.java new file mode 100644 index 0000000..c9c56df --- /dev/null +++ b/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/BetonQuestHook.java @@ -0,0 +1,190 @@ +/* + * 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.customcrops.compatibility.quest; + +import net.momirealms.customcrops.api.event.CropBreakEvent; +import net.momirealms.customcrops.api.event.CropPlantEvent; +import net.momirealms.customcrops.api.mechanic.world.level.WorldCrop; +import net.momirealms.customcrops.api.util.LogUtils; +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.config.quest.QuestPackage; +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; + +@SuppressWarnings("DuplicatedCode") +public class BetonQuestHook { + + public static void register() { + BetonQuest.getInstance().registerObjectives("customcrops_harvest", HarvestObjective.class); + BetonQuest.getInstance().registerObjectives("customcrops_plant", PlantObjective.class); + } + + public static class HarvestObjective extends CountingObjective implements Listener { + + private final CompoundLocation playerLocation; + private final VariableNumber rangeVar; + private final HashSet crop_ids; + + public HarvestObjective(Instruction instruction) throws InstructionParseException { + super(instruction, "crop_to_harvest"); + crop_ids = new HashSet<>(); + Collections.addAll(crop_ids, instruction.getArray()); + targetAmount = instruction.getVarNum(); + preCheckAmountNotLessThanOne(targetAmount); + final QuestPackage pack = instruction.getPackage(); + 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; + } + } + + @EventHandler (ignoreCancelled = true) + public void onBreakCrop(CropBreakEvent event) { + if (event.getPlayer() == null) { + return; + } + WorldCrop crop = event.getWorldCrop(); + if (crop == null) return; + + OnlineProfile onlineProfile = PlayerConverter.getID(event.getPlayer()); + if (!containsPlayer(onlineProfile)) { + return; + } + if (isInvalidLocation(event, onlineProfile)) { + return; + } + if (this.crop_ids.contains(crop.getConfig().getStageItemByPoint(crop.getPoint())) && this.checkConditions(onlineProfile)) { + getCountingData(onlineProfile).progress(1); + completeIfDoneOrNotify(onlineProfile); + } + } + + private boolean isInvalidLocation(CropBreakEvent 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) { + LogUtils.warn(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); + } + } + + public static class PlantObjective extends CountingObjective implements Listener { + + private final CompoundLocation playerLocation; + private final VariableNumber rangeVar; + private final HashSet loot_groups; + + public PlantObjective(Instruction instruction) throws InstructionParseException { + super(instruction, "crop_to_plant"); + loot_groups = new HashSet<>(); + Collections.addAll(loot_groups, instruction.getArray()); + targetAmount = instruction.getVarNum(); + preCheckAmountNotLessThanOne(targetAmount); + final QuestPackage pack = instruction.getPackage(); + 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; + } + } + + @EventHandler (ignoreCancelled = true) + public void onPlantCrop(CropPlantEvent event) { + OnlineProfile onlineProfile = PlayerConverter.getID(event.getPlayer()); + if (!containsPlayer(onlineProfile)) { + return; + } + if (isInvalidLocation(event, onlineProfile)) { + return; + } + if (this.loot_groups.contains(event.getCrop().getKey()) && this.checkConditions(onlineProfile)) { + getCountingData(onlineProfile).progress(1); + completeIfDoneOrNotify(onlineProfile); + } + } + + private boolean isInvalidLocation(CropPlantEvent 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) { + LogUtils.warn(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/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/ClueScrollsHook.java b/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/ClueScrollsHook.java new file mode 100644 index 0000000..997e379 --- /dev/null +++ b/plugin/src/main/java/net/momirealms/customcrops/compatibility/quest/ClueScrollsHook.java @@ -0,0 +1,67 @@ +/* + * 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.customcrops.compatibility.quest; + +import com.electro2560.dev.cluescrolls.api.*; +import net.momirealms.customcrops.api.CustomCropsPlugin; +import net.momirealms.customcrops.api.event.CropBreakEvent; +import net.momirealms.customcrops.api.event.CropPlantEvent; +import net.momirealms.customcrops.api.mechanic.world.level.WorldCrop; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class ClueScrollsHook implements Listener { + + private final CustomClue harvestClue; + private final CustomClue plantClue; + + public ClueScrollsHook() { + harvestClue = ClueScrollsAPI.getInstance().registerCustomClue(CustomCropsPlugin.getInstance(), "harvest", new ClueConfigData("id", DataType.STRING)); + plantClue = ClueScrollsAPI.getInstance().registerCustomClue(CustomCropsPlugin.getInstance(), "plant", new ClueConfigData("id", DataType.STRING)); + } + + public void register() { + Bukkit.getPluginManager().registerEvents(this, CustomCropsPlugin.get()); + } + + @EventHandler (ignoreCancelled = true) + public void onBreakCrop(CropBreakEvent event) { + final Player player = event.getPlayer(); + if (player == null) return; + + WorldCrop crop = event.getWorldCrop(); + if (crop == null) return; + + harvestClue.handle( + player, + 1, + new ClueDataPair("id", crop.getConfig().getStageItemByPoint(crop.getPoint())) + ); + } + + @EventHandler (ignoreCancelled = true) + public void onPlantCrop(CropPlantEvent event) { + plantClue.handle( + event.getPlayer(), + 1, + new ClueDataPair("id", event.getCrop().getKey()) + ); + } +}