浏览代码

BETA: Event Drop List OOP rework:
* Backward compatible.
* With support for one single item.
* Cleaner and formatted.

Now is possible to initialize all event drops statically for an event and keep them in one single array.
The old "constructor" is still present, but I'd recommend the use of the OOP form.

Zoey76 13 年之前
父节点
当前提交
8662f1572e

+ 57 - 53
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/EventDroplist.java

@@ -16,79 +16,77 @@ package com.l2jserver.gameserver.datatables;
 
 import java.util.Date;
 import java.util.List;
+import java.util.logging.Logger;
 
 import javolution.util.FastList;
 
+import com.l2jserver.Config;
 import com.l2jserver.gameserver.script.DateRange;
+import com.l2jserver.gameserver.script.EventDrop;
 
 /**
- * This class manage drop of Special Events created by GM for a defined period.
- * During a Special Event all L2Attackable can drop extra Items.
- * Those extra Items are defined in the table <B>allNpcDateDrops</B>.
- * Each Special Event has a start and end date to stop to drop extra Items automaticaly.<BR><BR>
+ * This class manage drop of Special Events created by GM for a defined period.<br>
+ * During a Special Event all L2Attackable can drop extra Items.<br>
+ * Those extra Items are defined in the table <b>allNpcDateDrops</b>.<br>
+ * Each Special Event has a start and end date to stop to drop extra Items automatically.
  */
-
 public class EventDroplist
 {
+	private static Logger _log = Logger.getLogger(EventDroplist.class.getName());
 	
-	//private static Logger _log = Logger.getLogger(EventDroplist.class.getName());
-	
-	/** The table containing all DataDrop object */
-	private List<DateDrop> _allNpcDateDrops;
-	
-	public static EventDroplist getInstance()
-	{
-		return SingletonHolder._instance;
-	}
+	/**
+	 * The table containing all DataDrop object
+	 */
+	private static final List<DateDrop> _allNpcDateDrops = new FastList<DateDrop>();
 	
 	public static class DateDrop
 	{
-		/** Start and end date of the Event */
-		public DateRange dateRange;
+		private final DateRange _dateRange;
+		private final EventDrop _eventDrop;
 		
-		/** The table containing Item identifier that can be dropped as extra Items during the Event */
-		public int[] items;
-		
-		/** The min number of Item dropped in one time during this Event */
-		public int min;
-		
-		/** The max number of Item dropped in one time during this Event */
-		public int max;
+		public DateDrop(DateRange dateRange, EventDrop eventDrop)
+		{
+			_dateRange = dateRange;
+			_eventDrop = eventDrop;
+		}
 		
-		/** The rate of drop for this Event */
-		public int chance;
+		/**
+		 * @return the _eventDrop
+		 */
+		public EventDrop getEventDrop()
+		{
+			return _eventDrop;
+		}
 		
+		/**
+		 * @return the _dateRange
+		 */
+		public DateRange getDateRange()
+		{
+			return _dateRange;
+		}
 	}
 	
 	/**
-	 * Constructor of EventDroplist.<BR><BR>
+	 * Create and Init a new DateDrop then add it to the allNpcDateDrops of EventDroplist .
+	 * @param itemIdList The table containing all item identifier of this DateDrop
+	 * @param count The table containing min and max value of this DateDrop
+	 * @param chance The chance to obtain this drop
+	 * @param dateRange The DateRange object to add to this DateDrop
 	 */
-	private EventDroplist()
+	public void addGlobalDrop(int[] itemIdList, int[] count, int chance, DateRange dateRange)
 	{
-		_allNpcDateDrops = new FastList<DateDrop>();
+		_allNpcDateDrops.add(new DateDrop(dateRange, new EventDrop(itemIdList, count[0], count[1], chance)));
 	}
 	
 	/**
-	 * Create and Init a new DateDrop then add it to the allNpcDateDrops of EventDroplist .<BR><BR>
-	 *
-	 * @param items The table containing all item identifier of this DateDrop
-	 * @param count The table containing min and max value of this DateDrop
-	 * @param chance The chance to obtain this drop
-	 * @param range The DateRange object to add to this DateDrop
-	 *
+	 * Adds an event drop for a given date range.
+	 * @param dateRange the date range.
+	 * @param eventDrop the event drop.
 	 */
-	public void addGlobalDrop(int[] items, int[] count, int chance, DateRange range)
+	public void addGlobalDrop(DateRange dateRange, EventDrop eventDrop)
 	{
-		
-		DateDrop date = new DateDrop();
-		
-		date.dateRange = range;
-		date.items = items;
-		date.min = count[0];
-		date.max = count[1];
-		date.chance = chance;
-		
-		_allNpcDateDrops.add(date);
+		_allNpcDateDrops.add(new DateDrop(dateRange, eventDrop));
 	}
 	
 	/**
@@ -96,21 +94,27 @@ public class EventDroplist
 	 */
 	public List<DateDrop> getAllDrops()
 	{
-		List<DateDrop> list = new FastList<DateDrop>();
-		
+		final List<DateDrop> list = new FastList<DateDrop>();
+		final Date currentDate = new Date();
 		for (DateDrop drop : _allNpcDateDrops)
 		{
-			Date currentDate = new Date();
-			//_log.info("From: "+drop.from+" To: "+drop.to+" Now: "+ currentDate);
-			if (drop.dateRange.isWithinRange(currentDate))
+			if (Config.DEBUG)
+			{
+				_log.info(drop._dateRange.toString() + " Now: " + currentDate);
+			}
+			if (drop._dateRange.isWithinRange(currentDate))
 			{
 				list.add(drop);
 			}
 		}
-		
 		return list;
 	}
 	
+	public static EventDroplist getInstance()
+	{
+		return SingletonHolder._instance;
+	}
+	
 	@SuppressWarnings("synthetic-access")
 	private static class SingletonHolder
 	{

+ 4 - 4
L2J_Server_BETA/java/com/l2jserver/gameserver/model/actor/L2Attackable.java

@@ -1754,14 +1754,14 @@ public class L2Attackable extends L2Npc
 		// Go through DateDrop of EventDroplist allNpcDateDrops within the date range
 		for (DateDrop drop : EventDroplist.getInstance().getAllDrops())
 		{
-			if (Rnd.get(L2DropData.MAX_CHANCE) < drop.chance)
+			if (Rnd.get(L2DropData.MAX_CHANCE) < drop.getEventDrop().getDropChance())
 			{
-				RewardItem item = new RewardItem(drop.items[Rnd.get(drop.items.length)], Rnd.get(drop.min, drop.max));
+				final RewardItem rewardItem = new RewardItem(drop.getEventDrop().getItemIdList()[Rnd.get(drop.getEventDrop().getItemIdList().length)], Rnd.get(drop.getEventDrop().getMinCount(), drop.getEventDrop().getMaxCount()));
 				
 				if (Config.AUTO_LOOT || isFlying())
-					player.doAutoLoot(this, item); // Give the item(s) to the L2PcInstance that has killed the L2Attackable
+					player.doAutoLoot(this, rewardItem); // Give the item(s) to the L2PcInstance that has killed the L2Attackable
 				else
-					dropItem(player, item); // drop the item on the ground
+					dropItem(player, rewardItem); // drop the item on the ground
 			}
 		}
 	}

+ 12 - 7
L2J_Server_BETA/java/com/l2jserver/gameserver/script/DateRange.java

@@ -22,17 +22,16 @@ import java.util.logging.Logger;
 
 /**
  * @author Luis Arias
- *
  */
 public class DateRange
 {
 	protected static final Logger _log = Logger.getLogger(DateRange.class.getName());
-	private Date _startDate, _endDate;
+	private final Date _startDate, _endDate;
 	
 	public DateRange(Date from, Date to)
 	{
-		_startDate   = from;
-		_endDate     = to;
+		_startDate = from;
+		_endDate = to;
 	}
 	
 	public static DateRange parse(String dateRange, DateFormat format)
@@ -42,8 +41,8 @@ public class DateRange
 		{
 			try
 			{
-				Date start  = format.parse(date[0]);
-				Date end    = format.parse(date[1]);
+				Date start = format.parse(date[0]);
+				Date end = format.parse(date[1]);
 				
 				return new DateRange(start, end);
 			}
@@ -57,7 +56,7 @@ public class DateRange
 	
 	public boolean isValid()
 	{
-		return _startDate == null || _endDate == null;
+		return (_startDate != null) && (_endDate != null) && _startDate.before(_endDate);
 	}
 	
 	public boolean isWithinRange(Date date)
@@ -74,4 +73,10 @@ public class DateRange
 	{
 		return _startDate;
 	}
+	
+	@Override
+	public String toString()
+	{
+		return "DateRange: From: " + getStartDate() + " To: " + getEndDate();
+	}
 }

+ 74 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/script/EventDrop.java

@@ -0,0 +1,74 @@
+/*
+ * 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.script;
+
+/**
+ * @author Zoey76
+ */
+public class EventDrop
+{
+	private final int[] _itemIdList;
+	private final int _minCount;
+	private final int _maxCount;
+	private final int _dropChance;
+	
+	public EventDrop(int[] itemIdList, int minCount, int maxCount, int dropChance)
+	{
+		_itemIdList = itemIdList;
+		_minCount = minCount;
+		_maxCount = maxCount;
+		_dropChance = dropChance;
+	}
+	
+	public EventDrop(int itemId, int minCount, int maxCount, int dropChance)
+	{
+		_itemIdList = new int[]{ itemId };
+		_minCount = minCount;
+		_maxCount = maxCount;
+		_dropChance = dropChance;
+	}
+	
+	/**
+	 * @return the _itemId
+	 */
+	public int[] getItemIdList()
+	{
+		return _itemIdList;
+	}
+	
+	/**
+	 * @return the _minCount
+	 */
+	public int getMinCount()
+	{
+		return _minCount;
+	}
+	
+	/**
+	 * @return the _maxCount
+	 */
+	public int getMaxCount()
+	{
+		return _maxCount;
+	}
+	
+	/**
+	 * @return the _dropChance
+	 */
+	public int getDropChance()
+	{
+		return _dropChance;
+	}
+}