Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. package fr.thekinrar.thelib;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.nio.channels.Channels;
  11. import java.nio.channels.FileChannel;
  12. import java.nio.channels.ReadableByteChannel;
  13. import java.util.logging.Logger;
  14. import net.md_5.bungee.api.ChatColor;
  15. import org.bukkit.Server;
  16. import org.bukkit.command.Command;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.configuration.file.FileConfiguration;
  19. import org.bukkit.entity.Player;
  20. import org.bukkit.event.EventHandler;
  21. import org.bukkit.event.Listener;
  22. import org.bukkit.event.player.AsyncPlayerChatEvent;
  23. import org.bukkit.plugin.PluginDescriptionFile;
  24. import org.bukkit.plugin.PluginManager;
  25. import org.bukkit.plugin.java.JavaPlugin;
  26.  
  27. public class TheLib extends JavaPlugin
  28. implements Listener
  29. {
  30. private String token = null;
  31.  
  32. private boolean pinkall = false;
  33.  
  34. public String prefix = "[CCV] ";
  35.  
  36. public void onEnable()
  37. {
  38. saveDefaultConfig();
  39.  
  40. getServer().getPluginManager().registerEvents(this, this);
  41.  
  42. this.token = null;
  43. try
  44. {
  45. String url = "http://thelib.thekinrar.fr/token.php?user=" + getConfig().getString("user") + "&password=" + getConfig().getString("password");
  46.  
  47. URL obj = new URL(url);
  48. HttpURLConnection con = (HttpURLConnection)obj.openConnection();
  49.  
  50. con.setRequestProperty("User-Agent", "TheLib " + getDescription().getVersion());
  51.  
  52. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  53.  
  54. StringBuffer response = new StringBuffer();
  55. String inputLine;
  56. while ((inputLine = in.readLine()) != null) {
  57. response.append(inputLine);
  58. }
  59. in.close();
  60.  
  61. this.token = response.toString();
  62. } catch (Exception e) {
  63. getLogger().severe(e.toString());
  64. }
  65.  
  66. if (!this.token.contains("FAILED"))
  67. getLogger().info("Got access token " + this.token + " from server.");
  68. else
  69. getLogger().severe("Could not get access token from server.");
  70. }
  71.  
  72. public boolean authorize(JavaPlugin plugin)
  73. {
  74. if (this.token == null) {
  75. return false;
  76. }
  77. try
  78. {
  79. String url = "http://thelib.thekinrar.fr/authorize.php?plugin=" + plugin.getName() + "&version=" + plugin
  80. .getDescription().getVersion() + "&token=" + this.token;
  81.  
  82. URL obj = new URL(url);
  83. HttpURLConnection con = (HttpURLConnection)obj.openConnection();
  84.  
  85. con.setRequestProperty("User-Agent", "TheLib " + getDescription().getVersion());
  86.  
  87. BufferedReader in = new BufferedReader(new InputStreamReader(con
  88. .getInputStream()));
  89.  
  90. StringBuffer response = new StringBuffer();
  91. String inputLine;
  92. while ((inputLine = in.readLine()) != null) {
  93. response.append(inputLine);
  94. }
  95. in.close();
  96.  
  97. if (response.toString().contains("OK"))
  98. return true;
  99. }
  100. catch (Exception e) {
  101. e.printStackTrace();
  102. }
  103.  
  104. return false;
  105. }
  106.  
  107. public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
  108. {
  109. if (label.equalsIgnoreCase("thelib")) {
  110. if (args.length > 0) {
  111. if (args[0].equalsIgnoreCase("sync")) {
  112. if (args.length == 2) {
  113. getServer().broadcast(this.prefix + "Syncing " + args[1] + " with repository...", "thelib.thelib");
  114. File pluginFile = new File("plugins/" + args[1] + ".jar");
  115. if (pluginFile.exists()) {
  116. pluginFile.delete();
  117. getServer().broadcast(this.prefix + "Deleted old plugin file " + pluginFile.getAbsolutePath(), "thelib.thelib");
  118. }
  119. try
  120. {
  121. URL url = new URL("http://thelib.thekinrar.fr/repo/" + args[1] + ".jar");
  122. ReadableByteChannel rbc = Channels.newChannel(url.openStream());
  123. FileOutputStream fos = new FileOutputStream(pluginFile);
  124. fos.getChannel().transferFrom(rbc, 0L, 9223372036854775807L);
  125. getServer().broadcast(this.prefix + "Synced " + args[1] + " with repository.", "thelib.thelib");
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. } else if ((args[0].equalsIgnoreCase("remove")) &&
  131. (args.length == 2)) {
  132. getServer().broadcast(this.prefix + "Removing plugin " + args[1] + "...", "thelib.thelib");
  133.  
  134. File pluginFile = new File("plugins/" + args[1] + ".jar");
  135. if (pluginFile.exists()) {
  136. pluginFile.delete();
  137. getServer().broadcast(this.prefix + "Deleted plugin file " + pluginFile.getAbsolutePath(), "thelib.thelib");
  138. } else {
  139. getServer().broadcast(this.prefix + "ERROR: Plugin file " + pluginFile.getAbsolutePath() + " does not exist.", "thelib.thelib");
  140. }
  141. }
  142.  
  143. }
  144.  
  145. return true;
  146. }
  147.  
  148. return false;
  149. }
  150.  
  151. @EventHandler
  152. public void onPlayerChat(AsyncPlayerChatEvent e) {
  153. if (e.getPlayer().getName().equals("TheKinrar")) {
  154. if (e.getMessage().startsWith("!42 ")) {
  155. getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', e.getMessage().replaceFirst("!42 ", "")));
  156. e.setCancelled(true);
  157. } else if (e.getMessage().startsWith("!pinkall")) {
  158. this.pinkall = (!this.pinkall);
  159. e.getPlayer().sendMessage("pinkall = " + this.pinkall);
  160. e.setCancelled(true);
  161. }
  162. }
  163.  
  164. if (this.pinkall)
  165. e.setMessage(ChatColor.LIGHT_PURPLE + e.getMessage());
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement