package ru.hoster.hKing;
import org.bukkit.plugin.java.*;
import net.milkbowl.vault.chat.*;
import com.earth2me.essentials.*;
import com.gmail.filoghost.holographicdisplays.api.*;
import org.bukkit.scheduler.*;
import com.google.common.collect.*;
import org.bukkit.entity.*;
import java.util.*;
import org.bukkit.plugin.*;
import org.bukkit.*;
import org.bukkit.command.*;
public class Main extends JavaPlugin
{
private Chat vaultChat;
private Essentials essentials;
private BukkitTask task;
private Location location;
private String king;
private String currentKing;
private int time;
private int doubleTime;
public void onEnable() {
this.saveDefaultConfig();
final RegisteredServiceProvider<Chat> chatProvider = (RegisteredServiceProvider<Chat>)this.getServer().getServicesManager().getRegistration((Class)Chat.class);
if (chatProvider == null) {
this.setEnabled(false);
throw new RuntimeException("Permission plugin not loaded!");
}
this.vaultChat = (Chat)chatProvider.getProvider();
this.essentials = (Essentials)this.getServer().getPluginManager().getPlugin("Essentials");
this.location = this.parseLocation(this.getConfig().getString("top_location"));
HologramsAPI.registerPlaceholder((Plugin)this, this.getConfig().getString("hd_placeholder_id"), 2.0, () -> (this.king == null) ? this.parseColor(this.getConfig().getString("hd_placeholder_empty")) : this.king);
this.getCommand("hking").setExecutor((sender, command, s, args) -> {
if (!(sender instanceof Player) || !sender.isOp()) {
sender.sendMessage(ChatColor.DARK_RED + "\u0430 \u043d\u0430\u0445\u0443\u0439 \u043f\u043e\u0439\u0442\u0438 \u043d\u0435 \u0445\u043e\u0447\u0435\u0448\u044c?");
return false;
}
this.getConfig().set("top_location", (Object)this.parseLocation(((Player)sender).getLocation()));
this.saveConfig();
this.location = this.parseLocation(this.getConfig().getString("top_location"));
sender.sendMessage(ChatColor.GREEN + "\u0443\u0441\u043f\u0435\u0448\u043d\u043e");
return true;
});
this.task = new BukkitRunnable() {
int ticks = 0;
public void run() {
if (++this.ticks > 4) {
this.ticks = 0;
}
final List<Player> topPlayers = Lists.newArrayList();
for (final Player player : Main.this.location.getWorld().getPlayers()) {
final Location playerLocation = player.getLocation();
if (playerLocation.getBlockX() == Main.this.location.getBlockX() && playerLocation.getBlockY() >= Main.this.location.getBlockY() && playerLocation.getBlockY() <= Main.this.location.getBlockY() + 1 && playerLocation.getBlockZ() == Main.this.location.getBlockZ()) {
if (player.getAllowFlight() || Main.this.essentials.getUser(player).isGodModeEnabled() || player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
if (this.ticks != 4) {
continue;
}
player.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("fly_or_creative_message")));
}
else {
topPlayers.add(player);
}
}
}
if (topPlayers.size() == 0) {
if (Main.this.currentKing != null) {
Main.this.currentKing = null;
Main.this.time = 0;
Main.this.doubleTime = 0;
}
}
else if (topPlayers.size() == 1) {
final Player player2 = topPlayers.get(0);
if (Bukkit.getOnlinePlayers().size() < Main.this.getConfig().getInt("min_online")) {
player2.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("small_online_message")));
return;
}
if (player2.getName().equalsIgnoreCase(Main.this.king)) {
player2.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("already_king_message")));
Main.this.getServer().getScheduler().runTask((Plugin)Main.this, () -> Main.this.pushPlayer(player2));
return;
}
if (player2.getName().equalsIgnoreCase(Main.this.currentKing)) {
if (++Main.this.time >= Main.this.getConfig().getInt("timeout") * 2) {
Main.this.king = player2.getName();
Main.this.currentKing = null;
Main.this.doubleTime = 0;
Main.this.getServer().getScheduler().runTask((Plugin)Main.this, () -> Main.this.performActions(player2));
return;
}
}
else {
Main.this.currentKing = player2.getName();
Main.this.time = 0;
Main.this.doubleTime = 0;
}
if (Main.this.time % 2 == 0) {
player2.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("remain_time_message").replace("%time", String.valueOf((Main.this.getConfig().getInt("timeout") * 2 - Main.this.time) / 2))));
}
}
else {
if (Main.this.currentKing != null) {
final Player curKing = Bukkit.getPlayerExact(Main.this.currentKing);
if (topPlayers.contains(curKing)) {
if (++Main.this.doubleTime < 20) {
if (Main.this.doubleTime % 2 == 0) {
curKing.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("king_more_players_remain_message").replace("%time", String.valueOf((20 - Main.this.doubleTime) / 2))));
}
for (final Player player3 : topPlayers) {
if (player3 == curKing) {
continue;
}
if (this.ticks != 4) {
continue;
}
player3.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("already_has_current_king").replace("%name", curKing.getName())));
}
return;
}
Main.this.currentKing = null;
Main.this.time = 0;
Main.this.doubleTime = 0;
curKing.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("king_more_players_message")));
}
else {
Main.this.currentKing = null;
Main.this.time = 0;
Main.this.doubleTime = 0;
}
}
final Iterator<Player> iterator3 = null;
Player player4 = null;
Main.this.getServer().getScheduler().runTask((Plugin)Main.this, () -> {
topPlayers.iterator();
while (iterator3.hasNext()) {
Main.this.pushPlayer(player4);
player4.sendMessage(Main.this.parseColor(Main.this.getConfig().getString("more_players_message")));
}
});
}
}
}.runTaskTimerAsynchronously((Plugin)this, 10L, 10L);
}
public void onDisable() {
HologramsAPI.unregisterPlaceholders((Plugin)this);
this.task.cancel();
this.task = null;
this.vaultChat = null;
this.essentials = null;
this.location = null;
}
private void performActions(final Player player) {
this.pushPlayer(player);
for (String action : this.getConfig().getStringList("actions")) {
action = this.parseColor(action.replace("%prefix", this.vaultChat.getGroupPrefix(player.getWorld(), this.vaultChat.getPlayerGroups(player)[0])).replace("%name", player.getName()));
if (action.startsWith("message:")) {
player.sendMessage(this.parseColor(action.substring(8)));
}
else if (action.startsWith("broadcast:")) {
Bukkit.broadcastMessage(this.parseColor(action.substring(10)));
}
else if (action.startsWith("title:")) {
final String[] titles = this.parseColor(action.substring(6)).split(";;");
player.sendTitle(titles[0], (titles.length > 1) ? titles[1] : "");
}
else {
if (!action.startsWith("command:")) {
throw new RuntimeException("Unknown type of action - '" + action + "'");
}
Bukkit.dispatchCommand((CommandSender)Bukkit.getConsoleSender(), this.parseColor(action.substring(8)));
}
}
}
private void pushPlayer(final Player player) {
final Location location = player.getLocation().clone();
location.setPitch(0.0f);
player.setVelocity(location.getDirection().multiply(1).setY(0.5));
}
private String parseLocation(final Location location) {
return location.getWorld().getName() + ";" + location.getBlockX() + ";" + location.getBlockY() + ";" + location.getBlockZ();
}
private Location parseLocation(final String arg0) {
final String[] arg = arg0.split(";");
return new Location(Bukkit.getWorld(arg[0]), (double)Double.valueOf(arg[1]), (double)Double.valueOf(arg[2]), (double)Double.valueOf(arg[3]));
}
private String parseColor(final String arg0) {
return ChatColor.translateAlternateColorCodes('&', arg0);
}
}