-
-
Notifications
You must be signed in to change notification settings - Fork 11
Developer API
LOOHP edited this page Jul 3, 2021
·
13 revisions
InteractionVisualizer provides an API for developers to create addons for the plugin. Here are some simple examples of how to use the API. This example is for InteractionVisualizer 1.16.0+
Remember to put InteractionVisualizer as a dependency or soft-dependency in your plugin.yml!
Here is a simple piece of code to do just that, I'll be putting it in my onEnable method.
@Override
public void onEnable() {
if (Bukkit.getPluginManager().getPlugin("InteractionVisualizer") != null) {
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "InteractionVisualizer is Enabled!");
}
}
Here is an example of how to play a fake item throw and catch animation with the API.
public void throw(Player thrower, Player catcher, EntryKey key) {
Location from = thrower.getEyeLocation();
Location to = catcher.getEyeLocation().add(0.0, -0.5, 0.0);
ItemStack item = new ItemStack(Material.DIAMOND, 1);
//The "true" in the at the end means a pickup sound will be played
InteractionVisualizerAPI.playFakeItemThrowAnimation(key, from, to, item, true);
}
Here is an example of creating an item display for a beacon.
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import com.loohp.interactionvisualizer.EntityHolders.Item;
import com.loohp.interactionvisualizer.objectholders.EntryKey;
//Remember to extend VisualizerInteractDisplay
public class BeaconItem extends VisualizerInteractDisplay implements Listener {
private EntryKey key;
private Map<Player, Item> playerItems = new HashMap<>();
public BeaconItem(Plugin plugin) {
//Create an EntryKey for InteractionVisualizer to identify this display type
key = new EntryKey(plugin, "beacon");
}
@Override
public EntryKey key() {
return key;
}
@Override
public void process(Player player) {
//Get the inventory view the player is looking at
InventoryView view = player.getOpenInventory();
//In a beacon gui, item slot 1 is the item input
ItemStack itemstack = view.getItem(0);
Location location = player.getTargetBlockExact(7, FluidCollisionMode.NEVER).getLocation().add(0.5, 1.05, 0.5);
//Check if an item is present
if (itemstack != null && !itemstack.getType().equals(Material.AIR)) {
//Create or get an item depends on whether a fake item has already been created
//import com.loohp.interactionvisualizer.EntityHolders.Item
Item fakeItem = playerItems.get(player);
if (fakeItem == null) {
fakeItem = InteractionVisualizerAPI.createItemObject(location);
fakeItem.setGravity(false);
playerItems.put(player, fakeItem);
InteractionVisualizerAPI.spawnFakeItem(fakeItem, key);
}
fakeItem.setItemStack(itemstack);
InteractionVisualizerAPI.updateItem(fakeItem, key);
} else {
//Remove the item if one is already spawned
Item fakeItem = playerItems.remove(player);
if (fakeItem != null) {
InteractionVisualizerAPI.removeItem(fakeItem, key);
}
}
}
@EventHandler
public void onCloseInventory(InventoryCloseEvent event) {
Player player = (Player) event.getPlayer();
if (!event.getView().getTopInventory().getType().equals(InventoryType.BEACON)) {
return;
}
//Remove the Item when a player closes the inventory
Item fakeItem = playerItems.remove(player);
if (fakeItem != null) {
InteractionVisualizerAPI.removeItem(fakeItem, key);
}
}
}
@Override
public void onEnable() {
BeaconItem bi = new BeaconItem(this);
bi.register(InventoryType.BEACON);
Bukkit.getPluginManager().registerEvents(bi, this);
}
And it's done!