Last active
August 29, 2015 14:06
-
-
Save jwne/9edb0756293afd1a0b5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.griffinpvp.hardcore.commands; | |
import com.griffinpvp.basic.util.Yaml; | |
import com.griffinpvp.hardcore.Hardcore; | |
import com.griffinpvp.hardcore.events.PlayerWarpEvent; | |
import com.griffinpvp.hardcore.util.LocationUtil; | |
import lib.fanciful.FancyMessage; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.EntityDamageByEntityEvent; | |
import org.bukkit.event.entity.EntityDamageEvent; | |
import org.bukkit.event.player.PlayerMoveEvent; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import utilities.commands.Command; | |
import utilities.commands.CommandInfo; | |
import java.util.*; | |
public class CommandGo implements Listener { | |
@Command( | |
name = "go", | |
aliases = {"goto", "warp", "warpto"}, | |
description = "Warping command.", | |
player = true | |
) | |
public void go(CommandInfo info, String[] args) { | |
Player player = info.getPlayer(); | |
if(args.length == 0) { | |
player.sendMessage(ChatColor.RED + "Invalid /" + info.getLabel() + " usage."); | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " set [warpName] - Set a warp"); | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " delete [warpName] - Delete a warp"); | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " [warpName] - Warp"); | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " list - List your warps."); | |
return; | |
} | |
if(args[0].equalsIgnoreCase("set")) { | |
if(args.length != 2) { | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " set [warpName]"); | |
return; | |
} | |
if(player.getLocation().toVector().isInAABB(new Location(Bukkit.getWorld("world"), -511, 0, -511).toVector(), new Location(Bukkit.getWorld("world"), 512, 256, 512).toVector())) { | |
player.sendMessage(ChatColor.RED + "You cannot set warps within 512 blocks of spawn."); | |
return; | |
} | |
if(args[1].contains(".")) { | |
player.sendMessage(ChatColor.RED + "Invalid warp name!"); | |
return; | |
} | |
Yaml file = new Yaml("plugins/" + Hardcore.getInstance().getName() + "/data/players/" + player.getUniqueId() + ".yml"); | |
int max = 0; | |
if (player.hasPermission("basic.rank.normal")) { | |
max = 5; | |
} if (player.hasPermission("basic.rank.vip")) { | |
max = 10; | |
} if (player.hasPermission("basic.rank.mvp")) { | |
max = 15; | |
} | |
if(max == 0) { | |
max = 25; | |
} | |
if(file.getKeys(false).size() >= max) { | |
player.sendMessage(ChatColor.GRAY + "You have the maximum amount of warps you can own!"); | |
return; | |
} | |
file.set("warps." + args[1], LocationUtil.toString(player.getLocation())); | |
file.save(); | |
player.sendMessage(ChatColor.GRAY + "Warp '" + args[1] + "' has been set."); | |
return; | |
} | |
if(args[0].equalsIgnoreCase("delete") || args[0].equalsIgnoreCase("del")) { | |
if(args.length != 2) { | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " " + args[0] + " [warpName]"); | |
return; | |
} | |
Yaml file = new Yaml("plugins/" + Hardcore.getInstance().getName() + "/data/players/" + player.getUniqueId() + ".yml"); | |
if(file.getConfigurationSection("warps") == null) { | |
player.sendMessage(ChatColor.RED + "Warp '" + args[1] + "' does not exist."); | |
return; | |
} | |
boolean found = false; | |
String name = null; | |
for(String str : file.getConfigurationSection("warps").getKeys(false)) { | |
if(str.equalsIgnoreCase(args[1])) { | |
found = true; | |
name = str; | |
break; | |
} | |
} | |
if(!found) { | |
player.sendMessage(ChatColor.RED + "Warp '" + args[1] + "' does not exist."); | |
return; | |
} | |
file.set("warps." + name, null); | |
file.save(); | |
player.sendMessage(ChatColor.GRAY + "Warp '" + name + "' has been deleted."); | |
return; | |
} | |
if(args[0].equalsIgnoreCase("list")) { | |
Yaml file = new Yaml("plugins/" + Hardcore.getInstance().getName() + "/data/players/" + player.getUniqueId() + ".yml"); | |
List<String> warps = new ArrayList<String>(); | |
for (String str : file.getConfigurationSection("warps").getKeys(false)) { | |
warps.add(str); | |
} | |
Collections.sort(warps); | |
int max = 5; | |
if (player.hasPermission("basic.rank.vip")) { | |
max = 10; | |
} | |
if (player.hasPermission("basic.rank.mvp")) { | |
max = 15; | |
} | |
if(player.hasPermission("basic.rank.pro")) { | |
max = 25; | |
} | |
if (warps.size() == 0) { | |
player.sendMessage(ChatColor.GRAY + "***Warp List(0/ " + max + ")***"); | |
player.sendMessage(ChatColor.GRAY + "[]"); | |
return; | |
} | |
player.sendMessage(ChatColor.GRAY + "***Warp List(" + file.getConfigurationSection("warps").getKeys(false).size() + "/" + max + ")***"); | |
FancyMessage message = new FancyMessage("[").color(ChatColor.GRAY); | |
Iterator it = warps.iterator(); | |
int i = 0; | |
while (it.hasNext()) { | |
String string = String.valueOf(it.next()); | |
i++; | |
if (i < warps.size()) { | |
message.then(string).color(ChatColor.GRAY).command("/" + info.getLabel() + " " + string).tooltip(ChatColor.GREEN +"Click here to warp to " + ChatColor.RESET + "'" + string + "'" + ChatColor.GREEN + ".").then(", ").color(ChatColor.GRAY); | |
} else if (i == warps.size()) { | |
message.then(string).color(ChatColor.GRAY).command("/" + info.getLabel() + " " + string).tooltip(ChatColor.GREEN +"Click here to warp to " + ChatColor.RESET + "'" + string + "'" + ChatColor.GREEN + "."); | |
} | |
} | |
message.then("]").color(ChatColor.GRAY); | |
message.send(player); | |
} else { | |
if(args.length != 1) { | |
player.sendMessage(ChatColor.GRAY + "/" + info.getLabel() + " [warpName]"); | |
return; | |
} | |
Yaml file = new Yaml("plugins/" + Hardcore.getInstance().getName() + "/data/players/" + player.getUniqueId() + ".yml"); | |
boolean found = false; | |
String name = null; | |
for(String str : file.getConfigurationSection("warps").getKeys(false)) { | |
if(str.equalsIgnoreCase(args[0])) { | |
found = true; | |
name = str; | |
break; | |
} | |
} | |
if(!found) { | |
player.sendMessage(ChatColor.RED + "Warp '" + args[0] + "' does not exist."); | |
return; | |
} | |
if(waiting.contains(player.getUniqueId())) { | |
waiting.remove(player.getUniqueId()); | |
player.sendMessage(ChatColor.GRAY + "Warp cancelled."); | |
} | |
Location warp = LocationUtil.fromString(file.getString("warps." + name)); | |
Bukkit.getPluginManager().callEvent(new PlayerWarpEvent(player, warp, name)); | |
} | |
} | |
private Set<UUID> waiting = new HashSet<UUID>(); | |
private Set<UUID> cantAttack = new HashSet<UUID>(); | |
@EventHandler | |
public void onPlayerWarp(final PlayerWarpEvent event) { | |
final Player player = event.getPlayer(); | |
boolean found = false; | |
for(Entity nearby : player.getNearbyEntities(15, 10000, 15)) { | |
if(nearby instanceof Player) { | |
found = true; | |
break; | |
} | |
} | |
if(!found) { | |
player.teleport(event.getWarpLocation()); | |
player.sendMessage(ChatColor.GRAY + "You cannot attack for 10 seconds."); | |
cantAttack.add(player.getUniqueId()); | |
new BukkitRunnable() { | |
public void run() { | |
cantAttack.remove(player.getUniqueId()); | |
} | |
}.runTaskLater(Hardcore.getInstance(), 10 * 20); | |
} else { | |
player.sendMessage(ChatColor.GRAY + "There is someone nearby! Warping in 10 seconds. Don't move."); | |
waiting.add(player.getUniqueId()); | |
new BukkitRunnable() { | |
public void run() { | |
if(waiting.contains(player.getUniqueId())) { | |
player.teleport(event.getWarpLocation()); | |
player.sendMessage(ChatColor.GRAY + "You cannot attack for 10 seconds."); | |
cantAttack.add(player.getUniqueId()); | |
new BukkitRunnable() { | |
public void run() { | |
cantAttack.remove(player.getUniqueId()); | |
} | |
}.runTaskLater(Hardcore.getInstance(), 10 * 20); | |
} | |
} | |
}.runTaskLater(Hardcore.getInstance(), 10 * 20); | |
} | |
} | |
@EventHandler | |
public void onEntityDamage(EntityDamageEvent event) { | |
if(event.getEntity() instanceof Player) { | |
Player player = (Player) event.getEntity(); | |
if(waiting.contains(player.getUniqueId())) { | |
player.sendMessage(ChatColor.GRAY + "Warp cancelled!"); | |
waiting.remove(player.getUniqueId()); | |
} | |
} | |
} | |
@EventHandler | |
public void onPlayerMove(PlayerMoveEvent event) { | |
Player player = event.getPlayer(); | |
if(waiting.contains(player.getUniqueId())) { | |
if(event.getFrom().getX() != event.getTo().getX() || event.getFrom().getY() != event.getTo().getY() || event.getFrom().getZ() != event.getTo().getZ()) { | |
player.sendMessage(ChatColor.GRAY + "Warp cancelled!"); | |
waiting.remove(player.getUniqueId()); | |
} | |
} | |
} | |
@EventHandler | |
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { | |
if(event.getEntity() instanceof Player && event.getDamager() instanceof Player) { | |
Player player = (Player) event.getDamager(); | |
if(cantAttack.contains(player.getUniqueId())) { | |
event.setCancelled(true); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment