Solved SQL implementation help

Discussion in 'Plugin Development' started by TH3_GR13F, Sep 28, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    TH3_GR13F

    So i will include the entire class's code at the end just in case it is useful, but the issue i am having is with the ps4 prepared statement, the purpose of it is to find the block that the player is breaking, inside the sql table, and then figure out who owns that block, and then assigns that "NAME" entry to 'blockowner' then it compares blockowner to breaker and if they are equal public boolean isOwner returns true, otherwise it returns false. through using Bukkit.getLogger.info() i have managed to figure out that when a block is broken the code executes all the way down to

    Code:
    while (results4.net()) {
    
    but then it just seems as though it stops, there is no error thrown in console, no exception, nothing, if i add a Bukkit.getLogger.info("test"); after the above line, it never shows up in console either. Can anyone tell me what i am doing wrong here? I've fiddled with things quite a bit and haven't had any luck.

    Entire class code
    Code:
    package com.n0grief.WatchBlock.EventHandler;
    
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import com.n0grief.WatchBlock.Main;
    import net.md_5.bungee.api.ChatColor;
    
    public class SQLLogger implements Listener {
        private Main plugin;
        public SQLLogger(Main plugin) {
            this.plugin = plugin;
        }
        public boolean isOwner(int blockx, int blocky, int blockz, String world, Player player) {
            try {
                PreparedStatement ps4 = plugin.SQL.getConnection().prepareStatement("SELECT * FROM wb_blocks WHERE WORLD=? and X=? and Y=? and Z=?");
                ps4.setString(1, world);
                ps4.setInt(2, blockx);
                ps4.setInt(3, blocky);
                ps4.setInt(4, blockz);
                String breaker = player.getName();
                ResultSet results4 = ps4.executeQuery();
                while (results4.next()) {
                    String blockowner = results4.getString("NAME");
                    if(blockowner.equalsIgnoreCase(breaker)) {
                        return true;
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
        public boolean blockexists(int blockx, int blocky, int blockz,String world) {
            try {
                PreparedStatement ps3 = plugin.SQL.getConnection().prepareStatement("SELECT * FROM wb_blocks WHERE WORLD=? and X=? and Y=? and Z=?");
                ps3.setString(1, world);
                ps3.setInt(2, blockx);
                ps3.setInt(3, blocky);
                ps3.setInt(4, blockz);
                ResultSet results1 = ps3.executeQuery();
                if(results1.next()) {
                    Bukkit.getLogger().info("[WATCHBLOCK] blockexists check returned true!");
                    return true;
                }
                Bukkit.getLogger().info("[WATCHBLOCK] blockexists check returned false!");
                return false;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
          
        }
      
        @EventHandler
        public void onBreak(BlockBreakEvent event) {
            Block block = event.getBlock();
            Player player = event.getPlayer();
            Location b_loc = block.getLocation();
            String world = b_loc.getWorld().getName();
            Integer blockx = b_loc.getBlockX();
            Integer blocky = b_loc.getBlockY();
            Integer blockz = b_loc.getBlockZ();
            if(blockexists(blockx, blocky, blockz, world)) {
                if(!isOwner(blockz, blockz, blockz, world, player)) {
                event.setCancelled(true);
                if(isOwner(blockz, blockz, blockz, world, player)) {
                    event.setCancelled(false);
                }
            }
            }
            if(!blockexists(blockx,blocky,blockz,world)) {
                event.setCancelled(false);
            }
        }
        @EventHandler
        public void onPlace(BlockPlaceEvent event) {
            Block block = event.getBlock();
            Player player = event.getPlayer();
            UUID uuid = player.getUniqueId();
            Location b_loc = block.getLocation();
            String world = b_loc.getWorld().getName();
            Integer blockx = b_loc.getBlockX();
            Integer blocky = b_loc.getBlockY();
            Integer blockz = b_loc.getBlockZ();
            if(blockexists(blockx,blocky,blockz,world)) {
                player.sendMessage(ChatColor.RED + "[WATCHBLOCK] The SQL server thinks this block is already registered to a player??? Possible SQL logging issue.");
                player.sendMessage(ChatColor.RED + "[WATCHBLOCK] If you see this error message the plugin may not be working right, contact server admin.");
                event.setCancelled(true);
            }
            else {
                event.setCancelled(false);
                try {
                    PreparedStatement ps2 = plugin.SQL.getConnection().prepareStatement("INSERT INTO wb_blocks" + "(NAME,UUID,WORLD,X,Y,Z) VALUES(?,?,?,?,?,?)");
                    ps2.setString(1, player.getName());
                    ps2.setString(2, uuid.toString());
                    ps2.setString(3, world);
                    ps2.setInt(4, blockx);
                    ps2.setInt(5, blocky);
                    ps2.setInt(6, blockz);
                    ps2.executeUpdate();
                    Bukkit.getLogger().info("[WATCHBLOCK] onPlace attempted to add block to database!");
                }
                catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    *edit* afterthought note: the block that 'ps4' looks for does exist in the database, there is only one entry of it, and it does have a "NAME" field as part of the row that it is in.
    The output of running the query command in HeidiSQL can be seen here.

    holy fuck i see what i did *facepalm* don't let Eclipse automatically assign your arguments for you, lesson learned

    Code:
    if(blockexists(blockx, blocky, blockz, world)) {
    if(!isOwner(blockz, blockz, blockz, world, player)) {
    event.setCancelled(true);
    if(isOwner(blockz, blockz, blockz, world, player)) {
    event.setCancelled(false);
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 28, 2021
Thread Status:
Not open for further replies.

Share This Page