Advertisement
Guest User

Untitled

a guest
Sep 13th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1. package me.neo.points;
  2.  
  3. import org.bukkit.ChatColor;
  4. import org.bukkit.command.Command;
  5. import org.bukkit.command.CommandSender;
  6. import org.bukkit.configuration.file.FileConfiguration;
  7. import org.bukkit.configuration.file.YamlConfiguration;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.player.PlayerJoinEvent;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13.  
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.logging.Level;
  17.  
  18. /**
  19.  * Created by Neotroost on 13-9-2015.
  20.  * Copyrighted!
  21.  */
  22. public class Library extends JavaPlugin implements Listener {
  23.  
  24.     public String prefix;
  25.     public String color1;
  26.     public String color2;
  27.  
  28.     @Override
  29.     public void onEnable() {
  30.         getConfig().options().copyDefaults();
  31.         saveDefaultConfig();
  32.  
  33.         getCommand("points").setExecutor(this);
  34.  
  35.  
  36.         load();
  37.  
  38.         getServer().getPluginManager().registerEvents(this, this);
  39.     }
  40.  
  41.     @Override
  42.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  43.         if (!(sender instanceof Player)) {
  44.             return true;
  45.         }
  46.         if (args.length == 0) {
  47.             Player p = (Player) sender;
  48.             String name = p.getName();
  49.             File userfile = new File(getUserFolder(), name + ".yml");
  50.             File file = new File(getDataFolder(), "userdata");
  51.             FileConfiguration userconfig = YamlConfiguration.loadConfiguration(userfile);
  52.             int points = userconfig.getInt("Points");
  53.             String message = ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.points"));
  54.             String replaced = message.replaceAll("<points>", String.valueOf(points));
  55.             p.sendMessage(replaced);
  56.             return true;
  57.  
  58.         }
  59.         if (args[0].equalsIgnoreCase("add")) {
  60.             if (!(sender.hasPermission("points.use"))) {
  61.                 sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.nopermission")));
  62.                 return true;
  63.             }
  64.             if (!(args.length >= 3)) {
  65.                 sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.noamount")));
  66.                 return true;
  67.             }
  68.             String name = args[1];
  69.             File userfile = new File(getUserFolder(), name + ".yml");
  70.             File file = new File(getDataFolder(), "userdata");
  71.             FileConfiguration userconfig = YamlConfiguration.loadConfiguration(userfile);
  72.             int points = userconfig.getInt("Points");
  73.  
  74.             Double pointss = null;
  75.             try {
  76.                 pointss = Double.parseDouble(args[2]);
  77.             }
  78.             catch (NumberFormatException e) {
  79.                 sender.sendMessage(color1 + "arguments" + color2 + " 2" + color1 + " must contain numbers only!");
  80.                 sender.sendMessage(color1 + "Argument:" + color2 + " " + args[2]);
  81.                 return true;
  82.             }
  83.             double amounts  = (points + pointss);
  84.             userconfig.set("Points", amounts);
  85.             try {
  86.                 userconfig.save(userfile);
  87.             } catch (IOException u) {
  88.                 getLogger().log(Level.SEVERE, "cannot save critical user information!", u);
  89.             }
  90.           return true;
  91.         }
  92.         if (args[0].equalsIgnoreCase("remove")) {
  93.             if (!(sender.hasPermission("points.use"))) {
  94.                 sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.nopermission")));
  95.                 return true;
  96.             }
  97.             if (!(args.length >= 3)) {
  98.                 sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.noamount")));
  99.                 return true;
  100.             }
  101.             String name = args[1];
  102.             File userfile = new File(getUserFolder(), name + ".yml");
  103.             File file = new File(getDataFolder(), "userdata");
  104.             FileConfiguration userconfig = YamlConfiguration.loadConfiguration(userfile);
  105.             int points = userconfig.getInt("Points");
  106.  
  107.             Double pointss = null;
  108.             try {
  109.                 pointss = Double.parseDouble(args[2]);
  110.             }
  111.             catch (NumberFormatException e) {
  112.                 sender.sendMessage(color1 + "arguments" + color2 + " 2" + color1 + " must contain numbers only!");
  113.                 sender.sendMessage(color1 + "Argument:" + color2 + " " + args[2]);
  114.                 return true;
  115.             }
  116.             double amounts  = (points - pointss);
  117.             userconfig.set("Points", amounts);
  118.             try {
  119.                 userconfig.save(userfile);
  120.             } catch (IOException u) {
  121.                 getLogger().log(Level.SEVERE, "cannot save critical user information!", u);
  122.             }
  123.  
  124.         }
  125.         return true;
  126.     }
  127.  
  128.  
  129.      @EventHandler
  130.     public void onPlayerJoin(PlayerJoinEvent e) {
  131.          String name = e.getPlayer().getName();
  132.         File userfile = new File(getUserFolder(), name + ".yml");
  133.         File file = new File(getDataFolder(), "userdata");
  134.  
  135.         try {
  136.             userfile.createNewFile();
  137.         } catch (IOException u) {
  138.             getLogger().log(Level.SEVERE, "User file not created!",u);
  139.             return;
  140.         }
  141.         FileConfiguration userconfig = YamlConfiguration.loadConfiguration(userfile);
  142.  
  143.         userconfig.set("Username", e.getPlayer().getName());
  144.         userconfig.set("Points", 0);
  145.  
  146.         try {
  147.             userconfig.save(userfile);
  148.         } catch (IOException u) {
  149.             getLogger().log(Level.SEVERE, "cannot save critical user information!", e);
  150.         }
  151.  
  152.     }
  153.  
  154.  
  155.     public void load() {
  156.         prefix = ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.prefix"));
  157.         String success = ChatColor.translateAlternateColorCodes('&', String.valueOf(getConfig().getStringList("Settings.success")));
  158.         color1 =  ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.color1"));
  159.         color2 = ChatColor.translateAlternateColorCodes('&', getConfig().getString("Settings.color2"));
  160.     }
  161.  
  162.  
  163.     public File getUserFolder() {
  164.         File file = new File(getDataFolder(), "userdata");
  165.         if (!file.exists()) {
  166.             //noinspection ResultOfMethodCallIgnored
  167.             file.mkdir();
  168.         }
  169.         return file;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement