xSweeTs

Easy zone creation

Jul 24th, 2021
947
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Eclipse Workspace Patch 1.0
  2. #P aCis_datapack
  3. Index: data/html/admin/zone.htm
  4. ===================================================================
  5. --- data/html/admin/zone.htm    (revision 15)
  6. +++ data/html/admin/zone.htm    (working copy)
  7. @@ -6,11 +6,12 @@
  8.             <td width=45><button value="Back" action="bypass -h admin_admin4" width=45 height=15 back="sek.cbui94" fore="sek.cbui92"></td>
  9.         </tr>
  10.     </table><br>
  11. -   <center><table width=225>
  12. +   <center><table width=260>
  13.         <tr>
  14. -           <td><button value="Refresh" action="bypass -h admin_zone_check" width=75 height=21 back="L2UI_ch3.Btn1_normalOn" fore="L2UI_ch3.Btn1_normal"></td>
  15. -           <td><button value="Visualize all" action="bypass -h admin_zone_visual all" width=75 height=21 back="L2UI_ch3.Btn1_normalOn" fore="L2UI_ch3.Btn1_normal"></td>
  16. -           <td><button value="Clear" action="bypass -h admin_zone_visual clear" width=75 height=21 back="L2UI_ch3.Btn1_normalOn" fore="L2UI_ch3.Btn1_normal"></td>
  17. +           <td><button value="Create" action="bypass -h admin_create_zone" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  18. +           <td><button value="Refresh" action="bypass -h admin_zone_check" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  19. +           <td><button value="Visualize" action="bypass -h admin_zone_visual all" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  20. +           <td><button value="Clear" action="bypass -h admin_zone_visual clear" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  21.         </tr>
  22.     </table>
  23.     <br><img src="l2ui.SquareGray" width=270 height=1><br>
  24. Index: data/html/admin/zone_create.htm
  25. ===================================================================
  26. --- data/html/admin/zone_create.htm (revision 0)
  27. +++ data/html/admin/zone_create.htm (revision 0)
  28. @@ -0,0 +1,33 @@
  29. +<html><title>Zone Creation</title><body>
  30. +   <table width=270>
  31. +       <tr>
  32. +           <td width=45><button value="Main" action="bypass -h admin_admin" width=45 height=15 back="sek.cbui94" fore="sek.cbui92"></td>
  33. +           <td width=180 align=center>Zone Creation</td>
  34. +           <td width=45><button value="Back" action="bypass -h admin_zone_check" width=45 height=15 back="sek.cbui94" fore="sek.cbui92"></td>
  35. +       </tr>
  36. +   </table><br>
  37. +   <center>
  38. +   <img src=l2ui.SquareGray width=270 height=1>
  39. +   <table width=270>
  40. +       <tr>
  41. +           <td width=65>Type:</td>
  42. +           <td width=140><combobox width="100" var="zoneType" list=NPoly;Cuboid;Cylinder></td>
  43. +           <td align=right width=65><button value="Select" action="bypass admin_setType $zoneType" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  44. +       </tr>
  45. +   </table><br>
  46. +   <img src=l2ui.SquareGray width=270 height=1>
  47. +   <table width=270>
  48. +       <tr><td width=140>Type: <font color=LEVEL>%zoneShape%</font></td></tr>
  49. +       <tr><td width=140>%locsSize%</td></tr>
  50. +   </table>   
  51. +   <img src=l2ui.SquareGray width=270 height=1><br>
  52. +   <table width=280>
  53. +       <tr>
  54. +           <td align=center width=70>%proceed%</td>
  55. +           <td align=center width=70>%undo%</td>
  56. +           <td align=center width=70><button value="Add Loc" action="bypass admin_saveLoc" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  57. +           <td align=center width=70><button value="Reset" action="bypass admin_reset" width=65 height=19 back="L2UI_ch3.smallbutton2_over" fore="L2UI_ch3.smallbutton2"></td>
  58. +       </tr>
  59. +   </table>
  60. +   <br><br>%dist%
  61. +</center></body></html>
  62. \ No newline at end of file
  63. Index: data/xml/adminCommands.xml
  64. ===================================================================
  65. --- data/xml/adminCommands.xml  (revision 15)
  66. +++ data/xml/adminCommands.xml  (working copy)
  67. @@ -335,4 +335,13 @@
  68.     <!-- ZONE -->
  69.     <aCar name="admin_zone_check" accessLevel="7"/>
  70.     <aCar name="admin_zone_visual" accessLevel="7"/>
  71. +  
  72. +   <!-- ZONE CREATE -->
  73. +   <aCar name="admin_create_zone" accessLevel="7"/>
  74. +   <aCar name="admin_setType" accessLevel="7"/>
  75. +   <aCar name="admin_saveLoc" accessLevel="7"/>
  76. +   <aCar name="admin_setRad" accessLevel="7"/>
  77. +   <aCar name="admin_reset" accessLevel="7"/>
  78. +   <aCar name="admin_removeLoc" accessLevel="7"/>
  79. +   <aCar name="admin_storeLocs" accessLevel="7"/>
  80.  </list>
  81. \ No newline at end of file
  82. #P aCis_gameserver
  83. Index: java/net/sf/l2j/gameserver/handler/AdminCommandHandler.java
  84. ===================================================================
  85. --- java/net/sf/l2j/gameserver/handler/AdminCommandHandler.java (revision 15)
  86. +++ java/net/sf/l2j/gameserver/handler/AdminCommandHandler.java (working copy)
  87. @@ -46,6 +46,7 @@
  88.  import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminTarget;
  89.  import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminTeleport;
  90.  import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminZone;
  91. +import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminZoneCreation;
  92.  
  93.  public class AdminCommandHandler
  94.  {
  95. @@ -96,6 +97,7 @@
  96.         registerHandler(new AdminTarget());
  97.         registerHandler(new AdminTeleport());
  98.         registerHandler(new AdminZone());
  99. +       registerHandler(new AdminZoneCreation());
  100.     }
  101.    
  102.     private void registerHandler(IAdminCommandHandler handler)
  103. Index: java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminZoneCreation.java
  104. ===================================================================
  105. --- java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminZoneCreation.java  (revision 0)
  106. +++ java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminZoneCreation.java  (revision 0)
  107. @@ -0,0 +1,322 @@
  108. +package net.sf.l2j.gameserver.handler.admincommandhandlers;
  109. +
  110. +import java.io.BufferedWriter;
  111. +import java.io.File;
  112. +import java.io.FileWriter;
  113. +import java.io.IOException;
  114. +import java.text.SimpleDateFormat;
  115. +import java.util.ArrayList;
  116. +import java.util.Date;
  117. +import java.util.List;
  118. +import java.util.StringTokenizer;
  119. +
  120. +import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  121. +import net.sf.l2j.gameserver.model.actor.instance.Player;
  122. +import net.sf.l2j.gameserver.model.location.Location;
  123. +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  124. +
  125. +/**
  126. + * @author melron
  127. + */
  128. +public class AdminZoneCreation implements IAdminCommandHandler
  129. +{
  130. +   private static final List<Location> savedLocs = new ArrayList<>();
  131. +   private static final String fileName = "coordinates" + "%s" + ".xml";
  132. +   private static ZoneShape shape = ZoneShape.NONE;
  133. +   private static final int zDifference = 1000;
  134. +   private static final String maxLocs = "You have reached the maximum locations for this shape.";
  135. +   private static int radius = 0;
  136. +  
  137. +   private static enum ZoneShape
  138. +   {
  139. +       NONE,
  140. +       NPoly,
  141. +       Cuboid,
  142. +       Cylinder
  143. +   }
  144. +  
  145. +   private static final String[] ADMIN_COMMANDS =
  146. +   {
  147. +       "admin_create_zone",
  148. +       "admin_setType",
  149. +       "admin_setRad",
  150. +       "admin_saveLoc",
  151. +       "admin_reset",
  152. +       "admin_removeLoc",
  153. +       "admin_storeLocs",
  154. +   };
  155. +  
  156. +   @Override
  157. +   public boolean useAdminCommand(String command, Player activeChar)
  158. +   {
  159. +       final StringTokenizer st = new StringTokenizer(command);
  160. +       st.nextToken();
  161. +       if (command.startsWith("admin_create_zone"))
  162. +           openHtml(activeChar);
  163. +       else if (command.startsWith("admin_setType"))
  164. +       {
  165. +           clear();
  166. +           shape = ZoneShape.valueOf(st.nextToken());
  167. +           switch (shape)
  168. +           {
  169. +               case NPoly:
  170. +                   activeChar.sendMessage("You can add unlimited but atleast 3 coordinates in this shape.");
  171. +                   break;
  172. +               case Cuboid:
  173. +                   activeChar.sendMessage("You must add 2 coordinates (in order to make a square) in this shape.");
  174. +                   break;
  175. +               case Cylinder:
  176. +                   activeChar.sendMessage("You must add 1 coordinates and radius (in order to make a circle) in this shape.");
  177. +                   break;
  178. +               default:
  179. +                   activeChar.sendMessage("You have to select the zone shape first.");
  180. +           }
  181. +           openHtml(activeChar);
  182. +       }
  183. +       else if (command.startsWith("admin_saveLoc"))
  184. +       {
  185. +           if (canSaveLoc(activeChar))
  186. +           {
  187. +               final Location loc = new Location(activeChar.getX(), activeChar.getY(), activeChar.getZ());
  188. +               if (savedLocs.add(loc))
  189. +                   activeChar.sendMessage(loc + " saved..");
  190. +           }
  191. +           openHtml(activeChar);
  192. +       }
  193. +       else if (command.startsWith("admin_reset"))
  194. +       {
  195. +           clear();
  196. +           activeChar.sendMessage("Reset completed.");
  197. +           openHtml(activeChar);
  198. +       }
  199. +       else if (command.startsWith("admin_removeLoc"))
  200. +       {
  201. +           if (savedLocs.size() > 0)
  202. +               activeChar.sendMessage(savedLocs.remove(savedLocs.size() - 1) + " removed.");
  203. +           openHtml(activeChar);
  204. +       }
  205. +       else if (command.startsWith("admin_storeLocs"))
  206. +       {
  207. +           if (savedLocs.isEmpty())
  208. +           {
  209. +               activeChar.sendMessage("Empty locs..");
  210. +               return false;
  211. +           }
  212. +           store(activeChar);
  213. +       }
  214. +       else if (command.startsWith("admin_setRad"))
  215. +       {
  216. +           if (!st.hasMoreTokens() || !setRadius(parseInt(st.nextToken())))
  217. +               activeChar.sendMessage("Invalid value or shape.");
  218. +           else
  219. +               activeChar.sendMessage("Radius stored.");
  220. +          
  221. +           openHtml(activeChar);
  222. +       }
  223. +       return true;
  224. +   }
  225. +  
  226. +   private static int calcZ(boolean minZ)
  227. +   {
  228. +       return (savedLocs.stream().mapToInt(loc -> loc.getZ()).sum() / savedLocs.size()) + (minZ ? -zDifference : zDifference);
  229. +   }
  230. +  
  231. +   private static int parseInt(String nextToken)
  232. +   {
  233. +       try
  234. +       {
  235. +           return Integer.parseInt(nextToken);
  236. +       }
  237. +       catch (NumberFormatException e)
  238. +       {
  239. +           return 0;
  240. +       }
  241. +   }
  242. +  
  243. +   private static void store(Player gm)
  244. +   {
  245. +       if (!canStoreLocs(gm))
  246. +           return;
  247. +       final String fName = String.format(fileName, "_" + shape.name() + "_" + getTimeStamp());
  248. +       String filePath = "";
  249. +       try (BufferedWriter writer = new BufferedWriter(new FileWriter(fName)))
  250. +       {
  251. +           final File file = new File(fName);
  252. +           filePath = file.getAbsolutePath().replaceAll("\\\\", "/");
  253. +           writer.write(getHeadLine());
  254. +           for (Location loc : savedLocs)
  255. +               writer.write(String.format("\t<node X=\"%s\" Y=\"%s\" />\r\n", loc.getX(), loc.getY()));
  256. +          
  257. +           writer.write("</zone>");
  258. +       }
  259. +       catch (IOException e)
  260. +       {
  261. +           gm.sendMessage(String.format("Couldn't store coordinates in %s file.", fName));
  262. +           e.printStackTrace();
  263. +       }
  264. +       gm.sendMessage("Coordinates has been successfully stored at " + filePath);
  265. +       clear();
  266. +       openHtml(gm);
  267. +   }
  268. +  
  269. +   private static String getTimeStamp()
  270. +   {
  271. +       return new SimpleDateFormat("hh-mm-ss").format(new Date());
  272. +   }
  273. +  
  274. +   private static boolean canStoreLocs(Player gm)
  275. +   {
  276. +       switch (shape)
  277. +       {
  278. +           case NPoly:
  279. +               if (savedLocs.size() < 3)
  280. +               {
  281. +                   gm.sendMessage("You have to set atleast 3 coordinates!");
  282. +                   return false;
  283. +               }
  284. +               return true;
  285. +           case Cuboid:
  286. +               if (savedLocs.size() != 2)
  287. +               {
  288. +                   gm.sendMessage("You have to set 2 coordinates.");
  289. +                   return false;
  290. +               }
  291. +               return true;
  292. +           case Cylinder:
  293. +               if (savedLocs.size() != 1)
  294. +               {
  295. +                   gm.sendMessage("Only 1 location required for this shape.");
  296. +                   return false;
  297. +               }
  298. +               return true;
  299. +           default:
  300. +               gm.sendMessage("You have to select the zone shape first.");
  301. +               return false;
  302. +       }
  303. +   }
  304. +  
  305. +   private static boolean canSaveLoc(Player activeChar)
  306. +   {
  307. +       switch (shape)
  308. +       {
  309. +           case NPoly:
  310. +               return true; // unlimited locs can be added
  311. +           case Cuboid:
  312. +               if (savedLocs.size() >= 2)
  313. +               {
  314. +                   activeChar.sendMessage(maxLocs);
  315. +                   return false;
  316. +               }
  317. +               return true;
  318. +           case Cylinder:
  319. +               if (savedLocs.size() >= 1)
  320. +               {
  321. +                   activeChar.sendMessage("Only 1 locations required for this shape.");
  322. +                   return false;
  323. +               }
  324. +               return true;
  325. +           default:
  326. +               activeChar.sendMessage("You have to select the zone shape first.");
  327. +               return false;
  328. +       }
  329. +   }
  330. +  
  331. +   private static String getHeadLine()
  332. +   {
  333. +       switch (shape)
  334. +       {
  335. +           case NPoly:
  336. +               return String.format("<zone shape='NPoly' minZ='%s' maxZ='%s'>\r\n", calcZ(true), calcZ(false));
  337. +           case Cuboid:
  338. +               return String.format("<zone shape='Cuboid' minZ='%s' maxZ='%s'>\r\n", calcZ(true), calcZ(false));
  339. +           case Cylinder:
  340. +               return String.format("<zone shape='Cylinder' minZ='%s' maxZ='%s' rad='%s'>\r\n", calcZ(true), calcZ(false), getRad());
  341. +           default:
  342. +               return "";
  343. +       }
  344. +   }
  345. +  
  346. +   private static boolean setRadius(int val)
  347. +   {
  348. +       if (shape == ZoneShape.Cylinder)
  349. +       {
  350. +           if (val == 0)
  351. +               return false;
  352. +           radius = val;
  353. +       }
  354. +      
  355. +       return shape == ZoneShape.Cylinder;
  356. +   }
  357. +  
  358. +   private static int getRad()
  359. +   {
  360. +       return radius;
  361. +   }
  362. +  
  363. +   private static void openHtml(Player activeChar)
  364. +   {
  365. +       final NpcHtmlMessage html = new NpcHtmlMessage(0);
  366. +       html.setFile("data/html/admin/zone_create.htm");
  367. +       switch (shape)
  368. +       {
  369. +           case NONE:
  370. +               html.replace("%zoneShape%", "Empty");
  371. +               html.replace("%locsSize%", "Locations Saved: Null");
  372. +               html.replace("%proceed%", "");
  373. +               html.replace("%undo%", savedLocs.size() > 0 ? "<button value=\"Undo\" action=\"bypass admin_removeLoc\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">" : "");
  374. +               html.replace("%dist%", "");
  375. +               break;
  376. +           case NPoly:
  377. +               html.replace("%zoneShape%", shape.name());
  378. +               html.replace("%locsSize%", "Locations Saved: " + savedLocs.size());
  379. +               html.replace("%proceed%", savedLocs.size() > 2 ? "<button value=\"Store\" action=\"bypass admin_storeLocs\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">" : "");
  380. +               html.replace("%undo%", savedLocs.size() > 0 ? "<button value=\"Undo\" action=\"bypass admin_removeLoc\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">" : "");
  381. +               html.replace("%dist%", "");
  382. +               break;
  383. +           case Cuboid:
  384. +               html.replace("%zoneShape%", shape.name());
  385. +               html.replace("%locsSize%", "Locations Saved: " + savedLocs.size());
  386. +               html.replace("%proceed%", savedLocs.size() == 2 ? "<button value=\"Store\" action=\"bypass admin_storeLocs\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">" : "");
  387. +               html.replace("%undo%", savedLocs.size() > 0 ? "<button value=\"Undo\" action=\"bypass admin_removeLoc\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">" : "");
  388. +               html.replace("%dist%", "");
  389. +               break;
  390. +           case Cylinder:
  391. +               html.replace("%zoneShape%", shape.name());
  392. +               html.replace("%locsSize%", "Locations Saved: " + savedLocs.size());
  393. +               html.replace("%undo%", savedLocs.size() == 1 ? "<button value=\"Undo\" action=\"bypass admin_removeLoc\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">" : "");
  394. +               if (savedLocs.size() == 1)
  395. +               {
  396. +                   if (radius == 0)
  397. +                   {
  398. +                       html.replace("%proceed%", "");
  399. +                       html.replace("%dist%", "Set the radius: <edit var=\"Radius\" width=110 height=15> <button value=\"Save\" action=\"bypass admin_setRad $Radius\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">");
  400. +                   }
  401. +                   else
  402. +                   {
  403. +                       html.replace("%proceed%", "<button value=\"Store\" action=\"bypass admin_storeLocs\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\">");
  404. +                       html.replace("%dist%", "");
  405. +                   }
  406. +               }
  407. +               else
  408. +               {
  409. +                   html.replace("%proceed%", "");
  410. +                   html.replace("%dist%", "");
  411. +               }
  412. +               break;
  413. +       }
  414. +       activeChar.sendPacket(html);
  415. +   }
  416. +  
  417. +   private static void clear()
  418. +   {
  419. +       shape = ZoneShape.NONE;
  420. +       savedLocs.clear();
  421. +       radius = 0;
  422. +   }
  423. +  
  424. +   @Override
  425. +   public String[] getAdminCommandList()
  426. +   {
  427. +       return ADMIN_COMMANDS;
  428. +   }
  429. +}
  430. \ No newline at end of file
RAW Paste Data