Sfoglia il codice sorgente

BETA: Adding more annotations for item and residence binding.

Rumen Nikiforov 11 anni fa
parent
commit
8ad7ab8e15

+ 56 - 10
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/AbstractScript.java

@@ -58,9 +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.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;
@@ -137,12 +142,15 @@ public abstract class AbstractScript extends ManagedScript
 	
 	private void initializeAnnotationListeners()
 	{
-		final List<Integer> npcIds = new ArrayList<>();
+		final List<Integer> ids = new ArrayList<>();
 		for (Method method : getClass().getMethods())
 		{
-			if (method.isAnnotationPresent(RegisterEvent.class))
+			if (method.isAnnotationPresent(RegisterEvent.class) && method.isAnnotationPresent(RegisterType.class))
 			{
 				final RegisterEvent listener = method.getAnnotation(RegisterEvent.class);
+				final RegisterType regType = method.getAnnotation(RegisterType.class);
+				
+				final ListenerRegisterType type = regType.value();
 				final EventType eventType = listener.value();
 				if (method.getParameterCount() != 1)
 				{
@@ -161,7 +169,7 @@ public abstract class AbstractScript extends ManagedScript
 				}
 				
 				// Clear the list
-				npcIds.clear();
+				ids.clear();
 				
 				// Scan for possible Npc ID filters
 				for (Annotation annotation : method.getAnnotations())
@@ -171,7 +179,7 @@ public abstract class AbstractScript extends ManagedScript
 						final Npc npc = (Npc) annotation;
 						for (int id : npc.value())
 						{
-							npcIds.add(id);
+							ids.add(id);
 						}
 					}
 					else if (annotation instanceof Npcs)
@@ -181,22 +189,60 @@ public abstract class AbstractScript extends ManagedScript
 						{
 							for (int id : npc.value())
 							{
-								npcIds.add(id);
+								ids.add(id);
+							}
+						}
+					}
+					else if (annotation instanceof Item)
+					{
+						final Item item = (Item) annotation;
+						for (int id : item.value())
+						{
+							ids.add(id);
+						}
+					}
+					else if (annotation instanceof Items)
+					{
+						final Items items = (Items) annotation;
+						for (Item item : items.value())
+						{
+							for (int id : item.value())
+							{
+								ids.add(id);
+							}
+						}
+					}
+					else if (annotation instanceof Residence)
+					{
+						final Item item = (Item) annotation;
+						for (int id : item.value())
+						{
+							ids.add(id);
+						}
+					}
+					else if (annotation instanceof Residences)
+					{
+						final Residences residences = (Residences) annotation;
+						for (Residence residence : residences.value())
+						{
+							for (int id : residence.value())
+							{
+								ids.add(id);
 							}
 						}
 					}
 				}
 				
-				if (!npcIds.isEmpty())
+				if (!ids.isEmpty())
 				{
-					if (!_registeredIds.containsKey(ListenerRegisterType.NPC))
+					if (!_registeredIds.containsKey(type))
 					{
-						_registeredIds.put(ListenerRegisterType.NPC, new FastList<Integer>().shared());
+						_registeredIds.put(type, new FastList<Integer>().shared());
 					}
-					_registeredIds.get(ListenerRegisterType.NPC).addAll(npcIds);
+					_registeredIds.get(type).addAll(ids);
 				}
 				
-				registerAnnotation(method, eventType, ListenerRegisterType.NPC, npcIds);
+				registerAnnotation(method, eventType, type, ids);
 			}
 		}
 	}

+ 36 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Item.java

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2004-2014 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.model.events.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Repeatable;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author UnAfraid
+ */
+@Repeatable(Items.class)
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface Item
+{
+	public int[] value();
+}

+ 34 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Items.java

@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2004-2014 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.model.events.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author UnAfraid
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface Items
+{
+	public Item[] value();
+}

+ 36 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/RegisterType.java

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2004-2014 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.model.events.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import com.l2jserver.gameserver.model.events.ListenerRegisterType;
+
+/**
+ * @author UnAfraid
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface RegisterType
+{
+	public ListenerRegisterType value();
+}

+ 36 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Residence.java

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2004-2014 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.model.events.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Repeatable;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author UnAfraid
+ */
+@Repeatable(Residences.class)
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface Residence
+{
+	public int[] value();
+}

+ 34 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/annotations/Residences.java

@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2004-2014 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.model.events.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author UnAfraid
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface Residences
+{
+	public Residence[] value();
+}

+ 6 - 2
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/listeners/AnnotationEventListener.java

@@ -47,11 +47,15 @@ public class AnnotationEventListener extends AbstractEventListener
 	{
 		try
 		{
-			return returnBackClass.cast(_callback.invoke(getOwner(), event));
+			final Object result = _callback.invoke(getOwner(), event);
+			if (_callback.getReturnType() == returnBackClass)
+			{
+				return returnBackClass.cast(result);
+			}
 		}
 		catch (Exception e)
 		{
-			_log.log(Level.WARNING, getClass().getSimpleName() + ": Error while invoking " + _callback.getName(), e);
+			_log.log(Level.WARNING, getClass().getSimpleName() + ": Error while invoking " + _callback.getName() + " on " + getOwner(), e);
 		}
 		return null;
 	}

+ 13 - 1
L2J_Server_BETA/java/com/l2jserver/gameserver/model/events/listeners/FunctionEventListener.java

@@ -19,6 +19,8 @@
 package com.l2jserver.gameserver.model.events.listeners;
 
 import java.util.function.Function;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import com.l2jserver.gameserver.model.events.EventType;
 import com.l2jserver.gameserver.model.events.ListenersContainer;
@@ -31,6 +33,7 @@ import com.l2jserver.gameserver.model.events.returns.AbstractEventReturn;
  */
 public class FunctionEventListener extends AbstractEventListener
 {
+	private static final Logger _log = Logger.getLogger(FunctionEventListener.class.getName());
 	private final Function<IBaseEvent, ? extends AbstractEventReturn> _callback;
 	
 	@SuppressWarnings("unchecked")
@@ -43,6 +46,15 @@ public class FunctionEventListener extends AbstractEventListener
 	@Override
 	public <R extends AbstractEventReturn> R executeEvent(IBaseEvent event, Class<R> returnBackClass)
 	{
-		return returnBackClass.cast(_callback.apply(event));
+		try
+		{
+			return returnBackClass.cast(_callback.apply(event));
+			
+		}
+		catch (Exception e)
+		{
+			_log.log(Level.WARNING, getClass().getSimpleName() + ": Error while invoking " + event + " on " + getOwner(), e);
+		}
+		return null;
 	}
 }