How to write code without modifying existing code.

Support for the latest build of L2J Server, get help here with installations, upgrades, problems.
Do not post bugs reports here, use viewforum.php?f=77 instead.
There is no support for other server builds than the official provided by l2jserver.com
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
kotkot90
Posts: 30
Joined: Sat Jun 19, 2010 2:01 pm

How to write code without modifying existing code.

Post 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
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: How to write code without modifying existing code.

Post 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.
Image
User avatar
kotkot90
Posts: 30
Joined: Sat Jun 19, 2010 2:01 pm

Re: How to write code without modifying existing code.

Post 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.
Hyrelius
Posts: 257
Joined: Thu Dec 16, 2010 5:16 am

Re: How to write code without modifying existing code.

Post 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.
Image
I don't mind helping - however: I only do so if I want to.
No support for other server packs than L2J.
User avatar
kotkot90
Posts: 30
Joined: Sat Jun 19, 2010 2:01 pm

Re: How to write code without modifying existing code.

Post by kotkot90 »

ok ty, I'll try it.
Post Reply