Rapha149 / SignGUI

An api to get input text via a sign in Minecraft.
MIT License
35 stars 12 forks source link

No sign gui is being displayed for the player #10

Closed cmptrwhz closed 2 months ago

cmptrwhz commented 2 months ago

What are you trying to do? I have an inventory gui displayed to the player. within the inventory gui I have a sign for the user to click on. The purpose of the sign is to get the numerical amount the player wants to sell their item for.

What do you need help with? I am not getting any errors when the player clicks the sign but yet there is no sign being displayed.

Code snippets Java plugin main

package org.cmptrwhz.dezomarket;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.cmptrwhz.dezomarket.dezcommands.DezCommands;
import org.cmptrwhz.dezomarket.dezevents.DezEvents;
import org.cmptrwhz.dezomarket.dezsql.Database;

import java.sql.SQLException;
import java.util.PrimitiveIterator;

public final class DezoMarket extends JavaPlugin {

    FileConfiguration config = this.getConfig();
    private static DezoMarket instance;
    private Database database;

    @Override
    public void onEnable() {
        // Plugin startup logic
        instance = this;
        this.saveDefaultConfig();

        String sqlhost = config.getString("host");
        String sqlport = config.getString("port");
        String sqldatabase = config.getString("database");
        String sqlusername = config.getString("username");
        String sqlpassword = config.getString("password");

        database = new Database(sqlhost, sqlport, sqldatabase, sqlusername, sqlpassword);

        try {
            database.initializeDatabase();
            System.out.println("[DezoMarket]: Initialize database successful.");
        }catch (SQLException e){
            e.printStackTrace();
            System.out.println("[DezoMarket]: Could not initialize database.");
        }

        getServer().getPluginManager().registerEvents(new DezEvents(), this);
        DezCommands commands = new DezCommands();
        getCommand("dezomarket").setExecutor(commands);
        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DezoMarket]: Plugin is enabled");
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        instance = null;
        database.disconnect();
        getServer().getConsoleSender().sendMessage(ChatColor.RED + "[DezoMarket]: Plugin is disabled");
    }

    public static DezoMarket getInstance() {
        return instance;
    }

}

Registered events for when a user clicks on something in the inventory gui that is open.

package org.cmptrwhz.dezomarket.dezevents;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.cmptrwhz.dezomarket.gui.MainSelect;
import org.cmptrwhz.dezomarket.gui.GetSellingItm;
import org.cmptrwhz.dezomarket.gui.SignScreen;

public class DezEvents implements Listener {

    private GetSellingItm SellItemGui = new GetSellingItm();

    @EventHandler
    public void onClick(InventoryClickEvent e){
        if (e.getClickedInventory() == null) { return; }

        //GUI.MainSelect
        if (e.getClickedInventory().getHolder() instanceof MainSelect) {
            e.setCancelled(true);
            Player player = (Player) e.getWhoClicked();
            if (e.getCurrentItem() == null) { return; }
            if (e.getSlot() == 11){
                //selling an item, open screen to accept user item
                player.openInventory(SellItemGui.getInventory());
            }
            else if (e.getSlot() == 13){
                //claiming an item, open screen to show user items purchased
                player.closeInventory();
            }
            else if (e.getSlot() == 15){
                //managing items for sale, open screen to show user items they have for sale
                player.closeInventory();
            }
        }

        //GUI.GetSellingItm
        if (e.getClickedInventory().getHolder() instanceof GetSellingItm) {
            e.setCancelled(true);
            Player player = (Player) e.getWhoClicked();
            if (e.getCurrentItem() == null) { return; }
            if (e.getSlot() == 11 || e.getCurrentItem().getType() == Material.STONE_BUTTON){
                //take player item, save to yml, open GUI.GetSellingItm replace button with item
                player.closeInventory();
            } else if (e.getSlot() == 11 || e.getCurrentItem().getType() != Material.STONE_BUTTON) {
                //return item back to their inventory
                player.closeInventory();
            } else if (e.getSlot() == 13){
                //get amount from player
                player.closeInventory();
                SignScreen signScreen = new SignScreen(player);
                signScreen.MakeSign();
            }
            else if (e.getSlot() == 15){
                //save transaction to mysql, clean up player item yml file, return to mainselect
                player.closeInventory();
            }
        }

        //GUI.CLAIMSCREEN

        //GUI.MANAGESCREEN

    }

}

Sign creation class

package org.cmptrwhz.dezomarket.gui;

import de.rapha149.signgui.SignGUI;
import de.rapha149.signgui.SignGUIAction;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.cmptrwhz.dezomarket.DezoMarket;

import java.util.Arrays;
import java.util.Collections;

public class SignScreen {

    private GetSellingItm SellItemGui = new GetSellingItm();
    private Player player;

    public SignScreen(Player p){
        this.player = p;
    }

    public void MakeSign () {
        //https://github.com/Rapha149/SignGUI
        SignGUI signGUI = SignGUI.builder()
                // set lines
                .setLines(null, "§6Enter Selling Amount", "§6--------------", "§6-------")

                // set the sign type
                .setType(Material.SPRUCE_SIGN)

                // set the handler/listener (called when the player finishes editing)
                .setHandler((p, result) -> {
                    // get a speficic line, starting index is 0
                    String line0 = result.getLine(0);

                    if (isNumeric(line0)) {
                        // close the sign and open an inventory
                        return Arrays.asList(
                                SignGUIAction.openInventory(DezoMarket.getInstance(), SellItemGui.getInventory()),
                                SignGUIAction.run(() -> player.sendMessage("Selling Item for: " + line0))
                        );
                    }
                    // Just close the sign by not returning any actions
                    return Collections.emptyList();
                })

                // build the SignGUI
                .build();

        // open the sign
        signGUI.open(player);
    }

    private static boolean isNumeric(String strNum) {
        if (strNum == null) {
            return false;
        }
        try {
            Integer i = Integer.parseInt(strNum);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
}

Error messages No errors are being reported during compile, none on player screen, and no errors in console.

Additional context Add any other context about the problem here.

Rapha149 commented 2 months ago

Does it work when you call signScreen.MakeSign() from somewhere else? For example try to do it when the player executes a command.

If it works then, you could try to wait a tick before opening the sign by running a synchronized task:

player.closeInventory();
Bukkit.getScheduler().schedule(DezoMarket.getInstance(), () -> {
    SignScreen signScreen = new SignScreen();
    signScreen.MakeSign();
});

For me it always worked when I called it directly from an InventoryClickEvent but maybe you're on another server version / software and that's because it doesn't.

Btw: this has nothing to do with your problem, but you maybe should inform yourself about Java naming conventions: variable and method names should always be lowerCamelCase and not UpperCamelCase.

cmptrwhz commented 2 months ago

Thank you