Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.04 KB | None | 0 0
  1. package com.lululombard.kingshop;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.Enumeration;
  10. import java.util.Hashtable;
  11. import java.util.List;
  12. import java.util.Stack;
  13.  
  14. import net.milkbowl.vault.economy.Economy;
  15.  
  16. import org.bukkit.Bukkit;
  17. import org.bukkit.ChatColor;
  18. import org.bukkit.Location;
  19. import org.bukkit.Material;
  20. import org.bukkit.World;
  21. import org.bukkit.command.Command;
  22. import org.bukkit.command.CommandSender;
  23. import org.bukkit.entity.Entity;
  24. import org.bukkit.entity.ItemFrame;
  25. import org.bukkit.entity.Player;
  26. import org.bukkit.event.EventHandler;
  27. import org.bukkit.event.EventPriority;
  28. import org.bukkit.event.Listener;
  29. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  30. import org.bukkit.event.hanging.HangingBreakByEntityEvent;
  31. import org.bukkit.event.hanging.HangingBreakEvent;
  32. import org.bukkit.event.hanging.HangingPlaceEvent;
  33. import org.bukkit.event.inventory.InventoryClickEvent;
  34. import org.bukkit.event.player.PlayerInteractEntityEvent;
  35. import org.bukkit.inventory.Inventory;
  36. import org.bukkit.inventory.ItemStack;
  37. import org.bukkit.inventory.meta.ItemMeta;
  38. import org.bukkit.plugin.RegisteredServiceProvider;
  39. import org.bukkit.plugin.java.JavaPlugin;
  40.  
  41. public class KingShop extends JavaPlugin implements Listener {
  42.  
  43. private String url;
  44. private String user;
  45. private String passwd;
  46. private Connection sqlserver;
  47. String prefixchat = ChatColor.GOLD + "[KingShop] ";
  48. String shopname = ChatColor.DARK_RED + " ACHETER — KingShop — VENDRE";
  49. String prefixdiscount = ChatColor.AQUA + "" + ChatColor.ITALIC;
  50. String prefixprice = "" + ChatColor.GOLD;
  51. Material itemquit = Material.ANVIL;
  52. Material itemdisabled = Material.STICK;
  53. Stack<Location> ShopList = new Stack<Location>();
  54. Hashtable<UniqueItem, Prices> ItemPrices = new Hashtable<UniqueItem, Prices>();
  55. Hashtable<Player, Integer> Cooldown = new Hashtable<Player, Integer>();
  56. private String commandprefix = "ks";
  57. private boolean debug = false;
  58.  
  59. public static Economy econ = null;
  60.  
  61. public void onEnable() {
  62. if (!setupEconomy()) {
  63. getLogger().info("Vault not found. Disabling.");
  64. getServer().getPluginManager().disablePlugin(this);
  65. return;
  66. }
  67. this.getServer().getPluginManager().registerEvents(this, this);
  68. this.saveDefaultConfig();
  69. url = "jdbc:mysql://" + this.getConfig().getString("mysql-host") + ":" + this.getConfig().getString("mysql-port")+ "/" + this.getConfig().getString("mysql-db");
  70. user = this.getConfig().getString("mysql-user");
  71. passwd = this.getConfig().getString("mysql-password");
  72. if (!connect()) {
  73. getLogger().info("MySQL failed. Disabling.");
  74. getServer().getPluginManager().disablePlugin(this);
  75. return;
  76. }
  77. new Thread(new Runnable() {
  78. public void run() {
  79. try {
  80. Thread.sleep(5000);
  81. ActuPrice();
  82. ActuShop();
  83. } catch (InterruptedException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }).start();
  88. }
  89.  
  90. private boolean setupEconomy() {
  91. if (getServer().getPluginManager().getPlugin("Vault") == null) {
  92. return false;
  93. }
  94. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
  95. if (rsp == null) {
  96. return false;
  97. }
  98. econ = rsp.getProvider();
  99. return econ != null;
  100. }
  101.  
  102. public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
  103. if (cmd.getName().equalsIgnoreCase(commandprefix)) {
  104. Player player = null;
  105. if (sender instanceof Player) {
  106. player = (Player) sender;
  107. }
  108. String option = "null";
  109. if (args.length >= 1) option = args[0];
  110. if (option.equalsIgnoreCase("ramlist")) {
  111. if (!(sender instanceof Player) ||player.hasPermission("ks.ramlist")||player.hasPermission("ks.admin")||player.hasPermission("ks.*")||player.hasPermission("*")) {
  112. if (sender instanceof Player) player.sendMessage(ChatColor.GOLD + "Liste des shops : ");
  113. else getLogger().info("Liste des shops : ");
  114. for (Location frame : ShopList) {
  115. if (sender instanceof Player) player.sendMessage(ChatColor.GREEN + "World: " +frame.getWorld().getName() + " X: " + frame.getBlockX() + " Y: " + frame.getBlockY() + " Z: " + frame.getBlockZ() + " Yaw: " +frame.getYaw());
  116. else getLogger().info("World: " + frame.getWorld().getName() + " X: " +frame.getBlockX() + " Y: " + frame.getBlockY() + " Z: " + frame.getBlockZ() + " Yaw: " + frame.getYaw());
  117. }
  118. }
  119. else player.sendMessage(prefixchat + ChatColor.RED + "Vous n'avez pas la permission de faire cela.");
  120. }
  121. else if (option.equalsIgnoreCase("bddlist")) {
  122. if (!(sender instanceof Player) ||player.hasPermission("ks.bddlist")||player.hasPermission("ks.admin")||player.hasPermission("ks.*")||player.hasPermission("*")) {
  123. if (sender instanceof Player) player.sendMessage(ChatColor.GOLD + "Liste des shops : ");
  124. else getLogger().info("Liste des shops : ");
  125. final Player finalplayer = player;
  126. new Thread(new Runnable() {
  127. public void run() {
  128. try {
  129. Statement querry = sqlserver.createStatement();
  130. ResultSet result;
  131. result = querry.executeQuery("SELECT * FROM `kingshop_list`");
  132. ShopList.clear();
  133. while (result.next()) {
  134. if (sender instanceof Player)finalplayer.sendMessage(ChatColor.GREEN + "World: " + result.getString("world") + " X: " + result.getInt("x") + " Y: " +result.getInt("y") + " Z: " + result.getInt("z") + " Yaw: " + result.getInt("facing"));
  135. else getLogger().info("World: " +result.getString("world") + " X: " + result.getInt("x") + " Y: " + result.getInt("y") + " Z: " + result.getInt("z") + " Yaw: " +result.getInt("facing"));
  136. }
  137. querry.close();
  138. }
  139. catch (SQLException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. }).start();
  144. }
  145. else player.sendMessage(prefixchat + ChatColor.RED + "Vous n'avez pas la permission de faire cela.");
  146. }
  147. else if (option.equalsIgnoreCase("syncshop")) {
  148. if (!(sender instanceof Player) ||player.hasPermission("ks.syncshop")||player.hasPermission("ks.admin")||player.hasPermission("ks.*")||player.hasPermission("*")) {
  149. final Player finalplayer = player;
  150. new Thread(new Runnable() {
  151. public void run() {
  152. if (sender instanceof Player) finalplayer.sendMessage(prefixchat +ChatColor.YELLOW + "Lancement de la synchro...");
  153. else getLogger().info("Lancement de la synchro...");
  154. ActuShop();
  155. if (sender instanceof Player) finalplayer.sendMessage(prefixchat +ChatColor.GREEN + "La liste des shops a été rechargée.");
  156. else getLogger().info("La liste des shops a été rechargée.");
  157. }
  158. }).start();
  159.  
  160. }
  161. else player.sendMessage(prefixchat + ChatColor.RED + "Vous n'avez pas la permission de faire cela.");
  162. }
  163. else if (option.equalsIgnoreCase("syncprice")) {
  164. if (!(sender instanceof Player) ||player.hasPermission("ks.syncprice")||player.hasPermission("ks.admin")||player.hasPermission("ks.*")||player.hasPermission("*")) {
  165. final Player finalplayer = player;
  166. new Thread(new Runnable() {
  167. public void run() {
  168. if (sender instanceof Player) finalplayer.sendMessage(prefixchat +ChatColor.YELLOW + "Lancement de la synchro...");
  169. else getLogger().info("Lancement de la synchro...");
  170. ActuPrice();
  171. if (sender instanceof Player) finalplayer.sendMessage(prefixchat +ChatColor.GREEN + "La liste des prix a été rechargée.");
  172. else getLogger().info("La liste des prix a été rechargée.");
  173. }
  174. }).start();
  175.  
  176. }
  177. else player.sendMessage(prefixchat + ChatColor.RED + "Vous n'avez pas la permission de faire cela.");
  178. }
  179. else {
  180. if (sender instanceof Player) {
  181. player.sendMessage(ChatColor.YELLOW + "-------" + ChatColor.WHITE + " KingShop " +ChatColor.YELLOW + "-------");
  182. player.sendMessage(ChatColor.GOLD + "/" + commandprefix + " ramlist" + ChatColor.YELLOW +" : " + ChatColor.WHITE + "Liste les shops chargés en RAM");
  183. player.sendMessage(ChatColor.GOLD + "/" + commandprefix + " bddlist" + ChatColor.YELLOW +" : " + ChatColor.WHITE + "Liste les shops depuis la base de données");
  184. player.sendMessage(ChatColor.GOLD + "/" + commandprefix + " syncshop" + ChatColor.YELLOW +" : " + ChatColor.WHITE + "Recharge les shops depuis la base de données");
  185. player.sendMessage(ChatColor.GOLD + "/" + commandprefix + " syncprice" + ChatColor.YELLOW+ " : " + ChatColor.WHITE + "Recharge les prix depuis la base de données");
  186. }
  187. else {
  188. getLogger().info(ChatColor.YELLOW + "------- KingShop -------");
  189. getLogger().info("/" + commandprefix + " ramlist : Liste les shops chargés en RAM");
  190. getLogger().info("/" + commandprefix + " bddlist : Liste les shops depuis la base de données");
  191. getLogger().info("/" + commandprefix + " syncshop : Recharge les shops depuis la base de données");
  192. getLogger().info("/" + commandprefix + " syncprice : Recharge les prix depuis la base de données");
  193. }
  194. }
  195. return true;
  196. }
  197. else return false;
  198. }
  199.  
  200. @EventHandler (priority = EventPriority.MONITOR)
  201. public void onItemFramePlace(final HangingPlaceEvent e) {
  202. if (!e.isCancelled() && e.getEntity().getType().name().equals("ITEM_FRAME")) {
  203. final Player player = e.getPlayer();
  204. final int x = e.getEntity().getLocation().getBlockX();
  205. final int y = e.getEntity().getLocation().getBlockY();
  206. final int z = e.getEntity().getLocation().getBlockZ();
  207. final int face = e.getEntity().getFacing().ordinal();
  208.  
  209. List<Entity> worldentities = e.getPlayer().getWorld().getEntities();
  210. int xlist;
  211. int ylist;
  212. int zlist;
  213. int facelist;
  214. Boolean framepresent = false;
  215. for (Entity entity: worldentities) {
  216. if (entity.getType().name().equals("ITEM_FRAME")) {
  217. xlist = entity.getLocation().getBlockX();
  218. ylist = entity.getLocation().getBlockY();
  219. zlist = entity.getLocation().getBlockZ();
  220. facelist = (int) entity.getLocation().getYaw();
  221. if (xlist==x&&ylist==y&&zlist==z&&facelist==face) framepresent = true;
  222. }
  223. }
  224. if (framepresent) {
  225. player.sendMessage(ChatColor.RED + "Il y a déjà une entité sur ce bloc.");
  226. e.setCancelled(true);
  227. }
  228. }
  229. }
  230.  
  231. @EventHandler (priority = EventPriority.MONITOR)
  232. public void onItemFrameUse(final PlayerInteractEntityEvent e) {
  233. if (e.getRightClicked().getType().name().equals("ITEM_FRAME")) {
  234. final Player player = e.getPlayer();
  235. if (debug) getLogger().info(player.getName() + " used itemframe");
  236. World world = e.getRightClicked().getLocation().getWorld();
  237. int x = e.getRightClicked().getLocation().getBlockX();
  238. int y = e.getRightClicked().getLocation().getBlockY();
  239. int z = e.getRightClicked().getLocation().getBlockZ();
  240. int face = (int) e.getRightClicked().getLocation().getYaw();
  241. ItemFrame iframe = (ItemFrame) e.getRightClicked();
  242. if (player.isSneaking() && (player.hasPermission("ks.*") || player.hasPermission("ks.admin") ||player.hasPermission("ks.create"))) {
  243. if (!IsShop(world, x, y, z, face) && player.isSneaking()) {
  244. e.setCancelled(true);
  245. CreateShop(world, x, y, z, face, player);
  246. if (player.getItemInHand().getType()!=Material.AIR && iframe.isEmpty())iframe.setItem(player.getItemInHand());
  247. }
  248. }
  249. else if (IsShop(world, x, y, z, face)) {
  250. e.setCancelled(true);
  251. UseShop(world, x, y, z, face, player, iframe);
  252. }
  253. }
  254. }
  255.  
  256. @EventHandler (priority = EventPriority.MONITOR)
  257. public void onItemFrameRemove(final EntityDamageByEntityEvent e) {
  258. final String entityname = e.getEntity().getType().name();
  259. if (!e.isCancelled() && entityname.equals("ITEM_FRAME")) {
  260. World world = e.getEntity().getWorld();
  261. int x = e.getEntity().getLocation().getBlockX();
  262. int y = e.getEntity().getLocation().getBlockY();
  263. int z = e.getEntity().getLocation().getBlockZ();
  264. int face = (int) e.getEntity().getLocation().getYaw();
  265. ItemFrame iframe = (ItemFrame) e.getEntity();
  266. if (IsShop(world, x, y, z, face)) {
  267. if (e.getCause().toString().equals("ENTITY_ATTACK")) {
  268. Player player = ((Player) e.getDamager());
  269. if (player.isSneaking() && (player.hasPermission("ks.*") ||player.hasPermission("ks.admin") || player.hasPermission("ks.delete"))) {
  270. DelShop(world, x, y, z, face, player);
  271. }
  272. else {
  273. e.setCancelled(true);
  274. UseShop(world, x, y, z, face, player, iframe);
  275. }
  276. }
  277. else e.setCancelled(true);
  278. }
  279. }
  280. }
  281.  
  282. @EventHandler (priority = EventPriority.MONITOR)
  283. public void onItemFrameBreak(final HangingBreakEvent e) {
  284. final String entityname = e.getEntity().getType().name();
  285. if (!e.isCancelled() && entityname.equals("ITEM_FRAME")) {
  286. if (e instanceof HangingBreakByEntityEvent) {
  287. final HangingBreakByEntityEvent evt = (HangingBreakByEntityEvent) e;
  288. if (evt.getRemover() instanceof Player) {
  289. final Player player = ((Player) evt.getRemover());
  290. World world = e.getEntity().getWorld();
  291. int x = e.getEntity().getLocation().getBlockX();
  292. int y = e.getEntity().getLocation().getBlockY();
  293. int z = e.getEntity().getLocation().getBlockZ();
  294. int face = (int) e.getEntity().getLocation().getYaw();
  295. ItemFrame iframe = (ItemFrame) e.getEntity();
  296. if (player.isSneaking() && (player.hasPermission("ks.*") ||player.hasPermission("ks.admin") || player.hasPermission("ks.delete"))) DelShop(world, x, y, z, face, player);
  297. else if (IsShop(world, x, y, z, face)) {
  298. e.setCancelled(true);
  299. UseShop(world, x, y, z, face, player, iframe);
  300. }
  301. }
  302. }
  303. }
  304. }
  305.  
  306. @EventHandler
  307. public void onInventoryClick(InventoryClickEvent e) {
  308. if (e.getCurrentItem() == null) return;
  309. String currency = econ.currencyNamePlural();
  310. Player player = (Player) e.getWhoClicked();
  311. Inventory inventory = e.getInventory();
  312. if (inventory.getName().equals(shopname)) {
  313. ItemStack clicked = e.getCurrentItem();
  314. if (clicked.getType() == itemquit) {
  315. e.setCancelled(true);
  316. player.closeInventory();
  317. }
  318. else if (clicked.getType() != Material.AIR) {
  319. e.setCancelled(true);
  320. player.closeInventory();
  321. ItemMeta clM = clicked.getItemMeta();
  322. Boolean isASell = false;
  323. if (clM.getDisplayName().substring(6, 7).equals("V")) isASell = true;
  324. List<String> lore = clM.getLore();
  325. String nom = "";
  326. for (String thislore : lore) if (thislore.contains("Nom : ")) nom = thislore.replace("Nom : ", "");
  327. clM.setLore(null);
  328. clM.setDisplayName(nom);
  329. clicked.setItemMeta(clM);
  330. int amount = clicked.getAmount();
  331.  
  332. if (debug) getLogger().info(clicked.getData().toString() + " | " +clicked.getItemMeta().toString() + " * " + clicked.getAmount());
  333.  
  334. String data = clicked.getData().toString();
  335. String meta = clicked.getItemMeta().toString();
  336. UniqueItem ItemofShop = new UniqueItem(data, meta);
  337.  
  338. double unitSellPrice = 0;
  339. double stackDiscountSell = 0;
  340.  
  341. double unitBuyPrice = Double.MAX_VALUE/64;
  342. double stackDiscountBuy = 0;
  343.  
  344. boolean isConfigured = false;
  345. Enumeration<UniqueItem> allPrices = ItemPrices.keys();
  346. while(allPrices.hasMoreElements()) {
  347. UniqueItem key = allPrices.nextElement();
  348. if (key.GetData().equals(ItemofShop.GetData()) &&key.GetItemMeta().equals(ItemofShop.GetItemMeta())) {
  349. isConfigured = true;
  350. unitSellPrice = ItemPrices.get(key).GetUnitSellPrice();
  351. stackDiscountSell = ItemPrices.get(key).GetDiscountSell();
  352. unitBuyPrice = ItemPrices.get(key).GetUnitBuyPrice();
  353. stackDiscountBuy = ItemPrices.get(key).GetDiscountBuy();
  354. break;
  355. }
  356. }
  357. if (!isConfigured) return;
  358. clM.setDisplayName("");
  359. clicked.setItemMeta(clM);
  360.  
  361. if (isASell) {
  362. data = clicked.getData().toString();
  363. meta = clicked.getItemMeta().toString();
  364. double price = unitSellPrice;
  365.  
  366. if (amount == 32 && stackDiscountSell < 50 && clicked.getMaxStackSize()*64>36) price +=price * stackDiscountSell / 100;
  367. if (amount == 64 && stackDiscountSell < 50) price += price * stackDiscountSell / 100;
  368.  
  369. if (!player.getInventory().contains(clicked.getType())) player.sendMessage(prefixchat +ChatColor.RED + "Il semble que vous n'ayez pas l'item demandé.");
  370. else {
  371. int quantityininv = 0;
  372. for (ItemStack invItem : player.getInventory()) if (invItem != null) {
  373. getLogger().info("clicked = " + clicked.getData().toString() + " | " +clicked.getItemMeta().toString() + " * " + clicked.getAmount());
  374. if (invItem.getData().toString().equals(data) &&invItem.getItemMeta().toString().equals(meta)) {
  375. getLogger().info("found = " + invItem.getData().toString() + " | "+ invItem.getItemMeta().toString() + " * " + invItem.getAmount());
  376. quantityininv += invItem.getAmount();
  377. }
  378. }
  379. if (quantityininv >= amount) {
  380. ItemStack soldItem = clicked;
  381. soldItem.setAmount(amount);
  382. player.getInventory().removeItem(soldItem);
  383. econ.depositPlayer(player, price*amount);
  384. double displayAmount = (double)Math.round(price*amount * 100) / 100;
  385.  
  386. player.sendMessage(prefixchat + ChatColor.GREEN + "Merci de votre vente ! " + displayAmount + " " + currency + " ont été ajoutés à votre compte. Au plaisir de vous revoir chez KingShop !");
  387. getLogger().info(player.getName() + " sold " + amount + " " +soldItem.toString() + " for " + (price*amount));
  388. }
  389. else player.sendMessage(prefixchat + ChatColor.RED + "Vous n'avez pas l'item demandé en quantité suffisante ou les enchantements/durabilités ne sont pas identiques.");
  390. }
  391. }
  392. else {
  393. double price = unitBuyPrice;
  394. if (amount == 64 && stackDiscountBuy < 50) price -= price * stackDiscountBuy / 100;
  395.  
  396. if (player.getInventory().firstEmpty()==-1) player.sendMessage(prefixchat + ChatColor.RED+ "Votre inventaire est plein, veuillez le vider et revenir pour faire la transaction ;)");
  397. else {
  398. if (econ.has(player, price*amount)) {
  399. econ.withdrawPlayer(player, price*amount);
  400. double displayAmount = (double)Math.round(price*amount * 100) / 100;
  401. player.getInventory().addItem(clicked);
  402. player.sendMessage(prefixchat + ChatColor.GREEN + "Merci de votre achat ! " + displayAmount + " " + currency + " ont été débités de votre compte. Au plaisir de vous revoir chez KingShop !");
  403. getLogger().info(player.getName() + " buyed " + amount + " " +clicked.toString() + " for " + (price*amount));
  404. }
  405. else player.sendMessage(prefixchat + ChatColor.RED + "Vous n'avez pas l'argent necessaire pour vous payer cela :(");
  406. }
  407. }
  408. }
  409. }
  410. }
  411.  
  412. public void UseShop(World world, int x, int y, int z, int face, Player player, ItemFrame iframe) {
  413. int ActualTime = (int) (System.currentTimeMillis() / 1000);
  414. if (debug) getLogger().info("Cd " + player.getName() + " Cooldown : " + Cooldown.get(player) + " Actu: " +ActualTime);
  415. if (Cooldown.containsKey(player) && Cooldown.get(player)>=(ActualTime)) {
  416. return;
  417. }
  418. else {
  419. Cooldown.remove(player);
  420. Cooldown.put(player, ActualTime);
  421. }
  422. String currency = econ.currencyNamePlural();
  423. if (debug) getLogger().info(player.getName() + " open shop at X" + x + " Y" + y + " Z" + z + " facing " + face + " at " + world.getName());
  424. if (iframe.getItem().getType()==Material.AIR) {
  425. player.sendMessage(prefixchat + ChatColor.RED + "Ce shop est vide, veuillez contacter un opérateur ou un administrateur.");
  426. }
  427. else if (iframe.getItem().getType()==itemquit) {
  428. player.sendMessage(prefixchat + ChatColor.RED + "Les " + itemquit.toString().toLowerCase() + " ne sont pas disponibles à la vente.");
  429. }
  430. else {
  431. final String data = iframe.getItem().getData().toString();
  432. final String meta = iframe.getItem().getItemMeta().toString();
  433. UniqueItem ItemofShop = new UniqueItem(data, meta);
  434.  
  435. double unitSellPrice = 0;
  436. double stackDiscountSell = 0;
  437.  
  438. double unitBuyPrice = Double.MAX_VALUE/64;
  439. double stackDiscountBuy = 0;
  440.  
  441. boolean isConfigured = false;
  442. Enumeration<UniqueItem> allPrices = ItemPrices.keys();
  443. while(allPrices.hasMoreElements()) {
  444. UniqueItem key = allPrices.nextElement();
  445. if (key.GetData().equals(ItemofShop.GetData()) &&key.GetItemMeta().equals(ItemofShop.GetItemMeta())) {
  446. isConfigured = true;
  447. unitSellPrice = ItemPrices.get(key).GetUnitSellPrice();
  448. stackDiscountSell = ItemPrices.get(key).GetDiscountSell();
  449. unitBuyPrice = ItemPrices.get(key).GetUnitBuyPrice();
  450. stackDiscountBuy = ItemPrices.get(key).GetDiscountBuy();
  451. break;
  452. }
  453. }
  454.  
  455. if (isConfigured) {
  456. boolean isMaxSell = false;
  457. Inventory inventory = Bukkit.getServer().createInventory(null, 9, shopname);
  458. List<String> Lore = new ArrayList<String>();
  459. ItemStack quit = new ItemStack(itemquit, 1);
  460. ItemMeta isM = quit.getItemMeta();
  461. isM.setDisplayName("" + ChatColor.RESET + ChatColor.DARK_RED + ChatColor.ITALIC + "Quitter le shop");
  462. quit.setItemMeta(isM);
  463.  
  464. ItemStack disabled = new ItemStack(itemdisabled, 1);
  465. isM = disabled.getItemMeta();
  466. isM.setDisplayName("" + ChatColor.RESET + ChatColor.DARK_RED + ChatColor.BOLD + "Non disponible");
  467. disabled.setItemMeta(isM);
  468.  
  469. ItemStack buy1 = iframe.getItem();
  470. buy1.setAmount(1);
  471. isM = buy1.getItemMeta();
  472. Lore.clear();
  473. Lore.add(prefixprice + (double)Math.round(unitBuyPrice * 1 * 100) / 100 + " " + currency);
  474. if (buy1.getItemMeta().hasDisplayName()) Lore.add("Nom : " + buy1.getItemMeta().getDisplayName());
  475. isM.setLore(Lore);
  476. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Acheter à l'unité");
  477. buy1.setItemMeta(isM);
  478.  
  479. ItemStack buy16 = iframe.getItem();
  480. buy16.setAmount(16);
  481. isM = buy16.getItemMeta();
  482. Lore.clear();
  483. Lore.add(prefixprice + (double)Math.round(unitBuyPrice * 16 * 100) / 100 + " " + currency);
  484. isM.setLore(Lore);
  485. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Acheter 16 unités");
  486. if (buy16.getItemMeta().hasDisplayName()) Lore.add("Nom : " +buy16.getItemMeta().getDisplayName());
  487. buy16.setItemMeta(isM);
  488.  
  489. ItemStack buy32 = iframe.getItem();
  490. buy32.setAmount(32);
  491. isM = buy32.getItemMeta();
  492. Lore.clear();
  493. Lore.add(prefixprice + (double)Math.round(unitBuyPrice * 32 * 100) / 100 + " " + currency);
  494. isM.setLore(Lore);
  495. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Acheter un demi stack");
  496. if (buy32.getItemMeta().hasDisplayName()) Lore.add("Nom : " +buy32.getItemMeta().getDisplayName());
  497. buy32.setItemMeta(isM);
  498.  
  499. ItemStack buy64 = iframe.getItem();
  500. buy64.setAmount(64);
  501. isM = buy64.getItemMeta();
  502. Lore.clear();
  503. double priceStackBuy = unitBuyPrice*64;
  504. if (stackDiscountBuy > 0 && stackDiscountBuy < 50) priceStackBuy -= priceStackBuy *stackDiscountBuy / 100;
  505. priceStackBuy = (double)Math.round(priceStackBuy * 100) / 100;
  506. Lore.add(prefixprice + priceStackBuy + " " + currency);
  507. if (buy64.getItemMeta().hasDisplayName()) Lore.add("Nom : " +buy64.getItemMeta().getDisplayName());
  508. if (stackDiscountBuy > 0 && stackDiscountBuy < 50) {
  509. Lore.add(prefixdiscount + "Réduction de " + stackDiscountBuy + "% pour le stack");
  510. Lore.add(prefixdiscount + "(Prix initial : " + (double)Math.round(unitBuyPrice * 64 * 100)/ 100 + " " + currency + ")");
  511. }
  512. isM.setLore(Lore);
  513. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Acheter un stack");
  514. buy64.setItemMeta(isM);
  515.  
  516. ItemStack sell1 = iframe.getItem();
  517. sell1.setAmount(1);
  518. isM = sell1.getItemMeta();
  519. Lore.clear();
  520. Lore.add(prefixprice + (double)Math.round(unitSellPrice * 1 * 100) / 100 + " " + currency);
  521. if (sell1.getItemMeta().hasDisplayName()) Lore.add("Nom : " +sell1.getItemMeta().getDisplayName());
  522. isM.setLore(Lore);
  523. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Vendre à l'unité");
  524. sell1.setItemMeta(isM);
  525.  
  526. ItemStack sell16 = iframe.getItem();
  527. sell16.setAmount(16);
  528. isM = sell16.getItemMeta();
  529. Lore.clear();
  530. Lore.add(prefixprice + (double)Math.round(unitSellPrice * 16 * 100) / 100 + " " + currency);
  531. if (sell16.getItemMeta().hasDisplayName()) Lore.add("Nom : " +sell16.getItemMeta().getDisplayName());
  532. isM.setLore(Lore);
  533. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Vendre 16 unités");
  534. sell16.setItemMeta(isM);
  535.  
  536. ItemStack sell32 = iframe.getItem();
  537. sell32.setAmount(32);
  538. isM = sell32.getItemMeta();
  539. Lore.clear();
  540. Lore.add(prefixprice + (double)Math.round(unitSellPrice * 32 * 100) / 100 + " " + currency);
  541. if (sell32.getItemMeta().hasDisplayName()) Lore.add("Nom : " +sell32.getItemMeta().getDisplayName());
  542. if (!isMaxSell && iframe.getItem().getMaxStackSize()<2 && stackDiscountSell > 0 &&stackDiscountSell < 50) {
  543. double priceHalfStackSell = unitSellPrice*32;
  544. if (stackDiscountSell > 0 && stackDiscountSell < 50) priceHalfStackSell +=priceHalfStackSell * stackDiscountSell / 100;
  545. Lore.clear();
  546. Lore.add(prefixprice + (double)Math.round(priceHalfStackSell * 100) / 100 + " " +currency);
  547. isMaxSell = true;
  548. Lore.add(prefixdiscount + "Rajout de " + stackDiscountSell + "% pour le demi-stack");
  549. Lore.add(prefixdiscount + "(Prix initial : " + (double)Math.round(unitSellPrice * 32 *100) / 100 + " " + currency + ")");
  550. }
  551. isM.setLore(Lore);
  552. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Vendre un demi-stack");
  553. sell32.setItemMeta(isM);
  554.  
  555. ItemStack sell64 = iframe.getItem();
  556. sell64.setAmount(64);
  557. isM = sell64.getItemMeta();
  558. isM.setDisplayName("" + ChatColor.RESET + ChatColor.GREEN + ChatColor.BOLD + "Vendre un stack");
  559. Lore.clear();
  560. double priceStackSell = unitSellPrice*64;
  561. if (stackDiscountSell > 0 && stackDiscountSell < 50) priceStackSell += priceStackSell *stackDiscountSell / 100;
  562. priceStackSell = (double)Math.round(priceStackSell * 100) / 100;
  563. Lore.add(prefixprice + priceStackSell + " " + currency);
  564. if (sell64.getItemMeta().hasDisplayName()) Lore.add("Nom : " +sell64.getItemMeta().getDisplayName());
  565. if (!isMaxSell && stackDiscountSell > 0 && stackDiscountSell < 50) {
  566. Lore.add(prefixdiscount + "Rajout de " + stackDiscountSell + "% pour le stack");
  567. Lore.add(prefixdiscount + "(Prix initial : " + (double)Math.round(unitSellPrice * 64 *100) / 100 + " " + currency + ")");
  568. }
  569. isM.setLore(Lore);
  570. sell64.setItemMeta(isM);
  571.  
  572. if (iframe.getItem().getMaxStackSize()<16) buy16 = disabled;
  573. if (iframe.getItem().getMaxStackSize()<32) buy32 = disabled;
  574. if (iframe.getItem().getMaxStackSize()<64) buy64 = disabled;
  575. if (iframe.getItem().getMaxStackSize()<2) {
  576. if (iframe.getItem().getMaxStackSize()*16>36) sell16 = disabled;
  577. if (iframe.getItem().getMaxStackSize()*32>36) sell32 = disabled;
  578. if (iframe.getItem().getMaxStackSize()*64>36) sell64 = disabled;
  579. }
  580.  
  581. inventory.setItem(0, buy1);
  582. inventory.setItem(1, buy16);
  583. inventory.setItem(2, buy32);
  584. inventory.setItem(3, buy64);
  585. inventory.setItem(4, quit);
  586. inventory.setItem(5, sell1);
  587. inventory.setItem(6, sell16);
  588. inventory.setItem(7, sell32);
  589. inventory.setItem(8, sell64);
  590. player.openInventory(inventory);
  591. }
  592. else {
  593. if (player.hasPermission("ks.*") || player.hasPermission("ks.admin") ||player.hasPermission("ks.create")) {
  594. player.sendMessage(prefixchat + ChatColor.YELLOW + "Informations pour configurer le shop : ");
  595. player.sendMessage(ChatColor.GOLD + "Data : " + ChatColor.GREEN + data);
  596. player.sendMessage(ChatColor.GOLD + "Meta : " + ChatColor.GREEN + meta);
  597. }
  598. player.sendMessage(prefixchat + ChatColor.RED + "Les prix de ce shop n'ont pas été fixés. Contactez un opérateur ou un administrateur pour qu'il le fasse.");
  599. new Thread(new Runnable() {
  600. public void run() {
  601. try {
  602. Statement querry = sqlserver.createStatement();
  603. querry.executeUpdate("INSERT INTO `kingshop_prices` (`data`, `meta`) VALUES ('" + data + "', '" + meta + "') ON DUPLICATE KEY UPDATE data=data;");
  604. querry.close();
  605. }
  606. catch (SQLException e1) {
  607. e1.printStackTrace();
  608. }
  609. }
  610. }).start();
  611. }
  612. }
  613. }
  614.  
  615. public void DelShop(final World world, final int x, final int y, final int z, final int face, final Player player) {
  616. if (debug) getLogger().info(player.getName() + " deleted shop at X" + x + " Y" + y + " Z" + z + " facing " + face + " at " + world.getName());
  617. new Thread(new Runnable() {
  618. public void run() {
  619. try {
  620. Statement querry = sqlserver.createStatement();
  621. querry.executeUpdate("DELETE FROM `kingshop_list` WHERE `world` = '" + world.getName() +"' AND `x` = '" + x + "' AND `y` = '" + y + "' AND `z` = '" + z + "' AND `facing` ='" + face + "';");
  622. ShopList.remove(new Location(world, x, y, z, face, 0));
  623. player.sendMessage(prefixchat + ChatColor.GREEN + "Le shop a bien été supprimé.");
  624. querry.close();
  625. } catch (SQLException e1) {
  626. e1.printStackTrace();
  627. connect();
  628. }
  629. }
  630. }).start();
  631. }
  632.  
  633. public void CreateShop(final World world, final int x, final int y, final int z, final int face, final Player player) {
  634. if (debug) getLogger().info(player.getName() + " created shop at X" + x + " Y" + y + " Z" + z + " facing " + face + " at " + world.getName());
  635. new Thread(new Runnable() {
  636. public void run() {
  637. try {
  638. Statement querry = sqlserver.createStatement();
  639. querry.executeUpdate("INSERT INTO `kingshop_list` (`world`, `x`, `y`, `z`, `facing`) VALUES ('" + world.getName() + "', '" + x + "', '" + y + "', '" + z + "', '" + face + "');");
  640. ShopList.add(new Location(world, x, y, z, face, 0));
  641. player.sendMessage(prefixchat + ChatColor.GREEN + "Le shop a bien été créé.");
  642. querry.close();
  643. } catch (SQLException e1) {
  644. }
  645. }
  646. }).start();
  647. }
  648.  
  649. public boolean IsShop(World world, int x, int y, int z, int face) {
  650. if (debug) getLogger().info("Someone asked if shop at X" + x + " Y" + y + " Z" + z + " facing " + face + " at " +world.getName());
  651. if (ShopList.contains(new Location(world, x, y, z, face, 0))) return true;
  652. else return false;
  653. }
  654.  
  655. public void ActuShop() {
  656. try {
  657. Statement querry = sqlserver.createStatement();
  658. ResultSet result;
  659. result = querry.executeQuery("SELECT * FROM `kingshop_list`");
  660. ShopList.clear();
  661. while (result.next()) {
  662. World world = Bukkit.getServer().getWorld(result.getString("world"));
  663. int x = result.getInt("x");
  664. int y = result.getInt("y");
  665. int z = result.getInt("z");
  666. int facing = result.getInt("facing");
  667. ShopList.add(new Location(world, x, y, z, facing, 0));
  668. }
  669. querry.close();
  670. }
  671. catch (SQLException e) {
  672. e.printStackTrace();
  673. }
  674. }
  675.  
  676. public void ActuPrice() {
  677. try {
  678. Statement querry = sqlserver.createStatement();
  679. ResultSet result;
  680. result = querry.executeQuery("SELECT * FROM `kingshop_prices` WHERE `buy_unit` IS NOT NULL AND `buy_percent` IS NOT NULL AND `sell_unit` IS NOT NULL AND `sell_percent` IS NOT NULL");
  681. ItemPrices.clear();
  682. while (result.next()) ItemPrices.put(new UniqueItem(result.getString("data"), result.getString("meta")), new Prices(result.getDouble("sell_unit"), result.getDouble("sell_percent"), result.getDouble("buy_unit"), result.getDouble("buy_percent")));
  683. querry.close();
  684. }
  685. catch (SQLException e) {
  686. e.printStackTrace();
  687. }
  688. }
  689.  
  690. public boolean connect() {
  691. try {
  692. Class.forName("com.mysql.jdbc.Driver");
  693. }
  694. catch (ClassNotFoundException e1) {
  695. e1.printStackTrace();
  696. }
  697.  
  698. try {
  699. sqlserver = DriverManager.getConnection(url, user, passwd);
  700. getLogger().info("MySQL connexion okay !");
  701. try {
  702. Statement querry = sqlserver.createStatement();
  703. querry.executeUpdate("CREATE TABLE IF NOT EXISTS `kingshop_list` (`world` VARCHAR(250) NOT NULL, `x` INT(17) NOT NULL, `y` INT(17) NOT NULL, `z` INT(17) NOT NULL, `facing` INT(9) UNSIGNED NOT NULL, UNIQUE INDEX `coords` (`world`, `x`, `y`, `z`, `facing`));");
  704. querry.executeUpdate("CREATE TABLE IF NOT EXISTS `kingshop_prices` (`data` VARCHAR(100) NOT NULL, `meta` VARCHAR(250) NOT NULL, `buy_unit` DOUBLE UNSIGNED NULL DEFAULT NULL, `buy_percent` DOUBLE UNSIGNED NULL DEFAULT NULL, `sell_unit` DOUBLE UNSIGNED NULL DEFAULT NULL, `sell_percent` DOUBLE UNSIGNED NULL DEFAULT NULL, UNIQUE INDEX `material_data` (`data`, `meta`));");
  705. querry.close();
  706. return true;
  707. }
  708. catch(SQLException e){
  709. e.printStackTrace();
  710. }
  711. }
  712. catch (SQLException e) {
  713. getLogger().info("MySQL failed, please check config.yml");
  714. }
  715. return false;
  716. }
  717. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement