spigot-lite-repositories

💼 An easy way to store objects in your spigot plugins!

MIT License

Stars
10
Committers
1

spigot-lite-repositories

Tired of creating your own systems for saving data/objects such as players in your plugins? This library has got you covered. Simply add this library in your plugin, and you're good to go!

Support me

Installation

Installing and using this library is very easy, simply include the maven dependency and add the shade plugin. Alternatively, you can also download the jar from maven.org.

Including dependency

<dependency>
    <groupId>com.github.expdev07</groupId>
    <artifactId>spigot-lite-repositories</artifactId>
    <version>1.0.0</version>
</dependency>

Shading

To use the library, you'll have to include it in your final plugin jar. This can be achieved using shading.

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Building

Now build your plugin with mvn clean package.

Usage

At the moment, only storing objects in JSON is supported. Look at an example below for storing a custom user.

The object

Every object that you store should implement Identifiable<ID>. This gives the object an id which it can be identified by. ID is the type of id and can be e.g Integer, UUID, String, etc. In the example below, our CustomPlayer uses UUID for its id.

public class CustomPlayer implements Identifiable<UUID>
{
    
    private UUID id;
    private String name;
    private int deaths;
    
    public CustomPlayer(Player player)
    {
        this.id = player.getUniqueId();
        this.name = player.getName();
        this.deaths = 0;
    }
    
    /**
    * Gets the spigot player associated with this player.
    * 
    * @return The spigot player.
    */
    public Player getSpigotPlayer()
    {
        return Bukkit.getPlayer(this.id);
    }
    
    /* SETTERS & GETTERS */

}

Repositories

Objects are saved and retrieved through something called repositories. Each top-level object you want to save should have their own repository. In this example, we'll save each CustomPlayer to their own .json files in a players directory. The implementation is the following.

public class CustomPlayerRepository extends JsonRepository<UUID, CustomPlayer>
{
    
    public CustomPlayerRepository(Plugin plugin)
    {
        super(plugin, CustomPlayer.class, "players");
    }

    /**
     * Finds a player by their spigot player instance.
     *
     * @param player The spigot player.
     * @return The player.
     */
    public CustomPlayer findByPlayer(Player player)
    {
        return this.find(player.getUniqueId());
    }

}

Using the repository

public class MyPlugin extends JavaPlugin
{
    
    /**
    * The custom players repository.
    */
    private CustomPlayerRepository customPlayers;
    
    @Override
    public void onEnable()
    {
        instance = this;
        
        // Create repository.
        this.customPlayers = new CustomPlayerRepository(this);
    }
    
    /**
    * Testing of the repository!
    */
    public void test()
    {
        // Create a new custom player!
        CustomPlayer player = new CustomPlayer(Bukkit.getPlayer("ExpDev07"));
        player.setDeaths(10);
        
        // Save it (returns the object that was saved).
        CustomPlayer saved = this.customPlayers.save(player);
        
        // Retrieving it by player.
        CustomPlayer retrieved = this.customPlayers.findByPlayer(Bukkit.getPlayer("ExpDev07"));
        System.out.println(retrieved.getDeaths()); // = 10
    }
    
}

Calling save would result in the following file being created for the object (my-plugin/players/7d4a521e-41ed-492f-8e45-dee2e86f7c15.json).

{
  "id": "7d4a521e-41ed-492f-8e45-dee2e86f7c15",
  "name": "ExpDev07",
  "deaths": 10
}

Contributors

List of people who have made contributions to this project.

Package Rankings
Top 41.93% on Repo1.maven.org
Badges
Extracted from project README
Maven Central
Related Projects