/API
Bergerkiller (talk | contribs) (Marked this version for translation) |
Bergerkiller (talk | contribs) No edit summary |
||
| Line 6: | Line 6: | ||
<!--T:2--> | <!--T:2--> | ||
Map Displays are programmed different from what you might be used to with Bukkit. They are completely callback driven, which means you don't start and stop the display yourself. Instead, you override methods in the Map Display class to load and display the contents you want. If you want to show unique information per display, this information must be stored in the Map Item itself. | Map Displays are programmed different from what you might be used to with Bukkit. They are completely callback driven, which means you don't start and stop the display yourself. Instead, you override methods in the Map Display class to load and display the contents you want. If you want to show unique information per display, this information must be stored in the Map Item itself. | ||
To display different information to different players, call `setGlobal(false)` in your custom display's onAttached(). | |||
=== Example Projects === | |||
You can look at these projects to get an idea about how the API works or can be used. | |||
* BKMinesweeper POC demo by bbayu [https://github.com/bbayu123/bkminesweeper-poc on Github] | |||
* Bouncing DVD logo demo by bbayu [https://github.com/bbayu123/bkbouncingdvd-poc on Github] | |||
* 2048 game demo POC by bbayu [https://github.com/bbayu123/bk2048-poc on Github] | |||
* [https://www.spigotmc.org/resources/traincarts.39592/ TrainCarts] uses the map display for [https://github.com/bergerhealer/TrainCarts/blob/master/src/main/java/com/bergerkiller/bukkit/tc/tickets/TCTicketDisplay.java ticket items] and for the [https://github.com/bergerhealer/TrainCarts/blob/master/src/main/java/com/bergerkiller/bukkit/tc/attachments/ui/AttachmentEditor.java attachment editor]. The attachment editor is very complex using a deep tree of widgets, but the ticket display is easy to understand. | |||
* [https://www.spigotmc.org/resources/tc-coasters.59583/ TC-Coasters] uses the map display for the [https://github.com/bergerhealer/TC-Coasters/blob/master/src/main/java/com/bergerkiller/bukkit/coasters/editor/TCCoastersDisplay.java coaster editor map]. It also uses a lot of widgets, but has an easier to understand layout. | |||
* [https://www.spigotmc.org/resources/maplands.46404/ Maplands] uses the map display to [https://github.com/bergerhealer/Maplands/blob/master/src/main/java/com/bergerkiller/bukkit/maplands/MaplandsDisplay.java show an isographic render of the world]. It draws 3D block sprites and makes use of the depth buffer. | |||
=== Execution Flow === <!--T:3--> | === Execution Flow === <!--T:3--> | ||
Revision as of 15:03, 12 August 2026
Introduction
Map Displays are programmed different from what you might be used to with Bukkit. They are completely callback driven, which means you don't start and stop the display yourself. Instead, you override methods in the Map Display class to load and display the contents you want. If you want to show unique information per display, this information must be stored in the Map Item itself.
To display different information to different players, call `setGlobal(false)` in your custom display's onAttached().
Example Projects
You can look at these projects to get an idea about how the API works or can be used.
- BKMinesweeper POC demo by bbayu on Github
- Bouncing DVD logo demo by bbayu on Github
- 2048 game demo POC by bbayu on Github
- TrainCarts uses the map display for ticket items and for the attachment editor. The attachment editor is very complex using a deep tree of widgets, but the ticket display is easy to understand.
- TC-Coasters uses the map display for the coaster editor map. It also uses a lot of widgets, but has an easier to understand layout.
- Maplands uses the map display to show an isographic render of the world. It draws 3D block sprites and makes use of the depth buffer.
Execution Flow
- BKCommonLib identifies an ItemStack in an ItemFrame or player inventory which declares a Map Display
- BKCommonLib optionally discovers neighbouring item frames to increase the resolution of the display
- BKCommonLib sees in item metadata what Map Display class should be loaded, constructs it, and starts the main update loop
- Main update loop calls MapDisplay onAttached(), then regularly calls onTick() and performs other background updates
- If a player is clicking on item frames with the map, or is holding the map and controlling it, event callbacks are fired
- Once no player is nearby anymore to view it, hold it or all chunks unload containing the map (Session Mode), onDetached() is called and the display update loop is shut down
All of the above means you should only concern yourself with a few things:
- Creating the initial ItemStack where you set the Map Display class and initial settings to use
- Implementing the Map Display class so that it responds to input and draws the right information, optionally using Widgets
The Do-Nots
Things you must not attempt to do because BKCommonLib's API does this for you automatically:
- Setting Map IDs, item frame tiling. IDs are automatically calculated and updated so they don't clash with other plugins.
- Starting the display after giving the ItemStack to a player or putting it in ItemFrames. This is done automatically within a tick.
API
There are javadocs here, or you can browse the Map Display related code yourself here.
Basic Structure
You start by adding a new class to your plugin that extends MapDisplay overriding a few methods:
package com.my.plugin;
import com.bergerkiller.bukkit.common.map.MapDisplay;
public class MyCustomDisplay extends MapDisplay {
@Override
public void onAttached() {
// Setup your display and draw the initial contents
}
@Override
public void onDetached() {
// Stop any tasks, unregister stuff or save important information to disk
}
@Override
public void onTick() {
// Perform per-tick updates here
}
}
Then to create an ItemStack that when given loads this display, use the following code:
ItemStack item = MapDisplay.createMapItem(MyCustomDisplay.class); ItemUtil.setDisplayName(item, "My Custom Display"); // Optional player.getInventory().addItem(item); seplayerder.sendMessage(ChatColor.GREEN + "Given My Custom Display map!");
To include additional metadata for use in the display (properties field) you can also use the MapDisplayProperties class to build the item:
MapDisplayProperties prop = MapDisplayProperties.createNew(MyCustomDisplay.class);
prop.set("creator", player.getName());
prop.set("background", "https://url/to/some/image.png");
ItemStack item = prop.getMapItem();
// Rest is the same