Ver código fonte

Hero Message implementation. Thx Charus.

JIV 15 anos atrás
pai
commit
b888987589

+ 3 - 0
L2_GameServer/java/com/l2jserver/gameserver/Shutdown.java

@@ -30,6 +30,7 @@ import com.l2jserver.gameserver.instancemanager.QuestManager;
 import com.l2jserver.gameserver.instancemanager.RaidBossSpawnManager;
 import com.l2jserver.gameserver.model.L2World;
 import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
+import com.l2jserver.gameserver.model.entity.Hero;
 import com.l2jserver.gameserver.model.olympiad.Olympiad;
 import com.l2jserver.gameserver.network.SystemMessageId;
 import com.l2jserver.gameserver.network.communityserver.CommunityServerThread;
@@ -528,6 +529,8 @@ public class Shutdown extends Thread
 		_log.info("TradeController: All count Item Saved");
 		Olympiad.getInstance().saveOlympiadStatus();
 		_log.info("Olympiad System: Data saved!!");
+		Hero.getInstance().shutdown();
+		_log.info("Hero System: Data saved!!");
 		ClanTable.getInstance().storeClanScore();
 		_log.info("Clan System: Data saved!!");
 		

+ 88 - 1
L2_GameServer/java/com/l2jserver/gameserver/model/entity/Hero.java

@@ -81,6 +81,7 @@ public class Hero
 	private static List<StatsSet> _fights;
 	
 	private static Map<Integer, List<StatsSet>> _herodiary;
+	private static Map<Integer, String> _heroMessage;
 	private static List<StatsSet> _diary;
 	
 	public static final String COUNT = "count";
@@ -112,6 +113,7 @@ public class Hero
 		_herofights = new FastMap<Integer, List<StatsSet>>();
 		_herocounts = new FastMap<Integer, StatsSet>();		
 		_herodiary = new FastMap<Integer, List<StatsSet>>();
+		_heroMessage = new FastMap<Integer, String>();
 		
 		Connection con = null;
 		Connection con2 = null;
@@ -138,8 +140,9 @@ public class Hero
 				hero.set(COUNT, rset.getInt(COUNT));
 				hero.set(PLAYED, rset.getInt(PLAYED));
 				
-				loadFights(charId);				
+				loadFights(charId);
 				loadDiary(charId);
+				loadMessage(charId);
 				
 				statement2 = con2.prepareStatement(GET_CLAN_ALLY);
 				statement2.setInt(1, charId);
@@ -263,6 +266,36 @@ public class Hero
 		return time;
 	}
 	
+	/**
+	 * Restore hero message from Db.
+	 * @param charId
+	 */
+	public void loadMessage(int charId)
+	{
+		Connection con = null;
+		try
+		{
+			String message = null;
+			con = L2DatabaseFactory.getInstance().getConnection();
+			PreparedStatement statement = con.prepareStatement("SELECT message FROM heroes WHERE charId=?");
+			statement.setInt(1, charId);
+			ResultSet rset = statement.executeQuery();
+			rset.next();
+			message = rset.getString("message");
+			_heroMessage.put(charId, message);
+			rset.close();
+            statement.close();
+		}
+		catch (SQLException e)
+		{
+			_log.log(Level.WARNING, "Hero System: Couldnt load Hero Message for CharId: " + charId, e);
+		}
+		finally
+		{
+			L2DatabaseFactory.close(con);
+		}
+	}
+	
 	public void loadDiary(int charId)
 	{
 		_diary = new FastList<StatsSet>();
@@ -510,6 +543,8 @@ public class Hero
 			{
 				DiaryReply.setHtml(htmContent);
 				DiaryReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
+				DiaryReply.replace("%message%", _heroMessage.get(charid));
+				DiaryReply.disableValidation();
 				
 				if (!_mainlist.isEmpty())
 				{
@@ -597,6 +632,7 @@ public class Hero
 			{
 				FightReply.setHtml(htmContent);
 				FightReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
+				FightReply.disableValidation();
 				
 				if (!_list.isEmpty())
 				{
@@ -1016,6 +1052,44 @@ public class Hero
 		}
 	}
 	
+	/**
+	 * Set new hero message for hero
+	 * @param charId character objid
+	 * @param message String to set
+	 */
+	public void setHeroMessage(int charId, String message)
+	{
+		_heroMessage.put(charId, message);
+	}
+	
+	/**
+	 * Update hero message in database
+	 * @param charId character objid
+	 */
+	public void saveHeroMessage(int charId)
+	{
+		if (_heroMessage.get(charId) == null)
+			return;
+		Connection con = null;
+		try
+		{
+			con = L2DatabaseFactory.getInstance().getConnection();
+			PreparedStatement statement = con.prepareStatement("UPDATE heroes SET message=? WHERE charId=?;");
+			statement.setString(1, _heroMessage.get(charId));
+			statement.setInt(2, charId);
+			statement.execute();
+			statement.close();
+		}
+		catch (SQLException e)
+		{
+			_log.log(Level.SEVERE, "SQL exception while saving HeroMessage.", e);
+		}
+		finally
+		{
+			L2DatabaseFactory.close(con);
+		}
+	}
+	
 	private void deleteItemsInDb()
 	{
 		Connection con = null;
@@ -1037,6 +1111,19 @@ public class Hero
 		}
 	}
 	
+	/**
+	 * Saving task for {@link Hero}<BR>
+	 * Save all hero messages to DB.
+	 */
+	public void shutdown()
+	{
+		for (int charId: _heroMessage.keySet())
+		{
+			saveHeroMessage(charId);
+		}
+	}
+	
+	
 	@SuppressWarnings("synthetic-access")
 	private static class SingletonHolder
 	{

+ 16 - 6
L2_GameServer/java/com/l2jserver/gameserver/network/clientpackets/RequestWriteHeroWords.java

@@ -14,6 +14,9 @@
  */
 package com.l2jserver.gameserver.network.clientpackets;
 
+import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
+import com.l2jserver.gameserver.model.entity.Hero;
+
 /**
  * Format chS
  * c (id) 0xD0
@@ -25,9 +28,9 @@ package com.l2jserver.gameserver.network.clientpackets;
 public final class RequestWriteHeroWords extends L2GameClientPacket
 {
 	private static final String _C__FE_0C_REQUESTWRITEHEROWORDS = "[C] D0:0C RequestWriteHeroWords";
-	@SuppressWarnings("unused")
+
 	private String _heroWords;
-	
+
 	/**
 	 * @param buf
 	 * @param client
@@ -37,16 +40,23 @@ public final class RequestWriteHeroWords extends L2GameClientPacket
 	{
 		_heroWords = readS();
 	}
-	
+
 	@Override
 	protected void runImpl()
 	{
+		final L2PcInstance player = getClient().getActiveChar();
+		if (player == null || !player.isHero())
+			return;
+
+		if (_heroWords == null || _heroWords.length() > 300)
+			return;
+
+		Hero.getInstance().setHeroMessage(player.getObjectId(), _heroWords);
 	}
-	
+
 	@Override
 	public String getType()
 	{
 		return _C__FE_0C_REQUESTWRITEHEROWORDS;
 	}
-	
-}
+}