Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Basic tutorial

filoghost edited this page Jul 26, 2014 · 47 revisions

This basic tutorial will explain you how to check that HolographicDisplays is enabled (if stricly required) and how to create your first hologram.

 

1) Always use the latest version

First of all, use the latest version of HolographicDisplays while coding and ask your plugin's users to do the same. The API is constantly being expanded, and some methods could be missing in previous versions.

2) Check if HolographicDisplays is enabled

To use the API, you have to check if the plugin is installed and enabled. You can use the following code in your plugin.yml and in your onEnable() to check that the plugin is installed and enabled.

 

Add a soft-dependency to your plugin.yml:

softdepend: [HolographicDisplays]

If you use depend in place of softdepend your users will see an error on the console when the server starts if HolographicDisplays is not enabled, and your onEnable() will not be called. I personally prefer to use softdepend because you can customize the error message.

 

If HolographicDisplays is strictly required, you can use the following code:

@Override
public void onEnable() {

	if (!Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays")) {
		getLogger().severe("*** HolographicDisplays is not installed or not enabled. ***");
		getLogger().severe("*** This plugin will be disabled. ***");
		this.setEnabled(false);
		return;
	}
	
	// Rest of the code
}

 

If it's not strictly required, you can just check if the plugin is enabled, and use a field in your plugin to see if you should make calls to the API:

private boolean useHolographicDisplays;

@Override
public void onEnable() {
	useHolographicDisplays = Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays");

	// Rest of the code
}

 

3) Start using the API

The API is a class called HolographicDisplaysAPI, all the methods are static. With the method createHologram(...) you can create a hologram with some lines. The method returns the hologram just created. Example:

// Suppose that "player" is a Player object obtained before (e.g. in a command), and "plugin" is your plugin's instance.
Hologram hologram = HolographicDisplaysAPI.createHologram(plugin, player.getEyeLocation(), "Test hologram");

Done! With just one line of code you created a hologram.

 

If you want to add more lines when creating the hologram, add them at the end of the method as much as you want:

HolographicDisplaysAPI.createHologram(plugin, player.getEyeLocation(), "Test hologram", "Line 2", "Line 3", "Line 4");

Static API methods

Hologram HolographicDisplaysAPI.createHologram(Plugin owner, Location location, String... lines)

  • owner - Replace this with your plugin's instance.
  • location - The location where the first line of the hologram will spawn.
  • lines - An array of lines. Can be null.
  • Returns the new Hologram created.

Hologram[] HolographicDisplaysAPI.getHolograms(Plugin plugin)

  • Returns an array of Hologram owned by a given plugin. Please note that this is a copy of the list, and changing this array will have no effect.

Check out the Hologram class for a complete list of methods.