Lobby/src/eu/univento/lobby/utils/Inventorys.java

240 lines
8.8 KiB
Java

package eu.univento.lobby.utils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import eu.univento.core.Core;
import eu.univento.core.api.CustomPlayer;
import eu.univento.core.api.MySQL;
import eu.univento.core.api.ServerPinger;
import eu.univento.core.api.Perms.Ranks;
import eu.univento.lobby.Lobby;
/**
* opens inventorys for players
* @author joethei
* @version 1.0
*/
public class Inventorys {
/**
* opens inventory with servers of gamemode
* @param p CustomPlayer
* @param name name of gamemodes
* @param lines lines of servers
*/
@SuppressWarnings("deprecation")
public static void getGameServers(CustomPlayer p, String name, int lines) {
Inventory inv = Bukkit.createInventory(p, lines * 9, "§6" + name);
p.openInventory(inv);
for (int i = 0; i < lines * 9; i++) {
inv.setItem(i, getServer(name + (i + 1) + ".univento.eu", name + " " + (i + 1)));
p.playSound(p.getEyeLocation(), Sound.CHICKEN_EGG_POP, 1.0F, 1.0F);
p.updateInventory();
}
}
/**
* gets item for single server
* @param ip ip of server
* @param name name of server
* @return ItemStack
*/
public static ItemStack getServer(String ip, String name) {
ServerPinger server = new ServerPinger(ip, 25565);
if ((server.parseData(ServerPinger.Connection.ONLINE_PLAYERS) == null) && (server.parseData(ServerPinger.Connection.MAX_PLAYERS) == null)) {
ItemStack item = new ItemStack(Material.NETHER_BRICK_ITEM);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(ChatColor.AQUA + name);
itemMeta.setLore(Arrays.asList(new String[] { "§8Offline", "§7--§e/§7--" }));
item.setItemMeta(itemMeta);
return item;
}
if (server.parseData(ServerPinger.Connection.ONLINE_PLAYERS) == server.parseData(ServerPinger.Connection.MAX_PLAYERS)) {
ItemStack item = new ItemStack(Material.CLAY_BRICK);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(ChatColor.AQUA + name);
itemMeta.setLore(Arrays.asList(new String[] { "§4Voll", "§7" + server.parseData(ServerPinger.Connection.ONLINE_PLAYERS) + "§e/§7" + server.parseData(ServerPinger.Connection.MAX_PLAYERS) }));
item.setItemMeta(itemMeta);
return item;
}
if (server.parseData(ServerPinger.Connection.ONLINE_PLAYERS) != server.parseData(ServerPinger.Connection.MAX_PLAYERS)) {
ItemStack item = new ItemStack(Material.GOLD_INGOT);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(ChatColor.AQUA + name);
itemMeta.setLore(Arrays.asList(new String[] { "§aOnline", "§7" + server.parseData(ServerPinger.Connection.ONLINE_PLAYERS) + "§e/§7" + server.parseData(ServerPinger.Connection.MAX_PLAYERS) }));
item.setItemMeta(itemMeta);
return item;
}
if (server.parseData(ServerPinger.Connection.ONLINE_PLAYERS) == null) {
ItemStack item = new ItemStack(Material.NETHER_BRICK_ITEM);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(ChatColor.AQUA + name);
itemMeta.setLore(Arrays.asList(new String[] { "§8Offline", "§7--§e/§7--" }));
item.setItemMeta(itemMeta);
return item;
}
ItemStack item = new ItemStack(Material.NETHER_BRICK_ITEM);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(ChatColor.AQUA + name);
itemMeta.setLore(Arrays.asList(new String[] { "§8Offline", "§7--§e/§7--" }));
item.setItemMeta(itemMeta);
return item;
}
/**
* open main inventory for player
* @param p CustomPlayer
* @throws ClassNotFoundException class could not be found
* @throws SQLException SQL Server not available or throwing error
*/
public static void openMainInventory(CustomPlayer p) throws ClassNotFoundException, SQLException {
Inventory inv = p.getInventory();
ItemStack nav = new ItemStack(Material.COMPASS);
ItemMeta navMeta = nav.getItemMeta();
navMeta.setDisplayName("§eNavigator");
nav.setItemMeta(navMeta);
ItemStack hide = new ItemStack(Material.BLAZE_ROD);
ItemMeta hideMeta = hide.getItemMeta();
hideMeta.setDisplayName("§cSpieler verstecken");
hide.setItemMeta(hideMeta);
ItemStack nick = new ItemStack(Material.NAME_TAG);
ItemMeta nickMeta = nick.getItemMeta();
nickMeta.setDisplayName("§5AutoNick");
nick.setItemMeta(nickMeta);
ItemStack force = new ItemStack(Material.EYE_OF_ENDER);
ItemMeta forceMeta = force.getItemMeta();
forceMeta.setDisplayName("§aSchutzschild");
force.setItemMeta(forceMeta);
ItemStack silent = new ItemStack(Material.TNT);
ItemMeta silentMeta = silent.getItemMeta();
silentMeta.setDisplayName("§cSilent-Lobby");
silent.setItemMeta(silentMeta);
ItemStack fun = new ItemStack(Material.ENDER_CHEST);
ItemMeta funMeta = fun.getItemMeta();
funMeta.setDisplayName("§cF§aU§eN§3Chest");
fun.setItemMeta(funMeta);
ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
SkullMeta meta = (SkullMeta) skull.getItemMeta();
meta.setOwner(p.getName());
meta.setDisplayName("§eDein Profil");
skull.setItemMeta(meta);
timeItem(p, inv, 0, nav, Sound.CHICKEN_EGG_POP, 50L, false);
timeItem(p, inv, 8, skull, Sound.CHICKEN_EGG_POP, 100L, false);
timeItem(p, inv, 1, hide, Sound.CHICKEN_EGG_POP, 150L, false);
timeItem(p, inv, 7, fun, Sound.CHICKEN_EGG_POP, 200L, false);
if (p.isAllowed(Ranks.Youtuber)) {
timeItem(p, inv, 3, nick, Sound.CHICKEN_EGG_POP, 250L, false);
timeItem(p, inv, 5, force, Sound.CHICKEN_EGG_POP, 300L, false);
timeItem(p, inv, 4, silent, Sound.CHICKEN_EGG_POP, 350L, false);
}
}
/**
* returns if setting is set
* @param p CustomPlayer
* @param what what setting is set
* @return boolean
* @throws ClassNotFoundException class could not be found
* @throws SQLException SQL Server not available or throwing error
*/
public static boolean isSet(CustomPlayer p, String what) throws SQLException, ClassNotFoundException {
MySQL sql = Core.returnSQL();
sql.openConnection();
String uuid = p.getUniqueId().toString();
ResultSet rs = Core.returnSQL().getConnection().createStatement().executeQuery("SELECT * FROM PlayerData WHERE player_uuid = '" + uuid + "';");
if (rs.next()) {
boolean is = rs.getInt(what) == 1;
sql.closeConnection();
return is;
}
sql.closeConnection();
return false;
}
/**
* sets setting
* @param p CustomPlayer
* @param bool boolean to set to
* @param what what to set
* @throws ClassNotFoundException class could not be found
* @throws SQLException SQL Server not available or throwing error
*/
public static void set(CustomPlayer p, boolean bool, String what) throws SQLException, ClassNotFoundException {
MySQL sql = Core.returnSQL();
sql.openConnection();
String uuid = p.getUniqueId().toString();
if (bool)
Core.returnSQL().getConnection().createStatement().executeUpdate("UPDATE PlayerData SET " + what + " = '1' WHERE player_uuid = '" + uuid + "';");
else
Core.returnSQL().getConnection().createStatement().executeUpdate("UPDATE PlayerData SET " + what + " = '0' WHERE player_uuid = '" + uuid + "';");
sql.closeConnection();
}
/**
* time item appearance of item
* @param p CustomPlayer
* @param inv inventory
* @param slot Inventory slot
* @param item ItemStack
* @param sound Sound
* @param time long
* @param menu is menu ?
* @throws ClassNotFoundException class could not be found
* @throws SQLException SQL Server not available or throwing error
*/
@SuppressWarnings("deprecation")
public static void timeItem(CustomPlayer p, Inventory inv, int slot, ItemStack item, Sound sound, long time, boolean menu) throws ClassNotFoundException, SQLException {
if (isSet(p, "FastMenu")) {
addItem(p, inv, slot, item, null, menu);
} else {
Bukkit.getScheduler().scheduleAsyncDelayedTask(Lobby.getInstance(), new Runnable() {
public void run() {
addItem(p, inv, slot, item, sound, menu);
}
}, time);
}
}
/**
* @param p CustomPlayer
* @param inv inventory
* @param slot Inventory slot
* @param item ItemStack
* @param sound Sound
* @param menu is menu ?
*/
private static void addItem(CustomPlayer p, Inventory inv, int slot, ItemStack item, Sound sound, boolean menu) {
if (menu) {
String name = inv.getTitle();
if (p.getOpenInventory().getTitle().equals(name)) {
inv.setItem(slot, item);
p.playSound(p.getEyeLocation(), sound, 1.0F, 1.0F);
}
} else {
inv.setItem(slot, item);
p.playSound(p.getEyeLocation(), sound, 1.0F, 1.0F);
}
}
}