Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.kittle.maxixpbank;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.ChatColor;
- import org.bukkit.GameMode;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockState;
- import org.bukkit.block.CommandBlock;
- import org.bukkit.block.Sign;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.command.ConsoleCommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Maxixpbank extends JavaPlugin implements Listener, CommandExecutor {
- public FileConfiguration config = this.getConfig();
- public ConsoleCommandSender console = this.getServer().getConsoleSender();
- public PluginManager pm = this.getServer().getPluginManager();
- private static Connection connection;
- public String urlbase = "jdbc:mysql://";
- public String host = config.getString("Mysql.host");
- public String database = config.getString("Mysql.database");
- public String username = config.getString("Mysql.username");
- public String password = config.getString("Mysql.password");
- public Integer maxExperienceStored = config.getInt("maxExperienceStored");
- public Integer transactionAmount = config.getInt("transactionAmount");
- public String configWorld = config.getString("World");
- public static String prefixe = ChatColor.GOLD + "" + ChatColor.BOLD + "[" + ChatColor.YELLOW + "Maxi" + ChatColor.AQUA + "XP" + ChatColor.GREEN + "bank" + ChatColor.GOLD + "] ";
- @Override
- public void onEnable() {
- loadConfig();
- console.sendMessage(prefixe + "§eConfiguration loaded");
- connect();
- //getCommand("storexp").setExecutor(new StoreXPCommand(console,connection));
- pm.registerEvents(this, this);
- getCommand("bank").setExecutor(new PlayerCommands());
- }
- @Override
- public void onDisable() {
- disconnect();
- }
- public void loadConfig() {
- config.addDefault("World", "world_ressources");
- config.addDefault("maxExperienceStored", 1395);
- config.addDefault("transactionAmount", 1395);
- config.addDefault("Mysql", null);
- config.addDefault("Mysql.host", "adresseIP");
- config.addDefault("Mysql.database", "nomBaseDeDonnées");
- config.addDefault("Mysql.username", "userName");
- config.addDefault("Mysql.password", "passWord");
- config.options().copyDefaults(true);
- saveConfig();
- }
- public Connection connect() {
- if (!isConnected()) {
- try {
- connection = DriverManager.getConnection(urlbase + host + "/" + database, username, password);
- console.sendMessage(prefixe +"§bMySQL successfully §aconnected");
- }
- catch (SQLException e) {
- e.printStackTrace();
- console.sendMessage(prefixe +"§cMySQL connection failed ! --> Check configuration file");
- }
- }
- return connection;
- }
- public void disconnect() {
- if(isConnected()) {
- try {
- connection.close();
- console.sendMessage(prefixe + "§bMySQL successfully §c§lclosed");
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- return connection != null;
- }
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if(sender instanceof Player) {
- Player p = (Player) sender;
- if(cmd.getName().equalsIgnoreCase("storexp")) {
- if(isConfigWorld(p.getWorld().getName())) {
- if(p.hasPermission("maxixpbank.admin.storexp")) {
- Material material = p.getTargetBlock(null, 3).getType();
- if(blockIsASign(material)) {
- Block block = p.getTargetBlock(null, 3);
- try {
- addStall(block, p);
- }
- catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return true;
- }
- else {
- p.sendMessage(prefixe + "§cVous devez cibler un panneau");
- return false;
- }
- }
- else {
- p.sendMessage(prefixe + "§cVous n'avez pas la permission pour cette commande");
- return false;
- }
- }
- else {
- p.sendMessage(prefixe + "§cUtilisable uniquement sur le monde construction");
- return false;
- }
- }
- }
- else if((sender instanceof CommandBlock) || (sender instanceof ConsoleCommandSender)) {
- console.sendMessage(prefixe + "§cCommande utilisable par un joueur uniquement");
- return false;
- }
- return false;
- }
- @EventHandler
- public void onClick(PlayerInteractEvent pie) throws SQLException {
- if (pie.getPlayer() instanceof Player) { //SI EVENTPLAYER INSTANCE OF PLAYER
- Player p = (Player) pie.getPlayer();
- Material block = pie.getClickedBlock().getType();
- String loc = pie.getClickedBlock().getLocation().toString();
- Action action = pie.getAction();
- if(isBlockRightClick(action) && (blockIsASign(block))) { //SI CLICK DROIT SUR UN PANNEAU
- if(locIsValidStall(loc)) { //SI LA BORNE EST VALIDE
- doDeposit(p);
- }
- }
- else if (isBlockLeftClick(action) && (blockIsASign(block))) {
- if (locIsValidStall(loc)) {
- doWithdrawal(p);
- }
- }
- }
- }
- @EventHandler
- public void onBreak(BlockBreakEvent bbe) throws SQLException {
- if (bbe.getPlayer() instanceof Player) {
- Player p = bbe.getPlayer();
- Block block = bbe.getBlock();
- if(isConfigWorld(p.getWorld().getName())) {
- if(blockIsASign(block)) {
- if(blockIsValidStall(block)) {
- if(p.getGameMode().equals(GameMode.SURVIVAL)) {
- p.sendMessage(prefixe + "§cSupprimez le guichet d'abord");
- bbe.setCancelled(true);
- }
- else {
- bbe.setCancelled(true);
- }
- }
- }
- }
- }
- }
- public boolean locIsValidStall(String loc) throws SQLException {
- String query = "SELECT LOCATION FROM stalls WHERE LOCATION = '" + loc + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- ResultSet result = statement.executeQuery();
- statement.closeOnCompletion();
- return result.next();
- }
- public boolean blockIsValidStall(Block block) throws SQLException {
- String query = "SELECT LOCATION FROM stalls WHERE LOCATION = '" + block.getLocation().toString() + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- ResultSet result = statement.executeQuery();
- statement.closeOnCompletion();
- return result.next();
- }
- public boolean playerHasReachLimit(Player p) throws SQLException {
- String query = "SELECT XP FROM xpbank WHERE UUID = '" + p.getUniqueId().toString() + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- ResultSet result = statement.executeQuery();
- statement.closeOnCompletion();
- if(result.next()) {
- return result.getInt("XP") >= maxExperienceStored;
- }
- return false;
- }
- public final boolean playerCanAfford(Player p) {
- return p.getTotalExperience() >= transactionAmount ;
- }
- public Integer playerBankAmount(Player p) throws SQLException {
- String query = "SELECT XP FROM xpbank WHERE UUID = '" + p.getUniqueId().toString() + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- ResultSet result = statement.executeQuery();
- statement.closeOnCompletion();
- if (result.next()) {
- return result.getInt("XP");
- }
- return null;
- }
- public boolean playerAlreadyRegistered(Player p) throws SQLException {
- String query = "SELECT UUID FROM xpbank WHERE UUID = '" + p.getUniqueId().toString() + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- ResultSet result = statement.executeQuery();
- statement.closeOnCompletion();
- return result.next();
- }
- public boolean isConfigWorld(String world) {
- return getConfig().getString("World").equalsIgnoreCase(world);
- }
- public boolean blockIsASign(Block block) {
- Material b = block.getType();
- return ((b == Material.SIGN_POST) || (b == Material.WALL_SIGN)) ;
- }
- public boolean blockIsASign(Material block) {
- return ((block == Material.SIGN_POST) || (block == Material.WALL_SIGN)) ;
- }
- public boolean isBlockRightClick(Action action) {
- return Action.RIGHT_CLICK_BLOCK == action;
- }
- public boolean isBlockLeftClick(Action action) {
- return Action.LEFT_CLICK_BLOCK == action;
- }
- public boolean doDeposit(Player p) throws SQLException {
- if(isCreative(p)) {
- p.sendMessage(prefixe + "§cVous devez être en gamemode SURVIVAL");
- return false;
- }
- if(!playerCanAfford(p)) {
- p.sendMessage(prefixe + "§cExpérience insuffisante pour pouvoir faire un dépôt");
- return false;
- }
- if (playerHasReachLimit(p)) {
- p.sendMessage(prefixe + "§cVous avez atteint la limite maximale autorisée");
- return false;
- }
- String uuid = p.getUniqueId().toString();
- String name = p.getName().toString();
- Integer currentXP = p.getTotalExperience();
- p.setTotalExperience(0);
- p.setLevel(0);
- p.setExp(0);
- p.giveExp(currentXP-transactionAmount);
- if (playerAlreadyRegistered(p)) {
- Integer bankAmount = playerBankAmount(p);
- bankAmount+=transactionAmount;
- String query = "UPDATE xpbank SET XP ='" + bankAmount + "'WHERE UUID ='"+ uuid + "';";
- try {
- PreparedStatement statement = connection.prepareStatement(query);
- statement.executeUpdate();
- statement.closeOnCompletion();
- }
- catch (SQLException e) {
- p.sendMessage(prefixe + "§cUne erreur est survenue");
- e.printStackTrace();
- return false;
- }
- p.sendMessage(prefixe + "§aSolde de votre compte: §b" + bankAmount + " §aXPs");
- }
- else {
- String query = "INSERT INTO xpbank (UUID, PLAYER, XP) VALUES ('" + uuid + "', '" + name + "', '" + transactionAmount + "');";
- try {
- PreparedStatement statement = connection.prepareStatement(query);
- statement.executeUpdate();
- statement.closeOnCompletion();
- }
- catch (SQLException e) {
- p.sendMessage(prefixe + "§cUne erreur est survenue");
- e.printStackTrace();
- return false;
- }
- p.sendMessage(prefixe + "§aSolde de votre compte: §b" + transactionAmount + " §aXPs");
- }
- return false;
- }
- public boolean doWithdrawal(Player p) throws SQLException {
- if(isCreative(p)) {
- p.sendMessage(prefixe + "§cVous devez être en gamemode SURVIVAL");
- return false;
- }
- if(!playerAlreadyRegistered(p)) {
- p.sendMessage(prefixe + "§cVous n'avez pas encore déposé d'XP");
- return false;
- }
- else if (playerBankAmount(p) < transactionAmount) {
- p.sendMessage(prefixe + "§cVous n'avez pas d'XP en banque");
- return false;
- }
- else {
- String uuid = p.getUniqueId().toString();
- Integer debitedAmount = playerBankAmount(p) - transactionAmount;
- String query = "UPDATE xpbank SET XP ='" + debitedAmount + "'WHERE UUID ='"+ uuid + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- statement.executeUpdate();
- statement.closeOnCompletion();
- p.giveExp(transactionAmount);
- p.sendMessage(prefixe + "§aSolde de votre compte: §b" + debitedAmount + " §aXPs");
- return true;
- }
- }
- public void addStall(Block block, Player p) throws SQLException {
- if(blockIsValidStall(block)) {
- removeStall(block, p);
- }
- else {
- String loc = block.getLocation().toString();
- String query = "INSERT INTO stalls (LOCATION) VALUES ('" + loc + "');";
- PreparedStatement statement = connection.prepareStatement(query);
- statement.executeUpdate();
- statement.closeOnCompletion();
- BlockState state =(Sign) block.getState();
- Sign sign = (Sign) state;
- sign.setLine(0, "§l" + prefixe);
- sign.setLine(1, "§7*****");
- sign.setLine(2, "§lDép.-> Clic D");
- sign.setLine(3, "§lRetr.-> Clic G");
- sign.update();
- p.sendMessage(prefixe + " §aGuichet créé avec succès !");
- }
- }
- public void removeStall(Block block, Player p) throws SQLException {
- String loc = block.getLocation().toString();
- String query = "DELETE FROM stalls WHERE LOCATION ='" + loc + "';";
- PreparedStatement statement = connection.prepareStatement(query);
- statement.executeUpdate();
- statement.closeOnCompletion();
- block.breakNaturally();
- p.sendMessage(prefixe + " §6Guichet supprimé avec succès");
- }
- public void playerBalance(Player p) throws SQLException {
- p.sendMessage("§aVous avez " + playerBankAmount(p) + " §aXPs en banque");
- }
- public boolean isCreative(Player p) {
- return p.getGameMode().equals(GameMode.CREATIVE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement