· 5 years ago · Jun 12, 2020, 02:44 PM
1SET NAMES utf8mb4;
2SET FOREIGN_KEY_CHECKS = 0;
3
4-- ----------------------------
5-- Table structure for custom_hero
6-- ----------------------------
7DROP TABLE IF EXISTS `custom_hero`;
8CREATE TABLE `custom_hero` (
9 `obj_Id` decimal(11, 0) NOT NULL DEFAULT 0,
10 `char_name` varchar(35) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
11 `hero` decimal(1, 0) NOT NULL DEFAULT 0,
12 `hero_end_date` bigint(20) NOT NULL DEFAULT 0,
13 PRIMARY KEY (`obj_Id`) USING BTREE
14) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
15
16SET FOREIGN_KEY_CHECKS = 1;
17
18+++ config/Mods/Custom Npcs.ini
19+#Hero Item
20+#When use this item will gain Hero Status.
21+EnableHeroCustomItem = True
22+#Id Item
23+HeroCustomItemId = 3481
24+#Hero for x days, 0 forever.
25+HeroCustomDay = 1
26
27+++ java/com/l2jhellas/Config.java
28+ public static boolean HERO_CUSTOM_ITEMS;
29+ public static int HERO_CUSTOM_ITEM_ID;
30+ public static int HERO_CUSTOM_DAY;
31+ HERO_CUSTOM_ITEMS = Boolean.parseBoolean(CustomNpcSettings.getProperty("EnableHeroCustomItem", "true"));
32+ HERO_CUSTOM_ITEM_ID = Integer.parseInt(CustomNpcSettings.getProperty("HeroCustomItemId", "3481"));
33+ HERO_CUSTOM_DAY = Integer.parseInt(CustomNpcSettings.getProperty("HeroCustomDay", "1"));
34
35+++ java/com/l2jhellas/gameserver/handlers/itemhandlers/HeroCustomItem.java
36+package com.l2jhellas.gameserver.handlers.itemhandlers;
37+
38+import java.sql.Connection;
39+import java.sql.PreparedStatement;
40+
41+import com.l2jhellas.Config;
42+import com.l2jhellas.gameserver.handler.IItemHandler;
43+import com.l2jhellas.gameserver.model.actor.L2Playable;
44+import com.l2jhellas.gameserver.model.actor.instance.L2PcInstance;
45+import com.l2jhellas.gameserver.model.actor.item.L2ItemInstance;
46+import com.l2jhellas.gameserver.network.serverpackets.SocialAction;
47+import com.l2jhellas.gameserver.network.serverpackets.SystemMessage;
48+import com.l2jhellas.gameserver.network.SystemMessageId;
49+import com.l2jhellas.shield.antiflood.FloodProtectors;
50+import com.l2jhellas.shield.antiflood.FloodProtectors.Action;
51+import com.l2jhellas.util.database.L2DatabaseFactory;
52+/**
53+ * @author bruns87
54+ */
55+public class HeroCustomItem implements IItemHandler
56+{
57+ String INSERT_DATA = "REPLACE INTO custom_hero (obj_Id, char_name, hero, hero_end_date) VALUES (?,?,?,?)";
58+
59+ @Override
60+ public void useItem(L2Playable playable, L2ItemInstance item)
61+ {
62+ if (Config.HERO_CUSTOM_ITEMS)
63+ {
64+
65+ if (!(playable.isPlayer()))
66+ return;
67+
68+ final L2PcInstance activeChar = (L2PcInstance) playable;
69+
70+ if (!FloodProtectors.performAction(activeChar.getClient(), Action.ITEM_HANDLER))
71+ {
72+ activeChar.sendMessage("You may not use the item at this time. Try again later.");
73+ return;
74+ }
75+
76+ final L2ItemInstance box = activeChar.getInventory().getItemByObjectId(item.getObjectId());
77+
78+ if(box == null)
79+ {
80+ activeChar.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
81+ return;
82+ }
83+
84+ if (activeChar.isInOlympiadMode())
85+ {
86+ activeChar.sendMessage("This Item Cannot Be Used On Olympiad Games.");
87+ return;
88+ }
89+
90+ if (activeChar.isHero())
91+ {
92+ activeChar.sendMessage("You Are Already A Hero!.");
93+ return;
94+ }
95+
96+ final int itemId = box.getItemId();
97+
98+ if (!activeChar.destroyItem("HeroCustomItem",box.getObjectId(), 1, null, false))
99+ {
100+ activeChar.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
101+ return;
102+ }
103+
104+ activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_DISAPPEARED).addItemName(itemId));
105+
106+ switch (itemId)
107+ {
108+ case 3481:
109+ activeChar.broadcastPacket(new SocialAction(activeChar.getObjectId(), 16));
110+ activeChar.setHero(true);
111+ updateDatabase(activeChar, Config.HERO_CUSTOM_DAY * 24L * 60L * 60L * 1000L);
112+ activeChar.sendMessage("You Are Now a Hero,You Are Granted With Hero Status , Skills ,Aura.");
113+ activeChar.broadcastUserInfo();
114+ break;
115+ }
116+ }
117+ }
118+
119+ private void updateDatabase(final L2PcInstance player, final long heroTime)
120+ {
121+ Connection con = null;
122+ try
123+ {
124+ if (player == null)
125+ return;
126+
127+ con = L2DatabaseFactory.getInstance().getConnection();
128+ PreparedStatement stmt = con.prepareStatement(INSERT_DATA);
129+
130+ stmt.setInt(1, player.getObjectId());
131+ stmt.setString(2, player.getName());
132+ stmt.setInt(3, 1);
133+ stmt.setLong(4, heroTime == 0 ? 0 : System.currentTimeMillis() + heroTime);
134+ stmt.execute();
135+ stmt.close();
136+ stmt = null;
137+ }
138+ catch (final Exception e)
139+ {
140+ e.printStackTrace();
141+ }
142+ finally
143+ {
144+ try
145+ {
146+ if (con == null || con.isClosed())
147+ con = L2DatabaseFactory.getInstance().getConnection();
148+ } catch (final Exception e)
149+ {
150+ e.printStackTrace();
151+ }
152+ con = null;
153+ }
154+ }
155+
156+ public void addItem(L2PcInstance activeChar, int itemId)
157+ {
158+ activeChar.addItem("HeroCustomItem", itemId, 1, null,false);
159+ activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1).addItemName(itemId));
160+ }
161+
162+ //TODO check
163+ private static final int[] ITEM_IDS =
164+ {
165+ // 3481//hero item
166+ Config.HERO_CUSTOM_ITEM_ID
167+ };
168+
169+ @Override
170+ public int[] getItemIds()
171+ {
172+ return ITEM_IDS;
173+ }
174+}
175\ No newline at end of file
176
177+++ java/com/l2jhellas/gameserver/model/actor/instance/L2PcInstance.java
178+ private static final String STATUS_DATA_GET = "SELECT hero, hero_end_date FROM custom_hero WHERE obj_Id = ?";
179+
180+ public void restoreCustomStatus()
181+ {
182+ int hero = 0;
183+ long hero_end = 0;
184+
185+ try (Connection con = L2DatabaseFactory.getInstance().getConnection())
186+ {
187+ PreparedStatement statement = con.prepareStatement(STATUS_DATA_GET);
188+ statement.setInt(1, getObjectId());
189+
190+ ResultSet rset = statement.executeQuery();
191+
192+ while (rset.next())
193+ {
194+ hero = rset.getInt("hero");
195+ hero_end = rset.getLong("hero_end_date");
196+ }
197+
198+ rset.close();
199+ statement.close();
200+ }
201+ catch (SQLException e)
202+ {
203+ e.printStackTrace();
204+ _log.warning("Error: could not restore char custom data info: " + e);
205+ }
206+
207+ if (hero > 0 && (hero_end == 0 || hero_end > System.currentTimeMillis()))
208+ {
209+ setHero(true);
210+ }
211+
212+ }
213+
214
215+++ java/com/l2jhellas/gameserver/network/clientpackets/EnterWorld.java
216+ activeChar.restoreCustomStatus();
217
218+++ java/com/l2jhellas/gameserver/scrips/loaders/MasterHandler.java
219+import com.l2jhellas.gameserver.handlers.itemhandlers.HeroCustomItem;
220+ HeroCustomItem.class,