Below I will use the Bukkit API to implement a simple quest, including a quest-giving NPC, objective, and reward.
Spawn Quest Giving NPC
First we need to spawn the NPC who will offer the quest to players as well as give out the rewards. Creating an NPC with Bukkit looks like this:
The above will create a Villager NPC, which isn’t ideal because every time we right-click him he will show us his inventory, but it will work for this tutorial.
Track Quest Progress
So now that we have an NPC, we need to give it a memory so it can track each player’s respective quest progress. To save each player’s current quest progress we save the index of their state in a array where each key is the player’s ID (similar to what we did in the Wolfbot post).
Let’s also create a method which initializes another hash that maps quest state to a blurb of text we want the NPC to spit out when interacted with by a player:
As you can see from the code our quest involves a gang of skeletons who are running amok nearby. The quest is for the player to simply kill 10 and then return for their reward. Of course we are still missing the logic that will cause the Villager to spit out the text and drop the rewards if the quest objective is met:
Watch For Quest Completion
There are only two bits of code left to round out our quest logic. We need a command for the player to accept the quest and also a listener to watch for skeleton deaths–specifically deaths that were at the hands of our questing player:
The full code list can be found at the end of this post. Drop that code in your js-plugin directory and you can start questing:
- /jsp spawn_npc
- Right-click to move through the quest prompts.
- /jsp accept
- Go hunt down and slay 10 skeletons.
- Return to the NPC and claim your XP reward!
There are a number of improvements you can add to this ScriptCraft plugin; however, if you really want to put time into creating quests I would recommend checking out the Citizens plugin which is much more powerful and lets you script NPC interactions with YAML. There is also a helpful repository with script examples.
Here is our little quest plugin in its entirety: