Guest User

Vote Reward

a guest
Aug 25th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. /*
  2.  * This is a script completely developed by Rain^ (?)
  3.  * You are not allowed to sell any copies of it.
  4.  * Since 2.0 (by Zoey76 for L2J Forums):
  5.  * Added logger instead of System.out.println()
  6.  * Moved to Datapack.
  7.  * Reworked AutoReward class.
  8.  * Reworked getVotes() method.
  9.  * Time is in minutes instead of milliseconds.
  10.  * Uses different amount for each item.
  11.  * Only rewards online players, not offline shops.
  12.  * Rewarded players count.
  13.  */
  14. package custom.VoteEngine;
  15.  
  16. import java.io.*;
  17. import java.net.*;
  18. import java.util.Collection;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21.  
  22. import net.sf.l2j.gameserver.Announcements;
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. import net.sf.l2j.gameserver.model.L2World;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26.  
  27. /**
  28.  * @author Zoey76
  29.  * @version 2.0
  30.  */
  31. public class AutoVoteRewardHandler
  32. {
  33.     protected static final Logger _log = Logger.getLogger(AutoVoteRewardHandler.class.getName());
  34.     //URL from your server site at HopZone.net
  35.     //Example: http://l2.hopzone.net/lineage2/moreinfo/YourServer/ID.html
  36.     private final static String _url = "http://l2.hopzone.net/lineage2/details/91914/L2NetherWorld";//Add your URL from HopZone here!
  37.     //Reward all online players each '_votesRequiredForReward' votes.
  38.     private final int _votesRequiredForReward = 1;//
  39.     //Initial check, time in minutes:
  40.     //Default: 1 minute
  41.     private final int initialCheck = 5;
  42.     //Delay interval, time in minutes (do not set it too low):
  43.     //Default: 10 minutes
  44.     private final int delayForCheck = 5;
  45.    
  46.     //Item Id, Amount.
  47.     private final static int[][] ITEMs =
  48.     {
  49.         { 5575, 10 }, //10 Ancient Adena
  50.         { 57, 1000000 }, //1kk Adena
  51.     };
  52.    
  53.     //Do not change
  54.     private int _lastVoteCount = 0;
  55.    
  56.     private AutoVoteRewardHandler()
  57.     {
  58.         _log.info("[AutoVoteRewardHandler]: Vote Reward System Initiated.");
  59.         ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), initialCheck * 60000, delayForCheck * 60000);
  60.     }
  61.    
  62.     private class AutoReward implements Runnable
  63.     {
  64.         public void run()
  65.         {
  66.             int votes = getVotes();
  67.             int rewardedPlayers = 0;
  68.             if (votes > -1)
  69.             {
  70.                 if  ((getLastVoteCount() != 0) && (votes >= (getLastVoteCount() + _votesRequiredForReward)))
  71.                 {
  72.                     Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  73.                     for (L2PcInstance player : pls)
  74.                     {
  75.                         if ((player != null) && !player.getClient().isDetached())
  76.                         {
  77.                             for (int[] reward : ITEMs)
  78.                             {
  79.                                 if (player.getInventory().validateCapacityByItemId(reward[0]))
  80.                                 {
  81.                                     player.addItem("reward", reward[0], reward[1], player, true);
  82.                                 }
  83.                             }
  84.                             rewardedPlayers++;
  85.                         }
  86.                     }
  87.                     setLastVoteCount(getLastVoteCount() + _votesRequiredForReward);
  88.                 }
  89.                 else if (getLastVoteCount() == 0)
  90.                 {
  91.                     setLastVoteCount(votes);
  92.                 }
  93.                 _log.info("[AutoVoteRewardHandler]: Server Votes: " + votes + ", Rewarded players: " + rewardedPlayers);
  94.                 Announcements.getInstance().announceToAll("Server Votes: " + votes + " | Next Reward on " + (getLastVoteCount() + _votesRequiredForReward) + " votes!");
  95.             }
  96.             else
  97.             {
  98.                 _log.log(Level.WARNING, "[AutoVoteRewardHandler]: Error retreiving server votes count!");
  99.             }
  100.         }
  101.     }
  102.    
  103.     private static int getVotes()
  104.     {
  105.             InputStreamReader isr = null;
  106.         BufferedReader in = null;
  107.         int votes = -1;
  108.         try
  109.         {
  110.         URLConnection conn = new URL(_url).openConnection();
  111.         conn.addRequestProperty("User-Agent", "Mozilla/4.76");
  112.         isr = new InputStreamReader(conn.getInputStream());
  113.             in = new BufferedReader(isr);
  114.             String inputLine;
  115.             while (((inputLine = in.readLine()) != null) && (votes == -1))
  116.             {
  117.                 if (inputLine.contains("Anonymous User Votes"))
  118.                 {
  119.                     try
  120.                     {
  121.                         votes = Integer.valueOf(inputLine.split(">")[2].replace("</span", ""));
  122.                     }
  123.                     catch (Exception e)
  124.                     {
  125.                     }
  126.                 }
  127.             }
  128.             in.close();
  129.         }
  130.         catch (Exception e)
  131.         {
  132.             _log.log(Level.WARNING, "[AutoVoteRewardHandler]: " + e.getMessage(), e);
  133.         }
  134.         return votes;
  135.     }
  136.    
  137.     private void setLastVoteCount(int voteCount)
  138.     {
  139.         _lastVoteCount = voteCount;
  140.     }
  141.    
  142.     private int getLastVoteCount()
  143.     {
  144.         return _lastVoteCount;
  145.     }
  146.    
  147.     public static AutoVoteRewardHandler getInstance()
  148.     {
  149.         return SingletonHolder._instance;
  150.     }
  151.    
  152.     @SuppressWarnings("synthetic-access")
  153.     private static class SingletonHolder
  154.     {
  155.         protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
  156.     }
  157.    
  158.     public static void main(String[] args)
  159.     {
  160.         //System.out.println("Server votes: " + getVotes());//Just a test.
  161.         AutoVoteRewardHandler.getInstance();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment