PageRenderTime 44ms CodeModel.GetById 31ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/james137137/LolnetIpProtect/LolnetIpProtect.java

https://bitbucket.org/james137137/lolnetipprotect
Java | 301 lines | 243 code | 48 blank | 10 comment | 58 complexity | 2f9fc6d899b44bbfb36dadba66e235ee MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package com.james137137.LolnetIpProtect;
  6
  7import java.io.File;
  8import java.io.IOException;
  9import java.net.InetAddress;
 10import java.net.UnknownHostException;
 11import java.util.List;
 12import java.util.logging.Logger;
 13import org.bukkit.ChatColor;
 14import org.bukkit.command.Command;
 15import org.bukkit.command.CommandSender;
 16import org.bukkit.configuration.file.FileConfiguration;
 17import org.bukkit.plugin.java.JavaPlugin;
 18import org.mcstats.Metrics;
 19
 20/**
 21 *
 22 * @author James
 23 */
 24public class LolnetIpProtect extends JavaPlugin {
 25
 26    static final Logger log = Logger.getLogger("Minecraft");
 27
 28    @Override
 29    public void onEnable() {
 30        getServer().getPluginManager().registerEvents(new LolnetIpProtectListener(this), this);
 31        log.info("LolnetIP: enabled");
 32        try {
 33    Metrics metrics = new Metrics(this);
 34    metrics.start();
 35    } catch (IOException e) {
 36    // Failed to submit the stats :-(
 37        System.out.println(e.toString());
 38    }
 39        //IpProtectedPlayerList = config.getStringList("Playerlist");
 40    }
 41
 42    public void onDisable() {
 43        log.info("LolnetIpProtect: disabled");
 44    }
 45
 46    public void removeConfigFile(CommandSender sender)
 47    {
 48        
 49        try{
 50
 51    		File file = new File("plugins/"+this.getName()+"/config.yml");
 52 
 53    		if(file.delete()){
 54    			sender.sendMessage(file.getName() + " is deleted!");
 55    		}else{
 56    			sender.sendMessage("Delete operation is failed.");
 57    		}
 58 
 59    	}catch(Exception e){
 60 
 61    		e.printStackTrace();
 62 
 63    	}
 64    }
 65    @Override
 66    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
 67        String commandName = command.getName().toLowerCase();
 68        String[] trimmedArgs = args;
 69        if (commandName.equalsIgnoreCase("lolnetIP")) {
 70            if ((sender.isOp()) || (sender == getServer().getConsoleSender())) {
 71                if (args.length == 0) {
 72                    sender.sendMessage(ChatColor.YELLOW + "[LolnetIpProtect]: " + ChatColor.RESET + "commands: ");
 73                    sender.sendMessage("/lolnetIP [add,remove,list,mode,removeConfigFile]");
 74                } else if (args[0].equalsIgnoreCase("add")) {
 75                    addPlayerIP(sender, args);
 76                } else if (args[0].equalsIgnoreCase("list")) {
 77                    listPlayerIP(sender, args);
 78                } else if (args[0].equalsIgnoreCase("remove")) {
 79                    RemovePlayerIP(sender, args); 
 80                }  else if (args[0].equalsIgnoreCase("Mode")) {
 81                    ModePlayerIP(sender, args);
 82                } else if (args[0].equalsIgnoreCase("RemoveConfigFile")) {
 83                    removeConfigFile(sender);
 84                } else {
 85                    sender.sendMessage(ChatColor.YELLOW + "[LolnetIpProtect]: " + ChatColor.RESET + "commands: ");
 86                    sender.sendMessage("/lolnetIP [add,remove,list,mode,removeConfigFile]");
 87                }
 88            } else {
 89                sender.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command. Please contact the server administrators if you believe that this is in error.");
 90            }
 91        }
 92        return false;
 93    }
 94
 95    public boolean isIpProtected(String playerName) {
 96        FileConfiguration config = getConfig();
 97        playerName = playerName.toLowerCase();
 98        List<String> IpProtectedPlayerList = config.getStringList("LolnetIP.Playerlist");
 99        for (int i = 0; i < IpProtectedPlayerList.size(); i++) {
100            if (playerName.equalsIgnoreCase(IpProtectedPlayerList.get(i))) {
101
102                return true;
103            }
104        }
105        return false;
106    }
107
108    public boolean isCorrectIP(String playerName, String currentIP) throws UnknownHostException {
109
110        FileConfiguration config = getConfig();
111        playerName = playerName.toLowerCase();
112        String IP;
113        List<String> IpProtectedPlayerList = config.getStringList("LolnetIP." + playerName);
114
115        
116
117        for (int i = 0; i < IpProtectedPlayerList.size(); i++) {
118
119            IP = IpProtectedPlayerList.get(i);
120
121            if (Character.getNumericValue(IP.charAt(0)) > 9) {
122                InetAddress dynAddress = java.net.InetAddress.getByName(IP);
123                IP = dynAddress.getHostAddress();
124            }
125
126            if (IP.equalsIgnoreCase(currentIP)) {
127                return true;
128
129            }
130
131        }
132        return false;
133    }
134
135    public void addPlayerIP(CommandSender sender, String[] args) {
136
137        if (args.length != 3) {
138            sender.sendMessage("Error; must be /lolnetIP add username IP");
139            return;
140        }
141        FileConfiguration config = getConfig();
142        List<String> PlayerIPList;
143        String playerName = args[1].toLowerCase();
144        String IP = args[2];
145        if (!isIpProtected(playerName)) {
146            List<String> IpProtectedPlayerList = config.getStringList("LolnetIP.Playerlist");
147            IpProtectedPlayerList.add(playerName);
148            config.set("LolnetIP.Playerlist", IpProtectedPlayerList);
149        }
150        PlayerIPList = config.getStringList("LolnetIP." + playerName);
151
152        PlayerIPList.add(IP);
153
154
155
156        config.set("LolnetIP." + playerName, PlayerIPList);
157        
158        sender.sendMessage("IP: " + IP + " added for player: " + playerName);
159        saveConfig();
160
161    }
162
163    public void RemovePlayerIP(CommandSender sender, String[] args) {
164        if (args.length <= 1 && args.length > 3) {
165            sender.sendMessage("Error; must be /lolnetIP remove username IP or /lolnetIP remove username");
166            return;
167        }
168        FileConfiguration config = getConfig();
169        List<String> PlayerIPList;
170        String playerName = args[1].toLowerCase();
171        
172        if (!isIpProtected(playerName))
173         {
174             sender.sendMessage("player has not been added yet. please use /lolnetip add");
175             return;
176         }
177
178        if (args.length == 2) {
179            RemovePlayer(playerName);
180            return;
181        }
182        String IP = args[2];
183        if (isIpProtected(playerName)) {
184            PlayerIPList = config.getStringList("LolnetIP." + playerName);
185            for (int i = 0; i < PlayerIPList.size(); i++) {
186                if (PlayerIPList.get(i).equalsIgnoreCase(IP)) {
187                    PlayerIPList.remove(i);
188                    sender.sendMessage("IP removed");
189                }
190            }
191            
192            if (IP.equalsIgnoreCase("all"))
193                {
194                    PlayerIPList.clear();
195                    sender.sendMessage("All IPs removed from player: " + playerName);
196                }
197            if (PlayerIPList.isEmpty()) {
198                RemovePlayer(playerName);
199            }
200            config.set("LolnetIP." + playerName, PlayerIPList);
201            saveConfig();
202        } else {
203            sender.sendMessage("Player not found. Try /lolnetIP list for spelling");
204        }
205    }
206
207    public void listPlayerIP(CommandSender sender, String[] args) {
208        FileConfiguration config = getConfig();
209        List<String> IpProtectedPlayerList = config.getStringList("LolnetIP.Playerlist");
210        if (IpProtectedPlayerList.isEmpty()) {
211            sender.sendMessage("Protected IP player List is empty");
212            return;
213        }
214        if (args.length == 1) {
215
216
217            if (IpProtectedPlayerList.isEmpty()) {
218                sender.sendMessage("player is not IP Protected");
219                return;
220            }
221            sender.sendMessage("Current Player list with IP protection:");
222            String message = "";
223            for (int i = 0; i < IpProtectedPlayerList.size(); i++) {
224                message += IpProtectedPlayerList.get(i)+ ":" + GetPlayerMode(IpProtectedPlayerList.get(i)) + ", ";
225            }
226            sender.sendMessage(message);
227        } else if (args.length == 2) {
228            String playerName = args[1].toLowerCase();
229            List<String> PlayerIPList = config.getStringList("LolnetIP." + playerName);
230            sender.sendMessage(playerName + " has the following IPs");
231            for (int i = 0; i < PlayerIPList.size(); i++) {
232                sender.sendMessage("*" +PlayerIPList.get(i) + "*");
233            }
234
235        } else {
236            sender.sendMessage(ChatColor.RED + "something is missing. Type /lolnetIP list or /lolnetIP list playername");
237        }
238    }
239
240    private void RemovePlayer(String playerName) {
241        FileConfiguration config = getConfig();
242        List<String> IpProtectedPlayerList = config.getStringList("LolnetIP.Playerlist");
243        for (int i = 0; i < IpProtectedPlayerList.size(); i++) {
244            if (IpProtectedPlayerList.get(i).equalsIgnoreCase(playerName)) {
245                IpProtectedPlayerList.remove(i);
246                break;
247            }
248        }
249        config.set("LolnetIP.Playerlist", IpProtectedPlayerList);
250        saveConfig();
251    }
252
253    private void ModePlayerIP(CommandSender sender, String[] args) {
254        if (args.length != 3) {
255            sender.sendMessage("Error; must be /lolnetIP mode username [protect,warn]");
256            return;
257        }
258        
259         FileConfiguration config = getConfig();
260         String playerName = args[1].toLowerCase();
261         
262         if (!isIpProtected(playerName))
263         {
264             sender.sendMessage("player has not been added yet. please use /lolnetip add");
265             return;
266         }
267         String Mode = args[2];
268                 if (!Mode.equalsIgnoreCase("protect") && !Mode.equalsIgnoreCase("warn"))
269                 {
270                     sender.sendMessage("Error; must be /lolnetIP mode username [protect,warn]");
271                     return;
272                 }
273                 
274                 if (Mode.equalsIgnoreCase("protect"))
275                 {
276                     config.set("lolnetIP.ProtectionMode" +playerName,"Protect");
277                     saveConfig();
278                 }else if (Mode.equalsIgnoreCase("warn"))
279                 {
280                     config.set("lolnetIP.ProtectionMode" +playerName,"Warn");
281                     saveConfig();
282                 }
283                 sender.sendMessage("Mode Changed to: " + Mode+ " for player: " + playerName);
284                 
285    }
286    
287    public String GetPlayerMode(String playerName)
288    {
289        String Mode;
290        FileConfiguration config = getConfig();
291        Mode = config.getString("lolnetIP.ProtectionMode" +playerName);
292        
293        if (Mode==null)
294        {
295            config.set("lolnetIP.ProtectionMode" +playerName,"Warn");
296            Mode = "Warn";
297            saveConfig();
298        }
299        return Mode;
300    }
301}