Example usage for org.apache.commons.lang3.text StrBuilder replaceFirst

List of usage examples for org.apache.commons.lang3.text StrBuilder replaceFirst

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrBuilder replaceFirst.

Prototype

public StrBuilder replaceFirst(final StrMatcher matcher, final String replaceStr) 

Source Link

Document

Replaces the first match within the builder with the replace string.

Usage

From source file:de.vandermeer.skb.interfaces.application.ApplicationErrorCode.java

/**
 * Creates a new error message with all arguments substituted.
 * The length of `arguments` must be equal to the number of expected arguments of this error code.
 * If the number of expected arguments is 0, then `args` can be null or of length 0.
 * @param args the arguments, must have same length as expected arguments
 * @return new error message//w ww  .ja  va2s. co  m
 */
default String getMessage(final Object... args) {
    if (this.getArgs() > 0) {
        Validate.noNullElements(args);
        Validate.validState(args.length == this.getArgs());
    } else {
        Validate.validState(args == null || args.length == 0);
        return this.getMessage();
    }

    final StrBuilder ret = new StrBuilder().append(this.getMessage());
    for (final Object arg : args) {
        ret.replaceFirst("{}", arg.toString());
    }
    return ret.toString();
}

From source file:handlers.voicedcommands.WhoAmI.java

/**
 * Method useVoicedCommand.//from w  w w .j  a va 2  s.  c om
 * @param command String
 * @param player Player
 * @param args String
 * @return boolean
 * @see lineage2.gameserver.handlers.IVoicedCommandHandler#useVoicedCommand(String, Player, String)
 */
@Override
public boolean useVoicedCommand(String command, Player player, String args) {
    Creature target = null;
    double hpRegen = Formulas.calcHpRegen(player);
    double cpRegen = Formulas.calcCpRegen(player);
    double mpRegen = Formulas.calcMpRegen(player);
    double hpDrain = player.calcStat(Stats.ABSORB_DAMAGE_PERCENT, 0., target, null);
    double mpDrain = player.calcStat(Stats.ABSORB_DAMAGEMP_PERCENT, 0., target, null);
    double hpGain = player.calcStat(Stats.HEAL_EFFECTIVNESS, 100., target, null);
    double mpGain = player.calcStat(Stats.MANAHEAL_EFFECTIVNESS, 100., target, null);
    double critPerc = 2 * player.calcStat(Stats.CRITICAL_DAMAGE, target, null);
    double critStatic = player.calcStat(Stats.CRITICAL_DAMAGE_STATIC, target, null);
    double mCritDmg = player.calcStat(Stats.MCRITICAL_DAMAGE, target, null);
    double blowRate = player.calcStat(Stats.FATALBLOW_RATE, target, null);
    ItemInstance shld = player.getSecondaryWeaponInstance();
    boolean shield = (shld != null) && (shld.getItemType() == WeaponType.NONE);
    double shieldDef = shield
            ? player.calcStat(Stats.SHIELD_DEFENCE, player.getTemplate().getBaseShldDef(), target, null)
            : 0.;
    double shieldRate = shield ? player.calcStat(Stats.SHIELD_RATE, target, null) : 0.;
    double xpRate = player.getRateExp();
    double spRate = player.getRateSp();
    double dropRate = player.getRateItems();
    double adenaRate = player.getRateAdena();
    double spoilRate = player.getRateSpoil();
    double fireResist = player.calcStat(Element.FIRE.getDefence(), 0., target, null);
    double windResist = player.calcStat(Element.WIND.getDefence(), 0., target, null);
    double waterResist = player.calcStat(Element.WATER.getDefence(), 0., target, null);
    double earthResist = player.calcStat(Element.EARTH.getDefence(), 0., target, null);
    double holyResist = player.calcStat(Element.HOLY.getDefence(), 0., target, null);
    double unholyResist = player.calcStat(Element.UNHOLY.getDefence(), 0., target, null);
    double bleedPower = player.calcStat(Stats.BLEED_POWER, target, null);
    double bleedResist = player.calcStat(Stats.BLEED_RESIST, target, null);
    double poisonPower = player.calcStat(Stats.POISON_POWER, target, null);
    double poisonResist = player.calcStat(Stats.POISON_RESIST, target, null);
    double stunPower = player.calcStat(Stats.STUN_POWER, target, null);
    double stunResist = player.calcStat(Stats.STUN_RESIST, target, null);
    double rootPower = player.calcStat(Stats.ROOT_POWER, target, null);
    double rootResist = player.calcStat(Stats.ROOT_RESIST, target, null);
    double sleepPower = player.calcStat(Stats.SLEEP_POWER, target, null);
    double sleepResist = player.calcStat(Stats.SLEEP_RESIST, target, null);
    double paralyzePower = player.calcStat(Stats.PARALYZE_POWER, target, null);
    double paralyzeResist = player.calcStat(Stats.PARALYZE_RESIST, target, null);
    double mentalPower = player.calcStat(Stats.MENTAL_POWER, target, null);
    double mentalResist = player.calcStat(Stats.MENTAL_RESIST, target, null);
    double debuffPower = player.calcStat(Stats.DEBUFF_POWER, target, null);
    double debuffResist = player.calcStat(Stats.DEBUFF_RESIST, target, null);
    double cancelPower = player.calcStat(Stats.CANCEL_POWER, target, null);
    double cancelResist = player.calcStat(Stats.CANCEL_RESIST, target, null);
    double swordResist = 100. - player.calcStat(Stats.SWORD_WPN_VULNERABILITY, target, null);
    double dualResist = 100. - player.calcStat(Stats.DUAL_WPN_VULNERABILITY, target, null);
    double bluntResist = 100. - player.calcStat(Stats.BLUNT_WPN_VULNERABILITY, target, null);
    double daggerResist = 100. - player.calcStat(Stats.DAGGER_WPN_VULNERABILITY, target, null);
    double bowResist = 100. - player.calcStat(Stats.BOW_WPN_VULNERABILITY, target, null);
    double crossbowResist = 100. - player.calcStat(Stats.CROSSBOW_WPN_VULNERABILITY, target, null);
    double poleResist = 100. - player.calcStat(Stats.POLE_WPN_VULNERABILITY, target, null);
    double fistResist = 100. - player.calcStat(Stats.FIST_WPN_VULNERABILITY, target, null);
    double critChanceResist = 100. - player.calcStat(Stats.CRIT_CHANCE_RECEPTIVE, target, null);
    double critDamResistStatic = player.calcStat(Stats.CRIT_DAMAGE_RECEPTIVE, target, null);
    double critDamResist = 100.
            - (100 * (player.calcStat(Stats.CRIT_DAMAGE_RECEPTIVE, 1., target, null) - critDamResistStatic));
    String dialog = HtmCache.getInstance().getNotNull("command/whoami.htm", player);
    NumberFormat df = NumberFormat.getInstance(Locale.ENGLISH);
    df.setMaximumFractionDigits(1);
    df.setMinimumFractionDigits(1);
    StrBuilder sb = new StrBuilder(dialog);
    sb.replaceFirst("%hpRegen%", df.format(hpRegen));
    sb.replaceFirst("%cpRegen%", df.format(cpRegen));
    sb.replaceFirst("%mpRegen%", df.format(mpRegen));
    sb.replaceFirst("%hpDrain%", df.format(hpDrain));
    sb.replaceFirst("%mpDrain%", df.format(mpDrain));
    sb.replaceFirst("%hpGain%", df.format(hpGain));
    sb.replaceFirst("%mpGain%", df.format(mpGain));
    sb.replaceFirst("%critPerc%", df.format(critPerc));
    sb.replaceFirst("%critStatic%", df.format(critStatic));
    sb.replaceFirst("%mCritDmg%", df.format(mCritDmg));
    sb.replaceFirst("%blowRate%", df.format(blowRate));
    sb.replaceFirst("%shieldDef%", df.format(shieldDef));
    sb.replaceFirst("%shieldRate%", df.format(shieldRate));
    sb.replaceFirst("%xpRate%", df.format(xpRate));
    sb.replaceFirst("%spRate%", df.format(spRate));
    sb.replaceFirst("%dropRate%", df.format(dropRate));
    sb.replaceFirst("%adenaRate%", df.format(adenaRate));
    sb.replaceFirst("%spoilRate%", df.format(spoilRate));
    sb.replaceFirst("%fireResist%", df.format(fireResist));
    sb.replaceFirst("%windResist%", df.format(windResist));
    sb.replaceFirst("%waterResist%", df.format(waterResist));
    sb.replaceFirst("%earthResist%", df.format(earthResist));
    sb.replaceFirst("%holyResist%", df.format(holyResist));
    sb.replaceFirst("%darkResist%", df.format(unholyResist));
    sb.replaceFirst("%bleedPower%", df.format(bleedPower));
    sb.replaceFirst("%bleedResist%", df.format(bleedResist));
    sb.replaceFirst("%poisonPower%", df.format(poisonPower));
    sb.replaceFirst("%poisonResist%", df.format(poisonResist));
    sb.replaceFirst("%stunPower%", df.format(stunPower));
    sb.replaceFirst("%stunResist%", df.format(stunResist));
    sb.replaceFirst("%rootPower%", df.format(rootPower));
    sb.replaceFirst("%rootResist%", df.format(rootResist));
    sb.replaceFirst("%sleepPower%", df.format(sleepPower));
    sb.replaceFirst("%sleepResist%", df.format(sleepResist));
    sb.replaceFirst("%paralyzePower%", df.format(paralyzePower));
    sb.replaceFirst("%paralyzeResist%", df.format(paralyzeResist));
    sb.replaceFirst("%mentalPower%", df.format(mentalPower));
    sb.replaceFirst("%mentalResist%", df.format(mentalResist));
    sb.replaceFirst("%debuffPower%", df.format(debuffPower));
    sb.replaceFirst("%debuffResist%", df.format(debuffResist));
    sb.replaceFirst("%cancelPower%", df.format(cancelPower));
    sb.replaceFirst("%cancelResist%", df.format(cancelResist));
    sb.replaceFirst("%swordResist%", df.format(swordResist));
    sb.replaceFirst("%dualResist%", df.format(dualResist));
    sb.replaceFirst("%bluntResist%", df.format(bluntResist));
    sb.replaceFirst("%daggerResist%", df.format(daggerResist));
    sb.replaceFirst("%bowResist%", df.format(bowResist));
    sb.replaceFirst("%crossbowResist%", df.format(crossbowResist));
    sb.replaceFirst("%fistResist%", df.format(fistResist));
    sb.replaceFirst("%poleResist%", df.format(poleResist));
    sb.replaceFirst("%critChanceResist%", df.format(critChanceResist));
    sb.replaceFirst("%critDamResist%", df.format(critDamResist));
    NpcHtmlMessage msg = new NpcHtmlMessage(0);
    msg.setHtml(Strings.bbParse(sb.toString()));
    player.sendPacket(msg);
    return true;
}

From source file:de.vandermeer.skb.interfaces.transformers.textformat.Text_To_WrappedFormat.java

@Override
default Pair<ArrayList<String>, ArrayList<String>> transform(String input) {
    Validate.notBlank(input);//w w  w .  ja  v  a  2  s.co  m
    Validate.isTrue(this.getWidth() > 0);

    ArrayList<String> topList = new ArrayList<>();
    ArrayList<String> bottomList = new ArrayList<>();

    //an emergency break, counting loops to avoid endless loops
    int count;

    String text = StringUtils.replacePattern(input, "\\r\\n|\\r|\\n", LINEBREAK);
    text = StringUtils.replace(text, "<br>", LINEBREAK);
    text = StringUtils.replace(text, "<br/>", LINEBREAK);

    StrBuilder sb = new StrBuilder(text);
    if (this.getTopSettings() != null) {
        //we have a top request, do that one first
        Validate.notNull(this.getTopSettings().getLeft());
        Validate.notNull(this.getTopSettings().getRight());
        Validate.isTrue(this.getTopSettings().getLeft() > 0);
        Validate.isTrue(this.getTopSettings().getRight() > 0);

        int topLines = this.getTopSettings().getLeft();
        int topWidth = this.getTopSettings().getRight();
        count = 0;

        while (sb.size() > 0 && topLines > 0 && count++ < 200) {
            if (sb.startsWith(LINEBREAK)) {
                sb.replaceFirst(LINEBREAK, "");
            }
            String s = null;
            boolean wln = false;
            if (sb.indexOf(LINEBREAK) > 0) {
                s = sb.substring(0, sb.indexOf(LINEBREAK));
                wln = true;
                //sb.replace(0, sb.indexOf(LINEBREAK) + LINEBREAK.length(), "");
            } else {
                s = sb.toString();
                //sb.clear();
            }
            String wrap = WordUtils.wrap(s, topWidth, LINEBREAK, true);
            StrTokenizer tok = new StrTokenizer(wrap, LINEBREAK).setIgnoreEmptyTokens(false);
            String[] ar = tok.getTokenArray();
            if (ar.length <= topLines) {
                //all lines done, cleanup
                for (String str : ar) {
                    topList.add(str.trim());
                }
                if (wln == true) {
                    //if we had a conditional linebreak there might be more text, remove the line we processed
                    sb.replace(0, sb.indexOf(LINEBREAK) + LINEBREAK.length(), "");
                } else {
                    //no conditional line break, clean builder
                    sb.clear();
                }
                topLines = 0;
            } else {
                //we have more lines than we need, so remove the text we have from the builder and copy processed lines
                StrBuilder replace = new StrBuilder();
                for (int i = 0; i < topLines; i++) {
                    topList.add(ar[i].trim());
                    replace.appendSeparator(' ').append(ar[i]);
                }
                if (wln == true) {
                    replace.append(LINEBREAK);
                }
                sb.replaceFirst(replace.toString(), "");
                topLines = 0;
            }
        }
    }

    //no top, simple wrapping with recognition of conditional line breaks
    count = 0;
    while (sb.size() > 0 && count++ < 200) {
        if (sb.startsWith(LINEBREAK)) {
            sb.replaceFirst(LINEBREAK, "");
        }
        String s = null;
        if (sb.indexOf(LINEBREAK) > 0) {
            s = sb.substring(0, sb.indexOf(LINEBREAK));
            sb.replace(0, sb.indexOf(LINEBREAK) + LINEBREAK.length(), "");
        } else {
            s = sb.toString();
            sb.clear();
        }
        s = WordUtils.wrap(s, this.getWidth(), LINEBREAK, true);
        StrTokenizer tok = new StrTokenizer(s, LINEBREAK).setIgnoreEmptyTokens(false);
        for (String str : tok.getTokenArray()) {
            bottomList.add(str.trim());
        }
    }

    return Pair.of(topList, bottomList);
}