Added some examples.

This commit is contained in:
Jitse Boonstra 2019-08-03 16:18:29 +02:00
parent 84bcd6a6c3
commit 7cf5e4d0bb
4 changed files with 148 additions and 0 deletions

5
examples/README.md Normal file
View File

@ -0,0 +1,5 @@
# NPCLib examples
Hopefully these examples will help you to get started (together with the documentations that have been written).
If you can't figure something out, find a bug or have a request for a feature. Feel free to contact me or to post an issue ticket. :)

40
examples/example-1.java Normal file
View File

@ -0,0 +1,40 @@
package net.jitse.npclib.example;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.NPC;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.plugin.java.JavaPlugin;
/**
* @author Jitse Boonstra
*/
public class NPCLibTest extends JavaPlugin implements Listener {
/*
The most basic example of spawning an NPC with NPCLib.
It will spawn an NPC on un-sneaking at the player's location.
And it will send a message with its ID on interact.
*/
private NPCLib npclib;
@Override
public void onEnable() {
this.npclib = new NPCLib(this);
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerToggleSneak(PlayerToggleSneakEvent event) {
if (event.isSneaking()) {
return;
}
NPC npc = npclib.createNPC();
npc.setLocation(event.getPlayer().getLocation());
npc.create();
npc.show(event.getPlayer());
}
}

56
examples/example-2.java Normal file
View File

@ -0,0 +1,56 @@
package net.jitse.npclib.example;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.api.events.NPCInteractEvent;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
import java.util.HashSet;
import java.util.SortedSet;
/**
* @author Jitse Boonstra
*/
public class NPCLibTest extends JavaPlugin implements Listener {
/*
This is an example class, it spawns an NPC with some text at the player's location when un-sneaking.
It will also send an interactor a message if it's the first NPC spawned.
*/
// To keep track of NPC IDs.
private final SortedSet<String> ids = new HashSet<>();
private NPCLib npclib;
@Override
public void onEnable() {
this.npclib = new NPCLib(this);
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerToggleSneak(PlayerToggleSneakEvent event) {
if (event.isSneaking()) {
return;
}
NPC npc = npclib.createNPC(Arrays.asList(ChatColor.WHITE + "Hi there (#2)", ChatColor.YELLOW + "Click on me!"));
npc.setLocation(event.getPlayer().getLocation());
ids.add(npc.getId());
npc.create();
npc.show(event.getPlayer());
}
@EventHandler
public void onNPCInteract(NPCInteractEvent event) {
String firstId = ids.first();
if (event.getNPC().getId().equals(firstId)) {
event.getWhoClicked().sendMessage(ChatColor.GREEN + "I'm the first NPC spawned since boot!");
}
}
}

47
examples/example-3.java Normal file
View File

@ -0,0 +1,47 @@
package net.jitse.npclib.example;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.api.skin.MineSkinFetcher;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
/**
* @author Jitse Boonstra
*/
public class NPCLibTest extends JavaPlugin {
/*
Another basic example of spawning an NPC with NPCLib.
This class will spawn an NPC with a custom skin on un-sneaking at the player's location.
*/
private NPCLib npclib;
@Override
public void onEnable() {
this.npclib = new NPCLib(this);
}
@EventHandler
public void onPlayerToggleSneak(PlayerToggleSneakEvent event) {
if (event.isSneaking()) {
return;
}
int skinId = 188100; // Read DOCUMENTATION.md on how and where to get this number.
MineSkinFetcher.fetchSkinFromIdAsync(skinId, skin -> {
NPC npc = npclib.createNPC(Arrays.asList(ChatColor.WHITE + "Hi there (#3)");
npc.setLocation(event.getPlayer().getLocation());
ids.add(npc.getId());
npc.create();
// The SkinFetcher fetches the skin async, you can only show the NPC to the player sync.
Bukkit.getScheduler().runTask(this, () -> npc.show(event.getPlayer()));
});
}
}