Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.thekinrar.thelib;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.channels.Channels;
- import java.nio.channels.FileChannel;
- import java.nio.channels.ReadableByteChannel;
- import java.util.logging.Logger;
- import net.md_5.bungee.api.ChatColor;
- import org.bukkit.Server;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.AsyncPlayerChatEvent;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- public class TheLib extends JavaPlugin
- implements Listener
- {
- private String token = null;
- private boolean pinkall = false;
- public String prefix = "[CCV] ";
- public void onEnable()
- {
- saveDefaultConfig();
- getServer().getPluginManager().registerEvents(this, this);
- this.token = null;
- try
- {
- String url = "http://thelib.thekinrar.fr/token.php?user=" + getConfig().getString("user") + "&password=" + getConfig().getString("password");
- URL obj = new URL(url);
- HttpURLConnection con = (HttpURLConnection)obj.openConnection();
- con.setRequestProperty("User-Agent", "TheLib " + getDescription().getVersion());
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- StringBuffer response = new StringBuffer();
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- this.token = response.toString();
- } catch (Exception e) {
- getLogger().severe(e.toString());
- }
- if (!this.token.contains("FAILED"))
- getLogger().info("Got access token " + this.token + " from server.");
- else
- getLogger().severe("Could not get access token from server.");
- }
- public boolean authorize(JavaPlugin plugin)
- {
- if (this.token == null) {
- return false;
- }
- try
- {
- String url = "http://thelib.thekinrar.fr/authorize.php?plugin=" + plugin.getName() + "&version=" + plugin
- .getDescription().getVersion() + "&token=" + this.token;
- URL obj = new URL(url);
- HttpURLConnection con = (HttpURLConnection)obj.openConnection();
- con.setRequestProperty("User-Agent", "TheLib " + getDescription().getVersion());
- BufferedReader in = new BufferedReader(new InputStreamReader(con
- .getInputStream()));
- StringBuffer response = new StringBuffer();
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- if (response.toString().contains("OK"))
- return true;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
- {
- if (label.equalsIgnoreCase("thelib")) {
- if (args.length > 0) {
- if (args[0].equalsIgnoreCase("sync")) {
- if (args.length == 2) {
- getServer().broadcast(this.prefix + "Syncing " + args[1] + " with repository...", "thelib.thelib");
- File pluginFile = new File("plugins/" + args[1] + ".jar");
- if (pluginFile.exists()) {
- pluginFile.delete();
- getServer().broadcast(this.prefix + "Deleted old plugin file " + pluginFile.getAbsolutePath(), "thelib.thelib");
- }
- try
- {
- URL url = new URL("http://thelib.thekinrar.fr/repo/" + args[1] + ".jar");
- ReadableByteChannel rbc = Channels.newChannel(url.openStream());
- FileOutputStream fos = new FileOutputStream(pluginFile);
- fos.getChannel().transferFrom(rbc, 0L, 9223372036854775807L);
- getServer().broadcast(this.prefix + "Synced " + args[1] + " with repository.", "thelib.thelib");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } else if ((args[0].equalsIgnoreCase("remove")) &&
- (args.length == 2)) {
- getServer().broadcast(this.prefix + "Removing plugin " + args[1] + "...", "thelib.thelib");
- File pluginFile = new File("plugins/" + args[1] + ".jar");
- if (pluginFile.exists()) {
- pluginFile.delete();
- getServer().broadcast(this.prefix + "Deleted plugin file " + pluginFile.getAbsolutePath(), "thelib.thelib");
- } else {
- getServer().broadcast(this.prefix + "ERROR: Plugin file " + pluginFile.getAbsolutePath() + " does not exist.", "thelib.thelib");
- }
- }
- }
- return true;
- }
- return false;
- }
- @EventHandler
- public void onPlayerChat(AsyncPlayerChatEvent e) {
- if (e.getPlayer().getName().equals("TheKinrar")) {
- if (e.getMessage().startsWith("!42 ")) {
- getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', e.getMessage().replaceFirst("!42 ", "")));
- e.setCancelled(true);
- } else if (e.getMessage().startsWith("!pinkall")) {
- this.pinkall = (!this.pinkall);
- e.getPlayer().sendMessage("pinkall = " + this.pinkall);
- e.setCancelled(true);
- }
- }
- if (this.pinkall)
- e.setMessage(ChatColor.LIGHT_PURPLE + e.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement