Thread: [PI] Improved Connection.java

Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1. #1 [PI] Improved Connection.java 
    RuneAdventure - The Epic Adventure

    Thock321's Avatar
    Join Date
    Jul 2011
    Posts
    1,809
    Thanks given
    706
    Thanks received
    363
    Rep Power
    416
    Code:
    package org.rs2;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    import org.rs2.model.entity.players.Client;
    
    /**
     * A class for handling punishments and connections.
     * @author Thock321
     *
     */
    public class Connection {
    	
    	/**
    	 * An enum containing the types of punishments.
    	 * @author Thock321
    	 *
    	 */
    	public static enum PunishmentType {
    		BAN(new ArrayList<String>()),
    		IPBAN(new ArrayList<String>()),
    		UUIDBAN(new ArrayList<String>()),
    		MUTE(new ArrayList<String>()),
    		IPMUTE(new ArrayList<String>());
    		
    		/**
    		 * The list that contains punishment data.
    		 */
    		private List<String> list;
    		
    		/**
    		 * Creates a new punishment type.
    		 * @param list A new list to contain punishment data.
    		 */
    		private PunishmentType(List<String> list) {
    			this.list = list;
    		}
    		
    		/**
    		 * Gets the list containing punishment data.
    		 * @return The list.
    		 */
    		public List<String> getList() {
    			return list;
    		}
    		
    		/**
    		 * Gets a punishment type for it's name.
    		 * @param name The name.
    		 * @return The punishment type.
    		 */
    		public static PunishmentType forName(String name) {
    			for (PunishmentType pt : PunishmentType.values()) {
    				if (name.equalsIgnoreCase(pt.toString()))
    					return pt;
    			}
    			return null;
    		}
    	}
    
    	public static Collection<String> loginLimitExceeded = new ArrayList<String> ();
    	private static String punishmentFilesFolder = "./Data/bans/";
    	private static File _punishmentFilesFolder = new File(punishmentFilesFolder);
    	private static File punishmentFile;
    	private static BufferedReader reader;
    	private static BufferedWriter writer;
    	
    	/**
    	 * Punishes a player.
    	 * @param c The player.
    	 * @param type The type of punishment.
    	 */
    	public static void punish(Client c, PunishmentType type) {
    		punishmentFile = new File(punishmentFilesFolder + type.toString().toLowerCase() + ".txt");
    		String toAdd = type.toString().contains("IP") ? c.playerName + ":" + c.connectedFrom : (type.toString().contains("UID") ? c.playerName + ":" + c.UUID : c.playerName);
    		type.getList().add(toAdd);
    		try {
    			writer = new BufferedWriter(new FileWriter(punishmentFile));
    			try {
    				writer.newLine();
    				writer.write(toAdd);
    			} finally {
    				writer.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	/**
    	 * Removes a punishment.
    	 * @param toRemove The string to remove from the file.
    	 * @param type The type of punishment.
    	 */
    	public static void removePunishment(String toRemove, PunishmentType type) {
    		punishmentFile = new File(punishmentFilesFolder + type.toString().toLowerCase() + ".txt");
    		type.getList().remove(toRemove);
    		try {
    			writer = new BufferedWriter(new FileWriter(punishmentFile));
    			reader = new BufferedReader(new FileReader(punishmentFile));
    			String line;
    			try {
    				while ((line = reader.readLine()) != null) {
    					line = line.trim();
    					if (!line.equalsIgnoreCase(toRemove)) {
    						writer.write(line);
    					}
    				}
    			} finally {
    				writer.close();
    				reader.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	/**
    	 * Loads all punishments.
    	 */
    	public static void loadPunishments() {
    		for (File file : _punishmentFilesFolder.listFiles()) {
    			PunishmentType type = PunishmentType.forName(file.getName().replace(".txt", ""));
    			if (type == null)
    				continue;
    			try {
    				reader = new BufferedReader(new FileReader(file));
    				String line;
    				try {
    					while ((line = reader.readLine()) != null) {
    						line = line.trim();
    						type.getList().add(line);
    					}
    				} finally {
    					reader.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	/**
    	 * Checks if a player is punished for a certain punishment type.
    	 * @param playerName The player.
    	 * @param type The punishment type.
    	 * @return <code>true</code> if the player is punished for the type, otherwise <code>false</code>.
    	 */
    	public static boolean isPunishedForType(String playerName, PunishmentType type) {
    		try {
    			punishmentFile = new File(punishmentFilesFolder + type.toString().toLowerCase() + ".txt");
    			reader = new BufferedReader(new FileReader(punishmentFile));
    			try {
    				String line;
    				while ((line = reader.readLine()) != null) {
    					if (line.toLowerCase().contains(playerName.toLowerCase()))
    						return true;
    				}
    			} finally {
    				reader.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return false;
    	}
    
    	/**
    	 * Checks if an ip has reached its login limit.
    	 * @param IP The ip.
    	 * @return <code>true</code> if the ip has reached its login limit, otherwise <code>false</code>.
    	 */
    	public static boolean checkLoginList(String IP) {
    		loginLimitExceeded.add(IP);
    		int num = 0;
    		for(String ips : loginLimitExceeded) {
    			if(IP.equals(ips)) {
    				num++;
    			}
    		}
    		return num > 5;
    	}
    
    
    }
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Registered Member
    Join Date
    Feb 2011
    Posts
    220
    Thanks given
    1
    Thanks received
    12
    Rep Power
    5
    good job.
    Reply With Quote  
     

  4. #3  
    Banned

    Join Date
    Jun 2012
    Posts
    653
    Thanks given
    233
    Thanks received
    128
    Rep Power
    0
    Very good job.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Zivik's Avatar
    Join Date
    Oct 2007
    Age
    25
    Posts
    4,432
    Thanks given
    892
    Thanks received
    1,525
    Rep Power
    3228
    Nice job. Looks clean.
    Reply With Quote  
     

  6. #5  
    PokeFrontier Java Developer

    Pokemon's Avatar
    Join Date
    May 2011
    Posts
    2,733
    Thanks given
    494
    Thanks received
    801
    Discord
    View profile
    Rep Power
    1242
    Good job nice use of arraylists

    [Only registered and activated users can see links. ]



    Pokemon loves his
    Reply With Quote  
     

  7. Thankful user:


  8. #6  
    Banned

    Join Date
    Jun 2012
    Posts
    653
    Thanks given
    233
    Thanks received
    128
    Rep Power
    0
    Quote Originally Posted by Pokemon View Post
    Good job nice use of arraylists
    Yes, I agree.
    Reply With Quote  
     

  9. #7  
    Registered MrClassic

    MrClassic's Avatar
    Join Date
    Oct 2008
    Age
    26
    Posts
    1,967
    Thanks given
    16,936
    Thanks received
    409
    Rep Power
    5000
    Nice, this looks very clean. Thanks
    Reply With Quote  
     

  10. #8  
    Father Of Lies


    Join Date
    May 2012
    Age
    23
    Posts
    1,216
    Thanks given
    267
    Thanks received
    289
    Rep Power
    242
    Nice, explain what you added and more people will use though
    Reply With Quote  
     

  11. #9  
    RuneAdventure - The Epic Adventure

    Thock321's Avatar
    Join Date
    Jul 2011
    Posts
    1,809
    Thanks given
    706
    Thanks received
    363
    Rep Power
    416
    Quote Originally Posted by jakeyy View Post
    Nice, explain what you added and more people will use though
    It's a snippet and documented.
    Quote Originally Posted by Pokemon View Post
    Good job nice use of arraylists
    Using ArrayLists is pretty basic though.
    Reply With Quote  
     

  12. #10  
    Banned

    Join Date
    Apr 2012
    Age
    24
    Posts
    2,948
    Thanks given
    1,126
    Thanks received
    1,081
    Rep Power
    0
    Anyone can shorten lines like the other ones, this one actually makes the system itself better. Nice job.
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. 100x cleaner Connection.java for fag pi.
    By Batukka in forum Snippets
    Replies: 22
    Last Post: 01-05-2013, 07:00 PM
  2. [Pi] Improved PvPConfig.java
    By Linus in forum Configuration
    Replies: 7
    Last Post: 05-07-2011, 09:43 PM
  3. SQL Connection Via Java
    By CTucker in forum Help
    Replies: 1
    Last Post: 09-06-2010, 05:47 AM
  4. Deltascape, improved item.Java
    By Linus in forum Configuration
    Replies: 7
    Last Post: 08-22-2010, 07:23 AM
  5. Replies: 13
    Last Post: 06-22-2010, 01:01 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •