Skip to content

Instantly share code, notes, and snippets.

@riftrsps
Created August 24, 2019 18:38
Show Gist options
  • Save riftrsps/cbcf56cd2cc70e672891218473320a6d to your computer and use it in GitHub Desktop.
Save riftrsps/cbcf56cd2cc70e672891218473320a6d to your computer and use it in GitHub Desktop.
package api.quest;
public class QuestModule implements Listener {
private final QuestManager manager;
private final YourAPI api;
public QuestModule(YourAPI api) {
this.api = api;
this.manager = new QuestManager(api);
}
@Override
public void onPlayerLogin(PlayerLoginEvent event) {
Player player = event.getPlayer();
manager.addLocal(player);
PlayerInfo info = api.getPlayerInfo(player);
if(info.hasQuests()) {
localQuests.put(player, info.quests());
}
}
@Override
public void onPlayerLogout(PlayerLogoutEvent event) {
Player player = event.getPlayer();
manager.removeLocal(player);
}
public static QuestModule load(YourAPI api) {
// prepare base quests if wanted
return new QuestModule(api);
}
}
public class QuestManager {
private final Map<Player, QuestGroup> localQuests = new HashMap<>();
private final YourAPI api;
public QuestManager(YourAPI api) {
this.api = api;
}
public void removeLocal(Player player, Quest quest) {
if(localQuests.contains(player) {
QuestGroup quests = localQuests.get(player);
api.deregisterListener(quests);
localQuests.remove(player);
}
}
public void addLocal(Player player, Quest quest) throws QuestAlreadyExistsException {
if(!localQuests.contains(player))
localQuests.put(player, new QuestGroup(quest)
QuestGroup quests = localQuests.get(player);
if(quests.contains(quest))
throw new QuestAlreadyExistsException();
quests.add(quest);
}
public static final class QuestAlreadyExistsException extends Exception { }
}
public class QuestGroup extends Group<Quest> {
}
public class Quest {
private final String name;
private int progress;
public Quest(String name) {
this.name = name;
}
public void increaseProgressBy(int amount) {
progress += amount;
if(progress > 100)
progress = 100;
}
public void decreaseProgressBy(int amount) {
progress -= amount;
if(progress < 0)
progress = 0;
}
@Override
public String toString() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment