Procházet zdrojové kódy

BETA: EnchantItemTable rework:
* Renamed to EnchantItemData
* Using DocumentParser
* Using HashMap instead of Troove map
* Simplify loading methods
* Removing double log in EffectHandler

Rumen Nikiforov před 13 roky
rodič
revize
bd66c69f71

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

@@ -45,7 +45,7 @@ import com.l2jserver.gameserver.datatables.ClassListData;
 import com.l2jserver.gameserver.datatables.DoorTable;
 import com.l2jserver.gameserver.datatables.EnchantGroupsTable;
 import com.l2jserver.gameserver.datatables.EnchantHPBonusData;
-import com.l2jserver.gameserver.datatables.EnchantItemTable;
+import com.l2jserver.gameserver.datatables.EnchantItemData;
 import com.l2jserver.gameserver.datatables.EventDroplist;
 import com.l2jserver.gameserver.datatables.ExperienceTable;
 import com.l2jserver.gameserver.datatables.FishData;
@@ -219,7 +219,7 @@ public class GameServer
 		
 		printSection("Items");
 		ItemTable.getInstance();
-		EnchantItemTable.getInstance();
+		EnchantItemData.getInstance();
 		SummonItemsData.getInstance();
 		EnchantHPBonusData.getInstance();
 		MerchantPriceConfigTable.getInstance().loadInstances();

+ 145 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/EnchantItemData.java

@@ -0,0 +1,145 @@
+/*
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ * 
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.gameserver.datatables;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+import com.l2jserver.gameserver.engines.DocumentParser;
+import com.l2jserver.gameserver.model.EnchantItem;
+import com.l2jserver.gameserver.model.EnchantScroll;
+import com.l2jserver.gameserver.model.StatsSet;
+import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
+
+/**
+ * @author UnAfraid
+ */
+public class EnchantItemData extends DocumentParser
+{
+	public final Map<Integer, EnchantScroll> _scrolls;
+	public final Map<Integer, EnchantItem> _supports;
+	
+	public EnchantItemData()
+	{
+		_scrolls = new HashMap<>();
+		_supports = new HashMap<>();
+		load();
+	}
+	
+	public void load()
+	{
+		_scrolls.clear();
+		_supports.clear();
+		parseDatapackFile("data/enchantData.xml");
+		_log.info(getClass().getSimpleName() + ": Loaded " + _scrolls.size() + " Enchant Scrolls");
+		_log.info(getClass().getSimpleName() + ": Loaded " + _supports.size() + " Support Items");
+	}
+	
+	@Override
+	protected void parseDocument(Document doc)
+	{
+		StatsSet set;
+		Node att;
+		for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
+		{
+			if ("list".equalsIgnoreCase(n.getNodeName()))
+			{
+				for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
+				{
+					if ("enchant".equalsIgnoreCase(d.getNodeName()))
+					{
+						NamedNodeMap attrs = d.getAttributes();
+						set = new StatsSet();
+						for (int i = 0; i < attrs.getLength(); i++)
+						{
+							att = attrs.item(i);
+							set.set(att.getNodeName(), att.getNodeValue());
+						}
+						
+						List<Integer> items = new ArrayList<Integer>();
+						
+						for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
+						{
+							if ("item".equalsIgnoreCase(cd.getNodeName()))
+							{
+								items.add(parseInt(cd.getAttributes(), "id"));
+							}
+						}
+						EnchantScroll item = new EnchantScroll(set, items);
+						_scrolls.put(item.getScrollId(), item);
+					}
+					else if ("support".equalsIgnoreCase(d.getNodeName()))
+					{
+						NamedNodeMap attrs = d.getAttributes();
+						
+						set = new StatsSet();
+						for (int i = 0; i < attrs.getLength(); i++)
+						{
+							att = attrs.item(i);
+							set.set(att.getNodeName(), att.getNodeValue());
+						}
+						
+						List<Integer> items = new ArrayList<Integer>();
+						
+						for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
+						{
+							if ("item".equalsIgnoreCase(cd.getNodeName()))
+							{
+								items.add(parseInt(cd.getAttributes(), "id"));
+							}
+						}
+						EnchantItem item = new EnchantItem(set, items);
+						_supports.put(item.getScrollId(), item);
+					}
+				}
+			}
+		}
+	}
+	
+	/**
+	 * @param scroll
+	 * @return enchant template for scroll
+	 */
+	public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
+	{
+		return _scrolls.get(scroll.getItemId());
+	}
+	
+	/**
+	 * @param item
+	 * @return enchant template for support item
+	 */
+	public final EnchantItem getSupportItem(L2ItemInstance item)
+	{
+		return _supports.get(item.getItemId());
+	}
+	
+	public static final EnchantItemData getInstance()
+	{
+		return SingletonHolder._instance;
+	}
+	
+	@SuppressWarnings("synthetic-access")
+	private static class SingletonHolder
+	{
+		protected static final EnchantItemData _instance = new EnchantItemData();
+	}
+}

+ 0 - 282
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/EnchantItemTable.java

@@ -1,282 +0,0 @@
-/*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- * 
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- * 
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package com.l2jserver.gameserver.datatables;
-
-import gnu.trove.map.hash.TIntObjectHashMap;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-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.model.EnchantItem;
-import com.l2jserver.gameserver.model.EnchantScroll;
-import com.l2jserver.gameserver.model.items.L2Item;
-import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
-
-
-/**
- * @author UnAfraid
- *
- */
-public class EnchantItemTable
-{
-	private static final Logger _log = Logger.getLogger(EnchantItemTable.class.getName());
-	
-	public final TIntObjectHashMap<EnchantScroll> _scrolls;
-	public final TIntObjectHashMap<EnchantItem> _supports;
-	
-	public EnchantItemTable()
-	{
-		_scrolls = new TIntObjectHashMap<EnchantScroll>();
-		_supports = new TIntObjectHashMap<EnchantItem>();
-		
-		load();
-	}
-	
-	public void load()
-	{
-		try
-		{
-			_scrolls.clear();
-			_supports.clear();
-			
-			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-			factory.setValidating(false);
-			factory.setIgnoringComments(true);
-			
-			File file = new File(Config.DATAPACK_ROOT + "/data/enchantData.xml");
-			if (!file.exists())
-			{
-				_log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] File is missing " + Config.DATAPACK_ROOT + "/data/enchantData.xml !");
-				return;
-			}
-			
-			Document doc = factory.newDocumentBuilder().parse(file);
-			Node first = doc.getFirstChild();
-			if (first != null && "list".equalsIgnoreCase(first.getNodeName()))
-			{
-				for (Node n = first.getFirstChild(); n != null; n = n.getNextSibling())
-				{
-					if ("enchant".equalsIgnoreCase(n.getNodeName()))
-					{
-						int scrollId = 0;
-						boolean isWeapon = true;
-						boolean isBlessed = false;
-						boolean isSafe = false;
-						int type = L2Item.CRYSTAL_NONE;
-						int maxEnchant = Config.MAX_ENCHANT_LEVEL;
-						double chance = Config.ENCHANT_CHANCE;
-						int[] items = null;
-						
-						NamedNodeMap attrs = n.getAttributes();
-						Node att = attrs.getNamedItem("id");
-						
-						if (att == null)
-						{
-							_log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Enchant id, skipping");
-							continue;
-						}
-						scrollId = Integer.parseInt(att.getNodeValue());
-						
-						att = attrs.getNamedItem("isWeapon");
-						if (att != null)
-						{
-							isWeapon = Boolean.parseBoolean(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("isBlessed");
-						if (att != null)
-						{
-							isBlessed = Boolean.parseBoolean(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("isSafe");
-						if (att != null)
-						{
-							isSafe = Boolean.parseBoolean(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("targetGrade");
-						if (att != null)
-						{
-							type = ItemTable._crystalTypes.get(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("maxEnchant");
-						if (att != null)
-						{
-							maxEnchant = Integer.parseInt(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("successRate");
-						if (att != null)
-						{
-							chance = Double.parseDouble(att.getNodeValue());
-							chance = Math.max(chance, 1.0); // Enchant bonus cannot be below 1.0
-						}
-						
-						List<Integer> itemz = new ArrayList<Integer>();
-						
-						for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
-						{
-							if ("item".equalsIgnoreCase(cd.getNodeName()))
-							{
-								att = cd.getAttributes().getNamedItem("id");
-								if (att != null)
-									itemz.add(Integer.parseInt(att.getNodeValue()));
-								else
-								{
-									_log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Item id, skipping");
-									continue;
-								}
-							}
-						}
-						
-						if (itemz.size() > 0)
-						{
-							items = new int[itemz.size()];
-							int i = 0;
-							for (Integer id : itemz)
-							{
-								items[i++] = id;
-							}
-							Arrays.sort(items);
-						}
-						
-						_scrolls.put(scrollId, new EnchantScroll(isWeapon, isBlessed, isSafe, type, maxEnchant, chance, items));
-					}
-					else if ("support".equalsIgnoreCase(n.getNodeName()))
-					{
-						int scrollId = 0;
-						boolean isWeapon = true;
-						int type = L2Item.CRYSTAL_NONE;
-						int maxEnchant = 0;
-						double chance = 1.0;
-						int[] items = null;
-						NamedNodeMap attrs = n.getAttributes();
-						Node att = attrs.getNamedItem("id");
-						
-						if (att == null)
-						{
-							_log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Support id, skipping");
-							continue;
-						}
-						scrollId = Integer.parseInt(att.getNodeValue());
-						
-						att = attrs.getNamedItem("isWeapon");
-						if (att != null)
-						{
-							isWeapon = Boolean.parseBoolean(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("targetGrade");
-						if (att != null)
-						{
-							type = ItemTable._crystalTypes.get(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("maxEnchant");
-						if (att != null)
-						{
-							maxEnchant = Integer.parseInt(att.getNodeValue());
-						}
-						
-						att = attrs.getNamedItem("successBonus");
-						if (att != null)
-						{
-							chance = Double.parseDouble(att.getNodeValue());
-							chance = Math.max(chance, 1.0); // Enchant bonus cannot be below 1.0
-						}
-						
-						List<Integer> itemz = new ArrayList<Integer>();
-						
-						for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
-						{
-							if ("item".equalsIgnoreCase(cd.getNodeName()))
-							{
-								att = cd.getAttributes().getNamedItem("id");
-								if (att != null)
-									itemz.add(Integer.parseInt(att.getNodeValue()));
-								else
-								{
-									_log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Item id, skipping");
-									continue;
-								}
-							}
-						}
-						
-						if (itemz.size() > 0)
-						{
-							items = new int[itemz.size()];
-							int i = 0;
-							for (Integer id : itemz)
-							{
-								items[i++] = id;
-							}
-							Arrays.sort(items);
-						}
-						_supports.put(scrollId, new EnchantItem(isWeapon, type, maxEnchant, chance, items));
-					}
-				}
-			}
-		}
-		catch (Exception e)
-		{
-			_log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Failed to parse xml: " + e.getMessage(), e);
-		}
-		
-		_log.info(getClass().getSimpleName() + ": Loaded " + _scrolls.size() + " Enchant Scrolls");
-		_log.info(getClass().getSimpleName() + ": Loaded " + _supports.size() + " Support Items");
-	}
-	
-	/**
-	 * @param scroll 
-	 * @return enchant template for scroll
-	 */
-	public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
-	{
-		return _scrolls.get(scroll.getItemId());
-	}
-	
-	/**
-	 * @param item 
-	 * @return enchant template for support item
-	 */
-	public final EnchantItem getSupportItem(L2ItemInstance item)
-	{
-		return _supports.get(item.getItemId());
-	}
-	
-	public static final EnchantItemTable getInstance()
-	{
-		return SingletonHolder._instance;
-	}
-	
-	@SuppressWarnings("synthetic-access")
-	private static class SingletonHolder
-	{
-		protected static final EnchantItemTable _instance = new EnchantItemTable();
-	}
-}

+ 0 - 1
L2J_Server_BETA/java/com/l2jserver/gameserver/handler/EffectHandler.java

@@ -67,7 +67,6 @@ public final class EffectHandler
 		{
 			_log.log(Level.WARNING, "Problems while running EffectMansterHandler", e);
 		}
-		_log.config("Loaded " + size() + " Effect handlers");
 	}
 	
 	public static EffectHandler getInstance()

+ 22 - 14
L2J_Server_BETA/java/com/l2jserver/gameserver/model/EnchantItem.java

@@ -14,8 +14,10 @@
  */
 package com.l2jserver.gameserver.model;
 
-import java.util.Arrays;
+import java.util.List;
 
+import com.l2jserver.Config;
+import com.l2jserver.gameserver.datatables.ItemTable;
 import com.l2jserver.gameserver.model.items.L2Item;
 import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
 
@@ -24,25 +26,24 @@ import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  */
 public class EnchantItem
 {
+	protected final int _id;
 	protected final boolean _isWeapon;
 	protected final int _grade;
 	protected final int _maxEnchantLevel;
 	protected final double _chanceAdd;
-	protected final int[] _itemIds;
+	protected final List<Integer> _itemIds;
 	
 	/**
-	 * @param wep
-	 * @param type
-	 * @param level
-	 * @param chance
+	 * @param set
 	 * @param items
 	 */
-	public EnchantItem(boolean wep, int type, int level, double chance, int[] items)
+	public EnchantItem(StatsSet set, List<Integer> items)
 	{
-		_isWeapon = wep;
-		_grade = type;
-		_maxEnchantLevel = level;
-		_chanceAdd = chance;
+		_id = set.getInteger("id");
+		_isWeapon = set.getBool("isWeapon", true);
+		_grade = ItemTable._crystalTypes.get(set.getString("targetGrade", "none"));
+		_maxEnchantLevel = set.getInteger("maxEnchant", Config.MAX_ENCHANT_LEVEL);
+		_chanceAdd = set.getDouble("successBonus", Config.ENCHANT_CHANCE);
 		_itemIds = items;
 	}
 	
@@ -60,16 +61,17 @@ public class EnchantItem
 		
 		else if (!isValidItemType(enchantItem.getItem().getType2()))
 			return false;
-
+		
 		else if (_maxEnchantLevel != 0 && enchantItem.getEnchantLevel() >= _maxEnchantLevel)
 			return false;
 		
 		else if (_grade != enchantItem.getItem().getItemGradeSPlus())
 			return false;
 		
-		else if ((enchantItem.isEnchantable() > 1 && (_itemIds == null || Arrays.binarySearch(_itemIds, enchantItem.getItemId()) < 0)) || _itemIds != null && Arrays.binarySearch(_itemIds, enchantItem.getItemId()) < 0)
+		else if ((enchantItem.isEnchantable() > 1 && (_itemIds.isEmpty() || !_itemIds.contains(enchantItem.getItemId())))
+				|| !_itemIds.isEmpty() && !_itemIds.contains(enchantItem.getItemId()))
 			return false;
-				
+		
 		return true;
 	}
 	
@@ -85,6 +87,7 @@ public class EnchantItem
 		}
 		return false;
 	}
+	
 	/**
 	 * @return chance increase
 	 */
@@ -92,4 +95,9 @@ public class EnchantItem
 	{
 		return _chanceAdd;
 	}
+	
+	public int getScrollId()
+	{
+		return _id;
+	}
 }

+ 8 - 14
L2J_Server_BETA/java/com/l2jserver/gameserver/model/EnchantScroll.java

@@ -14,14 +14,14 @@
  */
 package com.l2jserver.gameserver.model;
 
+import java.util.List;
+
 import com.l2jserver.Config;
 import com.l2jserver.gameserver.model.items.L2Item;
 import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
 
-
 /**
  * @author UnAfraid
- *
  */
 public class EnchantScroll extends EnchantItem
 {
@@ -29,20 +29,15 @@ public class EnchantScroll extends EnchantItem
 	private final boolean _isSafe;
 	
 	/**
-	 * @param wep
-	 * @param bless
-	 * @param safe
-	 * @param type
-	 * @param level
-	 * @param chance
+	 * @param set
 	 * @param items
 	 */
-	public EnchantScroll(boolean wep, boolean bless, boolean safe, int type, int level, double chance, int[] items)
+	public EnchantScroll(StatsSet set, List<Integer> items)
 	{
-		super(wep, type, level, chance, items);
+		super(set, items);
 		
-		_isBlessed = bless;
-		_isSafe = safe;
+		_isBlessed = set.getBool("isBlessed", false);
+		_isSafe = set.getBool("isSafe", false);
 	}
 	
 	/**
@@ -86,8 +81,7 @@ public class EnchantScroll extends EnchantItem
 			return -1;
 		
 		boolean fullBody = enchantItem.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR;
-		if (enchantItem.getEnchantLevel() < Config.ENCHANT_SAFE_MAX
-				|| (fullBody && enchantItem.getEnchantLevel() < Config.ENCHANT_SAFE_MAX_FULL))
+		if (enchantItem.getEnchantLevel() < Config.ENCHANT_SAFE_MAX || (fullBody && enchantItem.getEnchantLevel() < Config.ENCHANT_SAFE_MAX_FULL))
 			return 100;
 		
 		double chance = _chanceAdd;

+ 3 - 3
L2J_Server_BETA/java/com/l2jserver/gameserver/network/clientpackets/RequestEnchantItem.java

@@ -19,7 +19,7 @@ import java.util.logging.LogRecord;
 import java.util.logging.Logger;
 
 import com.l2jserver.Config;
-import com.l2jserver.gameserver.datatables.EnchantItemTable;
+import com.l2jserver.gameserver.datatables.EnchantItemData;
 import com.l2jserver.gameserver.datatables.SkillTable;
 import com.l2jserver.gameserver.model.EnchantItem;
 import com.l2jserver.gameserver.model.EnchantScroll;
@@ -89,7 +89,7 @@ public final class RequestEnchantItem extends L2GameClientPacket
 		}
 		
 		// template for scroll
-		EnchantScroll scrollTemplate = EnchantItemTable.getInstance().getEnchantScroll(scroll);
+		EnchantScroll scrollTemplate = EnchantItemData.getInstance().getEnchantScroll(scroll);
 		
 		// scroll not found in list
 		if (scrollTemplate == null)
@@ -106,7 +106,7 @@ public final class RequestEnchantItem extends L2GameClientPacket
 				activeChar.setActiveEnchantItem(null);
 				return;
 			}
-			supportTemplate = EnchantItemTable.getInstance().getSupportItem(support);
+			supportTemplate = EnchantItemData.getInstance().getSupportItem(support);
 		}
 		
 		// first validation check

+ 2 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/network/clientpackets/RequestExTryToPutEnchantSupportItem.java

@@ -14,7 +14,7 @@
  */
 package com.l2jserver.gameserver.network.clientpackets;
 
-import com.l2jserver.gameserver.datatables.EnchantItemTable;
+import com.l2jserver.gameserver.datatables.EnchantItemData;
 import com.l2jserver.gameserver.model.EnchantItem;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
 import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
@@ -52,7 +52,7 @@ public class RequestExTryToPutEnchantSupportItem extends L2GameClientPacket
 				if (item == null || support == null)
 					return;
 				
-				EnchantItem supportTemplate = EnchantItemTable.getInstance().getSupportItem(support);
+				EnchantItem supportTemplate = EnchantItemData.getInstance().getSupportItem(support);
 				
 				if (supportTemplate == null || !supportTemplate.isValid(item))
 				{

+ 2 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/network/clientpackets/RequestExTryToPutEnchantTargetItem.java

@@ -14,7 +14,7 @@
  */
 package com.l2jserver.gameserver.network.clientpackets;
 
-import com.l2jserver.gameserver.datatables.EnchantItemTable;
+import com.l2jserver.gameserver.datatables.EnchantItemData;
 import com.l2jserver.gameserver.model.EnchantScroll;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
 import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
@@ -54,7 +54,7 @@ public class RequestExTryToPutEnchantTargetItem extends L2GameClientPacket
 			return;
 		
 		// template for scroll
-		EnchantScroll scrollTemplate = EnchantItemTable.getInstance().getEnchantScroll(scroll);
+		EnchantScroll scrollTemplate = EnchantItemData.getInstance().getEnchantScroll(scroll);
 		
 		if (!scrollTemplate.isValid(item))
 		{