Preserve JARVIS menus while porting gameplay, Citizens NPCs and plugin-only CI
This commit is contained in:
parent
a16b7adc1d
commit
dbf4bb25ff
1 changed files with 2251 additions and 196035 deletions
220
DuneNPC/src/main/java/com/example/dunenpc/SmugglerCover.java
Normal file
220
DuneNPC/src/main/java/com/example/dunenpc/SmugglerCover.java
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
package com.example.dunenpc;
|
||||
|
||||
import java.util.*;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.*;
|
||||
import org.bukkit.event.block.CrafterCraftEvent;
|
||||
import org.bukkit.event.entity.*;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
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, Set<UUID> viewers) {}
|
||||
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.clamp(runs, 0, 5) * 2; }
|
||||
/** Passive: cover lasts 10 s longer per completed run, up to +120 s. */
|
||||
static int durationSeconds(int base, int runs) {
|
||||
return (int) Math.min(Integer.MAX_VALUE, Math.max(10L, base) + Math.clamp(runs, 0, 12) * 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 Hide Paste.");
|
||||
}
|
||||
int runs(Player player) { return player.getPersistentDataContainer().getOrDefault(RUNS, PersistentDataType.INTEGER, 0); }
|
||||
void recordRun(Player player) {
|
||||
player.getPersistentDataContainer().set(RUNS, PersistentDataType.INTEGER, Math.clamp(runs(player), 0, 11) + 1);
|
||||
}
|
||||
|
||||
// ---- crafting ----------------------------------------------------------------------------------------------
|
||||
private static boolean spyRecipe(Recipe recipe, ItemStack result) {
|
||||
return (recipe instanceof Keyed keyed && RECIPE.equals(keyed.getKey())) || module(result);
|
||||
}
|
||||
|
||||
static boolean canCraft(Player player, ItemStack[] matrix) {
|
||||
if (!player.hasDiscoveredRecipe(RECIPE)) return false;
|
||||
int egg = 0, spice = 0, paste = 0;
|
||||
for (ItemStack stack : matrix) {
|
||||
if (stack == null || stack.getType() == Material.AIR) continue;
|
||||
if (hasId(stack, Material.EGG, "jerboa_egg")) egg++;
|
||||
else if (hasId(stack, Material.ORANGE_DYE, "smuggled_spice")) spice++;
|
||||
else if (hasId(stack, Material.CLAY_BALL, "jerboa_hide_paste")) paste++;
|
||||
else return false;
|
||||
}
|
||||
return egg == 1 && spice == 1 && paste == 1;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST) public void prepare(PrepareItemCraftEvent event) {
|
||||
if (!spyRecipe(event.getRecipe(), event.getInventory().getResult())) return;
|
||||
if (!(event.getView().getPlayer() instanceof Player player) || !canCraft(player, event.getInventory().getMatrix()))
|
||||
event.getInventory().setResult(null);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void craft(CraftItemEvent event) {
|
||||
if (!spyRecipe(event.getRecipe(), event.getCurrentItem())) return;
|
||||
if (!(event.getWhoClicked() instanceof Player player) || !canCraft(player, event.getInventory().getMatrix()))
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void automate(CrafterCraftEvent event) {
|
||||
if (spyRecipe(event.getRecipe(), event.getResult())) event.setCancelled(true);
|
||||
}
|
||||
|
||||
// ---- 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 preventDamage(EntityDamageByEntityEvent event) {
|
||||
if (event.getDamager() instanceof Egg egg && module(egg.getItem())) event.setCancelled(true);
|
||||
}
|
||||
@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;
|
||||
// Cancelling the hit removes the hurt animation, knockback and sound: the target notices nothing.
|
||||
event.setCancelled(true);
|
||||
egg.remove();
|
||||
if (event.getHitEntity() instanceof Player target && !target.equals(spy)) 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));
|
||||
Disguise previous = disguises.get(spy.getUniqueId());
|
||||
Set<UUID> viewers = previous == null ? new HashSet<>() : previous.viewers();
|
||||
disguises.put(spy.getUniqueId(), new Disguise(kit, System.currentTimeMillis() + seconds * 1000L, viewers));
|
||||
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) return;
|
||||
double distance = Math.clamp(plugin.getConfig().getDouble("spy.view-distance", 80), 1, 256);
|
||||
Set<UUID> current = new HashSet<>();
|
||||
for (Player viewer : spy.getWorld().getPlayers()) {
|
||||
if (viewer.equals(spy) || !viewer.canSee(spy)
|
||||
|| viewer.getLocation().distanceSquared(spy.getLocation()) > distance * distance) continue;
|
||||
viewer.sendEquipmentChange(spy, disguise.kit());
|
||||
current.add(viewer.getUniqueId());
|
||||
}
|
||||
disguise.viewers().removeAll(current);
|
||||
restoreViewers(spy, disguise.viewers());
|
||||
disguise.viewers().clear();
|
||||
disguise.viewers().addAll(current);
|
||||
}
|
||||
private void clear(Player spy, boolean message) {
|
||||
Disguise disguise = disguises.remove(spy.getUniqueId());
|
||||
if (disguise == null) return;
|
||||
restoreViewers(spy, disguise.viewers());
|
||||
if (message) spy.sendMessage(ChatColor.GRAY + "Your cover is gone.");
|
||||
}
|
||||
private void restoreViewers(Player spy, Set<UUID> viewers) {
|
||||
if (viewers.isEmpty()) 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); }
|
||||
for (UUID id : viewers) {
|
||||
Player viewer = Bukkit.getPlayer(id);
|
||||
if (viewer != null && viewer.getWorld().equals(spy.getWorld()) && viewer.canSee(spy))
|
||||
viewer.sendEquipmentChange(spy, real);
|
||||
}
|
||||
}
|
||||
/** 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(priority = EventPriority.MONITOR, 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);
|
||||
}
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void hurt(EntityDamageEvent event) {
|
||||
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()); }
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff