Jump to content

Question

Posted (edited)

Hi

 

I have made a very cool system to hold condiitons(for item usage, skill usage and other things) that makes it very easy to add next condition with minimum amount of code required, but using reflection is required in order to make it work. I am wondering what would be a nice way to do it with lambda, which would increase performance speed drastically.

Quote

package l2.gameserver.stats.condition.impl;

import l2.gameserver.model.Creature;
import l2.gameserver.model.Skill;
import l2.gameserver.model.Zone;
import l2.gameserver.stats.condition.CreatureConditionGroup;
import l2.gameserver.stats.condition.param.IntParam;
import l2.gameserver.stats.condition.param.NullableParam;
import l2.gameserver.stats.condition.param.Param;
import l2.gameserver.stats.condition.param.StringParam;

public class CreatureConditions implements CreatureConditionGroup<Creature>
{
   @Override
   public Class<Creature> getActorType()
   {
      return Creature.class;
   }
   
   @Override
   public boolean testActor(Creature actor)
   {
      return true;
   }
   
   
   
   public static boolean level(Creature actor,
                        @Param(name = "min") int min,
                        @Param(name = "max") int max)
   {
      int level = actor.getLevel();
      if(min > level)
         return false;
      if(max < level)
         return false;
      return true;
   }
   
   public static boolean dead(Creature actor)
   {
      return actor.isDead();
   }
   
   public static boolean inCombat(Creature actor)
   {
      return actor.isInCombat();
   }
   
   public static boolean hasSkill(Creature actor,
                        @Param(name = "id") int id,
                        @IntParam(name = "level", def = -1) int level)
   {
      Skill skill = actor.getKnownSkill(id);
      if(skill == null)
         return false;
      return skill.getLevel() >= level;
   }
   
   public static boolean zone(Creature actor, 
                        @StringParam(name = "name", def = "") String zone, 
                        @NullableParam(name = "type") Zone.ZoneType type)
   {
      if(!zone.isEmpty())
      {
         if(!actor.isInZone(zone))
            return false;
      }
      if(type != null)
      {
         if(!actor.isInZone(type))
            return false;
      }
      return true;
   }
}

 

This is one of the classes holding condition. For example adding zone method is the only thing required to be done in source in order to have

<zone name="zone_name_here" type="peace_zone/>

condition be added to xmls

 

That's how I run the method:

Quote

package l2.gameserver.stats.condition;

import l2.gameserver.model.Creature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public final class ConditionInvoker
{
   private static final Logger _log = LoggerFactory.getLogger(ConditionInvoker.class);
   
   private ConditionInvoker()
   {}
   
   public static boolean test(String conditionName, Creature actor, List<Object> parameters)
   {
      Condition condition = ConditionsHolder.getInstance().getCondition(conditionName, actor != null);
      Method method = condition.getMethod();
      try
      {
         return (boolean) method.invoke(null, createParameters(actor, condition, parameters));
      }
      catch(IllegalAccessException | InvocationTargetException e)
      {
         _log.error("Error while invoking method \"" + conditionName + "\"", e);
         return false;
      }
   }
   
   private static Object[] createParameters(Creature actor, Condition condition, List<Object> parameters)
   {
      int actorSize = condition.isActorRequired() ? 1 : 0;
      Object[] result = new Object[parameters.size() + actorSize];
      if(actorSize > 0)
         result[0] = actor;
      
      for(int i = 0; i < parameters.size();i ++)
      {
         result[i + actorSize] = parameters.get(i);
      }
      
      return result;
   }
}

 

ConditionsHolder class is holding all methods, their parameters etc. The problem is that reflections are slow, so I am looking for the best way to make it with lambdas.

 

Any ideas?

Edited by vampir

11 answers to this question

Recommended Posts

  • 0
Posted

Something like this ?

 

package drake.aepvp.model.enums;

import drake.aepvp.drivers.PlayerDriver;
import drake.aepvp.model.interfaces.Getter;

public enum PlayerParser
{
	//event
	event_Kills(PlayerDriver::getEventKills),
	event_Deaths(PlayerDriver::getEventDeaths),
	event_Flags(PlayerDriver::getEventFlags),
	event_Spree(PlayerDriver::getEventSpree),
	event_Top(PlayerDriver::getEventPosition),
	event_AFK(PlayerDriver::getEventAFKSeconds),
	event_DamageDealt(PlayerDriver::getEventDamageDealt),
	event_DamageTaken(PlayerDriver::getEventHealingDone),
	event_HealingDone(PlayerDriver::getEventHealingDone),
	event_FastRegister(PlayerDriver::getEventRegistrationPosition),
	
	player_PvP(PlayerDriver::getPlayerPvPs),
	player_PK(PlayerDriver::getPlayerPKs),
	player_LvL(PlayerDriver::getPlayerLevel),
	player_Fame(PlayerDriver::getPlayerFame),
	player_Sex(PlayerDriver::getPlayerSex),
	player_Race(PlayerDriver::getPlayerRace),
	
	//clan
	clan_OnlineCount(PlayerDriver::getClanOnlineCount),
	clan_Rep(PlayerDriver::getClanReputation),
	clan_LvL(PlayerDriver::getClanLevel),
	clan_Size(PlayerDriver::getClanSize),
	
	//party
	party_Size(PlayerDriver::getPartySize),
	party_Healers(PlayerDriver::getPartyHealersCount),
	party_TotalPvP(PlayerDriver::getPartyTotalPvPs),
	party_TotalPK(PlayerDriver::getPartyTotalPKs);
	
	private final Getter<PlayerDriver> _eventGetter;

	private PlayerParser(Getter<PlayerDriver> eventGetter)
	{
		_eventGetter = eventGetter;
	}

	public int getValue(PlayerDriver playerData)
	{
		return _eventGetter.getValue(playerData);
	}
}

 

package drake.aepvp.model.interfaces;

import java.util.LinkedHashSet;
import java.util.Set;

public interface Getter<E>
{
	public LinkedHashSet<Getter<?>> getters = new LinkedHashSet<>();
	
	int getValue(E data);
	
	static void add(Getter<?> getter)
	{
		getters.add(getter);
	}
	
	public static Set<Getter<?>> values()
	{
		return getters;
	}
}

 

XML parser:

 

								if ("require".equalsIgnoreCase(n6.getNodeName()))
								{
									Requirement firstReq = null;
									for (PlayerParser det : PlayerParser.values())
									{
										final String[] nodeName = det.toString().split("_");
										final Node detNode = nnm3.getNamedItem(nodeName[0] + nodeName[1]);
										final Node noMsgNode = nnm3.getNamedItem("msg");
										final String noMsg = noMsgNode == null ? null : noMsgNode.getNodeValue();
										if (detNode != null)
										{
											final Requirement req = new Requirement(det, detNode.getNodeValue(), noMsg);
											if (firstReq == null)
												firstReq = req;
											else
												firstReq.attachReq(req);
										}
									}
									if (firstReq != null)
										reqs.add(firstReq);
								}

 

Something like that? Thats how I parse requirement conditions on my private event engine

  • 0
Posted (edited)

Yes I was thinking about that, but it has following drawbacks:

1. Each method would require position in enum

2. Each method will need to have 1 parameter which will be holder for all parameters, so for example level method will look like this:

Quote

