DuneNPC: Spy Module (thrown egg clones target kit cosmetically), Quiet Contacts passive, quest-unlocked recipe

Not compiled or tested.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012wQrHUdrwThVfoqXVv4TT2
This commit is contained in:
Claude 2026-11-28 16:16:58 +01:00 • committed by DuneRealms Development
commit 142b3ae074
4 changed files with 191 additions and 1 deletions

View file

@ -21,6 +21,8 @@ import java.util.*;
public final class DuneNPC extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private DuneCore core;
private Smugglers smugglers;
private SmugglerCover cover;
SmugglerCover cover() { return cover; }
private NamespacedKey guildKey;
private static final Set<String> PLANETS = Set.of("arrakis", "caladan", "kaitain", "giedi_prime", "sigma_draconis_iv");
private static final class Menu implements InventoryHolder {
@ -50,12 +52,14 @@ public final class DuneNPC extends JavaPlugin implements Listener, CommandExecut
if (registration == null) { getLogger().severe("Vault economy unavailable."); Bukkit.getPluginManager().disablePlugin(this); return; }
guildKey = new NamespacedKey("dunerealms", "guild");
smugglers = new Smugglers(this, registration.getProvider(), core.politics());
cover = new SmugglerCover(this);
cover.register();
Objects.requireNonNull(getCommand("dunenpc")).setExecutor(this);
Objects.requireNonNull(getCommand("dunenpc")).setTabCompleter(this);
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getScheduler().runTask(this, () -> Bukkit.getWorlds().forEach(this::ensureGuildNpc));
}
@Override public void onDisable() { if (smugglers != null) smugglers.shutdown(); }
@Override public void onDisable() { if (smugglers != null) smugglers.shutdown(); if (cover != null) cover.shutdown(); }
private void migrateData() throws IOException {
for (String name : List.of("smuggling.yml", "smuggler-camps.yml")) {
File source = new File(core.getDataFolder(), name), destination = new File(getDataFolder(), name);

View file

@ -0,0 +1,173 @@
package com.example.dunenpc;
import java.util.*;
import org.bukkit.*;
import org.bukkit.entity.*;
import org.bukkit.event.*;
import org.bukkit.event.entity.*;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Spy Module: a thrown egg (own texture) that copies the target's visible kit onto the thrower. Cosmetic only.
* The target gets no damage, knockback, message or sound. Cover ends when the spy attacks, is hurt, changes world,
* dies, logs out, or after the cover time. The recipe is added to the player's recipe book when they accept a smuggler
* contract (or any future spy quest calling {@link #unlock(Player)}). Passive: "Quiet Contacts", see {@link #bonusPercent}.
*/
final class SmugglerCover implements Listener {
static final NamespacedKey RECIPE = new NamespacedKey("dunenpc", "spy_module");
private static final NamespacedKey EI = new NamespacedKey("executableitems", "ei-id");
private static final NamespacedKey RUNS = new NamespacedKey("dunenpc", "smuggler_runs");
private static final List<EquipmentSlot> SLOTS = List.of(EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS,
EquipmentSlot.FEET, EquipmentSlot.HAND, EquipmentSlot.OFF_HAND);
private record Disguise(Map<EquipmentSlot, ItemStack> kit, long expiresAt) {}
private final JavaPlugin plugin;
private final Map<UUID, Disguise> disguises = new HashMap<>();
SmugglerCover(JavaPlugin plugin) { this.plugin = plugin; }
/** Passive "Quiet Contacts": +2% on every smuggling payout per completed run, up to +10%. */
static int bonusPercent(int runs) { return Math.max(0, Math.min(10, runs * 2)); }
/** Passive: cover lasts 10 s longer per completed run, up to +120 s. */
static int durationSeconds(int base, int runs) { return Math.max(10, base) + Math.max(0, Math.min(120, runs * 10)); }
void register() {
var recipe = new ShapelessRecipe(RECIPE, item());
recipe.addIngredient(Material.EGG); // Weirding Module (jerboa_egg), checked on craft
recipe.addIngredient(Material.ORANGE_DYE); // Smuggled Spice (smuggled_spice)
recipe.addIngredient(Material.CLAY_BALL); // Jerboa Hide Paste (jerboa_hide_paste)
Bukkit.removeRecipe(RECIPE);
Bukkit.addRecipe(recipe);
Bukkit.getPluginManager().registerEvents(this, plugin);
Bukkit.getScheduler().runTaskTimer(plugin, this::tick, 20L, 20L);
}
void shutdown() {
for (UUID id : new ArrayList<>(disguises.keySet())) { Player p = Bukkit.getPlayer(id); if (p != null) clear(p, false); }
disguises.clear();
Bukkit.removeRecipe(RECIPE);
}
/** Result of the recipe: Uncommon, MK IV (highest ingredient MK III + 1). */
static ItemStack item() {
ItemStack stack = new ItemStack(Material.EGG);
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Spy Module");
meta.setLore(List.of(ChatColor.GRAY + "UNCOMMON | MK IV", ChatColor.GRAY + "S1 · ARRAKIS", "",
ChatColor.DARK_PURPLE + "Throw at a player to copy how their kit looks.",
ChatColor.DARK_PURPLE + "Cosmetic only. They never notice."));
meta.setItemModel(new NamespacedKey("dunerealms", "spy_module"));
meta.setMaxStackSize(1);
var cooldown = meta.getUseCooldown();
cooldown.setCooldownSeconds(3);
cooldown.setCooldownGroup(new NamespacedKey("dunerealms", "spy_module"));
meta.setUseCooldown(cooldown);
var pdc = meta.getPersistentDataContainer();
pdc.set(EI, PersistentDataType.STRING, "spy_module");
pdc.set(new NamespacedKey("dunerealms", "quality"), PersistentDataType.STRING, "uncommon");
pdc.set(new NamespacedKey("dunerealms", "equipment_mark"), PersistentDataType.STRING, "4");
pdc.set(new NamespacedKey("dunerealms", "item_class"), PersistentDataType.STRING, "weapon");
pdc.set(new NamespacedKey("dunerealms", "metadata_policy"), PersistentDataType.STRING, "s1-item-metadata-v1");
stack.setItemMeta(meta);
return stack;
}
static boolean module(ItemStack item) { return hasId(item, Material.EGG, "spy_module"); }
private static boolean hasId(ItemStack item, Material type, String id) {
return item != null && item.getType() == type && item.hasItemMeta()
&& id.equals(item.getItemMeta().getPersistentDataContainer().get(EI, PersistentDataType.STRING));
}
// ---- quest hooks -------------------------------------------------------------------------------------------
/** Adds the recipe to the player's recipe book. Called when a smuggler contract is accepted; a spy quest can call it too. */
void unlock(Player player) {
if (player.hasDiscoveredRecipe(RECIPE)) return;
player.discoverRecipe(RECIPE);
player.sendMessage(ChatColor.LIGHT_PURPLE + "New recipe: Spy Module. Weirding Module + Smuggled Spice + Jerboa Leather.");
}
int runs(Player player) { return player.getPersistentDataContainer().getOrDefault(RUNS, PersistentDataType.INTEGER, 0); }
void recordRun(Player player) { player.getPersistentDataContainer().set(RUNS, PersistentDataType.INTEGER, runs(player) + 1); }
// ---- crafting ----------------------------------------------------------------------------------------------
@EventHandler public void prepare(PrepareItemCraftEvent event) {
if (!(event.getRecipe() instanceof Keyed keyed) || !keyed.getKey().equals(RECIPE)) return;
boolean unlocked = !event.getViewers().isEmpty() && event.getViewers().get(0) instanceof Player p && p.hasDiscoveredRecipe(RECIPE);
boolean egg = false, spice = false, paste = false;
for (ItemStack stack : event.getInventory().getMatrix()) {
if (stack == null) continue;
if (hasId(stack, Material.EGG, "jerboa_egg")) egg = true;
else if (hasId(stack, Material.ORANGE_DYE, "smuggled_spice")) spice = true;
else if (hasId(stack, Material.CLAY_BALL, "jerboa_hide_paste")) paste = true;
else { unlocked = false; }
}
if (!(unlocked && egg && spice && paste)) event.getInventory().setResult(null);
}
// ---- throw and clone ---------------------------------------------------------------------------------------
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void use(PlayerInteractEvent event) {
if (!event.getAction().isRightClick() || !module(event.getItem())) return;
if (event.getPlayer().hasCooldown(event.getItem())) event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGHEST) public void hatch(PlayerEggThrowEvent event) {
if (module(event.getEgg().getItem())) event.setHatching(false);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void hit(ProjectileHitEvent event) {
if (!(event.getEntity() instanceof Egg egg) || !module(egg.getItem()) || !(egg.getShooter() instanceof Player spy)) return;
if (!(event.getHitEntity() instanceof Player target) || target.equals(spy)) return;
// Cancelling the hit removes the hurt animation, knockback and sound: the target notices nothing.
event.setCancelled(true);
egg.remove();
copy(spy, target);
}
private void copy(Player spy, Player target) {
Map<EquipmentSlot, ItemStack> kit = new EnumMap<>(EquipmentSlot.class);
var inv = target.getInventory();
for (EquipmentSlot slot : SLOTS) { ItemStack item = inv.getItem(slot); kit.put(slot, item == null ? new ItemStack(Material.AIR) : item.clone()); }
int seconds = durationSeconds(plugin.getConfig().getInt("spy.duration-seconds", 180), runs(spy));
disguises.put(spy.getUniqueId(), new Disguise(kit, System.currentTimeMillis() + seconds * 1000L));
show(spy);
spy.sendMessage(ChatColor.LIGHT_PURPLE + "Cover set: you wear " + target.getName() + "'s kit for " + seconds + " seconds. Attacking or being hurt breaks it.");
}
// ---- showing and clearing the cosmetic kit -----------------------------------------------------------------
private void show(Player spy) {
Disguise disguise = disguises.get(spy.getUniqueId());
if (disguise != null) send(spy, disguise.kit());
}
private void clear(Player spy, boolean message) {
if (disguises.remove(spy.getUniqueId()) == null) return;
Map<EquipmentSlot, ItemStack> real = new EnumMap<>(EquipmentSlot.class);
for (EquipmentSlot slot : SLOTS) { ItemStack item = spy.getInventory().getItem(slot); real.put(slot, item == null ? new ItemStack(Material.AIR) : item); }
send(spy, real);
if (message) spy.sendMessage(ChatColor.GRAY + "Your cover is gone.");
}
private void send(Player spy, Map<EquipmentSlot, ItemStack> kit) {
double range = Math.pow(plugin.getConfig().getDouble("spy.view-distance", 80), 2);
for (Player viewer : spy.getWorld().getPlayers())
if (!viewer.equals(spy) && viewer.getLocation().distanceSquared(spy.getLocation()) <= range) viewer.sendEquipmentChange(spy, kit);
}
/** Every second: expire covers and re-send the copied kit, because the client resets it when the spy's real gear changes or they re-enter view. */
private void tick() {
long now = System.currentTimeMillis();
for (UUID id : new ArrayList<>(disguises.keySet())) {
Player spy = Bukkit.getPlayer(id);
if (spy == null || !spy.isOnline()) { disguises.remove(id); continue; }
if (disguises.get(id).expiresAt() <= now) clear(spy, true); else show(spy);
}
}
// ---- ending the cover --------------------------------------------------------------------------------------
@EventHandler(ignoreCancelled = true) public void attacked(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager() instanceof Projectile projectile && projectile.getShooter() instanceof Entity shooter ? shooter : event.getDamager();
if (damager instanceof Player spy && disguises.containsKey(spy.getUniqueId())) clear(spy, true);
if (event.getEntity() instanceof Player hurt && disguises.containsKey(hurt.getUniqueId())) clear(hurt, true);
}
@EventHandler public void world(PlayerChangedWorldEvent event) { clear(event.getPlayer(), true); }
@EventHandler public void death(PlayerDeathEvent event) { disguises.remove(event.getEntity().getUniqueId()); }
@EventHandler public void quit(PlayerQuitEvent event) { disguises.remove(event.getPlayer().getUniqueId()); }
}

View file

@ -249,6 +249,7 @@ final class Smugglers implements Listener {
contracts.put(player.getUniqueId(), offer(slot == 11 ? "spice" : "water", trader));
if (!save()) { contracts.remove(player.getUniqueId()); player.sendMessage("Contract could not be saved. Try again."); return; }
updateCamps();
plugin.cover().unlock(player);
player.sendMessage(ChatColor.GOLD + (slot == 11
? "Smuggler: 'The melange must vanish from the Imperial ledger. My distant contact will pay for your silence.'"
: "Smuggler: 'The hidden sietch has no water to spare. Reach my distant contact, and its people will remember.'"));
@ -303,6 +304,13 @@ final class Smugglers implements Listener {
player.getWorld().dropItemNaturally(player.getLocation(), bucket);
}
plugin.core().reportSmuggling(player, contract.commodity().toUpperCase(Locale.ROOT), contract.reward());
int contacts = SmugglerCover.bonusPercent(plugin.cover().runs(player));
if (contacts > 0 && net > 0) {
int extra = (int) Math.round(net * contacts / 100.0);
if (extra > 0 && economy.depositPlayer(player, extra).transactionSuccess())
player.sendMessage(ChatColor.LIGHT_PURPLE + "Quiet Contacts: +" + extra + " Solaris (+" + contacts + "%).");
}
plugin.cover().recordRun(player);
player.sendMessage(ChatColor.GOLD + (contract.commodity().equals("spice")
? "Smuggler: 'No seal, no names. The melange is already on its way. The Emperor's men may have noticed you.'"
: "Smuggler: 'Tonight the sietch drinks. Keep your route secret; the water monopoly has long eyes.'"));

View file

@ -5,3 +5,8 @@ smugglers:
max-traders: 6
spice-amount: 64
water-amount: 4
# Spy Module (thrown egg that copies another player's kit, cosmetic only)
spy:
duration-seconds: 180 # base cover time; +10 s per completed smuggling run, up to +120 s
view-distance: 80 # players within this many blocks see the copied kit