From 70119ad4ab3e57c226f2ad31705028d65ebcbdcd Mon Sep 17 00:00:00 2001
From: PikaMug <2267126+PikaMug@users.noreply.github.com>
Date: Tue, 23 Jul 2024 22:27:45 -0400
Subject: [PATCH] Special case for 1.21+ crops, fixes #2256
---
core/pom.xml | 14 ++++---------
.../me/pikamug/quests/BukkitQuestsPlugin.java | 21 +++++++++++++++----
.../quests/listeners/BukkitBlockListener.java | 11 +++++++++-
.../pikamug/quests/player/BukkitQuester.java | 16 +++++++++++++-
.../pikamug/quests/util/BukkitItemUtil.java | 11 ++++++++--
5 files changed, 55 insertions(+), 18 deletions(-)
diff --git a/core/pom.xml b/core/pom.xml
index fe54bb80a..982369d49 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -170,8 +170,8 @@
de.tr7zw
- item-nbt-api-plugin
- 2.12.4
+ item-nbt-api
+ 2.13.1
xyz.upperlevel.spigot.book
@@ -265,7 +265,7 @@
me.*
com.github.PikaMug:LocaleLib
- de.tr7zw:item-nbt-api-plugin
+ de.tr7zw:item-nbt-api
org.slf4j:slf4j-simple
org.slf4j:slf4j-api
xyz.upperlevel.spigot.book:spigot-book-api
@@ -285,12 +285,6 @@
plugin.yml
-
- de.tr7zw:item-nbt-api-plugin
-
- plugin.yml
-
-
@@ -298,7 +292,7 @@
me.pikamug.quests.libs.localelib
- de.tr7zw
+ de.tr7zw.changeme.nbtapi
me.pikamug.quests.libs.nbtapi
diff --git a/core/src/main/java/me/pikamug/quests/BukkitQuestsPlugin.java b/core/src/main/java/me/pikamug/quests/BukkitQuestsPlugin.java
index 38e2eff62..b14bc4662 100644
--- a/core/src/main/java/me/pikamug/quests/BukkitQuestsPlugin.java
+++ b/core/src/main/java/me/pikamug/quests/BukkitQuestsPlugin.java
@@ -10,6 +10,7 @@
package me.pikamug.quests;
+import de.tr7zw.changeme.nbtapi.NBT;
import me.pikamug.localelib.LocaleManager;
import me.pikamug.quests.actions.Action;
import me.pikamug.quests.actions.BukkitActionFactory;
@@ -22,10 +23,17 @@ import me.pikamug.quests.convo.misc.QuestAcceptPrompt;
import me.pikamug.quests.dependencies.BukkitDenizenTrigger;
import me.pikamug.quests.dependencies.BukkitDependencies;
import me.pikamug.quests.interfaces.ReloadCallback;
-import me.pikamug.quests.listeners.*;
+import me.pikamug.quests.listeners.BukkitBlockListener;
+import me.pikamug.quests.listeners.BukkitCitizensListener;
+import me.pikamug.quests.listeners.BukkitCommandManager;
+import me.pikamug.quests.listeners.BukkitConvoListener;
+import me.pikamug.quests.listeners.BukkitItemListener;
+import me.pikamug.quests.listeners.BukkitPartiesListener;
+import me.pikamug.quests.listeners.BukkitPlayerListener;
+import me.pikamug.quests.listeners.BukkitUniteListener;
+import me.pikamug.quests.listeners.BukkitZnpcsApiListener;
+import me.pikamug.quests.listeners.BukkitZnpcsListener;
import me.pikamug.quests.logging.BukkitQuestsLog4JFilter;
-import me.pikamug.quests.storage.implementation.jar.BukkitModuleJarStorage;
-import me.pikamug.quests.storage.implementation.ModuleStorageImpl;
import me.pikamug.quests.module.CustomObjective;
import me.pikamug.quests.module.CustomRequirement;
import me.pikamug.quests.module.CustomReward;
@@ -36,9 +44,11 @@ import me.pikamug.quests.quests.Quest;
import me.pikamug.quests.statistics.BukkitMetrics;
import me.pikamug.quests.storage.BukkitStorageFactory;
import me.pikamug.quests.storage.QuesterStorage;
+import me.pikamug.quests.storage.implementation.ModuleStorageImpl;
import me.pikamug.quests.storage.implementation.file.BukkitActionYamlStorage;
import me.pikamug.quests.storage.implementation.file.BukkitConditionYamlStorage;
import me.pikamug.quests.storage.implementation.file.BukkitQuestYamlStorage;
+import me.pikamug.quests.storage.implementation.jar.BukkitModuleJarStorage;
import me.pikamug.quests.tasks.BukkitNpcEffectThread;
import me.pikamug.quests.tasks.BukkitPlayerMoveThread;
import me.pikamug.quests.util.BukkitLang;
@@ -152,6 +162,9 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests {
conditionFactory = new BukkitConditionFactory(this);
depends = new BukkitDependencies(this);
trigger = new BukkitDenizenTrigger(this);
+ if (!NBT.preloadApi()) {
+ getLogger().warning("NBT-API wasn't initialized properly");
+ }
// 3 - Load main config
configSettings.init();
@@ -588,7 +601,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests {
*
* @param resourcePath jar file location starting from resource folder, i.e. "lang/el-GR/strings.yml"
* @param outputPath file destination starting from Quests folder, i.e. "lang/el-GR/strings.yml"
- * @param replace whether or not to replace the destination file
+ * @param replace whether to replace the destination file
*/
@Override
public void saveResourceAs(String resourcePath, final String outputPath, final boolean replace) {
diff --git a/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java b/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java
index 374d4c390..4eb40f6bd 100644
--- a/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java
+++ b/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java
@@ -10,6 +10,7 @@
package me.pikamug.quests.listeners;
+import de.tr7zw.changeme.nbtapi.NBT;
import me.pikamug.quests.BukkitQuestsPlugin;
import me.pikamug.quests.enums.ObjectiveType;
import me.pikamug.quests.events.quester.BukkitQuesterPostUpdateObjectiveEvent;
@@ -26,6 +27,7 @@ import me.pikamug.quests.util.BukkitLang;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
+import org.bukkit.block.data.Ageable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -403,7 +405,14 @@ public class BukkitBlockListener implements Listener {
// https://github.com/PikaMug/Quests/issues/2243
}
try {
- return new ItemStack(block.getBlockData().getPlacementMaterial(), 1, durability);
+ // Should only happen on Paper 1.21+
+ final ItemStack item = new ItemStack(block.getBlockData().getPlacementMaterial());
+ if (block.getBlockData() instanceof Ageable) {
+ NBT.modify(item, nbt -> {
+ nbt.setShort("quests_age", (short) ((Ageable)block.getBlockData()).getAge());
+ });
+ }
+ return item;
} catch (Exception e) {
// https://github.com/PikaMug/Quests/issues/2256
}
diff --git a/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java b/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java
index 51a4352b3..67f146453 100644
--- a/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java
+++ b/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java
@@ -14,6 +14,7 @@ import com.alessiodp.parties.api.interfaces.Party;
import com.alessiodp.parties.api.interfaces.PartyPlayer;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.util.player.UserManager;
+import de.tr7zw.changeme.nbtapi.NBT;
import io.github.znetworkw.znpcservers.npc.NPC;
import lol.pyr.znpcsplus.api.npc.Npc;
import me.clip.placeholderapi.PlaceholderAPI;
@@ -1827,6 +1828,19 @@ public class BukkitQuester implements Quester {
// Age toBreak unspecified so ignore durability
goal = toBreak;
}
+ } else if (Material.getMaterial("CRAFTER") != null && broken.getType().isEdible()) {
+ // Paper 1.21+ is special case
+ final short toBreakAge = NBT.get(toBreak, nbt -> (short) nbt.getShort("quests_age"));
+ final short brokenAge = NBT.get(broken, nbt -> (short) nbt.getShort("quests_age"));
+ if (toBreakAge > 0) {
+ // Age toBreak specified so check for durability
+ if (brokenAge == toBreakAge) {
+ goal = toBreak;
+ }
+ } else {
+ // Age toBreak unspecified so ignore durability
+ goal = toBreak;
+ }
} else if (broken.getType().name().equals("RED_ROSE")) {
// Flowers are unique so check for durability
if (broken.getDurability() == toBreak.getDurability()) {
@@ -3820,7 +3834,7 @@ public class BukkitQuester implements Quester {
final Stage stage = getCurrentStage(quest);
if (stage != null) {
quest.updateCompass(this, stage);
- // TODO - decide whether or not to handle this
+ // TODO - decide whether to handle this
/*if (q.equals(quest) == false) {
if (getPlayer() != null && getPlayer().isOnline()) {
quitQuest(quest, ChatColor.GOLD + Lang.get("questModified")
diff --git a/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java b/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java
index 6626df2d6..34bcb2007 100644
--- a/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java
+++ b/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java
@@ -10,6 +10,7 @@
package me.pikamug.quests.util;
+import de.tr7zw.changeme.nbtapi.NBT;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
@@ -227,11 +228,17 @@ public class BukkitItemUtil {
if (mat == null) {
return null;
}
+ ItemStack item;
if (mat.isBlock() && Material.getMaterial("CRAFTER") != null) {
// Paper 1.21+ does not allow ItemStack from unobtainable blocks (i.e. CARROTS block)
- mat = mat.createBlockData().getPlacementMaterial();
+ item = new ItemStack(mat.createBlockData().getPlacementMaterial(), amount);
+ NBT.modify(item, nbt -> {
+ nbt.setShort("quests_age", durability);
+ });
+ } else {
+ item = new ItemStack(mat, amount, durability);
}
- return new ItemStack(mat, amount, durability);
+ return item;
} catch (final Exception e) {
try {
Bukkit.getLogger().warning(material + " x " + amount