Advertisement
Giardev

autovotesystem

Jul 29th, 2022
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. AutoVoteRewardHandler.java
  2. -----------------------------------------------------------------------------------------------------------------
  3. public class AutoVoteRewardHandler
  4. {
  5.     protected static final Logger _LOG = LoggerFactory.getLogger(AutoVoteRewardHandler.class.getName());
  6.    
  7.     private int _hopzoneVotesCount = 0;
  8.     private int _l2jbrasilVotesCount = 0;
  9.     protected List<String> already_rewarded;
  10.    
  11.     protected static boolean _l2jbrasil = false;
  12.     protected static boolean _hopzone = false;
  13.    
  14.     private AutoVoteRewardHandler()
  15.     {
  16.         _LOG.info("Vote Reward System Initiated.");
  17.        
  18.         if (_hopzone)
  19.         {
  20.             int hopzone_votes = getHopZoneVotes();
  21.            
  22.             if (hopzone_votes == -1)
  23.             {
  24.                 hopzone_votes = 0;
  25.             }
  26.            
  27.             setHopZoneVoteCount(hopzone_votes);
  28.         }
  29.        
  30.         if (_l2jbrasil)
  31.         {
  32.             int _l2jbrasil_votes = getL2jBrasilVotes();
  33.            
  34.             if (_l2jbrasil_votes == -1)
  35.             {
  36.                 _l2jbrasil_votes = 0;
  37.             }
  38.            
  39.             setL2jBrasilVoteCount(_l2jbrasil_votes);
  40.         }
  41.        
  42.         ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), Config.VOTES_SYSYEM_INITIAL_DELAY, Config.VOTES_SYSYEM_STEP_DELAY);
  43.     }
  44.    
  45.     protected class AutoReward implements Runnable
  46.     {
  47.         @SuppressWarnings("unused")
  48.         @Override
  49.         public void run()
  50.         {
  51.             final int minutes = (Config.VOTES_SYSYEM_STEP_DELAY / 1000) / 60;
  52.            
  53.             if (_hopzone)
  54.             {
  55.                 final int _hotzone_votes = getHopZoneVotes();
  56.                
  57.                 if (_hotzone_votes != -1)
  58.                 {
  59.                     _LOG.info("HOPZONE Votes: " + _hotzone_votes);
  60.                     // Announcements.getInstance().gameAnnounceToAll("Hopzone: Votes are " + _hotzone_votes + "...");
  61.                    
  62.                     if ((_hotzone_votes != 0) && (_hotzone_votes >= (getHopZoneVoteCount() + Config.VOTES_FOR_REWARD)))
  63.                     {
  64.                         already_rewarded = new ArrayList<>();
  65.                        
  66.                         final Collection<L2PcInstance> pls = L2World.getInstance().getPlayers();
  67.                        
  68.                         Announcements.getInstance().gameAnnounceToAll("Hopzone: Great Work! Check your inventory!!");
  69.                        
  70.                         // L2ItemInstance item;
  71.                         for (L2PcInstance player : pls)
  72.                         {
  73.                             if ((player != null) && !player.isInOfflineMode() && (player.isOnline() == 1))
  74.                             {
  75.                                 if ((player._active_boxes <= 1) || ((player._active_boxes > 1) && checkSingleBox(player)))
  76.                                 {
  77.                                     final Set<Integer> items = Config.VOTES_REWARDS_LIST.keySet();
  78.                                     for (Integer i : items)
  79.                                     {
  80.                                         player.addItem("reward", i, Config.VOTES_REWARDS_LIST.get(i), player, true);
  81.                                     }
  82.                                 }
  83.                             }
  84.                         }
  85.                         setHopZoneVoteCount(_hotzone_votes);
  86.                     }
  87.                    
  88.                     // Announcements.getInstance().gameAnnounceToAll("Hopzone: Reward in " + minutes + " minutes at " + (getHopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes!!");
  89.                 }
  90.             }
  91.            
  92.             if (_l2jbrasil && _hopzone && (Config.VOTES_SYSYEM_STEP_DELAY > 0))
  93.             {
  94.                 try
  95.                 {
  96.                     Thread.sleep(Config.VOTES_SYSYEM_STEP_DELAY / 2);
  97.                 }
  98.                 catch (InterruptedException e)
  99.                 {
  100.                     if (Config.ENABLE_ALL_EXCEPTIONS)
  101.                     {
  102.                         e.printStackTrace();
  103.                     }
  104.                 }
  105.             }
  106.            
  107.             if (_l2jbrasil)
  108.             {
  109.                 int _l2jbrasil_votes = getL2jBrasilVotes();
  110.                
  111.                 if (_l2jbrasil_votes != -1)
  112.                 {
  113.                     _LOG.info("L2JBRASIL Votes: " + _l2jbrasil_votes);
  114.                     // Announcements.getInstance().gameAnnounceToAll("L2jBrasil: Votes are " + _l2jbrasil_votes + "...");
  115.                    
  116.                     if ((_l2jbrasil_votes != 0) && (_l2jbrasil_votes >= (getL2jBrasilVoteCount() + Config.VOTES_FOR_REWARD)))
  117.                     {
  118.                         already_rewarded = new ArrayList<>();
  119.                        
  120.                         final Collection<L2PcInstance> pls = L2World.getInstance().getPlayers();
  121.                        
  122.                         Announcements.getInstance().gameAnnounceToAll("L2jBrasil: Great Work! Check your inventory!!");
  123.                        
  124.                         // L2ItemInstance item;
  125.                         for (L2PcInstance player : pls)
  126.                         {
  127.                             if ((player != null) && !player.isInOfflineMode() && (player.isOnline() == 1))
  128.                             {
  129.                                 if ((player._active_boxes <= 1) || ((player._active_boxes > 1) && checkSingleBox(player)))
  130.                                 {
  131.                                    
  132.                                     final Set<Integer> items = Config.VOTES_REWARDS_LIST.keySet();
  133.                                     for (Integer i : items)
  134.                                     {
  135.                                         player.addItem("reward", i, Config.VOTES_REWARDS_LIST.get(i), player, true);
  136.                                     }
  137.                                 }
  138.                             }
  139.                         }
  140.                         setL2jBrasilVoteCount(_l2jbrasil_votes);
  141.                     }
  142.                     // Announcements.getInstance().gameAnnounceToAll("L2jBrasil: Reward in " + minutes + " minutes at " + (getL2jBrasilVoteCount() + Config.VOTES_FOR_REWARD) + " Votes!!");
  143.                 }
  144.             }
  145.         }
  146.     }
  147.    
  148.     protected boolean checkSingleBox(L2PcInstance player)
  149.     {
  150.         if (player.getClient() != null && player.getClient().getConnection() != null && !player.getClient().getConnection().isClosed() && !player.isInOfflineMode())
  151.         {
  152.             final String playerip = player.getClient().getConnection().getInetAddress().getHostAddress();
  153.            
  154.             if (already_rewarded.contains(playerip))
  155.             {
  156.                 return false;
  157.             }
  158.             already_rewarded.add(playerip);
  159.             return true;
  160.         }
  161.        
  162.         // if no connection (maybe offline shop) dnt reward
  163.         return false;
  164.     }
  165.    
  166.     protected int getHopZoneVotes()
  167.     {
  168.         int votes = 0;
  169.         try
  170.         {
  171.             URLConnection con = new URL(Config.VOTES_SITE_HOPZONE_URL).openConnection();
  172.            
  173.             con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
  174.             con.setConnectTimeout(5000);
  175.            
  176.             try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())))
  177.             {
  178.                 String line;
  179.                 boolean encontrado = false;
  180.                 int contador = 0;
  181.                 while ((line = in.readLine()) != null)
  182.                 {
  183.                     if (line.contains("Total Votes\">"))
  184.                     {
  185.                         for (String inputLine : line.split(">"))
  186.                         {
  187.                            
  188.                             if (encontrado)
  189.                             {
  190.                                 String inputLinefinal = inputLine.replace("</span", "");
  191.                                 if (5 == inputLinefinal.length())
  192.                                 {
  193.                                     StringTokenizer st = new StringTokenizer(inputLinefinal, ",");
  194.                                     String one = st.nextToken();
  195.                                     String two = st.nextToken();
  196.                                     inputLinefinal = one + "" + two;
  197.                                 }
  198.                                 votes = Integer.parseInt(inputLinefinal);
  199.                                 contador++;
  200.                                 break;
  201.                             }
  202.                            
  203.                             if (inputLine.contains("Total Votes\""))
  204.                             {
  205.                                 encontrado = true;
  206.                                 continue;
  207.                             }
  208.                         }
  209.                        
  210.                         if (encontrado && contador > 0)
  211.                         {
  212.                             break;
  213.                         }
  214.                     }
  215.                 }
  216.             }
  217.         }
  218.         catch (Exception e)
  219.         {
  220.             _LOG.info("HOPZONE is offline or something is wrong in link");
  221.             // Announcements.getInstance().gameAnnounceToAll("Hopzone: is offline.");
  222.             // e.printStackTrace();
  223.         }
  224.         return votes;
  225.     }
  226.    
  227.     protected int getL2jBrasilVotes()
  228.     {
  229.         try
  230.         {
  231.             URLConnection con = new URL(Config.VOTES_SITE_L2JBRASIL_URL).openConnection();
  232.             con.addRequestProperty("User-Agent", "Mozilla/5.0");
  233.             InputStreamReader isr = new InputStreamReader(con.getInputStream());
  234.             BufferedReader br = new BufferedReader(isr);
  235.            
  236.             String line;
  237.             while ((line = br.readLine()) != null)
  238.             {
  239.                 if (line.contains("<b>Entradas(Total):</b"))
  240.                 {
  241.                     String votesResult = String.valueOf(line.split(">")[2].replace("<br /", ""));
  242.                     int votes = Integer.valueOf(votesResult.replace(" ", ""));
  243.                     return votes;
  244.                 }
  245.             }
  246.            
  247.             br.close();
  248.             isr.close();
  249.         }
  250.         catch (Exception e)
  251.         {
  252.             _LOG.info("L2JBRASIL is offline or something is wrong in link");
  253.             // Announcements.getInstance().gameAnnounceToAll("L2jBrasil: is offline.");
  254.         }
  255.         return 0;
  256.     }
  257.    
  258.     protected void setHopZoneVoteCount(int voteCount)
  259.     {
  260.         _hopzoneVotesCount = voteCount;
  261.     }
  262.    
  263.     protected int getHopZoneVoteCount()
  264.     {
  265.         return _hopzoneVotesCount;
  266.     }
  267.    
  268.     protected void setL2jBrasilVoteCount(int voteCount)
  269.     {
  270.         _l2jbrasilVotesCount = voteCount;
  271.     }
  272.    
  273.     protected int getL2jBrasilVoteCount()
  274.     {
  275.         return _l2jbrasilVotesCount;
  276.     }
  277.    
  278.     public static AutoVoteRewardHandler getInstance()
  279.     {
  280.         if ((Config.VOTES_SITE_HOPZONE_URL != null) && !Config.VOTES_SITE_HOPZONE_URL.equals(""))
  281.         {
  282.             _hopzone = true;
  283.         }
  284.        
  285.         if ((Config.VOTES_SITE_L2JBRASIL_URL != null) && !Config.VOTES_SITE_L2JBRASIL_URL.equals(""))
  286.         {
  287.             _l2jbrasil = true;
  288.         }
  289.        
  290.         if (_l2jbrasil || _hopzone)
  291.         {
  292.             return SingletonHolder._instance;
  293.         }
  294.         return null;
  295.     }
  296.    
  297.     @SuppressWarnings("synthetic-access")
  298.     private static class SingletonHolder
  299.     {
  300.         protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
  301.     }
  302. }
  303.  
  304. -----------------------------------------------------------------------------------------------------------------
  305. config.java
  306.  
  307.     public static int VOTES_FOR_REWARD;
  308.     public static String VOTES_REWARDS;
  309.     public static String VOTES_SITE_L2JBRASIL_URL;
  310.     public static String VOTES_SITE_HOPZONE_URL;
  311.     public static FastMap<Integer, Integer> VOTES_REWARDS_LIST = new FastMap<>();
  312.     public static int VOTES_SYSYEM_INITIAL_DELAY;
  313.     public static int VOTES_SYSYEM_STEP_DELAY;
  314.     public static boolean AUTOVOTEREWARD_ENABLED;
  315.  
  316.  
  317.     AUTOVOTEREWARD_ENABLED = Boolean.parseBoolean(L2jOrionSettings.getProperty("VoteRewardSystem", "true"));
  318.             VOTES_FOR_REWARD = Integer.parseInt(L2jOrionSettings.getProperty("VotesRequiredForReward", "100"));
  319.             VOTES_SYSYEM_INITIAL_DELAY = Integer.parseInt(L2jOrionSettings.getProperty("VotesSystemInitialDelay", "60000"));
  320.             VOTES_SYSYEM_STEP_DELAY = Integer.parseInt(L2jOrionSettings.getProperty("VotesSystemStepDelay", "1800000"));
  321.             VOTES_SITE_HOPZONE_URL = L2jOrionSettings.getProperty("VotesSiteHopzoneUrl", "");
  322.             VOTES_SITE_L2JBRASIL_URL = L2jOrionSettings.getProperty("VotesSiteL2JBrasilUrl", "");
  323.             VOTES_REWARDS = L2jOrionSettings.getProperty("VotesRewards", "");
  324.             VOTES_REWARDS_LIST = new FastMap<>();
  325.            
  326.             String[] splitted_VOTES_REWARDS = VOTES_REWARDS.split(";");
  327.            
  328.             for (String reward : splitted_VOTES_REWARDS)
  329.             {
  330.                 String[] item_count = reward.split(",");
  331.                
  332.                 if (item_count.length != 2)
  333.                 {
  334.                     System.out.println("[VotesRewards]: invalid config property -> VotesRewards \"" + VOTES_REWARDS + "\"");
  335.                 }
  336.                 else
  337.                 {
  338.                     VOTES_REWARDS_LIST.put(Integer.parseInt(item_count[0]), Integer.parseInt(item_count[1]));
  339.                 }
  340.                
  341.             }
  342.  
  343.  
  344. -----------------------------------------------------------------------------------------------------------------
  345. Gameserver.java
  346.  
  347.  
  348.  
  349. if (Config.AUTOVOTEREWARD_ENABLED)
  350.         {
  351.             AutoVoteRewardHandler.getInstance();
  352.         }
  353.  
  354. -----------------------------------------------------------------------------------------------------------------
  355. config.ini
  356.  
  357. # Automatic Global Vote Reward System
  358. VoteRewardSystem= False
  359. VotesRequiredForReward = 1
  360. VotesSystemInitialDelay= 60000
  361. VotesSystemStepDelay= 300000
  362. VotesRewards= 9143,10;
  363.  
  364. #Leave empty to disable one site
  365. #if you need just one of these
  366. VotesSiteHopzoneUrl= https://l2.hopzone.net/lineage2/details/88242/Lineage2Argentina
  367. VotesSiteL2JBrasilUrl= https://top.l2jbrasil.com/index.php?a=stats&u=l2argentina
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement