Accept exact-tier hoppers with JARVIS decoration #41

Merged
unimatrix merged 2 commits from feat/dunechest-planets-stamped-recipe-compatibility into main 2026-11-30 23:23:24 +01:00
5 changed files with 89 additions and 11 deletions

View file

@ -36,8 +36,7 @@ jobs:
printf '%s\n' "$(git rev-parse HEAD)" > chest-artifacts/SOURCE_COMMIT
pack="chest-artifacts/DuneRealms-Resources-${GITHUB_SHA}.zip"
python3 tools/build-unified-resourcepack.py --source "$GITHUB_SHA" --output "$pack"
curl --fail --location --retry 2 https://www.dunerealms.com/downloads/DuneRealms-Resources-0726dbaf6a057a15c5d1753b3208cff010ae6239.zip -o navigator-baseline.zip
python3 tools/plugin_dunechest_pack.py navigator-baseline.zip "$pack"
echo "c15a074da8edcf1404f16fd284aa0d12d16104743ce99e5702dec48370b87b4d $pack" | sha256sum --check
python3 tools/write-plugin-manifest.py chest-artifacts "$GITHUB_SHA"
cp tools/install-plugin-fleet.py tools/plugin_menu_pack.py tools/plugin_backend_abi.py tools/plugin_pillager_abi.py tools/plugin_fleet_rollback.py tools/plugin_pack_properties.py tools/publish-plugin-menu.py chest-artifacts/
(cd chest-artifacts && sha256sum -- *.jar *.zip *.json *.py SOURCE_COMMIT > SHA256SUMS)

View file

@ -1,6 +1,6 @@
plugins { java }
group = "com.example"
version = "1.0.7"
version = "1.0.8"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")

View file

@ -1,6 +1,8 @@
package com.example.dunechest;
import java.util.*;
import java.util.function.Predicate;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
@ -75,23 +77,66 @@ public final class StorageItems {
return copy;
}
private static RecipeChoice.ExactChoice hopperChoice(ItemStack current, int tier) {
return new RecipeChoice.ExactChoice(current, withDisplayName(current, legacyHopperName(tier)));
private static final NamespacedKey JARVIS_HEADER = new NamespacedKey("dunerealms", "metadata_lore_header");
private static final NamespacedKey JARVIS_ORIGIN = new NamespacedKey("dunerealms", "origin_world");
private static final List<NamespacedKey> JARVIS_GRADE_FIELDS = List.of(
"quality", "equipment_mark", "item_class", "metadata_policy").stream()
.map(key -> new NamespacedKey("dunerealms", key)).toList();
private RecipeChoice hopperChoice(ItemStack current, int tier) {
try {
var factory = RecipeChoice.class.getMethod("predicateChoice", Predicate.class, ItemStack.class);
Predicate<ItemStack> exactDevice = item -> item != null && !item.isEmpty()
&& matchesCraftingIngredient(current, item);
return (RecipeChoice) factory.invoke(null, exactDevice, current);
} catch (NoSuchMethodException unavailable) {
return new RecipeChoice.ExactChoice(current, withDisplayName(current, legacyHopperName(tier)));
} catch (ReflectiveOperationException failure) {
throw new IllegalStateException("Cannot register the exact hopper upgrade ingredient", failure);
}
}
boolean matchesCraftingIngredient(ItemStack template, ItemStack source) {
if (template.isSimilar(source)) return true;
if (template.getType() != Material.HOPPER || source.getType() != Material.HOPPER
if (template.getType() != source.getType()) return false;
ItemStack comparableTemplate = withoutJarvisDecoration(template);
ItemStack comparableSource = withoutJarvisDecoration(source);
if (comparableTemplate.isSimilar(comparableSource)) return true;
if (template.getType() != Material.HOPPER
|| !HOPPER.equals(typeOf(template)) || !HOPPER.equals(typeOf(source))) return false;
int tier = tierOf(template);
if (tier < 1 || tier > 3 || tierOf(source) != tier) return false;
Component oldName = legacyHopperName(tier);
Component currentName = name(hopperName(tier), hopperColour(tier));
Component templateName = template.getItemMeta().displayName();
Component sourceName = source.getItemMeta().displayName();
Component templateName = comparableTemplate.getItemMeta().displayName();
Component sourceName = comparableSource.getItemMeta().displayName();
boolean sameTierRename = oldName.equals(templateName) && currentName.equals(sourceName)
|| currentName.equals(templateName) && oldName.equals(sourceName);
return sameTierRename && withDisplayName(template, sourceName).isSimilar(source);
return sameTierRename && withDisplayName(comparableTemplate, sourceName).isSimilar(comparableSource);
}
/** Compare without JARVIS grading and origin display fields. The supplied item is never modified. */
private static ItemStack withoutJarvisDecoration(ItemStack item) {
ItemMeta meta = item.getItemMeta();
var data = meta.getPersistentDataContainer();
if (!data.has(JARVIS_HEADER) && !data.has(JARVIS_ORIGIN)
&& JARVIS_GRADE_FIELDS.stream().noneMatch(data::has)) return item;
var lore = new ArrayList<>(meta.hasLore() ? meta.lore() : List.<Component>of());
var plain = PlainTextComponentSerializer.plainText();
String header = data.get(JARVIS_HEADER, PersistentDataType.STRING);
if (header != null && !lore.isEmpty() && header.equals(plain.serialize(lore.getFirst()))) {
lore.removeFirst();
if (!lore.isEmpty() && plain.serialize(lore.getFirst()).isBlank()) lore.removeFirst();
}
if (data.has(JARVIS_ORIGIN, PersistentDataType.STRING) && !lore.isEmpty()
&& plain.serialize(lore.getLast()).startsWith("Origin world: ")) lore.removeLast();
data.remove(JARVIS_HEADER);
data.remove(JARVIS_ORIGIN);
JARVIS_GRADE_FIELDS.forEach(data::remove);
meta.lore(lore.isEmpty() ? null : lore);
ItemStack copy = item.asOne();
copy.setItemMeta(meta);
return copy;
}
private static Component name(String s, NamedTextColor c) { return Component.text(s, c).decoration(TextDecoration.ITALIC, false); }

View file

@ -1,5 +1,5 @@
name: DuneChest
version: 1.0.7
version: 1.0.8
main: com.example.dunechest.DuneChest
api-version: '1.21.8'
softdepend: [floodgate, DuneMenus, DuneEss]

View file

@ -10,6 +10,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -20,6 +21,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
/** Test-only plugin for an isolated Paper server with a disposable world. */
@ -38,7 +40,7 @@ public final class DuneChestPaperValidation extends JavaPlugin implements Listen
Bukkit.getScheduler().runTask(this, () -> {
try {
validateCrafter();
getLogger().info("DUNECHEST_VALIDATION {\"ok\":true,\"cases\":7}");
getLogger().info("DUNECHEST_VALIDATION {\"ok\":true,\"cases\":9}");
} catch (Exception | AssertionError failure) {
getLogger().log(java.util.logging.Level.SEVERE, "DUNECHEST_VALIDATION {\"ok\":false}", failure);
} finally {
@ -100,6 +102,38 @@ public final class DuneChestPaperValidation extends JavaPlugin implements Listen
if (!output.containsAtLeast(mk2, 1) || !input.isEmpty()) {
throw new AssertionError("Renaming the hopper broke upgrades using the previous name");
}
ItemStack stamped = jarvisDecorated(legacy);
output.clear();
load(input, jarvisDecorated(mk3), 4, redstone);
assertRejected(craft, module, table, placed, input, output);
input.setItem(0, stamped.asQuantity(2));
input.setItem(1, jarvisDecorated(input.getItem(1)));
input.setItem(2, jarvisDecorated(input.getItem(2)));
craft.invoke(module, table, placed);
if (!output.containsAtLeast(mk2, 1) || !stamped.equals(input.getItem(0))
|| input.getItem(1) != null || input.getItem(2) != null) {
throw new AssertionError("Stamped ingredients did not upgrade with the original metadata preserved");
}
}
private static ItemStack jarvisDecorated(ItemStack source) {
ItemStack stamped = source.clone();
var meta = stamped.getItemMeta();
var data = meta.getPersistentDataContainer();
var fields = java.util.Map.of("quality", "common", "equipment_mark", "1",
"item_class", "resource", "metadata_policy", "validation",
"metadata_lore_header", "COMMON | MK I", "origin_world", "arrakis");
fields.forEach((key, value) -> data.set(new NamespacedKey("dunerealms", key), PersistentDataType.STRING, value));
var lore = new java.util.ArrayList<Component>();
lore.add(Component.text("COMMON | MK I"));
lore.add(Component.empty());
if (meta.hasLore()) lore.addAll(meta.lore());
lore.add(Component.text("Origin world: Arrakis"));
meta.lore(lore);
stamped.setItemMeta(meta);
return stamped;
}
private void validateRecipePreview(StorageModule module, World world, Class<?> placedType,