PageRenderTime 28ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/james137137/LolnetWarpSign/LolnetWarpSign.java

https://bitbucket.org/james137137/lolnetwarpsign
Java | 260 lines | 204 code | 48 blank | 8 comment | 33 complexity | cc9e2f85d81098f4aff79eeea19dc170 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package com.james137137.LolnetWarpSign;
  6
  7import java.io.BufferedReader;
  8import java.io.File;
  9import java.io.FileNotFoundException;
 10import java.io.FileReader;
 11import java.io.IOException;
 12import java.util.List;
 13import java.util.logging.Level;
 14import java.util.logging.Logger;
 15import org.bukkit.Bukkit;
 16import org.bukkit.ChatColor;
 17import org.bukkit.Location;
 18import org.bukkit.World;
 19import org.bukkit.command.Command;
 20import org.bukkit.command.CommandSender;
 21import org.bukkit.entity.Player;
 22import org.bukkit.event.Listener;
 23import org.bukkit.event.player.PlayerTeleportEvent;
 24import org.bukkit.plugin.java.JavaPlugin;
 25import net.milkbowl.vault.economy.Economy;
 26import org.bukkit.configuration.file.FileConfiguration;
 27import org.bukkit.plugin.RegisteredServiceProvider;
 28
 29/**
 30 *
 31 * @author James
 32 */
 33public class LolnetWarpSign extends JavaPlugin implements Listener {
 34
 35    static final Logger log = Logger.getLogger("Minecraft");
 36    public static Economy economy = null;
 37
 38    private boolean setupEconomy() {
 39        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
 40        if (economyProvider != null) {
 41            economy = economyProvider.getProvider();
 42        }
 43
 44        return (economy != null);
 45    }
 46
 47    @Override
 48    public void onEnable() {
 49
 50        FileConfiguration config = getConfig();
 51        config.addDefault("WarpSignCost", 1000);
 52        config.addDefault("NewWarpCost", 9000);
 53        config.options().copyDefaults(true);
 54        saveConfig();
 55
 56
 57
 58        if (!setupEconomy()) {
 59            log.warning("[LolnetWarpSign] could not setup the Economy");
 60        }
 61        String version = Bukkit.getServer().getPluginManager().getPlugin(this.getName()).getDescription().getVersion();
 62        log.log(Level.INFO, this.getName() + ":Version {0} enabled", version);
 63        getServer().getPluginManager().registerEvents(new LolnetWarpSignListener(this, economy), this);
 64    }
 65
 66    @Override
 67    public void onDisable() {
 68        log.log(Level.INFO, "{0}: disabled", this.getName());
 69    }
 70
 71    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
 72        String commandName = command.getName().toLowerCase();
 73        String[] trimmedArgs = args;
 74
 75
 76        if (commandName.equalsIgnoreCase("LolnetWarpSign") && sender.hasPermission("LolnetWarpSign.SetPrice")) {
 77            Player player = (Player) sender;
 78            if (args.length < 1) {
 79                sender.sendMessage("please use /LolnetWarpSign [setWarpSignCost,setNewWarpCost] <vaule>");
 80                return true;
 81            }
 82
 83            FileConfiguration config = getConfig();
 84            if (args[0].equalsIgnoreCase("setWarpSignCost")) {
 85                if (args.length == 1) {
 86                    player.sendMessage("Cost = $" + config.getInt("WarpSignCost"));
 87                } else {
 88                    config.set("WarpSignCost", Integer.parseInt(args[1]));
 89                    saveConfig();
 90                    player.sendMessage("Cost is now $" + config.getInt("WarpSignCost"));
 91
 92                }
 93            }
 94            if (args[0].equalsIgnoreCase("setNewWarpCost")) {
 95                if (args.length == 1) {
 96                    player.sendMessage("Cost = $" + config.getInt("NewWarpCost"));
 97                } else {
 98                    config.set("NewWarpCost", Integer.parseInt(args[1]));
 99                    saveConfig();
100                    player.sendMessage("Cost is now $" + config.getInt("NewWarpCost"));
101
102                }
103            }
104            if (args[0].equalsIgnoreCase("Cleanup")) {
105                CleanupInvaildWarps();
106                sender.sendMessage("cleanup complete");
107                return true;
108            }
109
110
111
112
113
114        } else if (commandName.equalsIgnoreCase("PayServer")) {
115            if (args.length >= 1) {
116                try {
117                    double ammount = Double.parseDouble(args[0]);
118                    Player player = (Player) sender;
119                    if (economy.getBalance(player.getName()) >= ammount) {
120                        economy.withdrawPlayer(player.getName(), ammount);
121                        player.sendMessage(ChatColor.GREEN + "$" + ammount + " has been given to the sever");
122                        if (args.length >= 2) {
123                            String reference = "";
124                            for (int i = 1; i < args.length; i++) {
125                                reference += args[i] + " ";
126                            }
127                            log.log(Level.INFO, "${0} has been taken from player: {1} reference: [{2}]", new Object[]{ammount, player.getName(), reference});
128                        } else {
129                            log.log(Level.INFO, "${0} has been taken from player: {1} reference: [none]", new Object[]{ammount, player.getName()});
130                        }
131
132                    } else {
133                        player.sendMessage(ChatColor.RED + "You don't have that ammount of money");
134                    }
135                    return true;
136                } catch (Exception e) {
137                    sender.sendMessage("The ammount is not a number");
138                    return false;
139                }
140
141
142            }
143
144        }
145
146
147
148        return false;
149    }
150
151    public void TeleportPlayer(Player player, String warpName, double Cost) {
152        BufferedReader in;
153        String world;
154        double x, y, z;
155        float yaw, pitch;
156
157        try {
158            in = new BufferedReader(new FileReader("plugins/Essentials/warps/" + warpName + ".yml"));
159            try {
160                world = in.readLine().substring(7);
161                String tempxyz;;
162                tempxyz = in.readLine().substring(3);
163                x = Double.parseDouble(tempxyz);
164                tempxyz = in.readLine().substring(3);
165                y = Double.parseDouble(tempxyz);
166                tempxyz = in.readLine().substring(3);
167                z = Double.parseDouble(tempxyz);
168                tempxyz = in.readLine().substring(5);
169                yaw = Float.parseFloat(tempxyz);
170                tempxyz = in.readLine().substring(7);
171                pitch = Float.parseFloat(tempxyz);
172                System.out.println(tempxyz);
173                in.close();
174
175
176
177            } catch (IOException ex) {
178                player.sendMessage(ChatColor.DARK_RED + "Something is wrong with the warp File");
179                try {
180                    in.close();
181                } catch (IOException ex1) {
182                    Logger.getLogger(LolnetWarpSign.class.getName()).log(Level.SEVERE, null, ex1);
183                }
184                return;
185            }
186        } catch (FileNotFoundException ex) {
187            player.sendMessage(ChatColor.DARK_RED + "Warp does not exist");
188            return;
189        }
190
191        List<World> worlds = getServer().getWorlds();
192        World teleWorld;
193        boolean worldExist = false;
194        boolean canTele;
195        for (int j = 0; j < worlds.size(); j++) {
196            if (worlds.get(j).getName().equalsIgnoreCase(world)) {
197                worldExist = true;
198                break;
199            }
200        }
201        if (worldExist) {
202            teleWorld = getServer().getWorld(world);
203
204        } else {
205            player.sendMessage(ChatColor.DARK_RED + "Can't find World");
206            return;
207        }
208        if (!player.hasPermission("multiverse.access." + teleWorld.getName())) {
209            player.sendMessage(ChatColor.DARK_RED + "you don't have permission: " + "multiverse.access." + teleWorld.getName());
210            return;
211        }
212
213
214
215        player.sendMessage(ChatColor.YELLOW + "Teleporting to: " + warpName);
216        Location loc = new Location(teleWorld, x, y, z, yaw, pitch);
217        player.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
218    }
219
220    public void CleanupInvaildWarps() {
221        List<World> worlds = getServer().getWorlds();
222        BufferedReader in;
223        String world;
224        boolean worldExist;
225        File[] fileList = new File("plugins/Essentials/warps").listFiles();
226        for (int i = 0; i < fileList.length; i++) {
227            worldExist = false;
228            try {
229                in = new BufferedReader(new FileReader(fileList[i].getAbsolutePath()));
230                try {
231                    world = in.readLine().substring(7);
232                    in.close();
233
234
235                    for (int j = 0; j < worlds.size(); j++) {
236                        if (worlds.get(j).getName().equalsIgnoreCase(world)) {
237                            worldExist = true;
238                            break;
239                        }
240                    }
241
242                    if (!worldExist) {
243                        fileList[i].delete();
244
245                    }
246
247
248                } catch (IOException ex) {
249                    Logger.getLogger(LolnetWarpSign.class.getName()).log(Level.SEVERE, null, ex);
250                }
251
252            } catch (FileNotFoundException ex) {
253                Logger.getLogger(LolnetWarpSign.class.getName()).log(Level.SEVERE, null, ex);
254            }
255        }
256    }
257
258    public static void main(String[] args) {
259    }
260}