Advertisement
Wolfy9247

IPRestrict.java

Jan 28th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. // IPRestrict.java
  2.  
  3. package com.wolfy9247.iprestrict;
  4.  
  5. import java.util.logging.Logger;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.command.Command;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.plugin.PluginDescriptionFile;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15. public class IPRestrict extends JavaPlugin {
  16.     public static IPRestrict instance;
  17.     protected FileConfiguration config;
  18.    
  19.     public final Logger log = Logger.getLogger("Minecraft");
  20.     public static String logTag = "[AdminIP] ";
  21.  
  22.     @Override
  23.     public void onDisable() {
  24.         PluginDescriptionFile pdfFile = this.getDescription();
  25.         log.info(logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " disabled!");
  26.     }
  27.    
  28.     @Override
  29.     public void onEnable() {
  30.         instance = this;
  31.         PluginDescriptionFile pdfFile = this.getDescription();
  32.         getConfig().options().copyDefaults(true);
  33.         saveConfig();
  34.         new IPRestrictJoinListener(this);
  35.         registerCommands();
  36.         log.info(logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " enabled!");
  37.     }
  38.    
  39.     private void registerCommands() {
  40.        
  41.     }
  42.    
  43.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  44.         if(!cmd.isRegistered()) {
  45.             return false;
  46.         }
  47.         else if(commandLabel.equalsIgnoreCase("ipr")) {
  48.             for(int i = 0; i < 3; i++) {
  49.                 if(args[i].isEmpty()) {
  50.                     return false;
  51.                 }
  52.             }
  53.             if(args[0] == "set") {
  54.                 if(Bukkit.getPlayer(args[1]) == null) {
  55.                     sender.sendMessage(ChatColor.RED + "The player you have entered is invalid!");
  56.                     return false;
  57.                 }
  58.                 Object[] playerIPs = getConfig().getList(instance + "." + sender.getName()).toArray();
  59.                 int lastIndex = playerIPs.length + 1;
  60.                 getConfig().set(instance + "." + sender.getName(), args[2]); // Need to figure out how to set next line "-";
  61.                
  62.                 return true;
  63.             }
  64.             else if(args[0] == "remove") {
  65.                 if(Bukkit.getPlayer(args[1]) == null) {
  66.                     sender.sendMessage(ChatColor.RED + "The player you have entered is invalid!");
  67.                     return false;
  68.                 }
  69.                 // Figure out how to remove a line if found.
  70.             }
  71.             return false;
  72.         }
  73.         return true;
  74.     }
  75. }
  76.  
  77. // IPRestrictJoinListener.java
  78.  
  79. package com.wolfy9247.iprestrict;
  80.  
  81. import java.net.InetSocketAddress;
  82.  
  83. import org.bukkit.entity.Player;
  84. import org.bukkit.event.EventHandler;
  85. import org.bukkit.event.Listener;
  86. import org.bukkit.event.player.PlayerJoinEvent;
  87.  
  88. public class IPRestrictJoinListener implements Listener {
  89.     public static IPRestrict plugin;
  90.    
  91.     public IPRestrictJoinListener(IPRestrict plugin) {
  92.         plugin.getServer().getPluginManager().registerEvents(this, plugin);
  93.     }
  94.    
  95.     @EventHandler
  96.     void onPlayerJoin(PlayerJoinEvent event) {
  97.         Player player = event.getPlayer();
  98.         InetSocketAddress joinIP = event.getPlayer().getAddress();
  99.         Object[] playerIPs = plugin.getConfig().getList(plugin + "." + player.getName()).toArray();
  100.        
  101.         for(int i = 0; i < playerIPs.length; i++) {
  102.             if(joinIP != playerIPs[i]) {
  103.                 if(player.isOp()) {
  104.                     return;
  105.                 }
  106.                 else {
  107.                     player.kickPlayer(plugin.getConfig().getString("IPRestrict.Kick Message"));
  108.                 }
  109.             }
  110.             else {
  111.                 return;
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement