Skip to content
Z4OLLIEZ4 edited this page Mar 18, 2024 · 25 revisions

The Interface4 API is used as a general utility for all of my plugins. If you'd like to implement it in your own plugin, please see the table of contents below.

Table of Contents

ActionBar

Sending ActionBar based messages simply takes a Player and String argument

ActionBar.send(Player player, String message);

Alternatively, ActionBar messages can take any amount of BaseComponent arguments, to allow for different formatting and fonts in the same ActionBar.

ActionBar.send(Player player, BaseComponent... components);

JSON

The JSON utility is responsible for sending the player automatically formatted and joined, version-proof JSON messages in chat.

JSON.send(Player player, JSONComponent... components);
JSON.send(Player player, ArrayList<JSONComponent> components);

It is also possible to provide a CommandSender argument, where it will return only the display text of the joined components provided the CommandSender is not an instance of the Player class.

JSON.send(CommandSender sender, JSONComponent... components);
JSON.send(CommandSender sender, ArrayList<JSONComponent> components);

JSON Example

JTextComponent text = new JTextComponent("This is raw text with a hover", "this is the hover text");
JWebComponent webText = new JWebComponent("This will open a website when clicked", "this is the hover text", "https://www.google.com");
JCommandComponent commandText = new JCommandComponent("This will run a command when clicked", "this is the hover text", "this is the command");
JSON.send(player, text, webText, commandText); // Unlimited components can be supplied and will be automagically appended

JSONComponent

JClipboardComponent

Accepts three String parameters.

  • display
    • The text to display.
    • Cannot be null.
  • hover
    • The text to display on hover.
    • Can be null.
  • clipboard
    • The text to copy to the clipboard on-click.
    • Can be null.

JPromptComponent

Accepts three String parameters.

  • display
    • The text to display.
    • Cannot be null.
  • hover
    • The text to display on hover.
    • Can be null.
  • prompt
    • The prompt to add to the textbox on-click.
    • Can be null.

JCommandComponent

Accepts three String parameters.

  • display
    • The text to display.
    • Cannot be null.
  • hover
    • The text to display on hover.
    • Can be null.
  • command
    • The command to run on-click.
    • Can be null.

JWebComponent

Accepts three String parameters.

  • display
    • The text to display.
    • Cannot be null.
  • hover
    • The text to display on hover.
    • Can be null.
  • website
    • The website URL to open on-click.
    • Can be null.

JChatComponent

Accepts three String parameters.

  • display
    • The text to display.
    • Cannot be null.
  • hover
    • The text to display on hover.
    • Can be null.
  • text
    • The text to chat on click
    • Can be null.

JTextComponent

Accepts two String parameters.

  • display
    • The text to display.
    • Cannot be null.
  • hover
    • The text to display on hover.
    • Can be null.

XMaterial

XMaterial is not my work, though is included for multi-version material support in my plugins.

PastebinUtils

PastebinUtils makes creating pastes on pastebin easy. The documentation on the required arguments is here.

The method will return a String, which is the link to the generated paste.

  • pasteName, pasteFormat and expiry can be null.
  • If expiry is null, the post will never expire
  • devKey and content must not be null
PastebinUtils.createPaste(String devKey, String pasteName, String pasteFormat, String expiry, String content);

ZipUtils

ZipUtils compresses an array of file names (All must be residing in the plugin data folder) into a zip file that will be formatted into it's own "backups" folder to aid in integrating backup functions and utilities into your plugins. All files provided must contain their suffix (.yml, .schem, etc)

ZipUtils.createBackup(JavaPlugin plugin, CommandSender sender, String prefix, ArrayList<String> files);
ZipUtils.createBackup(JavaPlugin main, ArrayList<String> files);
ZipUtils.createBackup(JavaPlugin main, String file); ZipUtils.createBackup(JavaPlugin main, CommandSender sender, String prefix, String file);

InventoryUtils

At this time, InventoryUtils simply can check whether a player has a slot free in their inventory or not.

  • Will return a boolean value, true or false
InventoryUtils.isSlotFree(Player player);

FakeBlock

FakeBlock acts as a general interface into providing management for "ghost blocks" (Client-side blocks).

// Create a fake block
FakeBlock block = new FakeBlock(location, material);
// Send the block to a player
block.addPlayer(player);
// Reset the block to its server-side counterpart
block.removePlayer(player);
// Get the location of the block
block.getLocation();

CustomBlock

CustomBlock is a simple class to be extended upon that only has two default functions, and should only be used as an interface.

// Sets the block to a barrier
CustomBlock#setBarrier(JavaPlugin main, Block block);
// Sets the block above the block to a barrier
CustomBlock#setAboveBarrier(JavaPlugin main, Block block)

The remaining functions in this class as interface-only and you will need to integrate your own methods, as well as call them at appropriate times. This is just a template.

ParticleUtils

ParticleUtils is capable of drawing static or moving lines between two locations, with a provided frequency (How many per block) and time to move between the two locations (If relevant)

ParticleUtils.drawLine(Location loc1, Location loc2, double frequency, Particle particle);
ParticleUtils.drawMovingLine(Location loc1, Location loc2, double time, double frequency, Particle particle, JavaPlugin plugin);
ParticleUtils.drawEffect(Location loc1, Location loc2, double frequency, Effect effect);
ParticleUtils.drawMovingEffect(Location loc1, Location loc2, double time, double frequency, Effect effect, JavaPlugin plugin);
ParticleUtils.drawLine(Location loc1, Location loc2, double frequency, Particle particle, DustOptions dustOptions);
ParticleUtils.drawMovingLine(Location loc1, Location loc2, double time, double frequency, Particle particle, JavaPlugin plugin, DustOptions dustOptions);

PluginUtils

PluginUtils has two purposes:

  • To be able to get a JavaPlugin instance of a plugin by plugin name
  • To be able to detect server reloads
PluginUtils.getMain(String plugin) plugin);
PluginUtils.isReloaded();

VersionUtils

VersionUtils is an easy way to see whether or not a plugin is using its latest version on Spigot, getting the latest version posted on Spigot, and updating the plugin (provided it is a free resource).

VersionUtils.isLatest(Plugin plugin, int projectID);
VersionUtils.getLatest(Plugin plugin, int projectID);
VersionUtils.isLatestByURL(Plugin plugin, String URL);
VersionUtils.getLatestByURL(Plugin plugin, String URL);

For auto-updating, it will return a Boolean value regarding the update status:

  • true - The plugin was successfully updated and the server must be restarted for changes to apply.
  • false - An error occurred while updating the plugin
  • null - The plugin is already running the latest version.
VersionUtils.updatePlugin(Plugin plugin, int projectID);
VersionUtils.updatePlugin(Plugin plugin, int projectID, String versionURL);

GuardianBeam

GuardianBeam is not my work, though is included for multi-version guardian beam support in my plugins.

ProtectionManager

ProtectionManager has three purposes:

  • To check whether a block can be placed.
  • To check whether a block can be interacted with.
  • To check whether a block can be broken. It is designed to hook into numerous other block protection plugins automatically (Even if they do not depend on Interface4) in order to create an easy way to perform these operations without huge amounts of dependencies and checks.
boolean canBreak = ProtectionManager.canBreak(JavaPlugin plugin, Location location, Player player);
boolean canPlace = ProtectionManager.canPlace(JavaPlugin plugin, Location location, Player player);
boolean canInteract = ProtectionManager.canInteract(JavaPlugin plugin, Location location, Player player);

A full list of integrated plugins is below:

  • Towny
  • WorldGuard
  • Residence
  • Lands
  • GriefPrevention
  • FactionsUUID

PlayerDoubleSneakEvent

Triggered when a player double-taps sneak twice within 400ms of each other. It is non-cancellable.

  • getPlayer() returns a Player object of the user who double-tapped sneak
  • getDelay() returns a double of the ms between sneaks

DirectablePlayerScrollEvent

Triggered when the player uses the scroll wheel. It is simply used to make detecting the direction of scrolling easier.

  • getPlayer() returns a Player object of the user who scrolled.
  • isCancelled() returns a boolean of whether or not the event has been cancelled.
  • setCancelled(boolean isCancelled) allows you to set the event cancellation status
  • getDirection() returns an enum ScrollDirection

ScrollDirection

This is a simple enum used to decide the direction of motion of the scroll wheel in the DirectablePlayerScrollEvent.

It contains two values:

  • ScrollDirection.RIGHT
  • ScrollDirection.LEFT

DebugMarker

DebugMarker is not my work, though is included for multi-version DebugMarker support in my plugins.

GUI

The GUI class is a simple inventory creator that automatically registers and dispatches of the necessary listeners. It is capable of storing multiple pages, having close actions and assigning actions to ItemStack's.

It is also handy in creating GUI borders and backgrounds which stay correct to the provided GUI size, and when the size provided is 5, the GUI type will be automatically turned into a hopper.

If multiple players are sent the same GUI (through the use of GUI#addPlayer(Player player)), then all players seeing the GUI will see live changes which others make.

In order to construct a new GUI:

GUI gui = new GUI(JavaPlugin plugin, int size, String title) // Creates a new GUI object
GUI gui = new GUI(JavaPlugin plugin, int size, String title, Player... players) // Creates a new GUI object, and opens it to any provided players

Internal methods provided:

GUI#setTitle(String title); // Will change the title of the GUI. This does not require players to re-open the GUI to take effect.
GUI#getTitle(); // Will return a String of the GUI title
GUI#setBorder(ItemStack item, BorderStyle style); // Will apply a border to the GUI of the item provided, in the style provided
GUI#setBackground(ItemStack item); // Will set all empty slots in the GUI to the item provided
GUI#addPlayer(Player player); // Will open the GUI for the provided player
GUI#removePlayer(Player player); // Will remove the GUI for the provided player
GUI#getSize(); // Will return an int value of the GUI size provided
GUI#getPlayers(); // Will return an ArrayList<Player> object of the players who currently have the GUI open
GUI#getRawInventory(); // Will return the Bukkit Inventory object of the GUI
GUI#getPlugin(); // Will return the JavaPlugin object to the GUI
GUI#isPaged(); // Will return a boolean value, true if the GUI is paged, false otherwise.
GUI#setPaged(); // Will make the GUI paged
GUI#getMaxPages(); // Returns the int value of the max pages the GUI has
GUI#setMaxPages(int maxPages); // Sets the max pages the GUI has. GUI#setPaged() must also be run for this to have an effect.
GUI#setPage(int page); // Sets the current page value
GUI#clear(); // Clears all items in the GUI and removes the function of their clickable functions provided.
GUI#isLocked(); // Will return a boolean value, true if the GUI is locked, false otherwise.
GUI#setLocked(boolean locked); // Will lock the GUI, meaning players cannot input or remove items from it in any way.
GUI#isDragLocked(); // Will return a boolean value, true if the GUI cannot have items dragged within, false otherwise. Defaults to true
GUI#setDragLocked(boolean dragLock); // Sets the drag lock status

// Sets the function which will fire when the GUI is closed.
GUI#setCloseFunction((InventoryCloseEvent event) -> {
  
});

// Sets the item to the provided slot and assigns a click function to it. This can be null to give it no function. 
GUI#setItem(int slot, ItemStack item, (InventoryClickEvent event) -> {
  
});

// Sets the item to the first available slot and assigns a click function to it. This can be null to give it no function.
GUI#addItem(ItemStack item, (InventoryClickEvent event) -> {
  
}); 

// Sets the function which will fire when the GUI has items dragged within.
GUI#addDragAction(InventoryDragEvent event) -> {
  
}); 

// Sets the function which will fire when the GUI is clicked.
GUI#addClickAction(InventoryClickEvent event) -> {
  
}); 

GUI Example

// Create a new GUI of size 45 (5 rows)
GUI myCustomGUI = new GUI(JavaPlugin plugin, 45, Player player);
// Set the full outer border of the GUI to redstone blocks
myCustomGUI.setBorder(new ItemStack(Material.REDSTONE_BLOCK), BorderStyle.FULL);
// Sets the full background of the GUI to diamond blocks
myCustomGUI.setBackground(new ItemStack(Material.DIAMOND_BLOCK));
// Sets the item in slot 25 to a stone block, which when pressed sends the player a message
myCustomGUI.setItem(25, new ItemStack(Material.STONE), (InventoryClickEvent event) -> {
    event.getWhoClicked().sendMessage("You clicked the stone button");
    event.setCancelled(true);
});

BorderStyle

This is a simple enum used to decide how the border of a GUI is drawn.

  • FULL - Will surround the entire GUI
  • VERTICAL - Will fill the left and right columns of the GUI
  • HORIZONTAL - Will fill the top and bottom columns of the GUI
  • TOP_ONLY - Will only fill the top column of the GUI
  • BOTTOM_ONLY - Will only fill the bottom column of the GUI
  • LEFT_ONLY - Will only fill the left column of the GUI
  • RIGHT_ONLY - Will only fill the right column of the GUI

Vehicles

In order to create your own vehicle, it is first necessary to create a class extending Vehicle. This will be our base.

public class CustomVehicle extends Vehicle {

	public CustomVehicle() {
		super(double topSpeed, double accelerationMultiplier, boolean shouldRotate, boolean canReverse, boolean canStep, boolean canFly, boolean canMoveOnGround, boolean airtight, ItemStack stack);
	}

}

Here is a breakdown of the arguments:

Argument Data type Function
topSpeed double Dictates the maximum velocity vector the vehicle can reach
accelerationMultiplier double Dictates the acceleration the vehicle will use when being controlled (MUST BE GREATER THAN 1)
shouldRotate boolean Dictates whether the vehicle model should rotate with where the player looks
canReverse boolean Dictates whether the vehicle can move backwards
canStep boolean Dictates whether the vehicle should automatically jump up one-block spaces
canFly boolean Dictates whether the vehicle can fly whilst holding the spacebar
canMoveOnGround boolean Dictates whether the vehicle can move whilst on the ground
airtight boolean Dictates whether the player is safe from space exposure whilst using the vehicle
stack ItemStack The actual item the vehicle is. This is the model that will display and that players can place to get the vehicle

It is then important to register your newly created Vehicle with Interface4, so that it can actually have function and work within the Vehicle ecosystem. In order to do this, use the following method:

VehicleManager.registerVehicle(new CustomVehicle())

Where new CustomVehicle() is our Vehicle class we just created. This should only be accessed in a static way as shown above.

VehicleRideEvent

Triggered when the player sits on a vehicle.

  • getPlayer() returns the relevant Player object.
  • getVehicle() returns the relevant Vehicle object
  • isCancelled() returns a boolean of whether or not the event has been cancelled.
  • setCancelled(boolean isCancelled) allows you to set the event cancellation status

VehicleMoveEvent

Triggered when the player moves a vehicle.

  • getPlayer() returns the relevant Player object.
  • getVehicle() returns the relevant Vehicle object
  • isCancelled() returns a boolean of whether or not the event has been cancelled.
  • setCancelled(boolean isCancelled) allows you to set the event cancellation status

VehiclePlaceEvent

Triggered when the player places a vehicle.

  • getPlayer() returns the relevant Player object.
  • getVehicle() returns the relevant Vehicle object
  • isCancelled() returns a boolean of whether or not the event has been cancelled.
  • setCancelled(boolean isCancelled) allows you to set the event cancellation status

VehicleBreakEvent

Triggered when the player breaks a vehicle.

  • getPlayer() returns the relevant Player object.
  • getVehicle() returns the relevant Vehicle object
  • isCancelled() returns a boolean of whether or not the event has been cancelled.
  • setCancelled(boolean isCancelled) allows you to set the event cancellation status