Explorar o código

BETA: Effect template loading cleanup.

Patch by: UnAfraid
Reviewed by: Zoey76
Zoey76 %!s(int64=12) %!d(string=hai) anos
pai
achega
6a82c415b7

+ 15 - 134
L2J_Server_BETA/java/com/l2jserver/gameserver/engines/DocumentBase.java

@@ -22,7 +22,6 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.NoSuchElementException;
 import java.util.StringTokenizer;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -36,7 +35,6 @@ import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 
 import com.l2jserver.gameserver.datatables.ItemTable;
-import com.l2jserver.gameserver.model.ChanceCondition;
 import com.l2jserver.gameserver.model.L2Object.InstanceType;
 import com.l2jserver.gameserver.model.StatsSet;
 import com.l2jserver.gameserver.model.base.PlayerState;
@@ -109,7 +107,6 @@ import com.l2jserver.gameserver.model.conditions.ConditionTargetWeight;
 import com.l2jserver.gameserver.model.conditions.ConditionUsingItemType;
 import com.l2jserver.gameserver.model.conditions.ConditionUsingSkill;
 import com.l2jserver.gameserver.model.conditions.ConditionWithSkill;
-import com.l2jserver.gameserver.model.effects.AbnormalEffect;
 import com.l2jserver.gameserver.model.effects.EffectTemplate;
 import com.l2jserver.gameserver.model.items.L2Item;
 import com.l2jserver.gameserver.model.items.type.L2ArmorType;
@@ -288,151 +285,35 @@ public abstract class DocumentBase
 	protected void attachEffect(Node n, Object template, Condition attachCond)
 	{
 		final NamedNodeMap attrs = n.getAttributes();
-		final String name = getValue(attrs.getNamedItem("name").getNodeValue().intern(), template);
-		
-		int ticks = 1;
-		if (attrs.getNamedItem("ticks") != null)
-		{
-			ticks = Integer.decode(getValue(attrs.getNamedItem("ticks").getNodeValue(), template));
-			if (ticks < 0)
-			{
-				ticks = Integer.MAX_VALUE; // -1 ticks means "infinite" time.
-			}
-		}
-		
-		int abnormalTime = 0;
-		if (attrs.getNamedItem("abnormalTime") != null)
-		{
-			abnormalTime = Integer.decode(getValue(attrs.getNamedItem("abnormalTime").getNodeValue(), template));
-		}
-		
-		boolean self = false;
-		if (attrs.getNamedItem("self") != null)
-		{
-			if (Integer.decode(getValue(attrs.getNamedItem("self").getNodeValue(), template)) == 1)
-			{
-				self = true;
-			}
-		}
-		
-		boolean icon = true;
-		if (attrs.getNamedItem("noicon") != null)
-		{
-			if (Integer.decode(getValue(attrs.getNamedItem("noicon").getNodeValue(), template)) == 1)
-			{
-				icon = false;
-			}
-		}
-		
-		Lambda lambda = getLambda(n, template);
-		Condition applayCond = parseCondition(n.getFirstChild(), template);
-		AbnormalEffect abnormalVisualEffect = AbnormalEffect.NULL;
-		if (attrs.getNamedItem("abnormalVisualEffect") != null)
-		{
-			String abn = attrs.getNamedItem("abnormalVisualEffect").getNodeValue();
-			abnormalVisualEffect = AbnormalEffect.getByName(abn);
-		}
-		AbnormalEffect[] special = null;
-		if (attrs.getNamedItem("special") != null)
-		{
-			final String[] specials = attrs.getNamedItem("special").getNodeValue().split(",");
-			special = new AbnormalEffect[specials.length];
-			for (int s = 0; s < specials.length; s++)
-			{
-				special[s] = AbnormalEffect.getByName(specials[s]);
-			}
-		}
-		AbnormalEffect event = AbnormalEffect.NULL;
-		if (attrs.getNamedItem("event") != null)
-		{
-			String spc = attrs.getNamedItem("event").getNodeValue();
-			event = AbnormalEffect.getByName(spc);
-		}
-		
-		double effectPower = -1;
-		if (attrs.getNamedItem("effectPower") != null)
-		{
-			effectPower = Double.parseDouble(getValue(attrs.getNamedItem("effectPower").getNodeValue(), template));
-		}
-		
-		final boolean isChanceSkillTrigger = name.equals("ChanceSkillTrigger");
-		int trigId = 0;
-		if (attrs.getNamedItem("triggeredId") != null)
-		{
-			trigId = Integer.parseInt(getValue(attrs.getNamedItem("triggeredId").getNodeValue(), template));
-		}
-		else if (isChanceSkillTrigger)
-		{
-			throw new NoSuchElementException(name + " requires triggerId");
-		}
-		
-		int trigLvl = 1;
-		if (attrs.getNamedItem("triggeredLevel") != null)
-		{
-			trigLvl = Integer.parseInt(getValue(attrs.getNamedItem("triggeredLevel").getNodeValue(), template));
-		}
-		
-		String chanceCond = null;
-		if (attrs.getNamedItem("chanceType") != null)
-		{
-			chanceCond = getValue(attrs.getNamedItem("chanceType").getNodeValue(), template);
-		}
-		else if (isChanceSkillTrigger)
-		{
-			throw new NoSuchElementException(name + " requires chanceType");
-		}
-		
-		int activationChance = -1;
-		if (attrs.getNamedItem("activationChance") != null)
-		{
-			activationChance = Integer.parseInt(getValue(attrs.getNamedItem("activationChance").getNodeValue(), template));
-		}
-		int activationMinDamage = -1;
-		if (attrs.getNamedItem("activationMinDamage") != null)
-		{
-			activationMinDamage = Integer.parseInt(getValue(attrs.getNamedItem("activationMinDamage").getNodeValue(), template));
-		}
-		String activationElements = null;
-		if (attrs.getNamedItem("activationElements") != null)
-		{
-			activationElements = getValue(attrs.getNamedItem("activationElements").getNodeValue(), template);
-		}
-		String activationSkills = null;
-		if (attrs.getNamedItem("activationSkills") != null)
-		{
-			activationSkills = getValue(attrs.getNamedItem("activationSkills").getNodeValue(), template);
-		}
-		boolean pvpOnly = false;
-		if (attrs.getNamedItem("pvpChanceOnly") != null)
-		{
-			pvpOnly = Boolean.parseBoolean(getValue(attrs.getNamedItem("pvpChanceOnly").getNodeValue(), template));
-		}
-		
-		ChanceCondition chance = ChanceCondition.parse(chanceCond, activationChance, activationMinDamage, activationElements, activationSkills, pvpOnly);
-		if ((chance == null) && isChanceSkillTrigger)
+		final StatsSet set = new StatsSet();
+		for (int i = 0; i < attrs.getLength(); i++)
 		{
-			throw new NoSuchElementException("Invalid chance condition: " + chanceCond + " " + activationChance);
+			Node att = attrs.item(i);
+			set.set(att.getNodeName(), getValue(att.getNodeValue(), template));
 		}
 		
-		final EffectTemplate lt = new EffectTemplate(attachCond, applayCond, name, lambda, ticks, abnormalTime, abnormalVisualEffect, special, event, icon, effectPower, trigId, trigLvl, chance);
-		parseTemplate(n, lt);
+		final Lambda lambda = getLambda(n, template);
+		final Condition applayCond = parseCondition(n.getFirstChild(), template);
+		final EffectTemplate effectTemplate = new EffectTemplate(attachCond, applayCond, lambda, set);
+		parseTemplate(n, effectTemplate);
 		if (template instanceof L2Item)
 		{
-			((L2Item) template).attach(lt);
+			((L2Item) template).attach(effectTemplate);
 		}
 		else if (template instanceof L2Skill)
 		{
-			if (self)
+			final L2Skill sk = (L2Skill) template;
+			if (set.getInteger("self", 0) == 1)
 			{
-				((L2Skill) template).attachSelf(lt);
+				sk.attachSelf(effectTemplate);
 			}
-			else if (((L2Skill) template).isPassive())
+			else if (sk.isPassive())
 			{
-				((L2Skill) template).attachPassive(lt);
+				sk.attachPassive(effectTemplate);
 			}
 			else
 			{
-				((L2Skill) template).attach(lt);
+				sk.attach(effectTemplate);
 			}
 		}
 	}

+ 5 - 1
L2J_Server_BETA/java/com/l2jserver/gameserver/model/effects/AbnormalEffect.java

@@ -19,6 +19,8 @@
 package com.l2jserver.gameserver.model.effects;
 
 import java.util.NoSuchElementException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * @author DrHouse
@@ -87,6 +89,7 @@ public enum AbnormalEffect
 	E_VESPER_3("vesper3", 0x000080),
 	HUNTING_BONUS("hunting_bonus", 0x80000);
 	
+	private static final Logger _log = Logger.getLogger(AbnormalEffect.class.getName());
 	private final int _mask;
 	private final String _name;
 	
@@ -120,6 +123,7 @@ public enum AbnormalEffect
 				return eff;
 			}
 		}
-		throw new NoSuchElementException(AbnormalEffect.class.getSimpleName() + ": Abnormal effect not found for name: " + name + "!");
+		_log.log(Level.WARNING, AbnormalEffect.class.getSimpleName() + ": Abnormal effect not found for name: " + name + "!");
+		return NULL;
 	}
 }

+ 27 - 23
L2J_Server_BETA/java/com/l2jserver/gameserver/model/effects/EffectTemplate.java

@@ -27,6 +27,7 @@ import java.util.logging.Logger;
 
 import com.l2jserver.gameserver.handler.EffectHandler;
 import com.l2jserver.gameserver.model.ChanceCondition;
+import com.l2jserver.gameserver.model.StatsSet;
 import com.l2jserver.gameserver.model.conditions.Condition;
 import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
 import com.l2jserver.gameserver.model.skills.funcs.Lambda;
@@ -40,7 +41,7 @@ public class EffectTemplate
 {
 	private static final Logger _log = Logger.getLogger(EffectTemplate.class.getName());
 	
-	private final Class<?> _func;
+	private final Class<?> _handler;
 	private final Constructor<?> _constructor;
 	private final Condition _attachCond;
 	// private final Condition _applyCond; // TODO: Use or cleanup.
@@ -53,38 +54,42 @@ public class EffectTemplate
 	private final AbnormalEffect _eventEffect;
 	private List<FuncTemplate> _funcTemplates;
 	private final boolean _showIcon;
-	private final String _funcName;
+	private final String _name;
 	private final double _effectPower; // to handle chance
 	private final int _triggeredId;
 	private final int _triggeredLevel;
 	private final ChanceCondition _chanceCondition;
 	
-	public EffectTemplate(Condition attachCond, Condition applyCond, String func, Lambda lambda, int totalTickCount, int abnormalTime, AbnormalEffect abnormalEffect, AbnormalEffect[] specialEffect, AbnormalEffect eventEffect, boolean showIcon, double power, int trigId, int trigLvl, ChanceCondition chanceCond)
+	public EffectTemplate(Condition attachCond, Condition applyCond, Lambda lambda, StatsSet set)
 	{
 		_attachCond = attachCond;
 		// _applyCond = applyCond;
 		_lambda = lambda;
-		_totalTickCount = totalTickCount;
-		_customAbnormalTime = abnormalTime;
-		_abnormalEffect = abnormalEffect;
-		_specialEffect = specialEffect;
-		_eventEffect = eventEffect;
-		_showIcon = showIcon;
-		_effectPower = power;
-		_triggeredId = trigId;
-		_triggeredLevel = trigLvl;
-		_chanceCondition = chanceCond;
-		_funcName = func;
-		_func = EffectHandler.getInstance().getHandler(func);
-		if (_func == null)
+		_name = set.getString("name");
+		_totalTickCount = set.getInteger("ticks", 1);
+		_customAbnormalTime = set.getInteger("abnormalTime", 0);
+		_abnormalEffect = set.getEnum("abnormalVisualEffect", AbnormalEffect.class, AbnormalEffect.NULL);
+		final String[] specialEffects = set.getString("special", "").split(",");
+		_specialEffect = new AbnormalEffect[specialEffects.length];
+		for (int i = 0; i < specialEffects.length; i++)
 		{
-			_log.warning(getClass().getSimpleName() + ": Requested Unexistent effect handler: " + func);
-			throw new RuntimeException();
+			_specialEffect[i] = AbnormalEffect.getByName(specialEffects[i]);
+		}
+		_eventEffect = set.getEnum("event", AbnormalEffect.class, AbnormalEffect.NULL);
+		_showIcon = set.getInteger("noicon", 0) == 0;
+		_effectPower = set.getDouble("effectPower", -1);
+		_triggeredId = set.getInteger("triggeredId", 0);
+		_triggeredLevel = set.getInteger("triggeredLevel", 1);
+		_chanceCondition = ChanceCondition.parse(set.getString("chanceType", null), set.getInteger("activationChance", -1), set.getInteger("activationMinDamage", -1), set.getString("activationElements", null), set.getString("activationSkills", null), set.getBool("pvpChanceOnly", false));
+		_handler = EffectHandler.getInstance().getHandler(_name);
+		if (_handler == null)
+		{
+			throw new RuntimeException(getClass().getSimpleName() + ": Requested unexistent effect handler: " + _name);
 		}
 		
 		try
 		{
-			_constructor = _func.getConstructor(Env.class, EffectTemplate.class);
+			_constructor = _handler.getConstructor(Env.class, EffectTemplate.class);
 		}
 		catch (NoSuchMethodException e)
 		{
@@ -106,8 +111,7 @@ public class EffectTemplate
 		
 		try
 		{
-			L2Effect effect = (L2Effect) _constructor.newInstance(env, this);
-			return effect;
+			return (L2Effect) _constructor.newInstance(env, this);
 		}
 		catch (IllegalAccessException | InstantiationException e)
 		{
@@ -116,7 +120,7 @@ public class EffectTemplate
 		}
 		catch (InvocationTargetException e)
 		{
-			_log.log(Level.WARNING, "Error creating new instance of Class " + _func + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
+			_log.log(Level.WARNING, "Error creating new instance of Class " + _handler + " Exception was: " + e.getTargetException().getMessage(), e.getTargetException());
 			return null;
 		}
 	}
@@ -129,7 +133,7 @@ public class EffectTemplate
 	 */
 	public L2Effect getStolenEffect(Env env, L2Effect stolen)
 	{
-		Class<?> func = EffectHandler.getInstance().getHandler(_funcName);
+		Class<?> func = EffectHandler.getInstance().getHandler(_name);
 		if (func == null)
 		{
 			throw new RuntimeException();