package net.brennholz.commands;
import java.util.ArrayList;
import net.brennholz.challenges.ChatUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.brennholz.challenges.Challenges;
public class Coords_Command implements CommandExecutor {
private Challenges chl = Challenges.getplugin();
public static Inventory CoordsGUI = Bukkit.createInventory(null, 45, "§6Koordinaten");
public static Inventory CoordsDeleteGUI = Bukkit.createInventory(null, 9, "§6Entferne Koordinate");
public static String CGUIN = "§6Koordinaten";
public static String CDGUIN = "§6Entferne Koordinate";
@Override
public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
if (args.length == 0) {
if (p.hasPermission("challenges.coords.view")) {
createMainGUI(p);
} else
ChatUtil.sendErrorMessage(p, "Keine Berechtigung!");
} else if (args.length == 1) {
if (args[0].equalsIgnoreCase("share")) {
if (p.hasPermission("challenges.coords.share")) {
Bukkit.broadcastMessage("§c" + p.getName() + " §6ist bei §c" + p.getLocation().getBlockX() + " " + p.getLocation().getBlockY() + " " + p.getLocation().getBlockZ() + " §6in Welt §6" + p.getWorld().getName());
} else
ChatUtil.sendErrorMessage(p, "Keine Berechtigung!");
} else if (args[0].equalsIgnoreCase("get")) {
if (p.hasPermission("challenges.coords.get")) {
ChatUtil.sendNormalMessage(p, "Verfügbare Positionen:");
for (String key : chl.getConfig().getConfigurationSection("Saved_Locations").getKeys(false)) {
ChatUtil.sendNormalMessage(p, key);
}
} else
ChatUtil.sendErrorMessage(p, "Keine Berechtigung!");
} else
ChatUtil.sendWarningMessage(p, "Benutze: /coords share/save/get/delete [Name]");
} else if (args.length >= 2) {
if (args[0].equalsIgnoreCase("save")) {
if (p.hasPermission("challenges.coords.save")) {
if (!chl.getConfig().contains("Saved_Locations." + args[1])) {
Bukkit.broadcastMessage("§7| §aPosition §8>> §2" + p.getName() + " §7- " + args[1] + " §7[§2" + p.getLocation().getBlockX() + "§7/§2" + p.getLocation().getBlockY() + "§7/§2" + p.getLocation().getBlockZ() + "§7]");
chl.getConfig().set("Saved_Locations." + args[1] + ".World", p.getWorld().getName());
chl.getConfig().set("Saved_Locations." + args[1] + ".BlockX", p.getLocation().getBlockX());
chl.getConfig().set("Saved_Locations." + args[1] + ".BlockY", p.getLocation().getBlockY());
chl.getConfig().set("Saved_Locations." + args[1] + ".BlockZ", p.getLocation().getBlockZ());
chl.saveConfig();
} else
ChatUtil.sendWarningMessage(p, "Diese Position existiert bereits! Benutze einen anderen Namen oder lösche die alte zuerst mit /coords delete " + args[1]);
} else
ChatUtil.sendErrorMessage(p, "Keine Berechtigung!");
} else if (args[0].equalsIgnoreCase("get")) {
if (p.hasPermission("challenges.coords.get")) {
if (chl.getConfig().contains("Saved_Locations." + args[1])) {
String w = chl.getConfig().getString("Saved_Locations." + args[1] + ".World");
String x = chl.getConfig().getString("Saved_Locations." + args[1] + ".BlockX");
String y = chl.getConfig().getString("Saved_Locations." + args[1] + ".BlockY");
String z = chl.getConfig().getString("Saved_Locations." + args[1] + ".BlockZ");
ChatUtil.sendNormalMessage(p, "§aPosition §8>> §2" + p.getName() + " §7- " + args[1] + " §7[§2" + x + "§7/§2" + y + "§7/§2" + z + " " + w + "§7]");
} else
ChatUtil.sendWarningMessage(p, "§cDiese Position existiert nicht!");
} else
ChatUtil.sendErrorMessage(p, "Keine Berechtigung!");
} else if (args[0].equalsIgnoreCase("delete")) {
if (p.hasPermission("challenges.coords.delete")) {
if (chl.getConfig().contains("Saved_Locations." + args[1])) {
createDeleteGUI(p, args[1]);
p.openInventory(CoordsDeleteGUI);
} else
ChatUtil.sendWarningMessage(p, "Diese Position existiert nicht!");
} else
ChatUtil.sendErrorMessage(p, "Keine Berechtigung!");
} else
ChatUtil.sendWarningMessage(p, "Benutze: /coords share/save/get/delete [Name]");
}
} else
ChatUtil.sendErrorMessage(sender, "You are not a Player :>");
return true;
}
public void createMainGUI(Player p) {
int i = 0;
String w;
String x;
String y;
String z;
for (String key : chl.getConfig().getConfigurationSection("Saved_Locations").getKeys(false)) {
if (i >= 45) {
break;
}
w = chl.getConfig().getString("Saved_Locations." + key + ".World");
x = chl.getConfig().getString("Saved_Locations." + key + ".BlockX");
y = chl.getConfig().getString("Saved_Locations." + key + ".BlockY");
z = chl.getConfig().getString("Saved_Locations." + key + ".BlockZ");
CoordsGUI.setItem(i, addGUIItem(getBlock(w), key, "§bX: §c" + x, "§bY: §c" + y, "§bZ: §c" + z));
i++;
}
for (int ii = i; ii < 45; ii++) {
CoordsGUI.setItem(ii, addGUIItem(Material.AIR, ""));
}
p.openInventory(CoordsGUI);
}
public void createDeleteGUI(Player p, String dname) {
CoordsDeleteGUI.setItem(0, addGUIItem(Material.LIME_TERRACOTTA, "§aLöschen bestätigen", "§bWegpunkt: §c" + dname));
CoordsDeleteGUI.setItem(4, addGUIItem(Material.ORANGE_BANNER, dname));
for (int i = 1; i < 8; i++) {
if (CoordsDeleteGUI.getItem(i) == null) {
CoordsDeleteGUI.setItem(i, addGUIItem(Material.ORANGE_STAINED_GLASS_PANE, ""));
}
}
CoordsDeleteGUI.setItem(8, addGUIItem(Material.RED_TERRACOTTA, "§cLöschen abbrechen", "§bWegpunkt: §c" + dname));
p.openInventory(CoordsDeleteGUI);
}
private ItemStack addGUIItem(Material mat, String name, String... lore) {
ItemStack item = new ItemStack(mat, 1);
ItemMeta meta = item.getItemMeta();
if (mat != Material.AIR) {
meta.setDisplayName(name);
ArrayList<String> metalore = new ArrayList<String>();
if (lore != null) {
for (String lorecomments : lore) {
metalore.add(lorecomments);
}
}
meta.setLore(metalore);
item.setItemMeta(meta);
}
return item;
}
private Material getBlock(String wname) {
ItemStack mat = new ItemStack(Material.GRASS_BLOCK);
if (wname.equals(Bukkit.getWorlds().get(1).getName())) {
mat.setType(Material.NETHERRACK);
} else if (wname.equals(Bukkit.getWorlds().get(2).getName())) {
mat.setType(Material.END_STONE);
}
return mat.getType();
}
}