Bug Closed · Resolved

[Help] How to convert skill-MythicConfig strings to be placeholdered?

#4255 opened by Unknown 3 years ago

Unknownreported this bug

Here is a custom skill code using MythicMobs API 3.5.0, but i dont know how to make MythicPlaceholders in the skill-config effective.
For example, the skill is - customdamage{a="<caster.level> * 2 + 1"} @target, then the placeholder will not be converted to a number.

public class customDamage implements ITargetedEntitySkill {

    protected String amount;
    protected String defenseName;
    protected String critChanceName;
    protected String critRateName;
    protected boolean ignoreCooldown;

    public customDamage(MythicLineConfig config){
        this.amount = config.getPlaceholderString(new String[]{"amount", "amt", "a"}, "0").get().replace("\"", "");
        this.defenseName = config.getPlaceholderString(new String[]{"defenseName", "defensename", "dn"}, "NULL").get().replace("\"", "");
        this.critChanceName = config.getPlaceholderString(new String[]{"critChanceName", "critchancename", "ccn"}, "NULL").get().replace("\"", "");
        this.critRateName = config.getPlaceholderString(new String[]{"critRateName", "critratename", "crn"}, "NULL").get().replace("\"", "");
        this.ignoreCooldown = config.getBoolean(new String[]{"ignoreCooldown", "ignorecooldown", "ic"}, false);
    }
    @Override
    public SkillResult castAtEntity(SkillMetadata data, AbstractEntity target) {
        Entity caster = BukkitAdapter.adapt(data.getCaster().getEntity());
        Entity entity = BukkitAdapter.adapt(target);

        if(caster instanceof LivingEntity && entity instanceof LivingEntity) {
            LivingEntity livingCaster = (LivingEntity) caster;
            LivingEntity livingEntity = (LivingEntity) entity;

            if(livingCaster instanceof Player) {
                // PlaceholderAPI
                amount = PlaceholderAPI.setPlaceholders((Player) livingCaster, amount);
            }

            double attackValue = Utils.evaluateFormula(AttributeAPI.getAttrData(livingCaster), amount, critChanceName, critRateName);
            double defenseValue = Utils.getValue(AttributeAPI.getAttrData(livingEntity), defenseName);

            if(attackValue > defenseValue) {
                attackValue -= defenseValue;
            }

            DecimalFormat decimalFormat = new DecimalFormat("#.00");
            attackValue = Double.parseDouble(decimalFormat.format(attackValue));

            if(ignoreCooldown) {
                livingEntity.setNoDamageTicks(0);
            }
            livingEntity.damage(attackValue);

            return SkillResult.SUCCESS;
        }
        return null;
    }
}
Unknowncommented

You can parse the placeholders using PlaceholderString#get, PlaceholderInt#get, PlaceholderDouble#get, etc...

public class CoolMechanic implements ITargetedEntitySkill {
  
  private final PlaceholderString message;
  private final PlaceholderDouble number;
  
  public CoolMechanic(MythicLineConfig config){
    this.message = config.getPlaceholderString(new String[]{"message", "m"}, "default message");
    this.number = config.getPlaceholderDouble(new String[]{"number", "n"}, 0);
  }
  
  @Override
  public SkillResult castAtEntity(SkillMetadata data, AbstractEntity target){
    //parse the placeholder here using `PlaceholderString#get` methods
    String parsedMessage = this.message.get(data, target);
    System.out.println(parsedMessage);
   
    //parse number placeholder here using `PlaceholderDouble#get` methods
    double parsedNumber = this.number.get(data, target);
    System.out.println(parsedNumber);
  }
}
Sign in to join the discussion.