Code Monkey home page Code Monkey logo

spigot-lite-repositories's Introduction

spigot-lite-repositories

Maven Central

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

Buy Me a Coffee at ko-fi.com

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.

spigot-lite-repositories's People

Contributors

expdev07 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mcmdev

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.