Last active
August 24, 2019 18:58
-
-
Save riftrsps/84e2151f0c366b3f9de058442af02aef to your computer and use it in GitHub Desktop.
Quest Package for MineCraft server API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api.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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api.quest; | |
import api.util.Group; // https://gist.github.com/riftrsps/69ba3473c77c2334fcdaac2e605f4e91#file-group-java | |
public class QuestGroup extends Group<Quest> { | |
public QuestGroup(Quest quest) { | |
add(quest); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api.quest; | |
import api.YourAPI; | |
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 { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api.quest; | |
import api.YourAPI; | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment