Преглед изворни кода

Auto announcements system, by Shansoft. Requires [DP5852]

DrLecter пре 16 година
родитељ
комит
edc965b726

+ 186 - 0
L2_GameServer/java/net/sf/l2j/gameserver/AutoAnnouncements.java

@@ -0,0 +1,186 @@
+/*
+ * 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 net.sf.l2j.gameserver;
+
+
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+
+import javolution.util.FastList;
+import net.sf.l2j.L2DatabaseFactory;
+
+import net.sf.l2j.gameserver.util.Broadcast;
+
+
+
+public class AutoAnnouncements
+{
+	static Logger _log = Logger.getLogger(Announcements.class.getName());
+
+	private static AutoAnnouncements _instance;
+
+	private List<AutoAnnounceThread> autothread = new FastList<AutoAnnounceThread>();
+
+	public AutoAnnouncements()
+	{
+		restore();
+		if (!autothread.isEmpty())
+			autoTask();
+
+	}
+
+	@SuppressWarnings("deprecation")
+    public void reload()
+	{
+		int count = 0;
+		for (AutoAnnounceThread exec : autothread)
+		{
+			autothread.get(count).stop();
+			autothread.remove(exec);
+			count++;
+		}
+
+		autothread =  new FastList<AutoAnnounceThread>();
+
+		restore();
+		if (!autothread.isEmpty())
+			autoTask();
+
+	}
+
+	public void autoTask()
+	{
+		int count = 0;
+		for (@SuppressWarnings("unused") AutoAnnounceThread exec : autothread)
+		{
+			autothread.get(count).start();
+			count++;
+		}
+	}
+
+	public void announce(String text)
+	{
+		Broadcast.announceToOnlinePlayers(text);
+		_log.warning("AutoAnnounce: "+text);
+	}
+
+	public static AutoAnnouncements getInstance()
+	{
+		if (_instance == null)
+		{
+			_instance = new AutoAnnouncements();
+		}
+
+		return _instance;
+	}
+
+    private void restore()
+	{
+
+		java.sql.Connection conn = null;
+		int count = 0;
+		try
+		{
+			conn = L2DatabaseFactory.getInstance().getConnection();
+			PreparedStatement statement = conn.prepareStatement("SELECT id, initial, delay, cycle, memo FROM auto_announcements");
+			ResultSet data = statement.executeQuery();
+			while(data.next())
+			{
+				int id = data.getInt("id");
+				long initial = data.getLong("initial");
+				long delay = data.getLong("delay");
+				int repeat = data.getInt("cycle");
+				String memo = data.getString("memo");
+				String[] text = memo.split("/n");
+				AutoAnnounceThread announces = new AutoAnnounceThread();
+				announces.id = id;
+				announces.initial = initial;
+				announces.delay = delay;
+		    	if (repeat > 0)
+				announces.repeat = repeat;
+				announces.memo = text;
+
+				autothread.add(announces);
+				count++;
+			}
+
+		}
+		catch (Exception e)
+		{
+			_log.log(Level.SEVERE, "AutoAnnoucements: Fail to load announcements data.", e);
+		}
+		_log.log(Level.SEVERE, "AutoAnnoucements: Load "+count+" Auto Annoucement Data.");
+	}
+
+	public class AutoAnnounceThread extends Thread
+	{
+		int id;
+		long initial;
+		long delay;
+		int repeat = -1;
+		int stop = 0;
+		String[] memo;
+
+	    public void run ()
+		{
+
+	    	try
+            {
+
+	            Thread.sleep(initial*1000);
+            }
+            catch (InterruptedException e1)
+            {
+	            e1.printStackTrace();
+            }
+
+		    while (true)
+		    {
+		    	if(repeat > 0)
+		    		repeat--;
+		    	for (String text: memo)
+		    	{
+		    		announce(text);
+
+		    	}
+	            if(repeat == 0)
+	            {
+	            	break;
+	            }
+
+		        try
+		        {
+		            Thread.sleep(delay*1000);
+
+		        }
+		        catch
+		        (InterruptedException e)
+		        {
+		    	    _log.log(Level.SEVERE, "AutoAnnoucements: ID ["+id+"] Auto Announcement thread stop", e);
+		        }
+
+		    }
+
+
+		 }
+	}
+
+
+
+}

+ 1 - 0
L2_GameServer/java/net/sf/l2j/gameserver/GameServer.java

@@ -299,6 +299,7 @@ public class GameServer
 		FourSepulchersManager.getInstance().init();
 		DimensionalRiftManager.getInstance();
 		Announcements.getInstance();
+		AutoAnnouncements.getInstance();
 		MapRegionTable.getInstance();
 		EventDroplist.getInstance();
 		

+ 9 - 1
L2_GameServer/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminAnnouncements.java

@@ -17,6 +17,7 @@ package net.sf.l2j.gameserver.handler.admincommandhandlers;
 import java.util.Collection;
 
 import net.sf.l2j.gameserver.Announcements;
+import net.sf.l2j.gameserver.AutoAnnouncements;
 import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
 import net.sf.l2j.gameserver.model.L2World;
 import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
@@ -43,7 +44,8 @@ public class AdminAnnouncements implements IAdminCommandHandler
 		"admin_add_announcement",
 		"admin_del_announcement",
 		"admin_announce",
-		"admin_announce_menu"
+		"admin_announce_menu",
+		"admin_reload_autoannounce"
 	};
 	
 	public boolean useAdminCommand(String command, L2PcInstance activeChar)
@@ -111,6 +113,12 @@ public class AdminAnnouncements implements IAdminCommandHandler
 			sys.handleAnnounce(command, 15);
 		}
 		
+		else if (command.startsWith("admin_reload_autoannounce"))
+		{
+			activeChar.sendMessage("AutoAnnouncement Reloaded.");
+			AutoAnnouncements.getInstance().reload();
+		}
+
 		return true;
 	}