package com.github.intangir.Tweaks;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;

public class Admin extends Tweak
{
	Admin(Tweaks plugin) {
		super(plugin);
		TWEAK_NAME = "Tweak_Admin";
		TWEAK_VERSION = "1.0";
		server.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
	}
	
	// teleports you to a world (tp command is a bit lacking)
	@CommandHandler("world")
	public void onCmdWorldTp(CommandSender sender, String[] args) {
		if(!sender.isOp()) return;
		World w = server.getWorld(args[0]);
		if(w != null) {
			server.getPlayer(sender.getName()).teleport(w.getSpawnLocation());
		}
	}

	// sets the worlds spawn
	@CommandHandler("setspawn")
	public void onCmdSetSpawn(CommandSender sender, String[] args) {
		if(!sender.isOp()) return;
		Player p = server.getPlayer(sender.getName());
		if(p != null) {
			World w = p.getWorld();
			Location l = p.getLocation();
			w.setSpawnLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ());
		}
	}

	// tells you were the player is
	@CommandHandler("where")
	public void onCmdWhere(CommandSender sender, String[] args) {
		if(!sender.isOp()) return;
		Player p = server.getPlayer(args[0]);
		if(p != null) {
			Location l = p.getLocation();
			sender.sendMessage(String.format("%s is at %s, %s %s %s", args[0], l.getWorld().getName(), l.getBlockX(), l.getBlockY(), l.getBlockZ()));
		}
	}

	// tells bungeecord proxy to move a player to another server
	@CommandHandler("changeserver")
	public void onCmdChangeServer(CommandSender sender, String[] args) {
		if(!sender.isOp()) return;
		Player p = server.getPlayer(args[0]);
		if(p != null) {
			ByteArrayDataOutput out = ByteStreams.newDataOutput();
			out.writeUTF("ConnectOther");
	        out.writeUTF(args[0]);
	        out.writeUTF(args[1]);
	        sender.sendMessage("sending player " + args[0] +  " to server " + args[1]);
	        p.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
		}
	}

	// randomly teleports a player somewhere
	@SuppressWarnings("deprecation")
	@CommandHandler("randomteleport")
	public void onCmdRandomSpawn(CommandSender sender, String[] args) {
		if(!sender.isOp()) return;
		Player p = server.getPlayer(args[0]);
		
		if(p != null)
		{
			Location l = Respawn.chooseSpawn_s("world");
			log.info("Randomly teleporting " + p.getName() + " to " + l);
			p.sendBlockChange(l, Material.BEACON, (byte) 0);
			p.teleport(l.add(0.5, 0.5, 0.5));
		}
	}

}

@@ -30,16 +30,38 @@
 weaks;%0A%0A
+import java.io.File;%0A%0A
 import o
@@ -375,16 +375,79 @@
 lugin);%0A
+%09%09CONFIG_FILE = new File(plugin.getDataFolder(), %22admin.yml%22);%0A
 %09%09TWEAK_
@@ -571,17 +571,446 @@
 ord%22);%0A%09
-%7D
+%09worldPermission = %22tweak.admin.world%22;%0A%09%09setSpawnPermission = %22tweak.admin.setspawn%22;%0A%09%09wherePermission = %22tweak.admin.where%22;%0A%09%09changeServerPermission = %22tweak.admin.changeserver%22;%0A%09%09randomTeleportPermission = %22tweak.admin.randomteleport%22;%0A%09%7D%0A%09%0A%09private String worldPermission;%0A%09private String setSpawnPermission;%0A%09private String wherePermission;%0A%09private String changeServerPermission;%0A%09private String randomTeleportPermission;
 %0A%09%0A%09// t
@@ -1165,32 +1165,74 @@
 f(!sender.isOp()
+ && !sender.hasPermission(worldPermission)
 ) return;%0A%09%09Worl
@@ -1491,32 +1491,77 @@
 f(!sender.isOp()
+ && !sender.hasPermission(setSpawnPermission)
 ) return;%0A%09%09Play
@@ -1890,32 +1890,74 @@
 f(!sender.isOp()
+ && !sender.hasPermission(wherePermission)
 ) return;%0A%09%09Play
@@ -2366,32 +2366,81 @@
 f(!sender.isOp()
+ && !sender.hasPermission(changeServerPermission)
 ) return;%0A%09%09Play
@@ -3009,16 +3009,67 @@
 r.isOp()
+ && !sender.hasPermission(randomTeleportPermission)
 ) return

add support for permissions for admin commands