Advertisement
Guest User

Untitled

a guest
Jan 1st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.32 KB | None | 0 0
  1. package fr.kittle.maxixpbank;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.GameMode;
  10. import org.bukkit.Material;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.block.BlockState;
  13. import org.bukkit.block.CommandBlock;
  14. import org.bukkit.block.Sign;
  15. import org.bukkit.command.Command;
  16. import org.bukkit.command.CommandExecutor;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.command.ConsoleCommandSender;
  19. import org.bukkit.configuration.file.FileConfiguration;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.event.EventHandler;
  22. import org.bukkit.event.Listener;
  23. import org.bukkit.event.block.Action;
  24. import org.bukkit.event.block.BlockBreakEvent;
  25. import org.bukkit.event.player.PlayerInteractEvent;
  26. import org.bukkit.plugin.PluginManager;
  27. import org.bukkit.plugin.java.JavaPlugin;
  28.  
  29.  
  30. public class Maxixpbank extends JavaPlugin implements Listener, CommandExecutor {
  31.  
  32.  
  33.  
  34. public FileConfiguration config = this.getConfig();
  35. public ConsoleCommandSender console = this.getServer().getConsoleSender();
  36. public PluginManager pm = this.getServer().getPluginManager();
  37.  
  38. private static Connection connection;
  39. public String urlbase = "jdbc:mysql://";
  40. public String host = config.getString("Mysql.host");
  41. public String database = config.getString("Mysql.database");
  42. public String username = config.getString("Mysql.username");
  43. public String password = config.getString("Mysql.password");
  44.  
  45. public Integer maxExperienceStored = config.getInt("maxExperienceStored");
  46. public Integer transactionAmount = config.getInt("transactionAmount");
  47. public String configWorld = config.getString("World");
  48. public static String prefixe = ChatColor.GOLD + "" + ChatColor.BOLD + "[" + ChatColor.YELLOW + "Maxi" + ChatColor.AQUA + "XP" + ChatColor.GREEN + "bank" + ChatColor.GOLD + "] ";
  49.  
  50.  
  51. @Override
  52. public void onEnable() {
  53.  
  54. loadConfig();
  55. console.sendMessage(prefixe + "§eConfiguration loaded");
  56. connect();
  57. //getCommand("storexp").setExecutor(new StoreXPCommand(console,connection));
  58. pm.registerEvents(this, this);
  59. getCommand("bank").setExecutor(new PlayerCommands());
  60. }
  61.  
  62.  
  63.  
  64. @Override
  65. public void onDisable() {
  66.  
  67. disconnect();
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. public void loadConfig() {
  79.  
  80. config.addDefault("World", "world_ressources");
  81. config.addDefault("maxExperienceStored", 1395);
  82. config.addDefault("transactionAmount", 1395);
  83. config.addDefault("Mysql", null);
  84. config.addDefault("Mysql.host", "adresseIP");
  85. config.addDefault("Mysql.database", "nomBaseDeDonnées");
  86. config.addDefault("Mysql.username", "userName");
  87. config.addDefault("Mysql.password", "passWord");
  88. config.options().copyDefaults(true);
  89. saveConfig();
  90.  
  91. }
  92.  
  93.  
  94. public Connection connect() {
  95.  
  96. if (!isConnected()) {
  97.  
  98. try {
  99.  
  100. connection = DriverManager.getConnection(urlbase + host + "/" + database, username, password);
  101. console.sendMessage(prefixe +"§bMySQL successfully §aconnected");
  102. }
  103.  
  104. catch (SQLException e) {
  105.  
  106. e.printStackTrace();
  107. console.sendMessage(prefixe +"§cMySQL connection failed ! --> Check configuration file");
  108. }
  109. }
  110.  
  111. return connection;
  112.  
  113. }
  114.  
  115.  
  116.  
  117. public void disconnect() {
  118.  
  119. if(isConnected()) {
  120.  
  121. try {
  122.  
  123. connection.close();
  124. console.sendMessage(prefixe + "§bMySQL successfully §c§lclosed");
  125. }
  126.  
  127. catch (SQLException e) {
  128.  
  129. e.printStackTrace();
  130. }
  131. }
  132. }
  133.  
  134.  
  135.  
  136. public boolean isConnected() {
  137.  
  138. return connection != null;
  139. }
  140.  
  141.  
  142.  
  143. @Override
  144. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  145.  
  146. if(sender instanceof Player) {
  147.  
  148. Player p = (Player) sender;
  149.  
  150. if(cmd.getName().equalsIgnoreCase("storexp")) {
  151.  
  152. if(isConfigWorld(p.getWorld().getName())) {
  153.  
  154. if(p.hasPermission("maxixpbank.admin.storexp")) {
  155.  
  156. Material material = p.getTargetBlock(null, 3).getType();
  157.  
  158. if(blockIsASign(material)) {
  159.  
  160. Block block = p.getTargetBlock(null, 3);
  161.  
  162. try {
  163.  
  164. addStall(block, p);
  165.  
  166. }
  167.  
  168. catch (SQLException e) {
  169.  
  170. // TODO Auto-generated catch block
  171. e.printStackTrace();
  172. }
  173.  
  174. return true;
  175.  
  176. }
  177. else {
  178. p.sendMessage(prefixe + "§cVous devez cibler un panneau");
  179. return false;
  180. }
  181. }
  182. else {
  183. p.sendMessage(prefixe + "§cVous n'avez pas la permission pour cette commande");
  184. return false;
  185. }
  186. }
  187. else {
  188. p.sendMessage(prefixe + "§cUtilisable uniquement sur le monde construction");
  189. return false;
  190. }
  191. }
  192. }
  193. else if((sender instanceof CommandBlock) || (sender instanceof ConsoleCommandSender)) {
  194.  
  195. console.sendMessage(prefixe + "§cCommande utilisable par un joueur uniquement");
  196. return false;
  197. }
  198.  
  199. return false;
  200. }
  201.  
  202.  
  203.  
  204. @EventHandler
  205. public void onClick(PlayerInteractEvent pie) throws SQLException {
  206.  
  207. if (pie.getPlayer() instanceof Player) { //SI EVENTPLAYER INSTANCE OF PLAYER
  208.  
  209. Player p = (Player) pie.getPlayer();
  210. Material block = pie.getClickedBlock().getType();
  211. String loc = pie.getClickedBlock().getLocation().toString();
  212. Action action = pie.getAction();
  213.  
  214. if(isBlockRightClick(action) && (blockIsASign(block))) { //SI CLICK DROIT SUR UN PANNEAU
  215.  
  216. if(locIsValidStall(loc)) { //SI LA BORNE EST VALIDE
  217.  
  218. doDeposit(p);
  219. }
  220.  
  221. }
  222.  
  223. else if (isBlockLeftClick(action) && (blockIsASign(block))) {
  224.  
  225. if (locIsValidStall(loc)) {
  226.  
  227. doWithdrawal(p);
  228. }
  229.  
  230. }
  231.  
  232. }
  233.  
  234. }
  235.  
  236.  
  237.  
  238. @EventHandler
  239. public void onBreak(BlockBreakEvent bbe) throws SQLException {
  240.  
  241. if (bbe.getPlayer() instanceof Player) {
  242.  
  243. Player p = bbe.getPlayer();
  244. Block block = bbe.getBlock();
  245.  
  246. if(isConfigWorld(p.getWorld().getName())) {
  247.  
  248. if(blockIsASign(block)) {
  249.  
  250. if(blockIsValidStall(block)) {
  251.  
  252. if(p.getGameMode().equals(GameMode.SURVIVAL)) {
  253.  
  254. p.sendMessage(prefixe + "§cSupprimez le guichet d'abord");
  255. bbe.setCancelled(true);
  256. }
  257.  
  258. else {
  259.  
  260. bbe.setCancelled(true);
  261. }
  262.  
  263. }
  264. }
  265. }
  266. }
  267.  
  268. }
  269.  
  270.  
  271.  
  272. public boolean locIsValidStall(String loc) throws SQLException {
  273.  
  274. String query = "SELECT LOCATION FROM stalls WHERE LOCATION = '" + loc + "';";
  275. PreparedStatement statement = connection.prepareStatement(query);
  276. ResultSet result = statement.executeQuery();
  277. statement.closeOnCompletion();
  278. return result.next();
  279. }
  280.  
  281.  
  282.  
  283.  
  284. public boolean blockIsValidStall(Block block) throws SQLException {
  285.  
  286. String query = "SELECT LOCATION FROM stalls WHERE LOCATION = '" + block.getLocation().toString() + "';";
  287. PreparedStatement statement = connection.prepareStatement(query);
  288. ResultSet result = statement.executeQuery();
  289. statement.closeOnCompletion();
  290. return result.next();
  291. }
  292.  
  293.  
  294. public boolean playerHasReachLimit(Player p) throws SQLException {
  295. String query = "SELECT XP FROM xpbank WHERE UUID = '" + p.getUniqueId().toString() + "';";
  296. PreparedStatement statement = connection.prepareStatement(query);
  297. ResultSet result = statement.executeQuery();
  298. statement.closeOnCompletion();
  299. if(result.next()) {
  300. return result.getInt("XP") >= maxExperienceStored;
  301. }
  302.  
  303. return false;
  304. }
  305.  
  306.  
  307.  
  308. public final boolean playerCanAfford(Player p) {
  309.  
  310. return p.getTotalExperience() >= transactionAmount ;
  311. }
  312.  
  313.  
  314.  
  315. public Integer playerBankAmount(Player p) throws SQLException {
  316. String query = "SELECT XP FROM xpbank WHERE UUID = '" + p.getUniqueId().toString() + "';";
  317. PreparedStatement statement = connection.prepareStatement(query);
  318. ResultSet result = statement.executeQuery();
  319. statement.closeOnCompletion();
  320. if (result.next()) {
  321. return result.getInt("XP");
  322. }
  323.  
  324. return null;
  325.  
  326. }
  327.  
  328.  
  329.  
  330. public boolean playerAlreadyRegistered(Player p) throws SQLException {
  331. String query = "SELECT UUID FROM xpbank WHERE UUID = '" + p.getUniqueId().toString() + "';";
  332. PreparedStatement statement = connection.prepareStatement(query);
  333. ResultSet result = statement.executeQuery();
  334. statement.closeOnCompletion();
  335. return result.next();
  336. }
  337.  
  338.  
  339. public boolean isConfigWorld(String world) {
  340. return getConfig().getString("World").equalsIgnoreCase(world);
  341. }
  342.  
  343.  
  344.  
  345.  
  346.  
  347. public boolean blockIsASign(Block block) {
  348. Material b = block.getType();
  349. return ((b == Material.SIGN_POST) || (b == Material.WALL_SIGN)) ;
  350. }
  351.  
  352.  
  353.  
  354.  
  355. public boolean blockIsASign(Material block) {
  356.  
  357. return ((block == Material.SIGN_POST) || (block == Material.WALL_SIGN)) ;
  358. }
  359.  
  360.  
  361. public boolean isBlockRightClick(Action action) {
  362.  
  363. return Action.RIGHT_CLICK_BLOCK == action;
  364. }
  365.  
  366.  
  367.  
  368.  
  369. public boolean isBlockLeftClick(Action action) {
  370.  
  371. return Action.LEFT_CLICK_BLOCK == action;
  372. }
  373.  
  374.  
  375.  
  376. public boolean doDeposit(Player p) throws SQLException {
  377.  
  378. if(isCreative(p)) {
  379. p.sendMessage(prefixe + "§cVous devez être en gamemode SURVIVAL");
  380. return false;
  381. }
  382.  
  383. if(!playerCanAfford(p)) {
  384. p.sendMessage(prefixe + "§cExpérience insuffisante pour pouvoir faire un dépôt");
  385. return false;
  386. }
  387.  
  388. if (playerHasReachLimit(p)) {
  389. p.sendMessage(prefixe + "§cVous avez atteint la limite maximale autorisée");
  390. return false;
  391. }
  392.  
  393. String uuid = p.getUniqueId().toString();
  394. String name = p.getName().toString();
  395. Integer currentXP = p.getTotalExperience();
  396. p.setTotalExperience(0);
  397. p.setLevel(0);
  398. p.setExp(0);
  399. p.giveExp(currentXP-transactionAmount);
  400.  
  401. if (playerAlreadyRegistered(p)) {
  402.  
  403. Integer bankAmount = playerBankAmount(p);
  404. bankAmount+=transactionAmount;
  405. String query = "UPDATE xpbank SET XP ='" + bankAmount + "'WHERE UUID ='"+ uuid + "';";
  406.  
  407. try {
  408. PreparedStatement statement = connection.prepareStatement(query);
  409. statement.executeUpdate();
  410. statement.closeOnCompletion();
  411. }
  412. catch (SQLException e) {
  413. p.sendMessage(prefixe + "§cUne erreur est survenue");
  414. e.printStackTrace();
  415. return false;
  416. }
  417. p.sendMessage(prefixe + "§aSolde de votre compte: §b" + bankAmount + " §aXPs");
  418. }
  419.  
  420. else {
  421. String query = "INSERT INTO xpbank (UUID, PLAYER, XP) VALUES ('" + uuid + "', '" + name + "', '" + transactionAmount + "');";
  422.  
  423. try {
  424. PreparedStatement statement = connection.prepareStatement(query);
  425. statement.executeUpdate();
  426. statement.closeOnCompletion();
  427. }
  428. catch (SQLException e) {
  429. p.sendMessage(prefixe + "§cUne erreur est survenue");
  430. e.printStackTrace();
  431. return false;
  432. }
  433.  
  434. p.sendMessage(prefixe + "§aSolde de votre compte: §b" + transactionAmount + " §aXPs");
  435. }
  436.  
  437.  
  438. return false;
  439.  
  440. }
  441.  
  442.  
  443.  
  444.  
  445. public boolean doWithdrawal(Player p) throws SQLException {
  446.  
  447. if(isCreative(p)) {
  448. p.sendMessage(prefixe + "§cVous devez être en gamemode SURVIVAL");
  449. return false;
  450. }
  451.  
  452. if(!playerAlreadyRegistered(p)) {
  453. p.sendMessage(prefixe + "§cVous n'avez pas encore déposé d'XP");
  454. return false;
  455. }
  456.  
  457. else if (playerBankAmount(p) < transactionAmount) {
  458. p.sendMessage(prefixe + "§cVous n'avez pas d'XP en banque");
  459. return false;
  460. }
  461. else {
  462. String uuid = p.getUniqueId().toString();
  463. Integer debitedAmount = playerBankAmount(p) - transactionAmount;
  464. String query = "UPDATE xpbank SET XP ='" + debitedAmount + "'WHERE UUID ='"+ uuid + "';";
  465. PreparedStatement statement = connection.prepareStatement(query);
  466. statement.executeUpdate();
  467. statement.closeOnCompletion();
  468.  
  469. p.giveExp(transactionAmount);
  470. p.sendMessage(prefixe + "§aSolde de votre compte: §b" + debitedAmount + " §aXPs");
  471. return true;
  472. }
  473.  
  474. }
  475.  
  476.  
  477. public void addStall(Block block, Player p) throws SQLException {
  478.  
  479. if(blockIsValidStall(block)) {
  480.  
  481. removeStall(block, p);
  482. }
  483. else {
  484. String loc = block.getLocation().toString();
  485. String query = "INSERT INTO stalls (LOCATION) VALUES ('" + loc + "');";
  486. PreparedStatement statement = connection.prepareStatement(query);
  487. statement.executeUpdate();
  488. statement.closeOnCompletion();
  489. BlockState state =(Sign) block.getState();
  490. Sign sign = (Sign) state;
  491.  
  492. sign.setLine(0, "§l" + prefixe);
  493. sign.setLine(1, "§7*****");
  494. sign.setLine(2, "§lDép.-> Clic D");
  495. sign.setLine(3, "§lRetr.-> Clic G");
  496. sign.update();
  497. p.sendMessage(prefixe + " §aGuichet créé avec succès !");
  498. }
  499.  
  500.  
  501. }
  502.  
  503.  
  504. public void removeStall(Block block, Player p) throws SQLException {
  505.  
  506. String loc = block.getLocation().toString();
  507. String query = "DELETE FROM stalls WHERE LOCATION ='" + loc + "';";
  508. PreparedStatement statement = connection.prepareStatement(query);
  509. statement.executeUpdate();
  510. statement.closeOnCompletion();
  511. block.breakNaturally();
  512. p.sendMessage(prefixe + " §6Guichet supprimé avec succès");
  513. }
  514.  
  515.  
  516.  
  517. public void playerBalance(Player p) throws SQLException {
  518.  
  519. p.sendMessage("§aVous avez " + playerBankAmount(p) + " §aXPs en banque");
  520. }
  521.  
  522.  
  523. public boolean isCreative(Player p) {
  524. return p.getGameMode().equals(GameMode.CREATIVE);
  525. }
  526.  
  527.  
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement