Page 1 of 1

How to write code without modifying existing code.

Posted: Mon Oct 21, 2013 1:03 am
by kotkot90
My question doesn't concern any revisions, it's more about java and coding, so I'll not include it.

I want to know what is the best way to write my own code without modifying existing code,
or if modified, I want it in a seperate file, so I can find it quickly, or when an update occurs, I can easily patch
the files with the updated ones without harming my code.

I thought something about function overriding. For example whenever I need a change in a function or some variable declarations, I will copy them in a new file and override them, and add my own code in it. I'm a little confused of how these methods work, and what is the best one, that's why I'm posting here.

Here's some code from the file Formulas.java and function calcSkillSuccess()

Code: Select all

public static boolean calcSkillSuccess(L2Character attacker, L2Character target, L2Skill skill, byte shld, boolean ss, boolean sps, boolean bss)    {        /*if (skill.getPower() == -1)        {            return true;        }                final boolean isPvP = (attacker instanceof L2Playable) && (target instanceof L2Playable);        final boolean isPvE = (attacker instanceof L2Playable) && (target instanceof L2Attackable);        if (skill.ignoreResists())        {            if (attacker.isDebug())            {                attacker.sendDebugMessage(skill.getName() + " ignoring resists");            }                        return (Rnd.get(100) < skill.getPower(isPvP, isPvE));        }        else if ((target.calcStat(Stats.DEBUFF_IMMUNITY, 0, null, skill) > 0) && skill.isDebuff())        {            return false;        }                if (shld == SHIELD_DEFENSE_PERFECT_BLOCK) // perfect block        {            if (attacker.isDebug())            {                attacker.sendDebugMessage(skill.getName() + " blocked by shield");            }                        return false;        }                int value = (int) skill.getPower(isPvP, isPvE);        double statModifier = calcSkillStatModifier(skill, target);                // Calculate BaseRate.        int rate = (int) (value * statModifier);                // Add Matk/Mdef Bonus        double mAtkModifier = 0;        int ssModifier = 0;        if (skill.isMagic())        {            mAtkModifier = target.getMDef(target, skill);            if (shld == SHIELD_DEFENSE_SUCCEED)            {                mAtkModifier += target.getShldDef();            }                        // Add Bonus for Sps/SS            if (bss)            {                ssModifier = 4;            }            else if (sps)            {                ssModifier = 2;            }            else            {                ssModifier = 1;            }                        mAtkModifier = (14 * Math.sqrt(ssModifier * attacker.getMAtk(target, skill))) / mAtkModifier;                        rate = (int) (rate * mAtkModifier);        }                // Resists        double vulnModifier = calcSkillVulnerability(attacker, target, skill);        double profModifier = calcSkillProficiency(skill, attacker, target);        double res = vulnModifier + profModifier;        double resMod = 1;        if (res != 0)        {            if (res < 0)            {                resMod = 1 - (0.075 * res);                resMod = 1 / resMod;            }            else            {                resMod = 1 + (0.02 * res);            }                        rate *= resMod;        }                int elementModifier = calcElementModifier(attacker, target, skill);        rate += elementModifier;                // lvl modifier.        int deltamod = calcLvlDependModifier(attacker, target, skill);        rate += deltamod;                if (rate > skill.getMaxChance())        {            rate = skill.getMaxChance();        }        else if (rate < skill.getMinChance())        {            rate = skill.getMinChance();        }                if (attacker.isDebug() || Config.DEVELOPER)        {            final StringBuilder stat = new StringBuilder(100);            StringUtil.append(stat, skill.getName(), " type:", skill.getSkillType().toString(), " power:", String.valueOf(value), " stat:", String.format("%1.2f", statModifier), " res:", String.format("%1.2f", resMod), "(", String.format("%1.2f", profModifier), "/", String.format("%1.2f", vulnModifier), ") elem:", String.valueOf(elementModifier), " mAtk:", String.format("%1.2f", mAtkModifier), " ss:", String.valueOf(ssModifier), " lvl:", String.valueOf(deltamod), " total:", String.valueOf(rate));            final String result = stat.toString();            if (attacker.isDebug())            {                attacker.sendDebugMessage(result);            }            if (Config.DEVELOPER)            {                _log.info(result);            }        }        return (Rnd.get(100) < rate);*/        return true;    }
What I simply did here, is that I commented out the whole function, and just returned true at the end, because I wanted all my casted skills to be successful and not missed, but I want a function like that

Code: Select all

public static boolean calcSkillSuccess(L2Character attacker, L2Character target, L2Skill skill, byte shld, boolean ss, boolean sps, boolean bss){     return true;}
so I don't have that bunch of lines in front of me, and if an update occurs, only the original function to be updated.

I hope I was clear.
Any help would be appreciated :D

Re: How to write code without modifying existing code.

Posted: Mon Oct 21, 2013 5:36 am
by UnAfraid
Well use our listeners to do your customs in datapack in separate classes if u're missing a listener just create it.
If it is a good one u can share it and it could eventually get committed.

Re: How to write code without modifying existing code.

Posted: Mon Oct 21, 2013 2:56 pm
by kotkot90
Hmm, I'm not sure about how to use listeners, but I'm pretty sure that it has to do with the file in "data\scripts\custom\Listeners\Listeners.java". What can I do with them? and how can that help me override a function inside the core in a seperate class? That would be great cause I won't have to recompile the whole project every time I need a change in my code. I need an example of how to do that.

Re: How to write code without modifying existing code.

Posted: Tue Oct 22, 2013 9:29 am
by Hyrelius
You could create a separate L2Script and register it to an available Listener - e.g. using addLoginLogoutNotify. Then you'd override the onPlayerLogin (or onPlayerLogout) method and make it so your script reacts at whenever a player logs in or out (or both). The only other thing you'd have to do is make sure that your script is loaded.

Such listeners are available for a wide range of actions. You could make your script respond to player login, item changes, equip-state changes, stat changes, environment changes etc. If you find something that you would like your script to respond to that is not yet available as a listener, you could try to make one yourself and offer it here. This does involve changing existing classes, but the staff might agree to commit it.

Re: How to write code without modifying existing code.

Posted: Tue Oct 22, 2013 4:40 pm
by kotkot90
ok ty, I'll try it.