Skip to content

Examples

n-tdi edited this page Feb 21, 2023 · 1 revision

This wiki page assumes you have an understanding of all the other pages. If you do not, read those first.

Creating a basic Spigot Plugin that uses Postglam

main.java

public final class Main extends JavaPlugin implements Listener {
    private Database db = ...;
    private Table stats;

    @Override
    public onEnable() {
        db.connect();

        String tableName = "stats"; // Table name
        Map.Entry<String, Datatypes> primaryKey = Map.entry("id", Datatypes.UUID); // Id column of the table

        LinkedHashMap<String, Datatypes> keys = new LinkedHashMap<>(); // Hashmap of the rest of the columns
        keys.put("username", Datatypes.TEXT);
        keys.put("level", Datatypes.INTEGER);

        stats = new Table(tableName, primaryKey, keys); // Initialize table

        getPluginManager.registerEvents(this, this); // Register main class as listener
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        UUID uuid = p.getUniqueId();
        String username = e.getName();

        if (!table.doesRowExist(uuid.toString)) { // Make sure player already isn't in the database
            table.insert(uuid.toString, username, "1"); // Initialize the player into the table
        }

        getLogger().info(username + " has a level of " + new Row(table, uuid.toString()).fetch(new Column(table, "level"))); // Print out the level of the player; E.g. "Ntdi has a level of 1"
    }
}

Super simple! We're using java because we love verbose languages, so the syntax is easy to read and understand!

I hope this helped give you an understanding of Postglam, but it can go far beyond just simple tables.

If you have any questions, make an issue or use the discussion page of this project!

Clone this wiki locally