public static boolean level(Creature actor, ParameterHolder parameters)
   {
      int level = actor.getLevel();
      if(parameters.getInteger("min") > level)
         return false;
      if(parameters.getInteger("max) < level)
         return false;
      return true;
   }

 

3. If I want to use this inside core(which doesn't have xml schema which blocks invalid parameter names), error will be shown only when method is executed

 

Any other thoughts?

Edited by vampir
  • 0
Posted

Yeah, I think some sort of annotation processing is the best way to do it. Thanks for the code

  • 0
Posted
33 minutes ago, ImBatman said:

You won't notice any difference. Sure reflection is way slower but the code must be really tough and heavy. In modern machines milliseconds is just too low to be noticed. We not use 1 core systems anymore. Most of PC's are using 4-8-12 cores e.t.c which make even reflection run in less than 1 ms no matter what.

 

But in case you want to explore a bit with lambdas download this jar: https://files.fm/u/z39ca69u

and use this:

 


Method method = MyClass.class.getDeclaredMethod("myStaticMethod", int.class, Integer.class); //Regular reflection call
Lambda lambda = LambdaFactory.create(method);  

 

 

Reflection has big overhead, the above code has big latency disadvantage, you should really benchmark it

  • 0
Posted

For my event engine I've ended up with something like this, I wanted something reflaction-like

 

Quote

package drake.aepvp.model.interfaces;

public interface IIdentifier
{
    public static enum BooleanIdentifier implements IIdentifier
    {
        WORLD__IS_CHAOS,
        WORLD__IS_STATIC_TELEPORT,
        
        HANDLE__PLAYER_PVP,
        HANDLE__PLAYER_RESPAWN,
        HANDLE__PLAYER_PARTY_INVITE,
        HANDLE__PLAYER_TO_VILLAGE,
        HANDLE__PLAYER_DEATH,
        HANDLE__PLAYER_NPC_INTERACT,
        HANDLE__PLAYER_CLAN_ALLY,
        
        PLAYER__ALLOW_SKILL,
        PLAYER__ALLOW_ITEM,
        PLAYER__ALLOW_PVP,
        PLAYER__ALLOW_DIE_PANEL,
        PLAYER__ALLOW_SEE_PLAYER,
        PLAYER__ALLOW_ESCAPE,
        PLAYER__ALLOW_TELEPORT,
        
        PLAYER__IS_IN_HEALZONE,
        PLAYER__IS_DISGUISED,
        PLAYER__USE_ITEM,
        PLAYER__IS_FREE_BUFFERHEAL;
    }
    
    public static enum ActionIdentifier implements IIdentifier
    {
        PLAYER__ON_RECONNECT,
        PLAYER__ON_EXIT,
        PLAYER__ON_WORLD_ENTER,
        PLAYER__ON_WORLD_EXIT,
        PLAYER__ON_TO_VILLAGE,
        PLAYER__ON_RESPAWN,
        PLAYER__ON_DEATH,
        PLAYER__ON_DAMAGE_DEALT,
        PLAYER__ON_HEALING_DONE,
        PLAYER__ON_REVIVE_MADE,
        
        PLAYER__REQUEST_TEAM_INFO,
        
        CHARACTER__ON_DIE;
    }
    
    public static enum IntIdentifier implements IIdentifier
    {
        PLAYER__GET_CLAN_CREST_ID,
        PLAYER__GET_CLAN_ID,
        PLAYER__GET_ALLY_CREST_ID,
        PLAYER__GET_ALLY_ID,
        PLAYER__GET_AGGRO_RADIUS,
        PLAYER__GET_PVP_FLAG,
        PLAYER__GET_NAME_COLOR,
        PLAYER__GET_RELATION;
    }
    
    public static enum StringIdentifier implements IIdentifier
    {
        PLAYER__GET_NAME,
        PLAYER__GET_TITLE;
    }
    
    public static enum FloatIdentifier implements IIdentifier
    {
        PLAYER__GET_HEALING_CHANGE,
        PLAYER__GET_DAMAGE_CHANGE;
    }
}
 

 

Quote

package drake.aepvp.model.controlers;

import static drake.aepvp.util.EventUtils.ret0;
import static drake.aepvp.util.EventUtils.ret1f;
import static drake.aepvp.util.EventUtils.retfalse;
import static drake.aepvp.util.EventUtils.rettrue;

import java.util.HashMap;

import drake.aepvp.model.InfoPackage;
import drake.aepvp.model.interfaces.IIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.ActionIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.BooleanIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.FloatIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.IntIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.StringIdentifier;
import drake.aepvp.model.interfaces.IInfoPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IActionPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IBooleanPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IFloatPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IIntPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IStringPackager;

public class PackageManager
{
    final HashMap<IIdentifier, IInfoPackager> activePackages = new HashMap<>();
    
    public PackageManager()
    {    

    }
    
    public static PackageManager withDefaultSettings()
    {
        return new PackageManager()
        {{    /** default world settings **/
            pack(BooleanIdentifier.WORLD__IS_CHAOS, retfalse);
            pack(BooleanIdentifier.WORLD__IS_STATIC_TELEPORT, rettrue);
            
            pack(BooleanIdentifier.HANDLE__PLAYER_DEATH, retfalse);
            pack(BooleanIdentifier.HANDLE__PLAYER_NPC_INTERACT, retfalse);
            pack(BooleanIdentifier.HANDLE__PLAYER_PARTY_INVITE, retfalse);
            pack(BooleanIdentifier.HANDLE__PLAYER_PVP, retfalse);
            pack(BooleanIdentifier.HANDLE__PLAYER_RESPAWN, retfalse);
            pack(BooleanIdentifier.HANDLE__PLAYER_TO_VILLAGE, retfalse);
            pack(BooleanIdentifier.HANDLE__PLAYER_CLAN_ALLY, retfalse);
            
            pack(BooleanIdentifier.PLAYER__ALLOW_DIE_PANEL, rettrue);
            pack(BooleanIdentifier.PLAYER__ALLOW_ESCAPE, rettrue);
            pack(BooleanIdentifier.PLAYER__ALLOW_ITEM, rettrue);
            pack(BooleanIdentifier.PLAYER__ALLOW_PVP, rettrue);
            pack(BooleanIdentifier.PLAYER__ALLOW_SEE_PLAYER, rettrue);
            pack(BooleanIdentifier.PLAYER__ALLOW_SKILL, rettrue);
            pack(BooleanIdentifier.PLAYER__ALLOW_TELEPORT, rettrue);
            
            pack(BooleanIdentifier.PLAYER__IS_DISGUISED, retfalse);
            pack(BooleanIdentifier.PLAYER__IS_FREE_BUFFERHEAL, rettrue);
            pack(BooleanIdentifier.PLAYER__IS_IN_HEALZONE, retfalse);
            
            
            pack(FloatIdentifier.PLAYER__GET_DAMAGE_CHANGE, ret1f);
            pack(FloatIdentifier.PLAYER__GET_HEALING_CHANGE, ret1f);
            
            pack(IntIdentifier.PLAYER__GET_AGGRO_RADIUS, ret0);
            pack(IntIdentifier.PLAYER__GET_PVP_FLAG, ret0);
        }};
    }
    
    public final boolean packs(IIdentifier identifier)
    {    return activePackages.containsKey(identifier);
    }
    
    public void pack(BooleanIdentifier identifier, IBooleanPackager booleanPackager)
    {    activePackages.put(identifier, booleanPackager);
    }
    
    public void pack(IntIdentifier identifier, IIntPackager intPackager)
    {    activePackages.put(identifier, intPackager);
    }
    
    public void pack(FloatIdentifier identifier, IFloatPackager floatPackager)
    {    activePackages.put(identifier, floatPackager);
    }    
    
    public void pack(StringIdentifier identifier, IStringPackager stringPackager)
    {    activePackages.put(identifier, stringPackager);
    }
    
    public void pack(ActionIdentifier identifier, IActionPackager actionPackager)
    {    activePackages.put(identifier, actionPackager);
    }
    
    public boolean get(final BooleanIdentifier funcId, InfoPackage infoPackage)
    {    return ((IBooleanPackager) activePackages.get(funcId)).unpack(infoPackage);
    }
    
    public int get(final IntIdentifier funcId, InfoPackage infoPackage)
    {    return ((IIntPackager) activePackages.get(funcId)).unpack(infoPackage);
    }
    
    public float get(final FloatIdentifier funcId, InfoPackage infoPackage)
    {    return ((IFloatPackager) activePackages.get(funcId)).unpack(infoPackage);
    }
    
    public String get(final StringIdentifier funcId, InfoPackage infoPackage)
    {    return ((IStringPackager) activePackages.get(funcId)).unpack(infoPackage);
    }
    
    public void get(final ActionIdentifier funcId, InfoPackage infoPackage)
    {    
        if (packs(funcId))
            ((IActionPackager) activePackages.get(funcId)).unpack(infoPackage);
    }
    
    public boolean isPacked(final ActionIdentifier funcId)
    {
        return activePackages.get(funcId) != null;
    }
}

 

 

Quote

package drake.aepvp.instance;


import drake.aepvp.model.InfoPackage;
import drake.aepvp.model.controlers.PackageManager;
import drake.aepvp.model.enums.WorldState;
import drake.aepvp.model.interfaces.IIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.ActionIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.BooleanIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.FloatIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.IntIdentifier;
import drake.aepvp.model.interfaces.IIdentifier.StringIdentifier;
import drake.aepvp.model.interfaces.IInfoPackager.IActionPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IBooleanPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IFloatPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IIntPackager;
import drake.aepvp.model.interfaces.IInfoPackager.IStringPackager;
import l2.ae.pvp.gameserver.model.L2Object;
import l2.ae.pvp.gameserver.model.actor.instance.L2PcInstance;

public abstract interface IWorld
{
    public default String getId()
    {
        return toString();
    }
    
    public default WorldState getState()
    {
        return WorldState.INACTIVE;
    }
    
    public default void onAssign(L2Object object)
    {
        final L2PcInstance actor = object.getActingPlayer();
        if (actor != null)
            onEnter(actor);
    }
    
    public default void onRemove(L2Object object)
    {
        final L2PcInstance actor = object.getActingPlayer();
        if (actor != null)
            onExit(actor);
    }
    
    public default void onEnter(final L2PcInstance player) {};
    public default void onExit(final L2PcInstance player) {};
    
    public default void inject(final L2PcInstance player) {};
    public default void eject(final L2PcInstance player) {};
    public default void spawn(final L2PcInstance player) {};
    
    public PackageManager getPackageManager();
    
    public default boolean handles(IIdentifier identifier)
    {    return getPackageManager().packs(identifier);
    }
    
    public default void pack(BooleanIdentifier identifier, IBooleanPackager booleanPackager)
    {     getPackageManager().pack(identifier, booleanPackager);
    }
    
    public default void pack(IntIdentifier identifier, IIntPackager intPackager)
    {    getPackageManager().pack(identifier, intPackager);
    }
    
    public default void pack(FloatIdentifier identifier, IFloatPackager floatPackager)
    {    getPackageManager().pack(identifier, floatPackager);
    }    
    
    public default void pack(StringIdentifier identifier, IStringPackager stringPackager)
    {    getPackageManager().pack(identifier, stringPackager);
    }
    
    public default void pack(ActionIdentifier identifier, IActionPackager actionPackager)
    {    getPackageManager().pack(identifier, actionPackager);
    }
    
    public default boolean get(final BooleanIdentifier funcId, InfoPackage infoPackage)
    {    return getPackageManager().get(funcId, infoPackage);
    }
    
    public default int get(final IntIdentifier funcId, InfoPackage infoPackage)
    {    return getPackageManager().get(funcId, infoPackage);
    }
    
    public default float get(final FloatIdentifier funcId, InfoPackage infoPackage)
    {    return getPackageManager().get(funcId, infoPackage);
    }
    
    public default String get(final StringIdentifier funcId, InfoPackage infoPackage)
    {    return getPackageManager().get(funcId, infoPackage);
    }
    
    public default void act(final ActionIdentifier funcId, InfoPackage infoPackage)
    {    getPackageManager().get(funcId, infoPackage);
    }

}

 

//child implementantion

Quote

    /** PACKAGES ***************************************************/
    {//feed the package manager
        pack(BooleanIdentifier.WORLD__IS_STATIC_TELEPORT, (pack) -> _template.isStaticTeleport());
        pack(BooleanIdentifier.WORLD__IS_CHAOS, this::p_worldIsChaos);
        pack(BooleanIdentifier.PLAYER__IS_DISGUISED, this::p_playerIsDisguised);
        pack(BooleanIdentifier.PLAYER__ALLOW_SKILL, this::p_playerAllowSkill);
        pack(BooleanIdentifier.PLAYER__ALLOW_ITEM,  this::p_playerAllowItem);
        
        pack(ActionIdentifier.CHARACTER__ON_DIE, this::p_characterOnDie);
    }
    
    private final boolean p_worldIsChaos(final InfoPackage infoPackage)
    {    return worldIsChaos(infoPackage.getPlayer());
    }
    
    private final boolean p_playerIsDisguised(final InfoPackage infoPackage)
    {    return playerIsDisguised(infoPackage.getPlayer(), infoPackage.getTargetPlayer());
    }
    
    private final boolean p_playerAllowSkill(final InfoPackage infoPackage)
    {    return playerAllowSkill(infoPackage.getPlayer(), infoPackage.getSkill(), infoPackage.getTarget());
    }

 

 

InfoPackage is pretty much your argument holder

  • 0
Posted
1 hour ago, xxdem said:

enum is really not mandatory, it can be whatever

Yeah, but methods still must be listed somewhere. There must be some collection of lambdas in order to make your method work.

 

Duplicates are unwanted in this case

  • 0
Posted
35 minutes ago, vampir said:

Yeah, but methods still must be listed somewhere. There must be some collection of lambdas in order to make your method work.

 

Duplicates are unwanted in this case

 

check the second one, its simmilar, unknown values will go through

  • Upvote 1
  • 0
Posted
17 minutes ago, ImBatman said:

 

But xxdem don't you use reflection once to create the object? 

Also j.fla? You gonna end up listening to k-pop. Please dont.

 

what's wrong with k-pop? orange caramel ftw

  • 0
Posted (edited)
9 hours ago, xxdem said:

 

check the second one, its simmilar, unknown values will go through

Yeah, I don't like it much though tbh. If reflection is used only during game server start then it is no problem. I will use annotation processor to generate code during compilation that will look like this:

 

Quote

public static boolean test(String conditionName, Creature actor, Object[] parameters)
{
   if(conditionName.equals("level"))
        return CreatureConditions.level(actor, (int) parameters[0], (int) parameters[1]);
   //other conditions here
}

 

 

Edited by vampir

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...