Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Quizes implements Listener {
- // Registers events
- public Quizes(<MainClass> mainClass) {
- Bukkit.getPluginManager().registerEvents(this, mainClass);
- }
- // Players current question
- public Map<UUID, Question> currentQuestion = new HashMap<>();
- // List of questions
- public List<Question> questions = new ArrayList<>();
- // Category index
- public Map<Integer, String> categories = new HashMap<>();
- // Generates questions
- public void setQuestions() {
- questions.add(new Question("You are playing minecraft.", true, "Newbie"));
- questions.add(new Question("Enderpearls teleport you to their location when thrown.", true, "Newbie"));
- questions.add(new Question("Sugar is needed to craft cake.", true, "Member"));
- questions.add(new Question("WorkBench's have a 3x3 crafting grid.", true, "Member"));
- }
- /*public Economy economy = null;
- public boolean setupEconomy() {
- RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
- if (economyProvider != null) {
- economy = economyProvider.getProvider();
- }
- return (economy != null);
- }*/
- // Question object.
- // * Example: new Question(String, boolean, String)
- // * string - Question
- // * boolean - Answer
- // * string - Category
- public class Question {
- private String question;
- private boolean answer = true;
- private String category;
- private int index;
- public Question(String question, boolean answer, String category) {
- this.question = question;
- this.answer = answer;
- this.category = category;
- index = getNextIndex(this);
- if(!categories.values().contains(category.toLowerCase()))
- categories.put(categories.size()+1, category.toLowerCase());
- }
- public String getQuestion() {return question;}
- public boolean getAnswer() {return answer;}
- public String getCategory() {return category;}
- public int getIndex() {return index;}
- }
- // Retrieves the next index from provided category
- private int getNextIndex(Question question) {
- List<Question> questionsFromCategory = questions.stream().filter(q -> q.getCategory().equalsIgnoreCase(question.getCategory())).collect(Collectors.toList());
- return questionsFromCategory.size()+1;
- }
- // Retrieves first question from provided category
- private Question getFirstQuestion(String category) {
- return questions.stream().filter(q -> q.getCategory().equalsIgnoreCase(category) && q.getIndex() == 1).findAny().orElse(null);
- }
- // Retrieves next question from provided question
- private Question getNextQuestion(Question question) {
- String category = question.getCategory();
- int index = question.getIndex();
- return questions.stream().filter(q -> q.getCategory().equalsIgnoreCase(category) && q.getIndex() == (index+1)).findAny().orElse(null);
- }
- // Sends the player true and false message
- private void sendTrueAndFalse(Player p) {
- TextComponent[] text = {new TextComponent(Messages.util().addColor(" &7&l[&aTrue&7&l] ")), new TextComponent(Messages.util().addColor(" &7&l[&cFalse&7&l] "))};
- text[0].setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answerquestion true"));
- text[1].setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answerquestion false"));
- p.spigot().sendMessage(text[0], text[1]);
- }
- @EventHandler
- public void onInteract(PlayerInteractEvent e) {
- Player p = e.getPlayer();
- Block b = e.getClickedBlock();
- // Checks if questions is empty and runs method to add questions.
- if(questions.isEmpty()) setQuestions();
- if(b == null) return;
- if(!(b.getState() instanceof Sign)) return;
- Sign s = (Sign) b.getState();
- // Checks if sign is for quizes
- if(!ChatColor.stripColor(s.getLine(0)).equalsIgnoreCase("[quiz]")) return;
- // Checks is player has permission to break sign while sneaking
- if(p.isSneaking() && p.hasPermission("admin.permission.node")) return;
- // Checks if the player has already started a quiz
- if(currentQuestion.containsKey(p.getUniqueId())) return;
- e.setCancelled(true);
- String category = s.getLine(1).isEmpty() ? "Newbie" : ChatColor.stripColor(s.getLine(1));
- //double price = s.getLine(2).isEmpty() ? 0 : Double.valueOf(ChatColor.stripColor(s.getLine(2)));
- // Checks if player already has permission from the current category
- if(p.hasPermission("perm.node."+category)) return;
- /*if(price > 0) {
- if(economy == null)
- setupEconomy();
- if(!economy.getBalance(Bukkit.getOfflinePlayer(p.getUniqueId())) >= price) {
- p.sendMessage("Insufficient funds.");
- return;
- }else {
- economy.withdrawPlayer(Bukkit.getOfflinePlayer(p.getUniqueId()), price);
- }
- }*/
- /* Checks permissions from categories in order they were added
- for(int i = 1; i <= categories.size(); i++) {
- String cat = categories.get(i);
- if(p.hasPermission("perm.node."+cat) && i == categories.size())
- return;
- if(p.hasPermission("perm.node."+cat))
- continue;
- else
- break;
- }*/
- // Gets the first question from the category "newbie" or the category specified on line 2 of the sign
- Question q = getFirstQuestion(category);
- if(q == null) return;
- // Adds player to the currentQuestion map
- currentQuestion.put(p.getUniqueId(), q);
- p.sendMessage(q.getQuestion());
- sendTrueAndFalse(p);
- }
- @EventHandler
- public void onPreCommand(PlayerCommandPreprocessEvent event) {
- Player p = event.getPlayer();
- String command = event.getMessage();
- // Checks if command is our answer question command
- if(!command.startsWith("/answerquestion")) return;
- event.setCancelled(true);
- // Checks if the player is answering quiz questions
- if(!currentQuestion.containsKey(p.getUniqueId())) return;
- // Splits the command so we can get the answer the player chose
- String[] parse = command.split(" ");
- // Gets current question and answer from map using the players UUID
- Question current = currentQuestion.get(p.getUniqueId());
- boolean a = current.getAnswer();
- if(a == Boolean.valueOf(parse[1])) {
- // Player chose the correct answer for the question and the next question is being retrieved
- p.sendMessage("Correct!");
- Question next = getNextQuestion(current);
- if(next != null) {
- // A new question was found and is being sent to the player
- currentQuestion.put(p.getUniqueId(), next);
- p.sendMessage(next.getQuestion());
- sendTrueAndFalse(p);
- }else {
- // The end of the quiz was reached and the player is being removed from the quiz map
- p.sendMessage("Completed quiz from category "+current.getCategory());
- currentQuestion.remove(p.getUniqueId());
- }
- }else {
- p.sendMessage("Incorrect!");
- p.sendMessage("Go reclick the sign.");
- /*p.sendMessage(current.getQuestion());
- sendTrueAndFalse(p);*/
- currentQuestion.remove(p.getUniqueId());
- }
- }
- @EventHandler
- public void onQuit(PlayerQuitEvent event) {
- currentQuestion.remove(event.getPlayer().getUniqueId());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement