diff --git a/PlugNHub/src/fr/zabricraft/plugnhub/PlugNHub.java b/PlugNHub/src/fr/zabricraft/plugnhub/PlugNHub.java new file mode 100644 index 0000000..97be978 --- /dev/null +++ b/PlugNHub/src/fr/zabricraft/plugnhub/PlugNHub.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018 FALLET Nathan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package fr.zabricraft.plugnhub; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import fr.zabricraft.plugnhub.events.InventoryClick; +import fr.zabricraft.plugnhub.events.PlayerInteract; +import fr.zabricraft.plugnhub.events.PlayerJoin; + +public class PlugNHub extends JavaPlugin { + + public void onEnable() { + Bukkit.getPluginManager().registerEvents(new InventoryClick(), this); + Bukkit.getPluginManager().registerEvents(new PlayerInteract(), this); + Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this); + } + +} diff --git a/PlugNHub/src/fr/zabricraft/plugnhub/events/InventoryClick.java b/PlugNHub/src/fr/zabricraft/plugnhub/events/InventoryClick.java new file mode 100644 index 0000000..d09cfb9 --- /dev/null +++ b/PlugNHub/src/fr/zabricraft/plugnhub/events/InventoryClick.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2018 FALLET Nathan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package fr.zabricraft.plugnhub.events; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.List; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; + +import fr.zabricraft.craftsearch.CraftSearch; +import fr.zabricraft.plugnapi.PlugNAPI; +import fr.zabricraft.plugnapi.utils.ServerStatus; +import fr.zabricraft.plugnapi.utils.ZabriPlayer; +import fr.zabricraft.plugnhub.utils.InventoryManager; + +public class InventoryClick implements Listener { + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + e.setCancelled(true); + if (e.getWhoClicked() instanceof Player) { + Player p = (Player) e.getWhoClicked(); + ZabriPlayer zp = PlugNAPI.getInstance().getPlayer(p.getUniqueId()); + if (zp != null) { + if (e.getInventory().getName().equals("§r§rNos jeux")) { + if (e.getSlot() == 31) { + p.closeInventory(); + } else if (e.getCurrentItem() != null && e.getCurrentItem().hasItemMeta() + && e.getCurrentItem().getItemMeta().hasLore()) { + List lore = e.getCurrentItem().getItemMeta().getLore(); + String id = lore.get(lore.size() - 1).replaceFirst("§9ID: ", ""); + InventoryManager.openGames(p, id); + } + } else if (e.getInventory().getName().startsWith("§r§rParties de")) { + if (e.getSlot() == 30) { + List lore = e.getCurrentItem().getItemMeta().getLore(); + String id = lore.get(lore.size() - 1).replaceFirst("§9ID: ", ""); + InventoryManager.openStart(p, id); + } else if (e.getSlot() == 32) { + p.closeInventory(); + } else if (e.getCurrentItem() != null && e.getCurrentItem().hasItemMeta() + && e.getCurrentItem().getItemMeta().hasLore()) { + List lore = e.getCurrentItem().getItemMeta().getLore(); + String id = lore.get(lore.size() - 1).replaceFirst("§9ID: ", ""); + try { + PreparedStatement state = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM servers WHERE id = ?"); + state.setString(1, id); + ResultSet result = state.executeQuery(); + if (result.next()) { + ServerStatus status = ServerStatus.valueOf(result.getString("status")); + if (status.equals(ServerStatus.OFFLINE)) { + p.sendMessage("§cCe serveur est hors ligne !"); + } else if (status.equals(ServerStatus.PLAYING)) { + p.sendMessage("§cCette partie est déjà en cours !"); + } else if (status.equals(ServerStatus.FINISHED)) { + p.sendMessage("§cCette partie est déjà terminée !"); + } else if (status.equals(ServerStatus.WAITING) + || status.equals(ServerStatus.COUNTDOWN)) { + CraftSearch.getInstance().connectSwitcher(p, "plugncraft" + (result.getInt("port")-25999)); + } else { + p.sendMessage("§cLe statut de la partie est inconnu !"); + } + } else { + p.sendMessage("§cImpossible de trouver ce serveur !"); + } + result.close(); + state.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } else if (e.getInventory().getName().startsWith("§r§rDémarrer un serveur ")) { + if (e.getSlot() == 31) { + p.closeInventory(); + } else if (e.getCurrentItem() != null && e.getCurrentItem().hasItemMeta() + && e.getCurrentItem().getItemMeta().hasLore()) { + List lore = e.getCurrentItem().getItemMeta().getLore(); + int id = Integer.parseInt(lore.get(lore.size() - 1).replaceFirst("§9ID: ", "")); + try { + PreparedStatement state = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM maps WHERE id = ?"); + state.setInt(1, id); + ResultSet result = state.executeQuery(); + if (result.next()) { + String game = result.getString("game"); + PreparedStatement state2 = PlugNAPI.getInstance().getConnection().prepareStatement( + "SELECT * FROM servers WHERE type = ? AND map = ? AND (status = ? OR status = ? OR status = ?)"); + state2.setString(1, game); + state2.setInt(2, id); + state2.setString(3, ServerStatus.WAITING.toString()); + state2.setString(4, ServerStatus.COUNTDOWN.toString()); + state2.setString(5, ServerStatus.STARTING.toString()); + ResultSet result2 = state2.executeQuery(); + if (!result2.next()) { + PreparedStatement state3 = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM start WHERE game = ? AND map = ?"); + state3.setString(1, game); + state3.setInt(2, id); + ResultSet result3 = state3.executeQuery(); + if (!result3.next()) { + PreparedStatement state4 = PlugNAPI.getInstance().getConnection() + .prepareStatement("INSERT INTO start (game, map) VALUES(?, ?)"); + state4.setString(1, game); + state4.setInt(2, id); + state4.executeUpdate(); + state4.close(); + p.sendMessage("§aDémarrage en cours d'un serveur avec la map §e" + + result.getString("name") + "§a..."); + } else { + p.sendMessage("§cUn serveur avec cette map est déjà en train de démarrer !"); + } + result3.close(); + state3.close(); + } else { + p.sendMessage("§cUn serveur avec cette map est déjà prêt à vous accueillir !"); + } + result2.close(); + state2.close(); + } + result.close(); + state.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + } + } + } + +} diff --git a/PlugNHub/src/fr/zabricraft/plugnhub/events/PlayerInteract.java b/PlugNHub/src/fr/zabricraft/plugnhub/events/PlayerInteract.java new file mode 100644 index 0000000..bab6bcf --- /dev/null +++ b/PlugNHub/src/fr/zabricraft/plugnhub/events/PlayerInteract.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 FALLET Nathan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package fr.zabricraft.plugnhub.events; + +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; + +import fr.zabricraft.plugnhub.utils.InventoryManager; + +public class PlayerInteract implements Listener { + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent e) { + if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) + || e.getAction().equals(Action.RIGHT_CLICK_AIR) && e.getPlayer().getItemInHand() != null) { + if (e.getPlayer().getItemInHand().getType().equals(Material.COMPASS)) { + InventoryManager.openMenu(e.getPlayer()); + } + } + e.setCancelled(true); + } + +} diff --git a/PlugNHub/src/fr/zabricraft/plugnhub/events/PlayerJoin.java b/PlugNHub/src/fr/zabricraft/plugnhub/events/PlayerJoin.java new file mode 100644 index 0000000..b29a6af --- /dev/null +++ b/PlugNHub/src/fr/zabricraft/plugnhub/events/PlayerJoin.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2018 FALLET Nathan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package fr.zabricraft.plugnhub.events; + +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.inventory.ItemStack; + +import fr.zabricraft.plugnapi.utils.Items; + +public class PlayerJoin implements Listener { + + @EventHandler(priority = EventPriority.MONITOR) + public void onPLayerJoin(PlayerJoinEvent e) { + e.getPlayer().getInventory().setItem(4, Items.setName(new ItemStack(Material.COMPASS), "Menu")); + e.getPlayer().updateInventory(); + } + +} diff --git a/PlugNHub/src/fr/zabricraft/plugnhub/utils/InventoryManager.java b/PlugNHub/src/fr/zabricraft/plugnhub/utils/InventoryManager.java new file mode 100644 index 0000000..f4d4563 --- /dev/null +++ b/PlugNHub/src/fr/zabricraft/plugnhub/utils/InventoryManager.java @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2018 FALLET Nathan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package fr.zabricraft.plugnhub.utils; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import fr.zabricraft.plugnapi.PlugNAPI; +import fr.zabricraft.plugnapi.utils.Items; +import fr.zabricraft.plugnapi.utils.ServerStatus; + +public class InventoryManager { + + public static void openMenu(Player player) { + Inventory i = Bukkit.createInventory(null, 36, "§r§rNos jeux"); + try { + Statement state = PlugNAPI.getInstance().getConnection().createStatement(); + ResultSet result = state.executeQuery("SELECT * FROM games"); + while (result.next()) { + int slot = result.getInt("slot"); + if (slot >= 0) { + ItemStack si = makeGameItem(result); + if (si != null) { + i.setItem(slot, si); + } else { + i.setItem(slot, Items.setName(new ItemStack(Material.WOOL), "§cError with this game !")); + } + } + } + result.close(); + state.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + i.setItem(31, Items.setName(new ItemStack(Material.WOOL), "§aFermer")); + player.openInventory(i); + } + + public static void openGames(Player player, String id) { + try { + PreparedStatement state = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM games WHERE id = ?"); + state.setString(1, id); + ResultSet result = state.executeQuery(); + if (result.next()) { + Inventory i = Bukkit.createInventory(null, 36, "§r§rParties de " + result.getString("name")); + PreparedStatement state2 = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM servers WHERE type = ?"); + state2.setString(1, result.getString("id")); + ResultSet result2 = state2.executeQuery(); + int j = 0; + while (result2.next()) { + ItemStack si = makeServItem(result, result2); + if (si != null) { + i.setItem(convertSlot(j), si); + } else { + i.setItem(convertSlot(j), + Items.setName(new ItemStack(Material.WOOL), "§cError with this server !")); + } + j++; + } + result2.close(); + state2.close(); + i.setItem(30, Items.setLore(Items.setName(new ItemStack(Material.WOOL), "§aCréer une partie"), "", + "§9ID: " + result.getString("id"))); + i.setItem(32, Items.setName(new ItemStack(Material.WOOL), "§aFermer")); + player.openInventory(i); + } + result.close(); + state.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void openStart(Player player, String id) { + try { + PreparedStatement state = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM games WHERE id = ?"); + state.setString(1, id); + ResultSet result = state.executeQuery(); + if (result.next()) { + Inventory i = Bukkit.createInventory(null, 36, "§r§rDémarrer un serveur " + result.getString("name")); + PreparedStatement state2 = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM maps WHERE game = ?"); + state2.setString(1, result.getString("id")); + ResultSet result2 = state2.executeQuery(); + int j = 0; + while (result2.next()) { + ItemStack si = makeStartItem(result, result2); + if (si != null) { + i.setItem(convertSlot(j), si); + } else { + i.setItem(convertSlot(j), + Items.setName(new ItemStack(Material.WOOL), "§cError with this map !")); + } + j++; + } + result2.close(); + state2.close(); + i.setItem(31, Items.setName(new ItemStack(Material.WOOL), "§aFermer")); + player.openInventory(i); + } + result.close(); + state.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static int convertSlot(int i) { + return (((i / 7) + 1) * 9) + (i % 7) + 1; + } + + public static ItemStack makeGameItem(ResultSet game) { + try { + ItemStack result = Items.setName(new ItemStack(Material.valueOf(game.getString("icon"))), "§6" + game.getString("name")); + ArrayList lore = new ArrayList(); + String current = ""; + for (char c : game.getString("description").toCharArray()) { + if ((current.length() > 40 && c == ' ') || c == '\n') { + lore.add("§a" + current); + current = ""; + } else { + current += c; + } + } + if (!current.isEmpty()) { + lore.add("§a" + current); + } + PreparedStatement state = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT SUM(players) as total FROM servers WHERE id = ?"); + state.setString(1, game.getString("id")); + ResultSet players = state.executeQuery(); + if (players.next()) { + lore.add(""); + lore.add("§b" + players.getInt("total") + " joueur" + (players.getInt("total") > 1 ? "s" : "") + + " en jeu"); + } + players.close(); + state.close(); + lore.add(""); + lore.add("§9ID: " + game.getString("id")); + result = Items.setLore(result, lore); + return result; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static ItemStack makeServItem(ResultSet game, ResultSet serv) { + try { + ServerStatus status = ServerStatus.valueOf(serv.getString("status")); + int color = status.equals(ServerStatus.OFFLINE) ? 15 : 5; + ItemStack result = Items.setName(Items.createItem(Material.WOOL, color), "§6" + game.getString("name")); + ArrayList lore = new ArrayList(); + if (!status.equals(ServerStatus.OFFLINE)) { + PreparedStatement state = PlugNAPI.getInstance().getConnection() + .prepareStatement("SELECT * FROM maps WHERE id = ?"); + state.setInt(1, serv.getInt("map")); + ResultSet map = state.executeQuery(); + if (map.next()) { + lore.add("§dMap : " + map.getString("name")); + lore.add(""); + String current = ""; + for (char c : map.getString("description").toCharArray()) { + if ((current.length() > 40 && c == ' ') || c == '\n') { + lore.add("§a" + current); + current = ""; + } else { + current += c; + } + } + if (!current.isEmpty()) { + lore.add("§a" + current); + } + } + map.close(); + state.close(); + } + lore.add(""); + lore.add("§7" + status.getText().replaceAll("%s", "quelques")); + lore.add(""); + lore.add("§b" + serv.getInt("players") + " joueur" + (serv.getInt("players") > 1 ? "s" : "") + " en jeu"); + lore.add(""); + lore.add("§9ID: " + serv.getString("id")); + result = Items.setLore(result, lore); + return result; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static ItemStack makeStartItem(ResultSet game, ResultSet map) { + try { + ItemStack result = Items.setName(new ItemStack(Material.valueOf(map.getString("icon"))), "§6" + map.getString("name")); + ArrayList lore = new ArrayList(); + String current = ""; + for (char c : map.getString("description").toCharArray()) { + if ((current.length() > 40 && c == ' ') || c == '\n') { + lore.add("§a" + current); + current = ""; + } else { + current += c; + } + } + if (!current.isEmpty()) { + lore.add("§a" + current); + } + lore.add(""); + lore.add("§9ID: " + map.getInt("id")); + result = Items.setLore(result, lore); + return result; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/PlugNHub/src/plugin.yml b/PlugNHub/src/plugin.yml new file mode 100644 index 0000000..7adcf3c --- /dev/null +++ b/PlugNHub/src/plugin.yml @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2018 FALLET Nathan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +name: PlugNHub +version: 1.0 +author: ZabriCraft +main: fr.zabricraft.plugnhub.PlugNHub \ No newline at end of file