Advertisement
Camer047

Untitled

Jan 16th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. package me.Camer047;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.UUID;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.ChatColor;
  14. import org.bukkit.Material;
  15. import org.bukkit.OfflinePlayer;
  16. import org.bukkit.command.Command;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.event.EventHandler;
  20. import org.bukkit.event.Listener;
  21. import org.bukkit.event.player.PlayerJoinEvent;
  22. import org.bukkit.inventory.Inventory;
  23. import org.bukkit.inventory.ItemStack;
  24. import org.bukkit.inventory.meta.ItemMeta;
  25. import org.bukkit.permissions.Permission;
  26. import org.bukkit.plugin.java.JavaPlugin;
  27.  
  28. public class Coins extends JavaPlugin implements Listener {
  29. static HashMap<UUID, Integer> map = new HashMap<>();
  30.  
  31. public Permission give = new Permission("coins.give");
  32.  
  33.  
  34. public void writetodisk()
  35. {
  36. try
  37. {
  38. FileOutputStream fileOut =
  39. new FileOutputStream("plugins/coindata.txt");
  40. ObjectOutputStream out = new ObjectOutputStream(fileOut);
  41. out.writeObject(map);
  42. out.close();
  43. fileOut.close();
  44. System.out.printf("Serialized data is saved in data.txt");
  45. }catch(IOException i)
  46. {
  47. i.printStackTrace();
  48. }
  49. }
  50.  
  51. @SuppressWarnings("unchecked")
  52. public void readfromdisk () {
  53. try
  54. {
  55. FileInputStream fileIn =
  56. new FileInputStream("plugins/coindata.txt");
  57. ObjectInputStream in = new ObjectInputStream(fileIn);
  58. map = (HashMap<UUID, Integer>) in.readObject();
  59. in.close();
  60. fileIn.close();
  61. System.out.printf("Serialized data is saved in data.txt");
  62. }catch(IOException i)
  63. {
  64. i.printStackTrace();
  65. }
  66. catch(ClassNotFoundException c)
  67. {
  68. System.out.println("Class not found");
  69. c.printStackTrace();
  70. }
  71.  
  72.  
  73. }
  74.  
  75. public static void giveCoins(UUID uuid, Integer amount)
  76. {
  77.  
  78. if (map.containsKey(uuid)) {
  79. int tempcoins = map.get(uuid);
  80. int finalcoins = tempcoins + amount;
  81. map.put(uuid, finalcoins);
  82. }
  83.  
  84.  
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91. @EventHandler
  92. public void onEvent(PlayerJoinEvent e) {
  93. Player player = e.getPlayer();
  94. UUID playeruuid = player.getUniqueId();
  95. if (map.containsKey(playeruuid)) {
  96. } else {
  97. getLogger().info("Player " + playeruuid + " has been added to the hashmap");
  98. map.put(playeruuid, 0);
  99.  
  100.  
  101.  
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. @Override
  110. public void onEnable() {
  111.  
  112. readfromdisk();
  113. this.getConfig().options().copyDefaults(true);
  114.  
  115. }
  116.  
  117. @Override
  118. public void onDisable() {
  119. writetodisk();
  120. }
  121.  
  122. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  123.  
  124. if (cmd.getName().equalsIgnoreCase("coins") && sender instanceof Player) {
  125.  
  126.  
  127.  
  128. if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
  129. Player player = (Player) sender;
  130.  
  131. player.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "COINS");
  132. player.sendMessage(ChatColor.GRAY + "By: " + ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Camer047");
  133. player.sendMessage(ChatColor.GOLD + "Commands:");
  134. player.sendMessage(ChatColor.YELLOW + "/coins balance " + ChatColor.GREEN + "Displays you cons balance");
  135.  
  136.  
  137.  
  138.  
  139. }
  140. if (args.length == 3 && args[0].equalsIgnoreCase("give")) {
  141. final Player player = (Player) sender;
  142. if (player.hasPermission(give)) {
  143.  
  144. boolean playerFound = false;
  145.  
  146. for (Player playertogive : Bukkit.getOnlinePlayers()) {
  147. if (playertogive.getName().equalsIgnoreCase(args[1])) {
  148. //Get the amount of tokens from the third argument
  149. int amount = Integer.parseInt(args[2]);
  150.  
  151.  
  152. giveCoins(playertogive.getUniqueId(), amount);
  153. player.sendMessage(ChatColor.GREEN + "Success! Sent " + amount + " coins to " + playertogive.getDisplayName() + "!");
  154. playerFound = true;
  155.  
  156. playertogive.sendMessage(ChatColor.GOLD + "+" + amount + " Coins!");
  157. break;
  158. }}
  159. if (playerFound == false) {
  160.  
  161. player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "That Player was not found!");
  162.  
  163. }
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170. }
  171. if (args.length >= 1 && args[0].equalsIgnoreCase("balance")) {
  172. Player player = (Player) sender;
  173. UUID playeruuid = player.getUniqueId();
  174. if (map.containsKey(playeruuid)) {
  175. int coinbalance = map.get(playeruuid);
  176. player.sendMessage("You Have: " + coinbalance + " coins");
  177. } else {
  178. map.put(playeruuid, 0);
  179. player.sendMessage("Error 0");
  180. }
  181.  
  182.  
  183.  
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. }
  197.  
  198. if (args.length >= 2 && args[0].equalsIgnoreCase("debug")) {
  199. Player player = (Player) sender;
  200.  
  201.  
  202. for (Player playertoget : Bukkit.getOnlinePlayers()){
  203. if (playertoget.getName() == args[1]) {
  204. player.sendMessage("Begining Debugging Process...");
  205. if (map.containsKey(playertoget.getUniqueId())) {
  206. player.sendMessage("No errors were found!");
  207. } else {
  208. player.sendMessage(ChatColor.RED + "Errors were detected with this players data!");
  209. player.sendMessage(ChatColor.GREEN + "Attempting to fix...");
  210. map.put(playertoget.getUniqueId(), 0);
  211. player.sendMessage("" + playertoget.getDisplayName() + "'s Data has been repaired!");
  212. }
  213.  
  214. }
  215.  
  216.  
  217. }
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224. return true;
  225.  
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement