Prechádzať zdrojové kódy

BETA: Standardizing ExperienceTable and HitConditionBonus:
* Added JavaDocs to ExperienceTable.
* Using proper Singleton pattern in HitConditionBonus.
* Added JavaDocs to HitConditionBonus.
* Cleanup and format.

Zoey76 13 rokov pred
rodič
commit
be9f7dfb7e

+ 2 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/GameServer.java

@@ -53,6 +53,7 @@ import com.l2jserver.gameserver.datatables.FishTable;
 import com.l2jserver.gameserver.datatables.HelperBuffTable;
 import com.l2jserver.gameserver.datatables.HennaData;
 import com.l2jserver.gameserver.datatables.HerbDropTable;
+import com.l2jserver.gameserver.datatables.HitConditionBonus;
 import com.l2jserver.gameserver.datatables.InitialEquipmentData;
 import com.l2jserver.gameserver.datatables.ItemTable;
 import com.l2jserver.gameserver.datatables.LevelUpData;
@@ -234,6 +235,7 @@ public class GameServer
 		ClassListData.getInstance();
 		InitialEquipmentData.getInstance();
 		ExperienceTable.getInstance();
+		HitConditionBonus.getInstance();
 		CharTemplateTable.getInstance();
 		CharNameTable.getInstance();
 		LevelUpData.getInstance();

+ 42 - 60
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/ExperienceTable.java

@@ -15,108 +15,90 @@
 package com.l2jserver.gameserver.datatables;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 
 import com.l2jserver.Config;
+import com.l2jserver.gameserver.engines.DocumentParser;
 
 /**
+ * This class holds the Experience points for each level for players and pets.
  * @author mrTJO
  */
-public class ExperienceTable
+public final class ExperienceTable extends DocumentParser
 {
-	private static Logger _log = Logger.getLogger(ExperienceTable.class.getName());
+	private final Map<Integer, Long> _expTable = new HashMap<>();
+	
 	private byte MAX_LEVEL;
 	private byte MAX_PET_LEVEL;
 	
-	private Map<Integer, Long> _expTable;
-	
-	public static ExperienceTable getInstance()
-	{
-		return SingletonHolder._instance;
-	}
-	
 	private ExperienceTable()
 	{
-		loadTable();
+		_expTable.clear();
+		final Document doc = parseFile(new File(Config.DATAPACK_ROOT, "data/stats/experience.xml"));
+		if (doc != null)
+		{
+			parseDocument(doc);
+		}
+		_log.info(getClass().getSimpleName() + ": Loaded " + _expTable.size() + " levels.");
+		_log.info(getClass().getSimpleName() + ": Max Player Level is: " + (MAX_LEVEL - 1));
+		_log.info(getClass().getSimpleName() + ": Max Pet Level is: " + (MAX_PET_LEVEL - 1));
 	}
 	
-	private void loadTable()
+	@Override
+	protected void parseDocument(Document doc)
 	{
-		File xml = new File(Config.DATAPACK_ROOT, "data/stats/experience.xml");
-		Document doc = null;
-		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-		factory.setValidating(false);
-		factory.setIgnoringComments(true);
-		if (xml.exists())
+		final Node table = doc.getFirstChild();
+		final NamedNodeMap tableAttr = table.getAttributes();
+		
+		MAX_LEVEL = (byte) (Byte.parseByte(tableAttr.getNamedItem("maxLevel").getNodeValue()) + 1);
+		MAX_PET_LEVEL = (byte) (Byte.parseByte(tableAttr.getNamedItem("maxPetLevel").getNodeValue()) + 1);
+		
+		NamedNodeMap attrs;
+		for (Node n = table.getFirstChild(); n != null; n = n.getNextSibling())
 		{
-			try
-			{
-				doc = factory.newDocumentBuilder().parse(xml);
-			}
-			catch (IOException e)
-			{
-				_log.log(Level.WARNING, "Could not read experience.xml table: " + e.getMessage(), e);
-				return;
-			}
-			catch (Exception e)
-			{
-				_log.log(Level.WARNING, "Could not parse experience.xml table: " + e.getMessage(), e);
-				return;
-			}
-			
-			Node table = doc.getFirstChild();
-			NamedNodeMap tableAttr = table.getAttributes();
-			
-			MAX_LEVEL = (byte)(Byte.parseByte(tableAttr.getNamedItem("maxLevel").getNodeValue())+1);
-			MAX_PET_LEVEL = (byte)(Byte.parseByte(tableAttr.getNamedItem("maxPetLevel").getNodeValue())+1);
-
-			_expTable = new HashMap<Integer, Long>(MAX_LEVEL+1);
-			
-			for (Node experience = table.getFirstChild(); experience != null; experience = experience.getNextSibling())
+			if ("experience".equals(n.getNodeName()))
 			{
-				if (experience.getNodeName().equals("experience"))
-				{
-					NamedNodeMap attrs = experience.getAttributes();
-					int level = Integer.parseInt(attrs.getNamedItem("level").getNodeValue());
-					long exp = Long.parseLong(attrs.getNamedItem("tolevel").getNodeValue());
-					
-					_expTable.put(level, exp);
-				}
+				attrs = n.getAttributes();
+				_expTable.put(parseInteger(attrs, "level"), parseLong(attrs, "tolevel"));
 			}
-			
-			_log.info("ExperienceTable: Loaded "+_expTable.size()+" levels");
-			_log.info("ExperienceTable: Max Player Level is: "+(MAX_LEVEL-1));
-			_log.info("ExperienceTable: Max Pet Level is: "+(MAX_PET_LEVEL-1));
 		}
-		else
-			_log.warning("ExperienceTable: experience.xml not found!");
 	}
 	
+	/**
+	 * @param level the level required.
+	 * @return the experience points required to reach the given level.
+	 */
 	public long getExpForLevel(int level)
 	{
 		return _expTable.get(level);
 	}
 	
+	/**
+	 * @return the maximum level acquirable by a player.
+	 */
 	public byte getMaxLevel()
 	{
 		return MAX_LEVEL;
 	}
 	
+	/**
+	 * @return the maximum level acquirable by a pet.
+	 */
 	public byte getMaxPetLevel()
 	{
 		return MAX_PET_LEVEL;
 	}
 	
+	public static ExperienceTable getInstance()
+	{
+		return SingletonHolder._instance;
+	}
+	
 	@SuppressWarnings("synthetic-access")
 	private static class SingletonHolder
 	{

+ 105 - 92
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/HitConditionBonus.java

@@ -15,124 +15,137 @@
 package com.l2jserver.gameserver.datatables;
 
 import java.io.File;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 
 import com.l2jserver.Config;
 import com.l2jserver.gameserver.GameTimeController;
+import com.l2jserver.gameserver.engines.DocumentParser;
 import com.l2jserver.gameserver.model.actor.L2Character;
 
 /**
- * 
+ * This class load, holds and calculates the hit condition bonuses.
  * @author Nik
- *
  */
-public class HitConditionBonus
+public final class HitConditionBonus extends DocumentParser
 {
-	protected static final Logger _log = Logger.getLogger(HitConditionBonus.class.getName());
+	private int frontBonus = 0;
+	private int sideBonus = 0;
+	private int backBonus = 0;
+	private int highBonus = 0;
+	private int lowBonus = 0;
+	private int darkBonus = 0;
+	private int rainBonus = 0;
 	
-	private static int frontBonus = 0;
-	private static int sideBonus = 0;
-	private static int backBonus = 0;
-	private static int highBonus = 0;
-	private static int lowBonus = 0;
-	private static int darkBonus = 0;
-	//private static int rainBonus = 0;
+	private HitConditionBonus()
+	{
+		final Document doc = parseFile(new File(Config.DATAPACK_ROOT, "data/stats/hitConditionBonus.xml"));
+		if (doc != null)
+		{
+			parseDocument(doc);
+		}
+		_log.info(getClass().getSimpleName() + ": Loaded Hit Condition bonuses.");
+		if (Config.DEBUG)
+		{
+			_log.info(getClass().getSimpleName() + ": Front bonus: " + frontBonus);
+			_log.info(getClass().getSimpleName() + ": Side bonus: " + sideBonus);
+			_log.info(getClass().getSimpleName() + ": Back bonus: " + backBonus);
+			_log.info(getClass().getSimpleName() + ": High bonus: " + highBonus);
+			_log.info(getClass().getSimpleName() + ": Low bonus: " + lowBonus);
+			_log.info(getClass().getSimpleName() + ": Dark bonus: " + darkBonus);
+			_log.info(getClass().getSimpleName() + ": Rain bonus: " + rainBonus);
+		}
+	}
 	
-	public static double getConditionBonus(L2Character attacker, L2Character target)
+	@Override
+	protected void parseDocument(Document doc)
+	{
+		final Node n = doc.getFirstChild();
+		NamedNodeMap attrs;
+		for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
+		{
+			attrs = d.getAttributes();
+			switch (d.getNodeName())
+			{
+				case "front":
+					frontBonus = parseInt(attrs, "val");
+					break;
+				case "side":
+					sideBonus = parseInt(attrs, "val");
+					break;
+				case "back":
+					backBonus = parseInt(attrs, "val");
+					break;
+				case "high":
+					highBonus = parseInt(attrs, "val");
+					break;
+				case "low":
+					lowBonus = parseInt(attrs, "val");
+					break;
+				case "dark":
+					darkBonus = parseInt(attrs, "val");
+					break;
+				case "rain":
+					rainBonus = parseInt(attrs, "val");
+					break;
+			}
+		}
+	}
+	
+	/**
+	 * @param attacker the attacking character.
+	 * @param target the attacked character.
+	 * @return the bonus of the attacker against the target.
+	 */
+	public double getConditionBonus(L2Character attacker, L2Character target)
 	{
 		double mod = 100;
 		// Get high or low bonus
-		if (attacker.getZ() - target.getZ() > 50)
-			mod += HitConditionBonus.highBonus;
-		else if (attacker.getZ() - target.getZ() < -50)
-			mod += HitConditionBonus.lowBonus;
+		if ((attacker.getZ() - target.getZ()) > 50)
+		{
+			mod += highBonus;
+		}
+		else if ((attacker.getZ() - target.getZ()) < -50)
+		{
+			mod += lowBonus;
+		}
 		
 		// Get weather bonus
 		if (GameTimeController.getInstance().isNowNight())
-			mod += HitConditionBonus.darkBonus;
-		//else if () No rain support yet.
-			//chance += hitConditionBonus.rainBonus;
+		{
+			mod += darkBonus;
+			// else if () No rain support yet.
+			// chance += hitConditionBonus.rainBonus;
+		}
 		
 		// Get side bonus
-		if(attacker.isBehindTarget())
-			mod += HitConditionBonus.backBonus;
-		else if(attacker.isInFrontOfTarget())
-			mod += HitConditionBonus.frontBonus;
-		else
-			mod += HitConditionBonus.sideBonus;
-		
-		// If (mod / 10) is less than 0, return 0, because we cant lower more than 100%.
-		return Math.max(mod / 100, 0); 
-	}
-	
-	static
-	{
-		final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-		factory.setIgnoringElementContentWhitespace(true);
-		factory.setIgnoringComments(true);
-		final File file = new File(Config.DATAPACK_ROOT, "data/stats/hitConditionBonus.xml");
-		Document doc = null;
-		
-		if (file.exists())
+		if (attacker.isBehindTarget())
 		{
-			try
-			{
-				doc = factory.newDocumentBuilder().parse(file);
-			}
-			catch (Exception e)
-			{
-				_log.log(Level.WARNING, "[hitConditionBonus] Could not parse file: " + e.getMessage(), e);
-			}
-			
-			String name;
-			for (Node list = doc.getFirstChild(); list != null; list = list.getNextSibling())
-			{
-				if ("hitConditionBonus".equalsIgnoreCase(list.getNodeName()) || "list".equalsIgnoreCase(list.getNodeName()))
-				{
-					for (Node cond = list.getFirstChild(); cond != null; cond = cond.getNextSibling())
-					{
-						int bonus = 0;
-						name = cond.getNodeName();
-						try
-						{
-							if (cond.hasAttributes())
-								bonus = Integer.parseInt(cond.getAttributes().getNamedItem("val").getNodeValue());							
-						}
-						catch (Exception e)
-						{
-							_log.log(Level.WARNING, "[hitConditionBonus] Could not parse condition: " + e.getMessage(), e);
-						}
-						finally
-						{
-							if ("front".equals(name))
-								frontBonus = bonus;
-							else if ("side".equals(name))
-								sideBonus = bonus;
-							else if ("back".equals(name))
-								backBonus = bonus;
-							else if ("high".equals(name))
-								highBonus = bonus;
-							else if ("low".equals(name))
-								lowBonus = bonus;
-							else if ("dark".equals(name))
-								darkBonus = bonus;
-							//else if ("rain".equals(name))
-								//rainBonus = bonus;
-						}
-						
-					}
-				}
-			}
+			mod += backBonus;
+		}
+		else if (attacker.isInFrontOfTarget())
+		{
+			mod += frontBonus;
 		}
 		else
 		{
-			throw new Error("[hitConditionBonus] File not found: "+file.getName());
+			mod += sideBonus;
 		}
+		
+		// If (mod / 100) is less than 0, return 0, because we can't lower more than 100%.
+		return Math.max(mod / 100, 0);
+	}
+	
+	public static HitConditionBonus getInstance()
+	{
+		return SingletonHolder._instance;
+	}
+	
+	@SuppressWarnings("synthetic-access")
+	private static class SingletonHolder
+	{
+		protected static final HitConditionBonus _instance = new HitConditionBonus();
 	}
 }

+ 10 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/engines/DocumentParser.java

@@ -157,6 +157,16 @@ public abstract class DocumentParser
 		return Integer.valueOf(n.getNodeValue());
 	}
 	
+	/**
+	 * @param n the named node map.
+	 * @param name the attribute name.
+	 * @return a parsed integer.
+	 */
+	protected static Long parseLong(NamedNodeMap n, String name)
+	{
+		return Long.valueOf(n.getNamedItem(name).getNodeValue());
+	}
+	
 	/**
 	 * Simple XML error handler.
 	 * @author Zoey76

+ 1 - 1
L2J_Server_BETA/java/com/l2jserver/gameserver/model/stats/Formulas.java

@@ -2373,7 +2373,7 @@ public final class Formulas
 		int chance = (80 + (2 * (attacker.getAccuracy() - target.getEvasionRate(attacker)))) * 10;
 		
 		// Get additional bonus from the conditions when you are attacking
-		chance *= HitConditionBonus.getConditionBonus(attacker, target);
+		chance *= HitConditionBonus.getInstance().getConditionBonus(attacker, target);
 		
 		chance = Math.max(chance, 200);
 		chance = Math.min(chance, 980);