Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/net/tfminecraft/magic/Magic.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.tfminecraft.magic.listener.ArtifactFillChestListener;
import net.tfminecraft.magic.listener.ArtifactListener;
import net.tfminecraft.magic.listener.CastDriftListener;
import net.tfminecraft.magic.listener.DungeonWeaponListener;
import net.tfminecraft.magic.listener.GearAlignmentListener;
import net.tfminecraft.magic.listener.GearRefreshListener;
import net.tfminecraft.magic.listener.MagicSessionListener;
Expand Down Expand Up @@ -123,6 +124,7 @@ public void onEnable() {
if (isMmoStackPresent()) {
getServer().getPluginManager().registerEvents(new ResonanceCastListener(), this);
getServer().getPluginManager().registerEvents(new WeaponWearListener(), this);
getServer().getPluginManager().registerEvents(new DungeonWeaponListener(), this);
getServer().getPluginManager().registerEvents(new CastDriftListener(), this);
getServer().getPluginManager().registerEvents(new GearAlignmentListener(), this);
}
Expand Down
199 changes: 199 additions & 0 deletions src/main/java/net/tfminecraft/magic/api/DungeonWeapons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package net.tfminecraft.magic.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.skill.trigger.TriggerType;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.LiveMMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.stat.data.AbilityListData;
import net.Indyuce.mmoitems.stat.data.BooleanData;
import net.Indyuce.mmoitems.stat.data.GemSocketsData;
import net.Indyuce.mmoitems.stat.data.GemstoneData;
import net.Indyuce.mmoitems.stat.data.StringData;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.data.type.Mergeable;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.tfminecraft.magic.Magic;
import net.tfminecraft.magic.gear.ArchetypeDef;
import net.tfminecraft.magic.gear.ArchetypeRegistry;
import net.tfminecraft.magic.gear.GearItemBuilder;
import net.tfminecraft.magic.gear.GearKeys;
import net.tfminecraft.magic.gear.GearModelResolver;
import net.tfminecraft.magic.gear.GearName;
import net.tfminecraft.magic.gear.GearProvenance;
import net.tfminecraft.magic.gear.GearStatApplicator;
import net.tfminecraft.magic.gear.GearType;
import net.tfminecraft.magic.gear.PartDef;
import net.tfminecraft.magic.gear.PartRegistry;
import net.tfminecraft.magic.gear.RuneKeybind;
import net.tfminecraft.magic.util.ItemRef;

/**
* Mage weapons for TrialRooms' dungeons: built finished, with their runes in, and kept as built.
*
* <p>They carry their own mark and none of a crafted mage weapon's. So no attunement or resonance
* gates their spells, casts don't wear them, they never refresh, the station and the Recycler don't
* take them, and nobody owns them. Their class limit is their MMOItems template's, as on crafted ones.
*/
public final class DungeonWeapons {

/** The material dungeon gear starts from; power is measured against it. */
private static final String MATERIAL = "steel";
private static final Set<String> SCHOOLS =
Set.of("SEITHR_RUNES", "OSENI_RUNES", "CERRITH_RUNES", "MITLAN_RUNES");
/** One each, in rune order: players can't rebind a rune that is already in a weapon. */
private static final List<TriggerType> TRIGGERS = List.of(TriggerType.RIGHT_CLICK,
TriggerType.SHIFT_RIGHT_CLICK, TriggerType.LEFT_CLICK, TriggerType.SHIFT_LEFT_CLICK);

private DungeonWeapons() {}

/** The archetypes a dungeon weapon can be built on. */
public static List<String> archetypes() {
return Arrays.stream(GearType.values()).filter(type -> core(type) != null)
.map(type -> type.name().toLowerCase(Locale.ROOT)).toList();
}

/** Item paths of the school runes that fit a socket slot (minor_rune, lesser_rune, ...), sorted. */
public static List<String> runes(String archetype, String slot) {
ArchetypeDef def = ArchetypeRegistry.get(GearType.fromId(archetype));
String colour = def == null ? "" : def.slotSuffix(slot);
if (colour.isEmpty() || !GearItemBuilder.mmoItemsPresent()) {
return List.of();
}
List<String> paths = new ArrayList<>();
for (String school : SCHOOLS) {
Type type = MMOItems.plugin.getTypes().get(school);
if (type == null) {
continue;
}
for (MMOItemTemplate rune : MMOItems.plugin.getTemplates().getTemplates(type)) {
if (rune.getBaseItemData().get(ItemStats.GEM_COLOR) instanceof StringData fits
&& colour.equals(fits.getString())) {
paths.add("m." + (school + "." + rune.getId()).toLowerCase(Locale.ROOT));
}
}
}
paths.sort(null);
return paths;
}

/**
* A finished, unbreakable weapon with exactly the sockets its runes fill, or null (with a
* warning) when it can't be built.
*
* @param power how strong it is next to the steel weapon. Its attack damage is multiplied by
* it, and its skill damage gives spells, heals and shields the same increase.
* Skill damage, not magic damage: wand shots and blade hits are magic damage
* too, and would grow twice.
* @param extraStats MMOItems stat ids to amounts, added on top as they are
*/
public static ItemStack build(String archetype, List<String> runePaths, double power,
Map<String, Double> extraStats, String name, List<String> lore) {
GearType type = GearType.fromId(archetype);
PartDef core = core(type);
if (core == null || runePaths.size() > TRIGGERS.size() || !(power > 0) || !GearItemBuilder.mmoItemsPresent()) {
return refuse(archetype, "unknown archetype, too many runes, bad power or no MMOItems");
}
ArchetypeDef def = ArchetypeRegistry.get(type);
try {
ItemStack base = ItemRef.build(def.getTemplate());
if (base == null) {
return refuse(archetype, "missing template " + def.getTemplate());
}
LiveMMOItem weapon = new LiveMMOItem(NBTItem.get(base));
stats(weapon, core, power, extraStats);
// The sockets go on before any rune: MMOItems drops data of gems that aren't socketed.
GemSocketsData sockets = new GemSocketsData(new ArrayList<>());
weapon.setData(ItemStats.GEM_SOCKETS, sockets);
for (int i = 0; i < runePaths.size(); i++) {
ItemStack rune = ItemRef.build(runePaths.get(i));
NBTItem nbt = rune == null ? null : NBTItem.get(rune);
String colour = nbt == null ? null : nbt.getString(ItemStats.GEM_COLOR.getNBTPath());
if (colour == null || !SCHOOLS.contains(nbt.getType()) || !def.getSlots().containsValue(colour)) {
return refuse(archetype, "not a rune it can hold: " + runePaths.get(i));
}
socket(weapon, sockets, new LiveMMOItem(nbt), colour, TRIGGERS.get(i));
}
weapon.setData(ItemStats.UNBREAKABLE, new BooleanData(true));
weapon.setData(ItemStats.LORE, new StringListData(new ArrayList<>(lore)));
GearName.write(weapon, name);
ItemStack built = GearModelResolver.apply(weapon.newBuilder().build(), type, List.of(core));
ItemMeta meta = built.getItemMeta();
meta.getPersistentDataContainer().set(GearKeys.dungeonWeapon(), PersistentDataType.BYTE, (byte) 1);
built.setItemMeta(meta);
return built;
} catch (RuntimeException ex) {
return refuse(archetype, ex.toString());
}
}

/**
* Where casting doesn't move a player's Equilibrium (a dungeon run: what a character trains belongs to
* the gear they own); null lets every cast move it again.
*/
public static void holdEquilibrium(java.util.function.Predicate<org.bukkit.entity.Player> where) {
net.tfminecraft.magic.listener.CastDriftListener.hold(where);
}

public static boolean isDungeonWeapon(ItemStack stack) {
return GearProvenance.isDungeonWeapon(stack);
}

/** The standard core, whose stats and look the weapon starts from; null when it has none. */
private static PartDef core(GearType type) {
return ArchetypeRegistry.get(type) == null ? null
: PartRegistry.get(MATERIAL + "_" + type.name().toLowerCase(Locale.ROOT) + "_core");
}

private static void stats(LiveMMOItem weapon, PartDef core, double power, Map<String, Double> extraStats) {
Map<String, Double> totals = GearStatApplicator.sum(List.of(core));
totals.computeIfPresent("attack_damage", (stat, value) -> round(value * power));
totals.put("skill_damage", round((power - 1) * 100));
extraStats.forEach((stat, value) -> totals.merge(stat.toLowerCase(Locale.ROOT), value, Double::sum));
Set<String> ids = GearStatApplicator.managedIds();
ids.addAll(totals.keySet());
ids.remove("max_item_damage");
GearStatApplicator.write(weapon, ids, totals);
}

/** What MMOItems does when a player sockets a gem, with the rune's spells on their own trigger. */
@SuppressWarnings({"rawtypes", "unchecked"})
private static void socket(LiveMMOItem weapon, GemSocketsData sockets, LiveMMOItem rune, String colour, TriggerType trigger) {
GemstoneData gem = new GemstoneData(rune, colour);
sockets.add(gem);
for (ItemStat stat : rune.getStats()) {
StatData data = rune.getData(stat);
if (stat instanceof GemStoneStat || !(data instanceof Mergeable)) {
continue;
}
if (data instanceof AbilityListData spells) {
data = RuneKeybind.rebind(spells, trigger);
}
weapon.mergeData(stat, data, gem.getHistoricUUID());
}
}

private static double round(double value) {
return Math.round(value * 100) / 100.0;
}

private static ItemStack refuse(String archetype, String why) {
Magic.plugin.getLogger().warning("[Magic] Can't build dungeon " + archetype + ": " + why);
return null;
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/tfminecraft/magic/gear/GearHand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
/**
* Finds the mage weapon a player is casting with. Main hand wins; offhand is the
* fallback so an off-hand focus still counts. Armor is deliberately never consulted:
* armor runes are passive and must not gate anything.
* armor runes are passive and must not gate anything. A dungeon weapon in the main hand is
* never gated or worn, so it hides a focus in the offhand.
*/
public final class GearHand {

Expand All @@ -30,7 +31,7 @@ public static ItemStack held(Player player) {

/** @return which hand carries the mage weapon, or null when neither hand has gear */
public static HeldSlot heldSlot(Player player) {
if (player == null) {
if (player == null || GearProvenance.isDungeonWeapon(player.getInventory().getItemInMainHand())) {
return null;
}
if (GearProvenance.isGear(player.getInventory().getItemInMainHand())) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/tfminecraft/magic/gear/GearKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public static NamespacedKey runeRevisions() {
return new NamespacedKey(Magic.plugin, "gear_rune_revisions");
}

/** Marks a weapon built by {@link net.tfminecraft.magic.api.DungeonWeapons}. */
public static NamespacedKey dungeonWeapon() {
return new NamespacedKey(Magic.plugin, "dungeon_weapon");
}

/** Marks the ItemDisplay that shows the weapon resting on a gear station. */
public static NamespacedKey stationDisplay() {
return new NamespacedKey(Magic.plugin, "gear_station_display");
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/tfminecraft/magic/gear/GearProvenance.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public static boolean isGear(ItemStack stack) {
return archetypeOf(stack) != null;
}

/** A finished weapon from {@link net.tfminecraft.magic.api.DungeonWeapons}; never gear. */
public static boolean isDungeonWeapon(ItemStack stack) {
return stack != null && stack.hasItemMeta()
&& stack.getItemMeta().getPersistentDataContainer().has(GearKeys.dungeonWeapon());
}

/** True when this weapon's archetype is one the station still offers. */
public static boolean stationCanCreate(ItemStack stack) {
return ArchetypeRegistry.get(archetypeOf(stack)) != null;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/tfminecraft/magic/gear/GearStatApplicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public static void apply(MMOItem mmo, Collection<PartDef> parts) {
if (mmo == null || !GearItemBuilder.mmoItemsPresent()) {
return;
}
Set<String> managed = managedIds();
if (managed.isEmpty()) {
return;
}
Map<String, Double> totals = sum(parts);
write(mmo, managedIds(), sum(parts));
}

/** Clears each listed stat on the weapon, then writes its total (0 when it has none). */
public static void write(MMOItem mmo, Set<String> managed, Map<String, Double> totals) {
for (String statId : managed) {
ItemStat<?, ?> itemStat = resolve(statId);
if (itemStat == null) {
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/net/tfminecraft/magic/gear/RuneKeybind.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,11 @@ public static Outcome apply(ItemStack held, TriggerType trigger) {
if (current == null || current.isEmpty()) {
return Outcome.of(Result.NO_ABILITIES);
}
List<AbilityData> copies = new ArrayList<>();
for (AbilityData old : current.getAbilities()) {
if (old == null || old.getAbility() == null) {
continue;
}
AbilityData copy = new AbilityData(old.getAbility(), trigger);
for (String modifier : old.getModifiers()) {
copy.setModifier(modifier, old.getParameter(modifier));
}
copies.add(copy);
}
AbilityListData copies = rebind(current, trigger);
if (copies.isEmpty()) {
return Outcome.of(Result.NO_ABILITIES);
}
mmo.setData(ItemStats.ABILITIES, new AbilityListData(copies));
mmo.setData(ItemStats.ABILITIES, copies);
ItemStack rebuilt = mmo.newBuilder().build();
if (rebuilt == null || rebuilt.getType().isAir()) {
return Outcome.of(Result.FAILED);
Expand All @@ -86,4 +76,20 @@ public static Outcome apply(ItemStack held, TriggerType trigger) {
return Outcome.of(Result.FAILED);
}
}

/** Copies of a rune's abilities that cast on {@code trigger}. */
public static AbilityListData rebind(AbilityListData abilities, TriggerType trigger) {
List<AbilityData> copies = new ArrayList<>();
for (AbilityData old : abilities.getAbilities()) {
if (old == null || old.getAbility() == null) {
continue;
}
AbilityData copy = new AbilityData(old.getAbility(), trigger);
for (String modifier : old.getModifiers()) {
copy.setModifier(modifier, old.getParameter(modifier));
}
copies.add(copy);
}
return new AbilityListData(copies);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.tfminecraft.magic.listener;

import java.util.function.Predicate;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -17,6 +19,14 @@

public final class CastDriftListener implements Listener {

/** Players whose casts leave their Equilibrium alone (in a dungeon run, say). */
private static Predicate<Player> held = p -> false;

/** Where casting doesn't move Equilibrium; null: everywhere it does. */
public static void hold(Predicate<Player> where) {
held = where == null ? p -> false : where;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSkillCast(SkillCastEvent event) {
if (Magic.plugin == null) {
Expand All @@ -27,7 +37,7 @@ public void onSkillCast(SkillCastEvent event) {
return;
}
Player player = event.getPlayer();
if (player == null) {
if (player == null || held.test(player)) {
return;
}
double mana = manaSpent(event.getMetadata(), cast);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.tfminecraft.magic.listener;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import net.Indyuce.mmoitems.api.event.MMOItemReforgeEvent;
import net.Indyuce.mmoitems.api.event.item.UnsocketGemStoneEvent;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.tfminecraft.magic.gear.GearProvenance;

/** Dungeon weapons keep their runes, and MMOItems never rebuilds one from its template. */
public final class DungeonWeaponListener implements Listener {

@EventHandler(priority = EventPriority.LOWEST)
public void onUnsocket(UnsocketGemStoneEvent event) {
if (event.getTargetItem() instanceof ReadMMOItem read
&& GearProvenance.isDungeonWeapon(read.getNBT().getItem())) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void onReforge(MMOItemReforgeEvent event) {
if (GearProvenance.isDungeonWeapon(event.getReforger().getStack())) {
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public final class WeaponWearListener implements Listener {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onItemDamage(PlayerItemDamageEvent event) {
ItemStack item = event.getItem();
if (GearProvenance.isDungeonWeapon(item)) {
event.setCancelled(true);
return;
}
if (!GearProvenance.isGear(item)) {
return;
}
Expand Down
Loading