Selaa lähdekoodia

BETA: Small rework to listeners:
* Reworked Npc, Item, Residence annotations to one Id
* Added new range annotations (id range and npc level range)
* Prevented from starting event dispatching operations if containers doesn't contains this event type.

Rumen Nikiforov 11 vuotta sitten
vanhempi
sitoutus
aaa7fc8635

+ 65 - 28
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/AbstractScript.java

@@ -58,14 +58,14 @@ import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
 import com.l2jserver.gameserver.model.entity.Castle;
 import com.l2jserver.gameserver.model.entity.Fort;
 import com.l2jserver.gameserver.model.entity.Instance;
-import com.l2jserver.gameserver.model.events.annotations.Item;
-import com.l2jserver.gameserver.model.events.annotations.Items;
-import com.l2jserver.gameserver.model.events.annotations.Npc;
-import com.l2jserver.gameserver.model.events.annotations.Npcs;
+import com.l2jserver.gameserver.model.events.annotations.Id;
+import com.l2jserver.gameserver.model.events.annotations.Ids;
+import com.l2jserver.gameserver.model.events.annotations.NpcLevelRange;
+import com.l2jserver.gameserver.model.events.annotations.NpcLevelRanges;
+import com.l2jserver.gameserver.model.events.annotations.Range;
+import com.l2jserver.gameserver.model.events.annotations.Ranges;
 import com.l2jserver.gameserver.model.events.annotations.RegisterEvent;
 import com.l2jserver.gameserver.model.events.annotations.RegisterType;
-import com.l2jserver.gameserver.model.events.annotations.Residence;
-import com.l2jserver.gameserver.model.events.annotations.Residences;
 import com.l2jserver.gameserver.model.events.impl.IBaseEvent;
 import com.l2jserver.gameserver.model.events.impl.character.OnCreatureKill;
 import com.l2jserver.gameserver.model.events.impl.character.OnCreatureZoneEnter;
@@ -171,21 +171,21 @@ public abstract class AbstractScript extends ManagedScript
 				// Clear the list
 				ids.clear();
 				
-				// Scan for possible Npc ID filters
+				// Scan for possible Id filters
 				for (Annotation annotation : method.getAnnotations())
 				{
-					if (annotation instanceof Npc)
+					if (annotation instanceof Id)
 					{
-						final Npc npc = (Npc) annotation;
+						final Id npc = (Id) annotation;
 						for (int id : npc.value())
 						{
 							ids.add(id);
 						}
 					}
-					else if (annotation instanceof Npcs)
+					else if (annotation instanceof Ids)
 					{
-						final Npcs npcs = (Npcs) annotation;
-						for (Npc npc : npcs.value())
+						final Ids npcs = (Ids) annotation;
+						for (Id npc : npcs.value())
 						{
 							for (int id : npc.value())
 							{
@@ -193,41 +193,78 @@ public abstract class AbstractScript extends ManagedScript
 							}
 						}
 					}
-					else if (annotation instanceof Item)
+					else if (annotation instanceof Range)
 					{
-						final Item item = (Item) annotation;
-						for (int id : item.value())
+						final Range range = (Range) annotation;
+						if (range.from() > range.to())
+						{
+							_log.log(Level.WARNING, getClass().getSimpleName() + ": Wrong " + annotation.getClass().getSimpleName() + " from is higher then to!");
+							continue;
+						}
+						
+						for (int id = range.from(); id <= range.to(); id++)
 						{
 							ids.add(id);
 						}
 					}
-					else if (annotation instanceof Items)
+					else if (annotation instanceof Ranges)
 					{
-						final Items items = (Items) annotation;
-						for (Item item : items.value())
+						final Ranges ranges = (Ranges) annotation;
+						for (Range range : ranges.value())
 						{
-							for (int id : item.value())
+							if (range.from() > range.to())
+							{
+								_log.log(Level.WARNING, getClass().getSimpleName() + ": Wrong " + annotation.getClass().getSimpleName() + " from is higher then to!");
+								continue;
+							}
+							
+							for (int id = range.from(); id <= range.to(); id++)
 							{
 								ids.add(id);
 							}
 						}
 					}
-					else if (annotation instanceof Residence)
+					else if (annotation instanceof NpcLevelRange)
 					{
-						final Item item = (Item) annotation;
-						for (int id : item.value())
+						final NpcLevelRange range = (NpcLevelRange) annotation;
+						if (range.from() > range.to())
 						{
-							ids.add(id);
+							_log.log(Level.WARNING, getClass().getSimpleName() + ": Wrong " + annotation.getClass().getSimpleName() + " from is higher then to!");
+							continue;
+						}
+						else if (type != ListenerRegisterType.NPC)
+						{
+							_log.log(Level.WARNING, getClass().getSimpleName() + ": ListenerRegisterType " + type + " for " + annotation.getClass().getSimpleName() + " NPC is expected!");
+							continue;
 						}
+						
+						for (int level = range.from(); level <= range.to(); level++)
+						{
+							final List<L2NpcTemplate> templates = NpcData.getInstance().getAllOfLevel(level);
+							templates.forEach(template -> ids.add(template.getId()));
+						}
+						
 					}
-					else if (annotation instanceof Residences)
+					else if (annotation instanceof NpcLevelRanges)
 					{
-						final Residences residences = (Residences) annotation;
-						for (Residence residence : residences.value())
+						final NpcLevelRanges ranges = (NpcLevelRanges) annotation;
+						for (NpcLevelRange range : ranges.value())
 						{
-							for (int id : residence.value())
+							if (range.from() > range.to())
 							{
-								ids.add(id);
+								_log.log(Level.WARNING, getClass().getSimpleName() + ": Wrong " + annotation.getClass().getSimpleName() + " from is higher then to!");
+								continue;
+							}
+							else if (type != ListenerRegisterType.NPC)
+							{
+								_log.log(Level.WARNING, getClass().getSimpleName() + ": ListenerRegisterType " + type + " for " + annotation.getClass().getSimpleName() + " NPC is expected!");
+								continue;
+							}
+							
+							for (int level = range.from(); level <= range.to(); level++)
+							{
+								final List<L2NpcTemplate> templates = NpcData.getInstance().getAllOfLevel(level);
+								templates.forEach(template -> ids.add(template.getId()));
 							}
 						}
 					}

+ 29 - 7
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/EventDispatcher.java

@@ -46,7 +46,7 @@ public class EventDispatcher extends ListenersContainer
 	 */
 	public <T extends AbstractEventReturn> T notifyEvent(IBaseEvent event)
 	{
-		return notifyEvent(event, null, null);
+		return hasListener(event.getType()) ? notifyEvent(event, null, null) : null;
 	}
 	
 	/**
@@ -57,7 +57,7 @@ public class EventDispatcher extends ListenersContainer
 	 */
 	public <T extends AbstractEventReturn> T notifyEvent(IBaseEvent event, Class<T> callbackClass)
 	{
-		return notifyEvent(event, null, callbackClass);
+		return hasListener(event.getType()) ? notifyEvent(event, null, callbackClass) : null;
 	}
 	
 	/**
@@ -71,7 +71,7 @@ public class EventDispatcher extends ListenersContainer
 	{
 		try
 		{
-			return notifyEventImpl(event, container, callbackClass);
+			return hasListener(event.getType()) || container.hasListener(event.getType()) ? notifyEventImpl(event, container, callbackClass) : null;
 		}
 		catch (Exception e)
 		{
@@ -87,7 +87,7 @@ public class EventDispatcher extends ListenersContainer
 	 * @param callbackClass
 	 * @return
 	 */
-	public <T extends AbstractEventReturn> T notifyEventToMultipleContainers(IBaseEvent event, ListenersContainer[] containers, Class<T> callbackClass)
+	private <T extends AbstractEventReturn> T notifyEventToMultipleContainers(IBaseEvent event, ListenersContainer[] containers, Class<T> callbackClass)
 	{
 		try
 		{
@@ -212,7 +212,23 @@ public class EventDispatcher extends ListenersContainer
 			throw new NullPointerException("Event cannot be null!");
 		}
 		
-		ThreadPoolManager.getInstance().executeEvent(() -> notifyEventToMultipleContainers(event, containers, null));
+		boolean hasListeners = hasListener(event.getType());
+		if (!hasListeners)
+		{
+			for (ListenersContainer container : containers)
+			{
+				if (container.hasListener(event.getType()))
+				{
+					hasListeners = true;
+					break;
+				}
+			}
+		}
+		
+		if (hasListeners)
+		{
+			ThreadPoolManager.getInstance().executeEvent(() -> notifyEventToMultipleContainers(event, containers, null));
+		}
 	}
 	
 	/**
@@ -223,7 +239,10 @@ public class EventDispatcher extends ListenersContainer
 	 */
 	public void notifyEventAsyncDelayed(final IBaseEvent event, ListenersContainer container, long delay)
 	{
-		ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay);
+		if (hasListener(event.getType()) || container.hasListener(event.getType()))
+		{
+			ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay);
+		}
 	}
 	
 	/**
@@ -235,7 +254,10 @@ public class EventDispatcher extends ListenersContainer
 	 */
 	public void notifyEventAsyncDelayed(final IBaseEvent event, ListenersContainer container, long delay, TimeUnit unit)
 	{
-		ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay, unit);
+		if (hasListener(event.getType()) || container.hasListener(event.getType()))
+		{
+			ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay, unit);
+		}
 	}
 	
 	public static final EventDispatcher getInstance()

+ 2 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Npc.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Id.java

@@ -27,10 +27,10 @@ import java.lang.annotation.Target;
 /**
  * @author UnAfraid
  */
-@Repeatable(Npcs.class)
+@Repeatable(Ids.class)
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Npc
+public @interface Id
 {
 	public int[] value();
 }

+ 2 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Npcs.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Ids.java

@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Npcs
+public @interface Ids
 {
-	public Npc[] value();
+	public Id[] value();
 }

+ 5 - 3
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Residence.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/NpcLevelRange.java

@@ -27,10 +27,12 @@ import java.lang.annotation.Target;
 /**
  * @author UnAfraid
  */
-@Repeatable(Residences.class)
+@Repeatable(NpcLevelRanges.class)
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Residence
+public @interface NpcLevelRange
 {
-	public int[] value();
+	public int from();
+	
+	public int to();
 }

+ 2 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Residences.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/NpcLevelRanges.java

@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Residences
+public @interface NpcLevelRanges
 {
-	public Residence[] value();
+	public NpcLevelRange[] value();
 }

+ 5 - 3
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Item.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Range.java

@@ -27,10 +27,12 @@ import java.lang.annotation.Target;
 /**
  * @author UnAfraid
  */
-@Repeatable(Items.class)
+@Repeatable(Ranges.class)
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Item
+public @interface Range
 {
-	public int[] value();
+	public int from();
+	
+	public int to();
 }

+ 2 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Items.java → L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Ranges.java

@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Items
+public @interface Ranges
 {
-	public Item[] value();
+	public Range[] value();
 }