Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.96 KB | None | 0 0
  1. package spielplatz.tuhlienn.spielplatzachievements;
  2.  
  3. import java.io.File;
  4. import java.io.PrintStream;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.text.DecimalFormat;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.HashMap;
  12. import java.util.Iterator;
  13. import java.util.Set;
  14. import java.util.UUID;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.ChatColor;
  17. import org.bukkit.Location;
  18. import org.bukkit.Material;
  19. import org.bukkit.Server;
  20. import org.bukkit.Sound;
  21. import org.bukkit.World;
  22. import org.bukkit.block.Block;
  23. import org.bukkit.command.PluginCommand;
  24. import org.bukkit.configuration.file.FileConfiguration;
  25. import org.bukkit.configuration.file.YamlConfiguration;
  26. import org.bukkit.entity.Player;
  27. import org.bukkit.event.EventHandler;
  28. import org.bukkit.event.Listener;
  29. import org.bukkit.event.block.Action;
  30. import org.bukkit.event.block.BlockBreakEvent;
  31. import org.bukkit.event.inventory.InventoryClickEvent;
  32. import org.bukkit.event.inventory.InventoryCloseEvent;
  33. import org.bukkit.event.player.PlayerInteractEvent;
  34. import org.bukkit.inventory.EquipmentSlot;
  35. import org.bukkit.inventory.Inventory;
  36. import org.bukkit.inventory.ItemStack;
  37. import org.bukkit.inventory.PlayerInventory;
  38. import org.bukkit.inventory.meta.ItemMeta;
  39. import org.bukkit.plugin.PluginManager;
  40. import org.bukkit.plugin.java.JavaPlugin;
  41. import org.bukkit.scheduler.BukkitScheduler;
  42.  
  43. public class SpielplatzAchievements
  44.   extends JavaPlugin
  45.   implements Listener
  46. {
  47.   File configFile;
  48.   FileConfiguration config;
  49.   public MySQLConnection mysql;
  50.   ResultSet rs;
  51.   Connection con;
  52.   boolean connected = true;
  53.   public ChatColor farbe = ChatColor.DARK_AQUA;
  54.   private String saHelp = this.farbe + "-----�f[" + this.farbe + "SpielplatzAchievements�f]" + this.farbe + "----- \n" +
  55.     this.farbe + "/sa add <1> <2>�r: Erstellt ein Achievement mit einem Wert " + this.farbe + "<1>�r und einem Namen " + this.farbe + "<2>�r an der aktuellen Position \n" +
  56.     this.farbe + "/sa modify <1> <2> <3>�r: Weist dem Achievement mit der ID " + this.farbe + "<1>�r einen neuen Wert " + this.farbe + "<2>�r und einen neuen Namen " + this.farbe + "<3>�r zu \n" +
  57.     this.farbe + "/sa info [achievement|stats]�r: Zeigt erweiterte Informationen an \n" +
  58.     this.farbe + "/sa remove <1>�r: Entfernt das Achievement mit ID " + this.farbe + "<1> \n" +
  59.     this.farbe + "/sa list (<1>)�r: Zeigt die Seite " + this.farbe + "<1>�r aller bestehenden Achievements an \n" +
  60.     this.farbe + "/sa tp <1>�r: Teleport den Spieler zum Achievement mit ID " + this.farbe + "<1> \n" +
  61.     this.farbe + "/sa set <1>�r: Setzt die Position des Achievements mit ID " + this.farbe + "<1>�r an die aktuelle Position \n" +
  62.     this.farbe + "/sa activate <1>�r: Aktiviert/Deaktiviert das Achievement mit der ID " + this.farbe + "<1>";
  63.   private int size = 9;
  64.   private int updatecycle = 12000;
  65.   private String reward = "";
  66.   private ItemStack pot = new ItemStack(Material.FLOWER_POT_ITEM);
  67.   private ItemMeta potmeta = this.pot.getItemMeta();
  68.   private ItemStack flower = new ItemStack(Material.DOUBLE_PLANT);
  69.   private ItemMeta flowermeta = this.flower.getItemMeta();
  70.   private HashMap<Player, Integer> inMenu = new HashMap();
  71.   private HashMap<Integer, String> achievementNames = new HashMap();
  72.   private HashMap<Integer, Integer> achievementValues = new HashMap();
  73.   private HashMap<Integer, Location> achievementLocations = new HashMap();
  74.   private HashMap<Integer, Boolean> achievementActive = new HashMap();
  75.   private HashMap<UUID, ArrayList<Integer>> players = new HashMap();
  76.   int timer = 0;
  77.   Runnable run = new Runnable()
  78.   {
  79.     public void run()
  80.     {
  81.       if (SpielplatzAchievements.this.connected) {
  82.         SpielplatzAchievements.this.writeToDataBase();
  83.       }
  84.     }
  85.   };
  86.  
  87.   public void onEnable()
  88.   {
  89.     getServer().getPluginManager().registerEvents(this, this);
  90.    
  91.     this.configFile = new File(getDataFolder(), "config.yml");
  92.     this.config = YamlConfiguration.loadConfiguration(this.configFile);
  93.    
  94.     saveDefaultConfig();
  95.    
  96.     this.size = getConfig().getInt("general.size");
  97.     this.updatecycle = getConfig().getInt("general.updatecycle");
  98.     this.reward = getConfig().getString("general.reward");
  99.    
  100.     String hostname = getConfig().getString("MySQL.hostname");
  101.     String hostport = getConfig().getString("MySQL.hostport");
  102.     String database = getConfig().getString("MySQL.database");
  103.     String user = getConfig().getString("MySQL.user");
  104.     String password = getConfig().getString("MySQL.password");
  105.    
  106.     this.mysql = new MySQLConnection(hostname, hostport, database, user, password);
  107.     try
  108.     {
  109.       this.mysql.open();
  110.       this.mysql.createTables();
  111.       this.mysql.close();
  112.     }
  113.     catch (Exception e)
  114.     {
  115.       this.connected = false;
  116.     }
  117.     if (this.connected)
  118.     {
  119.       loadFromDataBase();
  120.      
  121.       System.out.println("[SpielplatzAchievements] Connected to MySQL");
  122.      
  123.       this.timer = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this.run, 0L, this.updatecycle);
  124.     }
  125.     registerCommand();
  126.   }
  127.  
  128.   public void onDisable()
  129.   {
  130.     if (this.connected)
  131.     {
  132.       writeToDataBase();
  133.       Bukkit.getScheduler().cancelTask(this.timer);
  134.     }
  135.   }
  136.  
  137.   public void registerCommand()
  138.   {
  139.     Command_sa cmd_sa = new Command_sa(this);
  140.     getCommand("sa").setExecutor(cmd_sa);
  141.     Command_erfolge cmd_erfolge = new Command_erfolge(this);
  142.     getCommand("erfolge").setExecutor(cmd_erfolge);
  143.   }
  144.  
  145.   @EventHandler
  146.   public void onPlayerClick(PlayerInteractEvent event)
  147.   {
  148.     Player p = event.getPlayer();
  149.     if (p.hasPermission("sa.erfolge")) {
  150.       if ((event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) &&
  151.         (event.getHand().equals(EquipmentSlot.HAND))) {
  152.         if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
  153.         {
  154.           Location clicked = event.getClickedBlock().getLocation();
  155.           for (Integer id : this.achievementNames.keySet()) {
  156.             if (((Location)this.achievementLocations.get(id)).equals(clicked)) {
  157.               if (((Boolean)this.achievementActive.get(id)).booleanValue())
  158.               {
  159.                 UUID uuid = p.getUniqueId();
  160.                 String name = (String)this.achievementNames.get(id);
  161.                 int value = ((Integer)this.achievementValues.get(id)).intValue();
  162.                 if (this.players.containsKey(uuid)) {
  163.                   for (Integer test : (ArrayList)this.players.get(uuid)) {
  164.                     if (id == test)
  165.                     {
  166.                       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Du hast das Achievement " + this.farbe + id + ": " + name + "�r bereits gefunden!");
  167.                       return;
  168.                     }
  169.                   }
  170.                 }
  171.                 putAchievementToPlayer(uuid, id.intValue());
  172.                 if (this.reward.equals("diamond"))
  173.                 {
  174.                   p.getInventory().addItem(new ItemStack[] { new ItemStack(Material.DIAMOND, value) });
  175.                   p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Du hast das Achievement " + this.farbe + id + ": " + name + "�r gefunden und " + this.farbe + value + "�r Diamanten erhalten!");
  176.                 }
  177.                 else if (this.reward.equals("votepoint"))
  178.                 {
  179.                   Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "svp add " + p.getName() + " " + value);
  180.                   p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Du hast das Achievement " + this.farbe + id + ": " + name + "�r gefunden und " + this.farbe + value + "�r Votepunkte erhalten!");
  181.                 }
  182.                 else
  183.                 {
  184.                   p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Du hast das Achievement " + this.farbe + id + ": " + name + "�r gefunden!");
  185.                 }
  186.                 p.playSound(clicked, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 0.0F);
  187.                
  188.                 event.setCancelled(true);
  189.               }
  190.             }
  191.           }
  192.         }
  193.       }
  194.     }
  195.   }
  196.  
  197.   @EventHandler
  198.   public void onBlockBreak(BlockBreakEvent event)
  199.   {
  200.     Player p = event.getPlayer();
  201.    
  202.     Location broken = event.getBlock().getLocation();
  203.     ArrayList<Integer> toRemove = new ArrayList();
  204.     for (Integer id : this.achievementNames.keySet()) {
  205.       if (((Location)this.achievementLocations.get(id)).equals(broken)) {
  206.         toRemove.add(id);
  207.       }
  208.     }
  209.     if (!toRemove.isEmpty()) {
  210.       if (p.hasPermission("sa.op"))
  211.       {
  212.         for (Integer id : toRemove) {
  213.           removeAchievement(p, id.intValue());
  214.         }
  215.         updateIDs();
  216.       }
  217.       else
  218.       {
  219.         p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] �cDu kannst keine Achievements zerst�ren!");
  220.         event.setCancelled(true);
  221.       }
  222.     }
  223.   }
  224.  
  225.   @EventHandler
  226.   public void onInventoryClick(InventoryClickEvent event)
  227.   {
  228.     Player p = (Player)event.getWhoClicked();
  229.     if (this.inMenu.containsKey(p))
  230.     {
  231.       int page = ((Integer)this.inMenu.get(p)).intValue();
  232.       int clicked = event.getSlot();
  233.       if ((clicked == 0) && (page != 1))
  234.       {
  235.         showAchievementPage(page - 1, p, p);
  236.         event.setCancelled(true);
  237.       }
  238.       else if ((clicked == 8) && (page != 6))
  239.       {
  240.         showAchievementPage(page + 1, p, p);
  241.         event.setCancelled(true);
  242.       }
  243.       else
  244.       {
  245.         event.setCancelled(true);
  246.       }
  247.     }
  248.   }
  249.  
  250.   @EventHandler
  251.   public void onInventoryClose(InventoryCloseEvent event)
  252.   {
  253.     Player p = (Player)event.getPlayer();
  254.     if (this.inMenu.containsKey(p)) {
  255.       this.inMenu.remove(p);
  256.     }
  257.   }
  258.  
  259.   public void lockPlayer(Player p)
  260.   {
  261.     this.inMenu.put(p, Integer.valueOf(1));
  262.   }
  263.  
  264.   public int getPage(Player p)
  265.   {
  266.     return ((Integer)this.inMenu.get(p)).intValue();
  267.   }
  268.  
  269.   public int getNextID()
  270.   {
  271.     if (this.achievementNames.isEmpty()) {
  272.       return 0;
  273.     }
  274.     int max = 0;
  275.     for (Integer id : this.achievementNames.keySet()) {
  276.       max = id.intValue() > max ? id.intValue() : max;
  277.     }
  278.     return max + 1;
  279.   }
  280.  
  281.   public void updateIDs()
  282.   {
  283.     HashMap<Integer, String> newAchievementNames = new HashMap();
  284.     HashMap<Integer, Integer> newAchievementValues = new HashMap();
  285.     HashMap<Integer, Location> newAchievementLocations = new HashMap();
  286.     HashMap<Integer, Boolean> newAchievementActive = new HashMap();
  287.    
  288.     HashMap<UUID, ArrayList<Integer>> newPlayers = new HashMap();
  289.    
  290.     int counter = 0;
  291.     for (Integer id : this.achievementNames.keySet())
  292.     {
  293.       newAchievementNames.put(Integer.valueOf(counter), (String)this.achievementNames.get(id));
  294.       newAchievementValues.put(Integer.valueOf(counter), (Integer)this.achievementValues.get(id));
  295.       newAchievementLocations.put(Integer.valueOf(counter), (Location)this.achievementLocations.get(id));
  296.       newAchievementActive.put(Integer.valueOf(counter), (Boolean)this.achievementActive.get(id));
  297.       for (UUID uuid : this.players.keySet()) {
  298.         if (((ArrayList)this.players.get(uuid)).contains(id)) {
  299.           if (newPlayers.containsKey(uuid))
  300.           {
  301.             ArrayList<Integer> ids = (ArrayList)newPlayers.get(uuid);
  302.             ids.add(Integer.valueOf(counter));
  303.             newPlayers.remove(uuid);
  304.             newPlayers.put(uuid, ids);
  305.           }
  306.           else
  307.           {
  308.             ArrayList<Integer> ids = new ArrayList();
  309.             ids.add(Integer.valueOf(counter));
  310.             newPlayers.put(uuid, ids);
  311.           }
  312.         }
  313.       }
  314.       counter++;
  315.     }
  316.     this.achievementNames = newAchievementNames;
  317.     this.achievementValues = newAchievementValues;
  318.     this.achievementLocations = newAchievementLocations;
  319.     this.achievementActive = newAchievementActive;
  320.    
  321.     this.players = newPlayers;
  322.   }
  323.  
  324.   public void showAchievementPage(int page, Player sender, Player shown)
  325.   {
  326.     int current = page;
  327.    
  328.     lockPlayer(sender);
  329.    
  330.     Inventory inv = Bukkit.createInventory(sender, this.size + 9, "Erfolge - Seite " + page);
  331.     ItemStack glass = null;
  332.     for (int index = 0; index < 9; index++)
  333.     {
  334.       if (index == 0)
  335.       {
  336.         if (current != 1)
  337.         {
  338.           glass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14);
  339.           ItemMeta glassmeta = glass.getItemMeta();
  340.           glassmeta.setDisplayName("�cZur�ck");
  341.           glass.setItemMeta(glassmeta);
  342.         }
  343.         else
  344.         {
  345.           glass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)0);
  346.         }
  347.       }
  348.       else if (index == 8)
  349.       {
  350.         if (current != 6)
  351.         {
  352.           glass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)5);
  353.           ItemMeta glassmeta = glass.getItemMeta();
  354.           glassmeta.setDisplayName("�aWeiter");
  355.           glass.setItemMeta(glassmeta);
  356.         }
  357.       }
  358.       else {
  359.         glass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)0);
  360.       }
  361.       inv.setItem(index, glass);
  362.     }
  363.     int i = 0;
  364.     for (Integer id : this.achievementNames.keySet())
  365.     {
  366.       if ((i >= (current - 1) * this.size) && (i < current * this.size))
  367.       {
  368.         String name = (String)this.achievementNames.get(id);
  369.         String world = ((Location)this.achievementLocations.get(id)).getWorld().getName();
  370.        
  371.         boolean hasAchievement = false;
  372.         if ((this.players.containsKey(shown.getUniqueId())) &&
  373.           (((ArrayList)this.players.get(shown.getUniqueId())).contains(id))) {
  374.           hasAchievement = true;
  375.         }
  376.         if (hasAchievement)
  377.         {
  378.           this.flowermeta.setDisplayName("�a" + id + ": " + name);
  379.           this.flowermeta.setLore(Arrays.asList(new String[] { "�f" + world }));
  380.           this.flower.setItemMeta(this.flowermeta);
  381.           inv.setItem(i % this.size + 9, this.flower);
  382.         }
  383.         else
  384.         {
  385.           this.potmeta.setDisplayName("�c" + id + ": " + name);
  386.           this.potmeta.setLore(Arrays.asList(new String[] { "�f" + world }));
  387.           this.pot.setItemMeta(this.potmeta);
  388.           inv.setItem(i % this.size + 9, this.pot);
  389.         }
  390.       }
  391.       i++;
  392.     }
  393.     sender.openInventory(inv);
  394.     this.inMenu.put(sender, Integer.valueOf(current));
  395.   }
  396.  
  397.   public void showListPage(int page, Player p)
  398.   {
  399.     int entries = 10;
  400.     int min = (page - 1) * entries;
  401.     int max = page * entries - 1;
  402.    
  403.     p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Achievements (" + this.farbe + "Seite " + page + "�f):�r");
  404.     for (Integer id : this.achievementNames.keySet()) {
  405.       if ((id.intValue() >= min) && (id.intValue() <= max))
  406.       {
  407.         ChatColor color;
  408.         ChatColor color;
  409.         if (((Boolean)this.achievementActive.get(id)).booleanValue()) {
  410.           color = ChatColor.GREEN;
  411.         } else {
  412.           color = ChatColor.RED;
  413.         }
  414.         p.sendMessage(color + id + "�f:" + color + (String)this.achievementNames.get(id));
  415.       }
  416.     }
  417.   }
  418.  
  419.   public void addAchievement(Player p, int value, String name)
  420.   {
  421.     Location loc = p.getLocation().getBlock().getLocation();
  422.     int id = getNextID();
  423.    
  424.     this.achievementNames.put(Integer.valueOf(id), name);
  425.     this.achievementValues.put(Integer.valueOf(id), Integer.valueOf(value));
  426.     this.achievementLocations.put(Integer.valueOf(id), loc);
  427.     this.achievementActive.put(Integer.valueOf(id), Boolean.valueOf(true));
  428.    
  429.     p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Das neue Achievement " + this.farbe + id + ": " + name + "�r mit dem Wert " + this.farbe + value + "�r wurde an der Position " + this.farbe + loc.getBlockX() + "�r, " + this.farbe + loc.getBlockY() + "�r, " + this.farbe + loc.getBlockZ() + "�r gesetzt!");
  430.   }
  431.  
  432.   public void removeAchievement(Player p, int id)
  433.   {
  434.     if (this.achievementNames.containsKey(Integer.valueOf(id)))
  435.     {
  436.       this.achievementNames.remove(Integer.valueOf(id));
  437.       this.achievementLocations.remove(Integer.valueOf(id));
  438.       this.achievementValues.remove(Integer.valueOf(id));
  439.       this.achievementActive.remove(Integer.valueOf(id));
  440.       for (UUID uuid : this.players.keySet())
  441.       {
  442.         ArrayList<Integer> achievements = (ArrayList)this.players.get(uuid);
  443.         if (achievements.contains(Integer.valueOf(id)))
  444.         {
  445.           achievements.remove(Integer.valueOf(id));
  446.           this.players.replace(uuid, achievements);
  447.         }
  448.       }
  449.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Das Achievement mit der ID " + this.farbe + id + "�r wurde entfernt!");
  450.     }
  451.     else
  452.     {
  453.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Ein Achievement mit der ID " + this.farbe + id + "�r existiert nicht!");
  454.     }
  455.   }
  456.  
  457.   public void setAchievement(Player p, int id)
  458.   {
  459.     if (this.achievementNames.containsKey(Integer.valueOf(id)))
  460.     {
  461.       Location loc = p.getLocation().getBlock().getLocation();
  462.      
  463.       this.achievementLocations.remove(Integer.valueOf(id));
  464.       this.achievementLocations.put(Integer.valueOf(id), loc);
  465.      
  466.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Das Achievement mit der ID " + this.farbe + id + "�r wurde auf die Position " + this.farbe + loc.getBlockX() + "�r, " + this.farbe + loc.getBlockY() + "�r, " + this.farbe + loc.getBlockZ() + "�r verschoben!");
  467.     }
  468.     else
  469.     {
  470.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Ein Achievement mit der ID " + this.farbe + id + "�r existiert nicht!");
  471.     }
  472.   }
  473.  
  474.   public void activateAchievement(Player p, int id)
  475.   {
  476.     if (this.achievementNames.containsKey(Integer.valueOf(id)))
  477.     {
  478.       boolean activated = ((Boolean)this.achievementActive.get(Integer.valueOf(id))).booleanValue();
  479.       this.achievementActive.remove(Integer.valueOf(id));
  480.       this.achievementActive.put(Integer.valueOf(id), Boolean.valueOf(!activated));
  481.       if (activated) {
  482.         p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Das Achievement mit der ID " + this.farbe + id + "�r wurde deaktiviert!");
  483.       } else {
  484.         p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Das Achievement mit der ID " + this.farbe + id + "�r wurde aktiviert!");
  485.       }
  486.     }
  487.     else
  488.     {
  489.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] �cEs existiert kein Achievement mit der ID " + this.farbe + id + "�c!");
  490.     }
  491.   }
  492.  
  493.   public void tpToAchievement(Player p, int id)
  494.   {
  495.     if (this.achievementNames.containsKey(Integer.valueOf(id)))
  496.     {
  497.       Location achievementLoc = (Location)this.achievementLocations.get(Integer.valueOf(id));
  498.       Location newLoc = new Location(achievementLoc.getWorld(), achievementLoc.getX() + 0.5D, achievementLoc.getY() + 1.0D, achievementLoc.getZ() + 0.5D);
  499.       p.teleport(newLoc);
  500.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Du wurdest zu dem Achievement " + this.farbe + id + ": " + (String)this.achievementNames.get(Integer.valueOf(id)) + "�r teleportiert!");
  501.     }
  502.     else
  503.     {
  504.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] �cEs existiert kein Achievement mit der ID " + this.farbe + id + "�c!");
  505.     }
  506.   }
  507.  
  508.   public void modifyAchievement(Player p, int id, int value, String name)
  509.   {
  510.     if (this.achievementNames.containsKey(Integer.valueOf(id)))
  511.     {
  512.       String newName = name.equals("0") ? (String)this.achievementNames.get(Integer.valueOf(id)) : name;
  513.       int newValue = value == 0 ? ((Integer)this.achievementValues.get(Integer.valueOf(id))).intValue() : value;
  514.      
  515.       this.achievementNames.remove(Integer.valueOf(id));
  516.       this.achievementNames.put(Integer.valueOf(id), newName);
  517.       this.achievementValues.remove(Integer.valueOf(id));
  518.       this.achievementValues.put(Integer.valueOf(id), Integer.valueOf(newValue));
  519.      
  520.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] Das Achievement mit der ID " + this.farbe + id + "�r tr�gt nun den Namen" + this.farbe + name + "�r und den Votepunkte-Wert " + this.farbe + value + "�r!");
  521.     }
  522.     else
  523.     {
  524.       p.sendMessage("�f[" + this.farbe + "SpielplatzAchievements�f] �cEs existiert kein Achievement mit der ID " + this.farbe + id + "�c!");
  525.     }
  526.   }
  527.  
  528.   public void getAchievementInfo(Player p, int id)
  529.   {
  530.     String name = (String)this.achievementNames.get(Integer.valueOf(id));
  531.     int value = ((Integer)this.achievementValues.get(Integer.valueOf(id))).intValue();
  532.     String world = ((Location)this.achievementLocations.get(Integer.valueOf(id))).getWorld().getName();
  533.     String active = ((Boolean)this.achievementActive.get(Integer.valueOf(id))).booleanValue() ? "Ja" : "Nein";
  534.     int count = 0;
  535.     for (UUID uuid : this.players.keySet()) {
  536.       if (((ArrayList)this.players.get(uuid)).contains(Integer.valueOf(id))) {
  537.         count++;
  538.       }
  539.     }
  540.     p.sendMessage(this.farbe + "-----�f[" + this.farbe + "Achievement " + id + "�f]" + this.farbe + "----- \n" +
  541.       this.farbe + "Name�r: " + name + "\n" +
  542.       this.farbe + "Votepunkte�r: " + value + "\n" +
  543.       this.farbe + "Welt�r: " + world + " \n" +
  544.       this.farbe + "Aktiv�r: " + active + "\n" +
  545.       this.farbe + "Gesamtanzahl Funde�r: " + count);
  546.   }
  547.  
  548.   public void getStats(Player p)
  549.   {
  550.     int count = 0;
  551.     int totalValue = 0;
  552.     double involvedPlayers = 0.0D;
  553.     double totalFound = 0.0D;
  554.     String average = "";
  555.     DecimalFormat df = new DecimalFormat("#.##");
  556.     for (Iterator localIterator1 = this.achievementNames.keySet().iterator(); localIterator1.hasNext();)
  557.     {
  558.       int id = ((Integer)localIterator1.next()).intValue();
  559.       count++;
  560.       totalValue += ((Integer)this.achievementValues.get(Integer.valueOf(id))).intValue();
  561.     }
  562.     Iterator localIterator2;
  563.     for (localIterator1 = this.players.keySet().iterator(); localIterator1.hasNext(); localIterator2.hasNext())
  564.     {
  565.       UUID uuid = (UUID)localIterator1.next();
  566.       involvedPlayers += 1.0D;
  567.       localIterator2 = ((ArrayList)this.players.get(uuid)).iterator(); continue;int id = ((Integer)localIterator2.next()).intValue();
  568.       totalFound += 1.0D;
  569.     }
  570.     average = average + (involvedPlayers == 0.0D ? "Nicht vorhanden!" : df.format(totalFound / involvedPlayers));
  571.    
  572.     p.sendMessage(this.farbe + "-----�f[" + this.farbe + "Statistiken�f]" + this.farbe + "----- \n" +
  573.       this.farbe + "Anzahl an Achievements�r: " + count + "\n" +
  574.       this.farbe + "Gesamtwert der Achievements�r: " + totalValue + "\n" +
  575.       this.farbe + "Spieler mit mindestens einem Achievement�r: " + (int)involvedPlayers + " \n" +
  576.       this.farbe + "Durchschnittlich gefundene Achievements�r: " + average);
  577.   }
  578.  
  579.   public void loadFromDataBase()
  580.   {
  581.     this.achievementNames.clear();
  582.     this.achievementLocations.clear();
  583.     this.achievementValues.clear();
  584.     this.achievementActive.clear();
  585.     this.players.clear();
  586.     try
  587.     {
  588.       this.mysql.open();
  589.       this.con = this.mysql.getConnection();
  590.      
  591.       PreparedStatement achievements = this.con.prepareStatement("SELECT * FROM Achievements");
  592.       this.rs = achievements.executeQuery();
  593.       while (this.rs.next())
  594.       {
  595.         int id = this.rs.getInt("id");
  596.         String name = this.rs.getString("name");
  597.         Location loc = new Location(getServer().getWorld(this.rs.getString("world")), this.rs.getInt("x"), this.rs.getInt("y"), this.rs.getInt("z"));
  598.         int value = this.rs.getInt("value");
  599.         Boolean activated = Boolean.valueOf(this.rs.getBoolean("activated"));
  600.        
  601.         this.achievementNames.put(Integer.valueOf(id), name);
  602.         this.achievementLocations.put(Integer.valueOf(id), loc);
  603.         this.achievementValues.put(Integer.valueOf(id), Integer.valueOf(value));
  604.         this.achievementActive.put(Integer.valueOf(id), activated);
  605.       }
  606.       PreparedStatement playerAchievements = this.con.prepareStatement("SELECT * FROM Players");
  607.       this.rs = playerAchievements.executeQuery();
  608.       while (this.rs.next())
  609.       {
  610.         UUID uuid = UUID.fromString(this.rs.getString("uuid"));
  611.         int achievement = this.rs.getInt("achievement");
  612.         putAchievementToPlayer(uuid, achievement);
  613.       }
  614.     }
  615.     catch (Exception e)
  616.     {
  617.       e.printStackTrace();
  618.     }
  619.   }
  620.  
  621.   public void writeToDataBase()
  622.   {
  623.     try
  624.     {
  625.       this.mysql.open();
  626.       this.con = this.mysql.getConnection();
  627.       PreparedStatement clear = this.con.prepareStatement("TRUNCATE TABLE Achievements");
  628.       clear.executeUpdate();
  629.       clear.close();
  630.       PreparedStatement clear2 = this.con.prepareStatement("TRUNCATE TABLE Players");
  631.       clear2.executeUpdate();
  632.       clear2.close();
  633.       for (Integer id : this.achievementNames.keySet())
  634.       {
  635.         PreparedStatement achievement = this.con.prepareStatement("INSERT INTO Achievements(id,name,world,x,y,z,value,activated) VALUES (?,?,?,?,?,?,?,?)");
  636.         achievement.setInt(1, id.intValue());
  637.         achievement.setString(2, (String)this.achievementNames.get(id));
  638.         achievement.setString(3, ((Location)this.achievementLocations.get(id)).getWorld().getName());
  639.         achievement.setDouble(4, ((Location)this.achievementLocations.get(id)).getX());
  640.         achievement.setDouble(5, ((Location)this.achievementLocations.get(id)).getY());
  641.         achievement.setDouble(6, ((Location)this.achievementLocations.get(id)).getZ());
  642.         achievement.setInt(7, ((Integer)this.achievementValues.get(id)).intValue());
  643.         achievement.setBoolean(8, ((Boolean)this.achievementActive.get(id)).booleanValue());
  644.         achievement.executeUpdate();
  645.         achievement.close();
  646.       }
  647.       Iterator localIterator2;
  648.       for (??? = this.players.keySet().iterator(); ???.hasNext(); localIterator2.hasNext())
  649.       {
  650.         UUID uuid = (UUID)???.next();
  651.         localIterator2 = ((ArrayList)this.players.get(uuid)).iterator(); continue;Integer achievement = (Integer)localIterator2.next();
  652.         PreparedStatement player = this.con.prepareStatement("INSERT INTO Players(uuid,achievement) VALUES (?,?)");
  653.         player.setString(1, uuid.toString());
  654.         player.setInt(2, achievement.intValue());
  655.         player.executeUpdate();
  656.         player.close();
  657.       }
  658.     }
  659.     catch (Exception e)
  660.     {
  661.       e.printStackTrace();
  662.     }
  663.   }
  664.  
  665.   public void putAchievementToPlayer(UUID uuid, int id)
  666.   {
  667.     if (this.players.containsKey(uuid))
  668.     {
  669.       ArrayList<Integer> ids = (ArrayList)this.players.get(uuid);
  670.       ids.add(Integer.valueOf(id));
  671.       this.players.remove(uuid);
  672.       this.players.put(uuid, ids);
  673.     }
  674.     else
  675.     {
  676.       ArrayList<Integer> ids = new ArrayList();
  677.       ids.add(Integer.valueOf(id));
  678.       this.players.put(uuid, ids);
  679.     }
  680.   }
  681.  
  682.   public String getSaHelp()
  683.   {
  684.     return this.saHelp;
  685.   }
  686. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement