Jump to content

Question

Posted (edited)

Hello so recently i started working on my server again but i have problems with one vote script it doesnt want to start and check for votes

 

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.sf.l2j.gameserver.voteengine;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

import net.sf.l2j.Config;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.model.L2World;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.network.L2GameClient;
import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
import net.sf.l2j.gameserver.util.Broadcast;



public class AutoVoteReward
{
	public static Logger _log = Logger.getLogger(AutoVoteReward.class.getName());

	private int hopzoneVotesCount = 0;
	private int topzoneVotesCount = 0;
	private int networkVotesCount = 0;
	protected List<String> already_rewarded;

	protected static boolean topzone = false;
	protected static boolean hopzone = false;
	protected static boolean network = false;

	private AutoVoteReward()
	{
		_log.info("VoteRewardSystem: Initialized.");
		if(hopzone)
		{
			int hopzone_votes = getHopZoneVotes();
			if(hopzone_votes == -1)
			{
				hopzone_votes = 0;
			}

			setHopZoneVoteCount(hopzone_votes);
		}
		
		if(Config.VOTE_TOPZONE_ENABLED)
		{
		   if(topzone)
		   {
			int topzone_votes = getTopZoneVotes();
			if(topzone_votes == -1)
			{
				topzone_votes = 0;
			}

			setTopZoneVoteCount(topzone_votes);
		   }
		}
		
		if(Config.VOTE_NETWORK_ENABLED)
		{
		   if(network)
		   {
			int network_votes = getNetworkVotes();
			if(network_votes == -1)
			{
				network_votes = 0;
			}

			setNetworkVoteCount(network_votes);
		   }
		}
		
		ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), Config.VOTES_SYSTEM_INITIAL_DELAY, Config.VOTES_SYSTEM_STEP_DELAY);
	}

	protected class AutoReward implements Runnable
	{
		@Override
		public void run()
		{
			if(hopzone)
			{
				int hopzone_votes = getHopZoneVotes();
				if(hopzone_votes != -1)
				{
					_log.info("Hopzone Votes: " + hopzone_votes);
					announce("[Hopzone] Actual Votes are " + hopzone_votes + "...");

					if(hopzone_votes != 0 && hopzone_votes >= getHopZoneVoteCount() + Config.VOTES_FOR_REWARD)
					{
						already_rewarded = new ArrayList<>();

						Collection<L2PcInstance> pls = L2World.getInstance().getPlayers();
						announce("[Hopzone] Great Work! Check your inventory for Reward!!");

						for(L2PcInstance player : pls)
						{
							if(player != null)
							{
								if(checkSingleBox(player))
								{
									player.addItem("reward", Config.HOPZONE_REWARD_ID, Config.HOPZONE_REWARD_COUNT, player, true);
								}
							}
						}

						setHopZoneVoteCount(hopzone_votes);
					}
					announce("[Hopzone] Next Reward will be on - " + (getHopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes!!");
					announce("[Hopzone] For Vote Visit our website - ");
				}
			}
			if (hopzone && Config.VOTES_SYSTEM_STEP_DELAY > 0)
			{
				try
				{
					Thread.sleep(Config.VOTES_SYSTEM_STEP_DELAY / 2);
				}
				catch (final InterruptedException e)
				{
						e.printStackTrace();
				}
			}
			
	if(Config.VOTE_TOPZONE_ENABLED)	
	{
			if(topzone)
			{
				int topzone_votes = getTopZoneVotes();
				if(topzone_votes != -1)
				{
					_log.info("TopZone Votes: " + topzone_votes);
					announce("[Topzone] Actual Votes are " + topzone_votes + "...");

					if(topzone_votes != 0 && topzone_votes >= getTopZoneVoteCount() + Config.VOTES_FOR_REWARD)
					{
						already_rewarded = new ArrayList<>();

						Collection<L2PcInstance> pls = L2World.getInstance().getPlayers();
						announce("[Topzone] Great Work! Check your inventory for Reward!!");

						for(L2PcInstance player : pls)
						{
							if(player != null)
							{
								if(checkSingleBox(player))
								{
								   player.addItem("reward", Config.TOPZONE_REWARD_ID, Config.TOPZONE_REWARD_COUNT, player, true);
								}
							}
						}

						setTopZoneVoteCount(topzone_votes);
					}
					announce("[Topzone] Next Reward will be on - " + (getTopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes!!");
					announce("[Topzone] For Vote Visit our website - ");
				}
			}
		}
	if (topzone && hopzone && Config.VOTES_SYSTEM_STEP_DELAY > 0)
	{
		try
		{
			Thread.sleep(Config.VOTES_SYSTEM_STEP_DELAY / 2);
		}
		catch (final InterruptedException e)
		{
				e.printStackTrace();
		}
	}
	if(Config.VOTE_NETWORK_ENABLED)	
	{
			if(network)
			{
				int network_votes = getNetworkVotes();
				if(network_votes != -1)
				{
					_log.info("Network Votes: " + network_votes);
					announce("[Network] Actual Votes are " + network_votes + "...");

					if(network_votes != 0 && network_votes >= getNetworkVoteCount() + Config.VOTES_FOR_REWARD)
					{
						already_rewarded = new ArrayList<>();

						Collection<L2PcInstance> pls = L2World.getInstance().getPlayers();
						announce("[Network] Great Work! Check your inventory for Reward!!");

						for(L2PcInstance player : pls)
						{
							if(player != null)
							{
								if(checkSingleBox(player))
								{
								   player.addItem("reward", Config.NETWORK_REWARD_ID, Config.NETWORK_REWARD_COUNT, player, true);
								}
							}
						}

						setNetworkVoteCount(network_votes);
					}
					announce("[Network] Next Reward will be on - " + (getNetworkVoteCount() + Config.VOTES_FOR_REWARD) + " Votes!!");
					announce("[Network] For Vote Visit our website - ");
				}
			}
		}
	if (topzone && hopzone && network && Config.VOTES_SYSTEM_STEP_DELAY > 0)
	{
		try
		{
			Thread.sleep(Config.VOTES_SYSTEM_STEP_DELAY / 2);
		}
		catch (final InterruptedException e)
		{
				e.printStackTrace();
		}
	}
	  }
	}
	
	protected boolean checkSingleBox(L2PcInstance player)
	{
		L2GameClient client = player.getClient();
		if(player.getClient()!=null && player.getClient().getConnection()!=null && !client.isDetached() && !player.getClient().getConnection().isClosed()){
			
			String playerip = player.getClient().getConnection().getInetAddress().getHostAddress();
			
			if(already_rewarded.contains(playerip))
				return false;
			already_rewarded.add(playerip);
			return true;
		}
		return false;
	}
	

	
	protected int getHopZoneVotes()
	{
		InputStreamReader isr = null;
        BufferedReader br = null;
       
        try
        {
                URLConnection con = new URL(Config.VOTES_SITE_HOPZONE_URL).openConnection();
                con.addRequestProperty("User-Agent", "Mozilla/4.76");
                isr = new InputStreamReader(con.getInputStream());
                br = new BufferedReader(isr);
               
                String line;
                while ((line = br.readLine()) != null)
                {
                        if (line.contains("<li><span class=\"rank anonymous tooltip\" title"))
                        {
                                int votes = Integer.valueOf(line.split(">")[2].replace("</span", ""));
                                return votes;
                        }
                }
               
                br.close();
                isr.close();
        }
        catch (final Exception e)
        {
                _log.warning("[AutoVoteReward] Server L2Hopzone is offline or something is wrong in link");
                announce("[Hopzone] is offline. We will check reward as it will be online again");
        }
        return -1;
    }

	protected int getTopZoneVotes()
    {
		InputStreamReader isr = null;
		BufferedReader br = null;
		try
        {
     	   URLConnection con = new URL(Config.VOTES_SITE_TOPZONE_URL).openConnection();
                 con.addRequestProperty("User-Agent", "L2TopZone");
                 isr = new InputStreamReader(con.getInputStream());
                 br = new BufferedReader(isr);

                 String line;
                 while ((line = br.readLine()) != null)
                 {
                         
                                 int votes = Integer.valueOf(line);
                                 return votes;
                       
                 }
                
                 br.close();
                 isr.close();
         }
        catch (Exception e)
        {
            if (topzone)
            	System.out.println("TOPZONE is offline. We will check reward as it will be online again.");
            announce("[Topzone] is offline. We will check reward as it will be online again");
        }
		return -1;
    }
	
	protected int getNetworkVotes()
    {
        InputStreamReader isr = null;
        BufferedReader br = null;
        try
        {
                URLConnection con = new URL(Config.VOTES_SITE_NETWORK_URL).openConnection();
                con.addRequestProperty("User-Agent", "Mozilla/5.0");
                isr = new InputStreamReader(con.getInputStream());
                br = new BufferedReader(isr);
               
                String line;
                while ((line = br.readLine()) != null)
                {
                        if (line.contains("<div class=\"tls-in-sts\"><b style"))
                        {
                                int votes = Integer.valueOf(line.split(">")[2].replace("</b", ""));
                                return votes;
                        }
                }
               
                br.close();
                isr.close();
        }
        catch (Exception e)
        {
            if (network)
            	System.out.println("NETWORK is offline. We will check reward as it will be online again.");
            announce("[Network] is offline. We will check reward as it will be online again");
        }
        return -1;
    }

	protected void setHopZoneVoteCount(int voteCount)
	{
		hopzoneVotesCount = voteCount;
	}

	protected  int getHopZoneVoteCount()
	{
		return hopzoneVotesCount;
	}

	protected  void setTopZoneVoteCount(int voteCount)
	{
		topzoneVotesCount = voteCount;
	}

	protected  int getTopZoneVoteCount()
	{
		return topzoneVotesCount;
	}
	
	protected  void setNetworkVoteCount(int voteCount)
	{
		networkVotesCount = voteCount;
	}

	protected  int getNetworkVoteCount()
	{
		return networkVotesCount;
	}

	public static AutoVoteReward getInstance()
	{
		if(Config.VOTES_SITE_HOPZONE_URL != null && !Config.VOTES_SITE_HOPZONE_URL.equals("")){
			hopzone = true;
		}
		
		if(Config.VOTES_SITE_TOPZONE_URL != null && !Config.VOTES_SITE_TOPZONE_URL.equals("")){
			topzone = true;
		}
		
		if(Config.VOTES_SITE_NETWORK_URL != null && !Config.VOTES_SITE_NETWORK_URL.equals("")){
			network = true;
		}
		
		if(topzone || hopzone || network)
			return SingletonHolder._instance;
		return null;
	}
	
	static void announce(String msg)
    {
            CreatureSay cs = new CreatureSay(0, 18, "", msg);
            Broadcast.toAllOnlinePlayers(cs);
    }
	
	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder
	{
		protected static final AutoVoteReward _instance = new AutoVoteReward();
	}

 

Edited by haskovo

5 answers to this question

Recommended Posts

  • 0
Posted (edited)

You can start read about Jsoup lib it will helps you with this kind of systems.

https://jsoup.org/download
https://jsoup.org/cookbook/extracting-data/selector-syntax

Edited by StinkyMadness
  • Thanks 1
  • 0
Posted (edited)
16 minutes ago, haskovo said:

it is working but the vote check is old they have changed they websites

Yes but because all the code is old.. if you search a bit about Jsoup you will create better code and for any toplist you wish.

And finally stop asking for ready food.. I give you the links you need to search the new way on your own.. trust me you will feel a lot better if you code it on your own than keep asking for ready results.

enough with the admins that want to use L2J without having the wish to learn basic java to use the files.. you can't use a Car correct if you don't learn to drive... is that hard to understand?

Edited by StinkyMadness
  • 0
Posted
7 minutes ago, haskovo said:

its fine man dont be angry

I’m not angry at all.. just you asking help and i give it to you.. just you waiting back ready code.. and thats not an way that really helps you.. its better to try learn you find the mistake or how that system works.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...