Difference between revisions of "Map Display/API"

From BergerHealer Wiki
Jump to navigation Jump to search
(Marked this version for translation)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Introduction ==
+
<languages/>
 +
<translate>
 +
 
 +
== Introduction == <!--T:1-->
 +
 
 +
<!--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.
  
=== Execution Flow ===
+
=== Execution Flow === <!--T:3-->
 +
 
 +
<!--T:4-->
 
# BKCommonLib identifies an ItemStack in an ItemFrame or player inventory which declares a Map Display
 
# 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 optionally discovers neighbouring item frames to increase the resolution of the display
Line 10: Line 17:
 
# 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
 
# 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
  
 +
<!--T:5-->
 
All of the above means you should only concern yourself with a few things:
 
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
 
* 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'''
 
* Implementing the Map Display class so that it responds to input and draws the right information, optionally using '''Widgets'''
  
=== The Do-Nots ===
+
=== The Do-Nots === <!--T:6-->
 +
 
 +
<!--T:7-->
 
Things you '''must not attempt to do''' because BKCommonLib's API does this for you automatically:
 
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.
 
* 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.
 
* Starting the display after giving the ItemStack to a player or putting it in ItemFrames. This is done automatically within a tick.
  
=== API ===
+
=== API === <!--T:8-->
 +
 
 +
<!--T:9-->
 
There are javadocs [https://ci.mg-dev.eu/javadocs/BKCommonLib here], or you can browse the Map Display related code yourself [https://github.com/bergerhealer/BKCommonLib/tree/master/src/main/java/com/bergerkiller/bukkit/common/map here].
 
There are javadocs [https://ci.mg-dev.eu/javadocs/BKCommonLib here], or you can browse the Map Display related code yourself [https://github.com/bergerhealer/BKCommonLib/tree/master/src/main/java/com/bergerkiller/bukkit/common/map here].
  
== Basic Structure ==
+
== Basic Structure == <!--T:10-->
 +
 
 +
<!--T:11-->
 
You start by adding a new class to your plugin that '''extends MapDisplay''' overriding a few methods:
 
You start by adding a new class to your plugin that '''extends MapDisplay''' overriding a few methods:
  
<syntaxhighlight lang="java" line>
+
<!--T:12-->
 +
<pre>
 
package com.my.plugin;
 
package com.my.plugin;
  
 +
<!--T:13-->
 
import com.bergerkiller.bukkit.common.map.MapDisplay;
 
import com.bergerkiller.bukkit.common.map.MapDisplay;
  
 +
<!--T:14-->
 
public class MyCustomDisplay extends MapDisplay {
 
public class MyCustomDisplay extends MapDisplay {
 
     @Override
 
     @Override
Line 36: Line 53:
 
     }
 
     }
  
     @Override
+
     <!--T:15-->
 +
@Override
 
     public void onDetached() {
 
     public void onDetached() {
 
         // Stop any tasks, unregister stuff or save important information to disk
 
         // Stop any tasks, unregister stuff or save important information to disk
 
     }
 
     }
  
     @Override
+
     <!--T:16-->
 +
@Override
 
     public void onTick() {
 
     public void onTick() {
 
         // Perform per-tick updates here
 
         // Perform per-tick updates here
 
     }
 
     }
 
}
 
}
</syntaxhighlight>
+
</pre>
 +
 
 +
<!--T:17-->
 +
Then to create an ItemStack that when given loads this display, use the following code:
 +
<pre>
 +
ItemStack item = MapDisplay.createMapItem(MyCustomDisplay.class);
 +
ItemUtil.setDisplayName(item, "My Custom Display"); // Optional
 +
 
 +
<!--T:18-->
 +
player.getInventory().addItem(item);
 +
seplayerder.sendMessage(ChatColor.GREEN + "Given My Custom Display map!");
 +
</pre>
 +
 
 +
<!--T:19-->
 +
To include additional metadata for use in the display (''properties'' field) you can also use the ''MapDisplayProperties'' class to build the item:
 +
<pre>
 +
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
 +
</pre>
 +
</translate>

Latest revision as of 12:34, 3 September 2024

Other languages:
English

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.

Execution Flow

  1. BKCommonLib identifies an ItemStack in an ItemFrame or player inventory which declares a Map Display
  2. BKCommonLib optionally discovers neighbouring item frames to increase the resolution of the display
  3. BKCommonLib sees in item metadata what Map Display class should be loaded, constructs it, and starts the main update loop
  4. Main update loop calls MapDisplay onAttached(), then regularly calls onTick() and performs other background updates
  5. If a player is clicking on item frames with the map, or is holding the map and controlling it, event callbacks are fired
  6. 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