diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..60e7ba1 --- /dev/null +++ b/examples/README.md @@ -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. :) \ No newline at end of file diff --git a/examples/example-1.java b/examples/example-1.java new file mode 100644 index 0000000..b8608fa --- /dev/null +++ b/examples/example-1.java @@ -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()); + } +} diff --git a/examples/example-2.java b/examples/example-2.java new file mode 100644 index 0000000..f969e7d --- /dev/null +++ b/examples/example-2.java @@ -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 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!"); + } + } +} diff --git a/examples/example-3.java b/examples/example-3.java new file mode 100644 index 0000000..679ede8 --- /dev/null +++ b/examples/example-3.java @@ -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())); + }); + } +}