Minecraft Plugin - Redstonecrafter0/RedstoneAPI GitHub Wiki
Intro
The jar is a full Spigot and Bungeecord plugin capable to provide the functionalities of the RedstoneAPI.
Install
To install the RedstoneAPI on your Spigot/Paper or Bungeecord/Waterfall server just drop the latest released jar in your plugins folder.
Use in your Plugin
To use the RedstoneAPI in your plugin just add the Maven dependency as described here and add it as dependency in your plugin.yml like this.
depend:
- RedstoneAPI
In case you make a Spigot and Bungeecord plugin put it also in your bungee.yml.
permissions
At the moment there is only one permission for both Spigot and Bungeecord.
- redstoneapi.admin.notifyupdate: Get notified when a newer version is available
Bungeecord
config
usermanager:
enabled: true # Enable or disable the usermanager
fetchonmissing: true # Get the user from the MojangAPI when the persistance is over or the player was not logged in before
persistance: 30 # persistance in days how long the stored playernames are valid
storage:
type: SQLITE # SQLITE or MYSQL | how to store the playernames and their uuids
mysql:
host: 'localhost'
port: 3306
database: 'redstoneapi'
username: 'redstoneapi'
password: 'redstoneapi'
sqlite:
name: 'redstoneapi.db' # filename
update:
notify:
console: true # Log on console when a new version is available
adminjoin: true # Send to players with the notify permissions that a new version is available on join
ipc:
enabled: false # Whether to enable the builtin IPCServer. If enabled make sure its protected by a firewall. Undocumented!
host: '127.0.0.1' # The hostname to listen for
port: 2000 # The server port
PluginBase
This is a more advanced base class for Bungeecord plugins.
public class TestPlugin extends BungeecordPlugin { ... }
Config
This base class provides a config.yml
by default.
Get
To get the config use this example code.
Configuration config = getConfig();
Load
Load / reload the config.
loadConfig();
saveDefaultConfig
Save the config stored in the resources
saveDefaultConfig("bungeeconfig.yml"); // the filename is the filename in the resources folder
Render Colors
Render the String to a colored String for Minecraft using the key '&'
String colored = renderColors("&cThis is red");
Register Permissions
Register permissions so permission managers like LuckPerms as able to autocomplete it.
registerPermissions("permission1.test", "permission2.admin", ...);
Register Commands
Register all commands at once in the simplest way possible.
registerCommands(command1, command2, ...);
Register Listeners
Register all listeners at once in the simplest way possible.
registerListeners(listener1, listener2, ...);
Send Console message
Send a message to the console
sendConsoleMessage("test");
Scheduler
Schedule some stuff
Task
Schedule a Task for later
schedule(() -> System.out.println("This is printed after 5 seconds"), 5, TimeUnit.SECONDS);
TimerTask
Schedule a Task to be run every n units.
schedule(() -> System.out.println("This is printed every 5 seconds beginning after 15 seconds"), 15, 5, TimeUnit.SECONDS);
Get the instance
To get the plugin instance use this example code.
RedstoneAPIBungee instance = RedstoneAPIBungee.getInstance();
UserManager
Since Bungeecord does not provide a native ability for offline user management the RedstoneAPI implements this.
Get the instance
To get the instance of the UserManager you can use this example code.
UserManager userManager = RedstoneAPIBungee.getInstance().getUserManager();
The User object
The User object provides some basic features and can provide the ProxiedPlayer if online.
getUniqueId
Get the UUID of the User
UUID uuid = user.getUniqueId();
getPlayerName
Does what it says. Get the playername
String playername = user.getPlayerName();
isOnline
Returns true if the player is online.
boolean online = user.isOnline();
getOnlinePlayer
Get the ProxiedPlayer instance. Returns 'null' if the player is offline.
ProxiedPlayer player = user.getOnlinePlayer();
Use the UserManager
You can user the UserManager to get User objects or check them.
Get a User
Get a User instance by one of the following options
By name
Get the User by his name
User user = RedstoneAPIBungee.getInstance().getUserManager().getUser("playername");
By UUID
Get the User by his UUID
User user = RedstoneAPIBungee.getInstance().getUserManager().getUser(UUID.fromString("00000000-0000-0000-0000-000000000000"));
By ProxiedPlayer
Get the user by a ProxiedPlayer instance
ProxiedPlayer player = ProxyServer.getInstance.getPlayer("playername");
User user = RedstoneAPIBungee.getInstance().getUserManager().getUser(player);
By PendingConnection
The PendingConnection is for provided by the PreLoginEvent
User user = RedstoneAPIBungee.getInstance().getUserManager().getUser(pendingConnection);
isRegistered
Check if the player if already registered / known in the database. Since players connecting to the server are automatically stored in the database the check is useless for ProxiedPlayer and PendingConnection.
By Name
Returns true if the name is already in the database.
boolean registered = RedstoneAPIBungee.getInstance().getUserManager().isRegistered("playername");
By UUID
Returns true if the UUID is already in the database.
boolean registered = RedstoneAPIBungee.getInstance().getUserManager().isRegistered(UUID.fromString("00000000-0000-0000-0000-000000000000"));
By User
Because it is possible to create a User instance by the contructor this check is possible but propably useless. The check is done using the UUID. Use it or ignore it.
User user = RedstoneAPIBungee.getInstance().getUserManager().getUser("playername");
boolean registered = RedstoneAPIBungee.getInstance().getUserManager().isRegistered(user);
updateUser
If the UUID doesn't exist already a new User will be stored in the database else the playername will be updated.
RedstoneAPIBungee.getInstance().getUserManager().updateUser(UUID.fromString("00000000-0000-0000-0000-000000000000"), "playername");
Settings
Fetch on missing
Whether fetching a User from the MojangAPI is enabled if the User is missing or the name is outdated or not.
Get
boolean enabled = RedstoneAPIBungee.getInstance().getUserManager().isFetchOnMissing();
Set
RedstoneAPIBungee.getInstance().getUserManager().setFetchOnMissing(true);
Persistance
The amount of milliseconds the username is valid if fetchOnMissing is enabled.
Get
long millis = RedstoneAPIBungee.getInstance().getUserManager().getPersistance();
Set
RedstoneAPIBungee.getInstance().getUserManager().setPersistance(2592000000);
Spigot
PluginBase
This is a advanced Plugin base for Spigot plugins.
public class TestPlugin extends SpigotPlugin {
@Override
public void onEnabled() { ... } // Notice the methods name ends with a 'd'
@Override
public void onServerLoaded() { ... } // This is executed after the Server completed loading
}
Register Listener
Register multiple Listener easily.
registerListeners(listener1, listener2);
Render Colors
Color a String by the key '&'
renderColors("&cThis is red.");
Events
PlayerJumpEvent
Since this event is missing on Spigot the RedstoneAPI adds it. The event is fired when a Player jumps.
@EventHandler
public void onJump(PlayerJumpEvent event) { ... }
getPlayer
Get the player that jumped.
Player player = event.getPlayer();
getFrom
The Location of the Player before the jump.
Location from = event.getFrom();
getTo
The Location of the Player after the jump (in air).
Location to = event.getTo();
InventoryHelper
The InventoryHelper can store inventories, itemstacks and itemmeta in json objects and restore it.
ItemMeta
Serializing is easy but deserializing must be explained. To deserialize you have to first get the ItemMeta from an ItemStack, deserialize and set the ItemMeta again.
Serialize
JSONObject obj = InventoryHelper.serializeItemMeta(itemMeta);
Deserialize
ItemStack itemStack;
JSONObject obj;
ItemMeta itemMeta = itemStack.getItemMeta();
InventoryHelper.deserializeItemMeta(data, itemMeta);
ItemStack
Serialize
JSONObject obj = InventoryHelper.serializeItemStack(itemStack);
Deserialize
ItemStack itemStack = InventoryHelper.deserializeItemStack(obj);
Inventory
Use this only for custom inventories without owners.
Serialize
JSONObject obj = InventoryHelper.serializeInventory(inventory);
Deserialize
Inventory inventory = InventoryHelper.deserializeInventory(obj);
GuiInventory
There is a wrapper for using inventories with items to create a clickable GUI.
Create
// title |rows| on open | on close
GuiInventory gui = new GuiInventory("title", 6, (event) -> {}, (event) -> {});
// OnOpen and OnClose could be used for loading and storing settings.
// You can set onOpen and onClose later also.
gui.setOnGuiOpen((event) -> {});
gui.setOnGuiClose((event) -> {});
int size = gui.size();
String title = gui.getTitle();
// Get open inventories for players. This is needed so the different options are different for different players.
Map<Player, Inventory> inventories = gui.getInventories();
// Get open inventory of player.
Inventory inventory = gui.getFor(player);
// Open the inventory for a player.
gui.openFor(player);
// Example for a switch with items diamond on and emerald off. Can be further customized with lores etc.
Map<Boolean, ItemStack> states = new HashMap<>();
states.put(true, new ItemStack(Material.DIAMOND));
states.put(false, new ItemStack(Material.EMERALD));
GuiOptionStates guiOptionStates = new GuiOptionStates(states, false); // The second argument must be type of the maps key and is the default state that must be in the states map.
// Set options with items and corresponding states.
gui.setOption(new GuiInventory.Option(0, 0, ), guiOptionStates); // Set clickevent for topleft item.
Register
On its own the GuiInventory won't do much so it has to be known to the GuiInventoryManager which takes care of firing events etc.
// Get the instance.
GuiInventoryManager manager = RedstoneAPISpigot.getInstance().getInventoryManager();
// Add GuiInventory
manager.addInventory(guiInventory);
// Or remove GuiInventory
manager.removeInventory(guiInventory);