Remove a item when a player clicks in a chest

Tireyy

New Member
Hello i have a question

How do i remove the golden ingot from the chest when a player clicks on it?


Code:
package com.svenkoggel.bhbedrock;

import cn.nukkit.Player;
import cn.nukkit.blockentity.BlockEntityChest;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.Listener;
import cn.nukkit.event.inventory.InventoryTransactionEvent;
import cn.nukkit.inventory.transaction.action.InventoryAction;
import cn.nukkit.inventory.transaction.action.SlotChangeAction;
import cn.nukkit.item.Item;
import cn.nukkit.utils.Config;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GoldItemClickListener implements Listener {

    private final Main plugin;

    public GoldItemClickListener(Main plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onGoldPickupFromChest(InventoryTransactionEvent event) {
        for (InventoryAction action : event.getTransaction().getActions()) {
            if (action instanceof SlotChangeAction) {
                SlotChangeAction slotAction = (SlotChangeAction) action;
                Item targetItem = slotAction.getTargetItem();

                // Controleer of het een "§650 Gold" Goudstaaf is
                if (targetItem.getId() == Item.GOLD_INGOT &&
                        targetItem.hasCustomName() &&
                        targetItem.getCustomName().equals("§650 Gold")) {

                    Player player = event.getTransaction().getSource();
                    // Ensure the holder is an instance of BlockEntityChest before casting
                    if (slotAction.getInventory().getHolder() instanceof BlockEntityChest) {
                        BlockEntityChest chestEntity = (BlockEntityChest) slotAction.getInventory().getHolder();

                        // Voeg 50 gold toe aan de speler
                        boolean useMySQL = plugin.getConfig().getBoolean("mysql", false);
                        if (useMySQL) {
                            addGoldToMySQL(player, 50);
                        } else {
                            addGoldToYML(player, 50);
                        }

                        String goldMessage = plugin.getConfig().getString("gold-message", "§1+§650 gold!");

                        // Verzend de gold-message naar de speler
                        player.sendMessage(goldMessage);

                        // Annuleer de transactie zodat het item niet opnieuw in de inventaris van de kist verschijnt
                        event.setCancelled(true);
                        break;
                    }
                }
            }
        }
    }


    private void addGoldToYML(Player player, int amount) {
        File statsFile = new File(plugin.getDataFolder(), "playerstatistics.yml");
        Config config = new Config(statsFile, Config.YAML);

        String uuid = player.getUniqueId().toString();
        int currentGold = config.getInt("players." + uuid + ".gold", 0);
        config.set("players." + uuid + ".gold", currentGold + amount);
        config.save();
    }

    private void addGoldToMySQL(Player player, int amount) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = MySQL.getConnection(
                    plugin.getConfig().getString("adress", "localhost"),
                    plugin.getConfig().getString("port", "3306"),
                    plugin.getConfig().getString("database", "DB"),
                    plugin.getConfig().getString("username", "root"),
                    plugin.getConfig().getString("password", "")
            );

            String uuid = player.getUniqueId().toString();
            String name = player.getName();

            // Controleer of speler al bestaat
            stmt = conn.prepareStatement("SELECT COUNT(*) FROM bountyhunterstatistics WHERE uuid = ?");
            stmt.setString(1, uuid);
            ResultSet rs = stmt.executeQuery();
            if (rs.next() && rs.getInt(1) == 0) {
                stmt = conn.prepareStatement("INSERT INTO bountyhunterstatistics (uuid, playername, kills, deaths, gold) VALUES (?, ?, 0, 0, 0)");
                stmt.setString(1, uuid);
                stmt.setString(2, name);
                stmt.executeUpdate();
            }

            // Voeg gold toe
            stmt = conn.prepareStatement("UPDATE bountyhunterstatistics SET gold = gold + ? WHERE uuid = ?");
            stmt.setInt(1, amount);
            stmt.setString(2, uuid);
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            plugin.getLogger().error("Failed to update gold in MySQL.");
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
Thanks
 
Top