Giter Club home page Giter Club logo

signgui's Introduction

SignGUI Maven Central Javadoc

An api to get input text via a sign in Minecraft.
The api supports the Minecraft versions from 1.8 to 1.20.4.

Integration

Maven dependency:

<dependency>
    <groupId>io.github.rapha149.signgui</groupId>
    <artifactId>signgui</artifactId>
    <version>2.2.2</version>
</dependency>

Usage

To open a sign editor gui for a player, do the following:

SignGUI gui = SignGUI.builder()
    // set lines
    .setLines("§6Line 1", null, "§6Line 3")

    // set specific line, starting index is 0
    .setLine(3, "Line 4")

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

    // set the sign color
    .setColor(DyeColor.YELLOW)

    // 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);

        // get a specific line without color codes
        String line1 = result.getLineWithoutColor(1);

        // get all lines
        String[] lines = result.getLines();

        // get all lines without color codes
        String[] linesWithoutColor = result.getLinesWithoutColor();

        if (line1.isEmpty()) {
            // The user has not entered anything on line 2, so we open the sign again
            return List.of(SignGUIAction.displayNewLines("§6Line 1", null, "§6Line 3", "Line 4"));
        }

        if (line1.equals("inv")) {
            // close the sign and open an inventory
            return List.of(
                // "this" = your JavaPlugin instance
                SignGUIAction.openInventory(this, Bukkit.createInventory(player, 27)),
                SignGUIAction.run(() -> player.sendMessage("Inventory opened!"))
            );
        }

        // Just close the sign by not returning any actions
        return Collections.emptyList();
    })

    // build the SignGUI
    .build();

// open the sign
gui.open(player);

// you can also open the sign for multiple players
gui.open(player2);

You don't have to call all methods. Only setHandler is mandatory.

By default, the handler is called by an asynchronous thread. You can change that behaviour by calling the method callHandlerSynchronously of the builder. An explanation for the different methods can be found on the Javadoc.

Limitations

The location of the sign

Especially in newer versions, I encountered the problem that the sign had to be near the player in order to edit it.
In older versions it worked by placing the sign at the bottom of the world but that does not seem to be the case anymore, in my tests anyway.

Because of that the default location is three blocks behind the player (three blocks in the opposite view direction). The only problem with this is that if you tell the api to redisplay the sign if the player e.g. typed something wrong, the player could slowly turn and then see the sign because there is a slight delay before the gui opens again and in that time the player can turn around a little bit.
You are, naturally, free to experiment with the location of the sign yourself.

Of course the sign is not really placed, it's just send to the player, so other players won't see it.

Opening a sign after a player joins

Since the sign is not actually placed on the server, it can get overwritten when the chunks are sent to the player, which is the case when the player joins.

Because of that you may encounter the problem that the sign does not display any text when you send it to the player directly when he joins.
But even with a 20 tick (1 second) delay after the PlayerJoinEvent, the sign in my tests was empty sometimes.
I would recommend waiting at least a few more seconds before opening the gui.

Credits

This project's structure was inspired by WesJD's AnvilGUI and I used some code from Cleymax's SignGUI.

signgui's People

Contributors

rapha149 avatar cyr1en avatar ebicep avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.