Skip to content

Simple but useful generic reload library for Java projects.

License

Notifications You must be signed in to change notification settings

Ghost-chu/SimpleReloadLib

Repository files navigation

SimpleReloadLib

Simple Java generic reload library.

Introduce

SimpleReloadLib isvused to be a part of QuickShop-Reremake. But it is really easy to use and powerful, so I made it to a standalone lib.
You can insert this to any Java project including Bukkit, Paper, BungeeCord, Forge, Fabric etc.
This lib is already used in QuickShop-Reremake over 1 year and no errors were found.

Features

  • Reloading mechanism based on registration order, no need to worry about reload order。
  • Simple and easy to use, just need to register with eyes closed.
  • Lightweight, powered by 5 java classes. You can package it into any project.
  • WeakReference to save your memory and prevent memory leaking.
  • Support both object and static method.

How to use

  1. Create a Reload Manager
ReloadManager reloadManager = new ReloadManager();
  1. Implement Reloadable and register it into ReloadManager (or static method if register for static util)
public class Example implements Reloadable {
    public Example(){
        Instance.getReloadManager().register(this);
    }
    
    @Override
    public ReloadResult reloadModule() throws Exception {
        try{
            // Reload code here
             return ReloadResult.builder().status(ReloadStatus.SUCCESS).build();
        } catch (IllegalStateException scheduleException) {
              return ReloadResult.builder().status(ReloadStatus.SCHEDULED).reason("Resource in use").build();
        } catch (RuntimeException requireRestartException) {
              return ReloadResult.builder().status(ReloadStatus.REQUIRE_RESTART).reason("Restart required").build();
        } catch (Exception otherException){
              return ReloadResult.builder().status(ReloadStatus.EXCEPTION).exception(otherException).reason("Unkown error raised").build();
        }
        // If there have any Exception not be catched, Manager will catch it and report with ReloadStatus.EXCEPTION
    }
}
  1. Reload it!
Map<ReloadableContainer, ReloadResult> results = reloadManager.reload();
Map<ReloadableContainer, ReloadResult> results = reloadManager.reload(Example.class);

Registerable reloadables

  • Any classes that implement Reloadable
  • Any no args and returns ReloadResult static Method

Get summary

SimpleReloadLib offers a Map contains ReloadContainer and ReloadResults:

  • SUCCESS - Successfully to reloading.
  • OUTDATED - WeakReferenced object already invalid and will be removed in next reload.
  • REQUIRE_RESTART - Reload is impossible, restart required.
  • SCHEDULED - Cannot reload in this time but already scheduled reloading if possible.
  • EXCEPTION - Something just exploded

Maven

We're in central.

    <dependencies>
        <dependency>
            <groupId>com.ghostchu</groupId>
            <artifactId>simplereloadlib</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>

License

MIT