MMOItemType multiple scripts

Now MMOItemType can have multiple scripts (1 script/trigger type)
This commit is contained in:
Roch Blonndiaux 2023-03-06 16:27:50 +01:00
parent 3622203e32
commit 6f4a5dbc3e
3 changed files with 44 additions and 15 deletions

View File

@ -110,8 +110,8 @@ public class Weapon extends UseItem {
return false;
// Handle item set attack effects
if (getMMOItem().getType().hasAttackEffect() && !getNBTItem().getBoolean("MMOITEMS_DISABLE_ATTACK_PASSIVE"))
getMMOItem().getType().applyAttackEffect(attackMeta, playerData, target, this);
if (getMMOItem().getType().hasScript() && !getNBTItem().getBoolean("MMOITEMS_DISABLE_ATTACK_PASSIVE"))
getMMOItem().getType().applyScript(attackMeta, playerData, target, this);
return true;
}

View File

@ -2,24 +2,30 @@ package net.Indyuce.mmoitems.api.item.type;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.damage.AttackMetadata;
import io.lumine.mythic.lib.player.modifier.ModifierSource;
import io.lumine.mythic.lib.script.Script;
import io.lumine.mythic.lib.skill.SimpleSkill;
import io.lumine.mythic.lib.skill.SkillMetadata;
import io.lumine.mythic.lib.skill.handler.MythicLibSkillHandler;
import io.lumine.mythic.lib.skill.result.SkillResult;
import io.lumine.mythic.lib.skill.trigger.TriggerType;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.interaction.weapon.Weapon;
import net.Indyuce.mmoitems.api.item.util.identify.UnidentifiedItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
* mmoitems
@ -38,10 +44,10 @@ public class MMOItemType {
private final ItemStack item;
private final UnidentifiedItem unidentifiedTemplate;
private final List<ItemStat<?, ?>> stats;
private final @org.jetbrains.annotations.Nullable Script script;
private final Map<TriggerType, Script> scripts;
protected MMOItemType(String id, String name, Type type, ModifierSource modifierSource, boolean weapon, String loreFormat, ItemStack item, @org.jetbrains.annotations.Nullable Script script, List<ItemStat<?, ?>> stats) {
protected MMOItemType(String id, String name, Type type, ModifierSource modifierSource, boolean weapon, String loreFormat, ItemStack item, Map<TriggerType, Script> scripts, List<ItemStat<?, ?>> stats) {
this.id = id;
this.name = name;
this.type = type;
@ -50,7 +56,7 @@ public class MMOItemType {
this.loreFormat = loreFormat;
this.item = item;
this.unidentifiedTemplate = new UnidentifiedItem(this);
this.script = script;
this.scripts = scripts;
this.stats = stats;
}
@ -98,8 +104,23 @@ public class MMOItemType {
return new ConfigFile("/item", getId().toLowerCase());
}
public @org.jetbrains.annotations.Nullable Script getScript() {
return script;
public Map<TriggerType, Script> getScripts() {
return this.scripts;
}
public boolean hasScript(TriggerType type) {
return this.scripts.containsKey(type);
}
public Optional<Script> getScript(TriggerType type) {
return Optional.ofNullable(this.scripts.get(type));
}
public @Nullable SkillResult applyScript(TriggerType type, AttackMetadata attackMetadata, PlayerData playerData, LivingEntity target, Weapon weapon) {
return getScript(type)
.map(script -> new SimpleSkill(type, new MythicLibSkillHandler(script)))
.map(simpleSkill -> simpleSkill.cast(new SkillMetadata(simpleSkill, attackMetadata, playerData.getPlayer().getLocation(), target.getLocation(), target)))
.orElse(null);
}
public static ItemStack read(String str) {
@ -179,17 +200,25 @@ public class MMOItemType {
final boolean weapon = section.getBoolean("weapon");
final String loreFormat = section.getString("lore-format");
final ItemStack item = read(section.getString("display", Material.STONE.toString()));
final Script script = section.isString("script") ? MythicLib.plugin.getSkills().getScriptOrThrow(section.getString("script")) : null;
final Type superType = Arrays.stream(Type.values())
.filter(type1 -> Objects.equals(section.getString("type").toLowerCase(), type1.name().toLowerCase()))
.findFirst()
.orElse(Type.NONE);
final Map<TriggerType, Script> scripts = new HashMap<>();
ConfigurationSection scriptSection = section.getConfigurationSection("scripts");
if (scriptSection != null)
scriptSection.getKeys(true)
.forEach(key -> TriggerType.values()
.stream()
.filter(triggerType -> triggerType.name().equalsIgnoreCase(key))
.findFirst()
.ifPresent(triggerType -> scripts.put(triggerType, MythicLib.plugin.getSkills().getScriptOrThrow(scriptSection.getString(key)))));
// TODO: Load the stats
final List<ItemStat<?, ?>> stats = new ArrayList<>();
MMOItemType type = new MMOItemType(id, name, superType, modifierSource, weapon, loreFormat, item, script, stats);
MMOItemType type = new MMOItemType(id, name, superType, modifierSource, weapon, loreFormat, item, scripts, stats);
type.getUnidentifiedTemplate().update(section.getConfigurationSection("unident-item"));
return type;
}

View File

@ -85,10 +85,10 @@ public class ItemBrowser extends PluginInventory {
ItemStack item = currentType.getItem();
item.setAmount(Math.max(1, Math.min(64, items)));
ItemMeta meta = item.getItemMeta();
AdventureUtils.setDisplayName(meta, "&a%s&8 (lick to browse)".formatted(currentType.getName()));
AdventureUtils.setDisplayName(meta, String.format("&a%s&8 (lick to browse)", currentType.getName()));
meta.addItemFlags(ItemFlag.values());
List<String> lore = new ArrayList<>();
lore.add("&7&oThere %s %s &7&oitem%s in that currentType.".formatted(items == 1 ? "is" : "are", items < 1 ? "&c&ono" : "&6&o%d".formatted(items), items == 1 ? "" : "s"));
lore.add(String.format("&7&oThere %s %s &7&oitem%s in that currentType.", items == 1 ? "is" : "are", items < 1 ? "&c&ono" : String.format("&6&o%d", items), items == 1 ? "" : "s"));
AdventureUtils.setLore(meta, lore);
item.setItemMeta(meta);