goscinak

I have written a plugin for guild , but i have a problem with breaking heart . can you help me? here is the heartbreak listener code ...

📎 1 file 5,474 chars
📄
pasted-code-1767727405588.txt
txt 128 lines
▶
package pl.goscinakcore;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

public class HeartBreakListener implements Listener {
    private final GoscinakCore plugin;

    public HeartBreakListener(GoscinakCore plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBreak(BlockBreakEvent event) {
        // Interesuje nas tylko blok Redstone
        if (event.getBlock().getType() != Material.REDSTONE_BLOCK) return;

        Player p = event.getPlayer();
        Location blockLoc = event.getBlock().getLocation();
        
        // --- DEBUG: Widzisz to tylko Ty na czacie ---
        p.sendMessage(ChatColor.DARK_GRAY + "Sprawdzanie bloku na: " + blockLoc.getBlockX() + ", " + blockLoc.getBlockY() + ", " + blockLoc.getBlockZ());

        try {
            // Pobieramy wszystkie gildie z bazy, by sprawdzić ich serca
            try (PreparedStatement ps = plugin.getDatabase().getConnection().prepareStatement(
                    "SELECT name, tag, leader_uuid, heart_x, heart_y, heart_z FROM guilds WHERE heart_x IS NOT NULL")) {
                
                ResultSet rs = ps.executeQuery();
                boolean isGuildHeart = false;

                while (rs.next()) {
                    int hX = rs.getInt("heart_x");
                    int hY = rs.getInt("heart_y");
                    int hZ = rs.getInt("heart_z");

                    // Jeśli koordynaty się zgadzają
                    if (blockLoc.getBlockX() == hX && blockLoc.getBlockY() == hY && blockLoc.getBlockZ() == hZ) {
                        isGuildHeart = true;
                        String leaderUUID = rs.getString("leader_uuid");
                        String tag = rs.getString("tag");
                        String guildName = rs.getString("name");

                        // Sprawdzanie czy niszczy lider
                        if (!p.getUniqueId().toString().equals(leaderUUID)) {
                            event.setCancelled(true);
                            p.sendMessage(ChatColor.RED + "Tylko lider gildii " + ChatColor.WHITE + guildName + ChatColor.RED + " moze to zrobic!");
                            return;
                        }

                        // --- LOGIKA NISZCZENIA ---
                        
                        // 1. Kasujemy drop zwykłego redstone
                        event.setDropItems(false);
                        event.setExpToDrop(0);

                        // 2. Usuwamy hologramy (ArmorStandy)
                        removeHolograms(blockLoc);

                        // 3. Czyścimy bazę danych
                        clearDatabaseLocation(guildName);

                        // 4. Dajemy serce bezpośrednio do ręki
                        ItemStack heart = createHeartItem(tag);
                        if (p.getInventory().firstEmpty() != -1) {
                            p.getInventory().addItem(heart);
                        } else {
                            p.getWorld().dropItemNaturally(p.getLocation(), heart);
                        }

                        p.sendMessage(ChatColor.GREEN + "Serce gildii zostalo pomyslnie schowane do ekwipunku.");
                        break;
                    }
                }

                if (!isGuildHeart) {
                    p.sendMessage(ChatColor.GRAY + "To nie jest zarejestrowane serce zadnej gildii.");
                }
            }
        } catch (SQLException e) {
            p.sendMessage(ChatColor.RED + "Blad bazy danych przy niszczeniu serca!");
            e.printStackTrace();
        }
    }

    private void removeHolograms(Location loc) {
        // Szukamy w małym promieniu wokół bloku
        for (Entity entity : loc.getWorld().getNearbyEntities(loc.clone().add(0.5, 1.2, 0.5), 1.0, 2.0, 1.0)) {
            if (entity instanceof ArmorStand) {
                entity.remove();
            }
        }
    }

    private void clearDatabaseLocation(String guildName) throws SQLException {
        try (PreparedStatement ps = plugin.getDatabase().getConnection().prepareStatement(
                "UPDATE guilds SET heart_x = NULL, heart_y = NULL, heart_z = NULL, heart_world = NULL WHERE name = ?")) {
            ps.setString(1, guildName);
            ps.executeUpdate();
        }
    }

    private ItemStack createHeartItem(String tag) {
        ItemStack heart = new ItemStack(Material.REDSTONE_BLOCK);
        ItemMeta meta = heart.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(ChatColor.RED + "Serce Gildii " + ChatColor.DARK_GRAY + "[" + ChatColor.WHITE + tag + ChatColor.DARK_GRAY + "]");
            meta.setLore(Arrays.asList(ChatColor.GRAY + "Postaw ten blok, aby", ChatColor.GRAY + "wyznaczyc srodek bazy."));
            heart.setItemMeta(meta);
        }
        return heart;
    }
}
Kodari AI
KodariJan 6 at 7:24 pm 2,112 (v2)

I’ll help you identify and fix the issue with your heart breaking system. Let me first examine your project structure to understand the full context.

This is a new project. Let me set it up and fix the heart breaking system issues.

GoscinakCore
A guild management plugin featuring territory control with placeable guild hearts. Players can create guilds, claim territory around their heart block, and manage guild members. The heart system uses custom redstone blocks with holograms that only guild leaders can place or break. Includes database persistence for guild data and territory coordinates.

Issues Found in Your Code:

  1. Missing null check for ItemMeta

  2. Potential memory leak - not closing ResultSet

  3. Race condition - hologram removal might fail if entities aren’t loaded

  4. No validation if heart was actually placed (could break any redstone block)

  5. Inefficient database query - loading all guilds instead of direct lookup

Let me create the corrected version with the full project structure:

[Generation stopped]

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
6 195
src/main/java/pl/goscinakcore/GoscinakCore.java
package pl.goscinakcore;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class GoscinakCore extends JavaPlugin {
    private Database database;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        
        this.database = new Database(this);
        
        if (!database.connect()) {
            getLogger().severe("Failed to connect to database! Disabling plugin...");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
        
        database.createTables();
        
        Bukkit.getPluginManager().registerEvents(new HeartBreakListener(this), this);
        Bukkit.getPluginManager().registerEvents(new HeartPlaceListener(this), this);
        
        getLogger().info("GoscinakCore enabled successfully!");
    }

    @Override
    public void onDisable() {
        if (database != null) {
            database.disconnect();
        }
        getLogger().info("GoscinakCore disabled!");
    }

    public Database getDatabase() {
        return database;
    }
}