Advertisement
Yapperyapps

Minecraft Quizes

Feb 18th, 2022 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.09 KB | None | 0 0
  1. public class Quizes implements Listener {
  2.      
  3.     // Registers events
  4.     public Quizes(<MainClass> mainClass) {
  5.         Bukkit.getPluginManager().registerEvents(this, mainClass);
  6.     }
  7.    
  8.     // Players current question
  9.     public Map<UUID, Question> currentQuestion = new HashMap<>();
  10.    
  11.     // List of questions
  12.     public List<Question> questions = new ArrayList<>();
  13.    
  14.     // Category index
  15.     public Map<Integer, String> categories = new HashMap<>();
  16.    
  17.     // Generates questions
  18.     public void setQuestions() {
  19.         questions.add(new Question("You are playing minecraft.", true, "Newbie"));
  20.         questions.add(new Question("Enderpearls teleport you to their location when thrown.", true, "Newbie"));
  21.        
  22.         questions.add(new Question("Sugar is needed to craft cake.", true, "Member"));
  23.         questions.add(new Question("WorkBench's have a 3x3 crafting grid.", true, "Member"));
  24.     }
  25.  
  26.     /*public Economy economy = null;
  27.     public boolean setupEconomy() {
  28.         RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
  29.         if (economyProvider != null) {
  30.             economy = economyProvider.getProvider();
  31.         }
  32.         return (economy != null);
  33.     }*/
  34.    
  35.     // Question object.
  36.     // * Example: new Question(String, boolean, String)
  37.     // * string - Question
  38.     // * boolean - Answer
  39.     // * string - Category
  40.     public class Question {
  41.         private String question;
  42.         private boolean answer = true;
  43.         private String category;
  44.         private int index;
  45.        
  46.         public Question(String question, boolean answer, String category) {
  47.             this.question = question;
  48.             this.answer = answer;
  49.             this.category = category;
  50.             index = getNextIndex(this);
  51.            
  52.             if(!categories.values().contains(category.toLowerCase()))
  53.                 categories.put(categories.size()+1, category.toLowerCase());
  54.         }
  55.        
  56.         public String getQuestion() {return question;}
  57.         public boolean getAnswer() {return answer;}
  58.         public String getCategory() {return category;}
  59.         public int getIndex() {return index;}
  60.     }
  61.    
  62.     // Retrieves the next index from provided category
  63.     private int getNextIndex(Question question) {
  64.         List<Question> questionsFromCategory = questions.stream().filter(q -> q.getCategory().equalsIgnoreCase(question.getCategory())).collect(Collectors.toList());
  65.         return questionsFromCategory.size()+1;
  66.     }
  67.    
  68.     // Retrieves first question from provided category
  69.     private Question getFirstQuestion(String category) {
  70.         return questions.stream().filter(q -> q.getCategory().equalsIgnoreCase(category) && q.getIndex() == 1).findAny().orElse(null);
  71.     }
  72.    
  73.     // Retrieves next question from provided question
  74.     private Question getNextQuestion(Question question) {
  75.         String category = question.getCategory();
  76.         int index = question.getIndex();
  77.        
  78.         return questions.stream().filter(q -> q.getCategory().equalsIgnoreCase(category) && q.getIndex() == (index+1)).findAny().orElse(null);
  79.     }
  80.    
  81.     // Sends the player true and false message
  82.     private void sendTrueAndFalse(Player p) {
  83.         TextComponent[] text = {new TextComponent(Messages.util().addColor(" &7&l[&aTrue&7&l] ")), new TextComponent(Messages.util().addColor(" &7&l[&cFalse&7&l] "))};
  84.         text[0].setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answerquestion true"));
  85.         text[1].setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answerquestion false"));
  86.        
  87.         p.spigot().sendMessage(text[0], text[1]);
  88.     }
  89.    
  90.     @EventHandler
  91.     public void onInteract(PlayerInteractEvent e) {
  92.         Player p = e.getPlayer();
  93.         Block b = e.getClickedBlock();
  94.        
  95.         // Checks if questions is empty and runs method to add questions.
  96.         if(questions.isEmpty()) setQuestions();
  97.        
  98.         if(b == null) return;
  99.         if(!(b.getState() instanceof Sign)) return;
  100.        
  101.         Sign s = (Sign) b.getState();
  102.        
  103.         // Checks if sign is for quizes
  104.         if(!ChatColor.stripColor(s.getLine(0)).equalsIgnoreCase("[quiz]")) return;
  105.        
  106.         // Checks is player has permission to break sign while sneaking
  107.         if(p.isSneaking() && p.hasPermission("admin.permission.node")) return;
  108.        
  109.         // Checks if the player has already started a quiz
  110.         if(currentQuestion.containsKey(p.getUniqueId())) return;
  111.         e.setCancelled(true);
  112.        
  113.         String category = s.getLine(1).isEmpty() ? "Newbie" : ChatColor.stripColor(s.getLine(1));
  114.         //double price = s.getLine(2).isEmpty() ? 0 : Double.valueOf(ChatColor.stripColor(s.getLine(2)));
  115.        
  116.         // Checks if player already has permission from the current category
  117.         if(p.hasPermission("perm.node."+category)) return;
  118.  
  119.         /*if(price > 0) {
  120.             if(economy == null)
  121.                 setupEconomy();
  122.            
  123.             if(!economy.getBalance(Bukkit.getOfflinePlayer(p.getUniqueId())) >= price) {
  124.                 p.sendMessage("Insufficient funds.");
  125.                 return;
  126.             }else {
  127.                 economy.withdrawPlayer(Bukkit.getOfflinePlayer(p.getUniqueId()), price);
  128.             }
  129.         }*/
  130.        
  131.         /* Checks permissions from categories in order they were added
  132.         for(int i = 1; i <= categories.size(); i++) {
  133.             String cat = categories.get(i);
  134.             if(p.hasPermission("perm.node."+cat) && i == categories.size())
  135.                 return;
  136.            
  137.             if(p.hasPermission("perm.node."+cat))
  138.                 continue;
  139.             else
  140.                 break;
  141.         }*/
  142.        
  143.         // Gets the first question from the category "newbie" or the category specified on line 2 of the sign
  144.         Question q = getFirstQuestion(category);
  145.         if(q == null) return;
  146.        
  147.         // Adds player to the currentQuestion map
  148.         currentQuestion.put(p.getUniqueId(), q);
  149.        
  150.         p.sendMessage(q.getQuestion());
  151.         sendTrueAndFalse(p);
  152.     }
  153.    
  154.     @EventHandler
  155.     public void onPreCommand(PlayerCommandPreprocessEvent event) {
  156.         Player p = event.getPlayer();
  157.         String command = event.getMessage();
  158.        
  159.         // Checks if command is our answer question command
  160.         if(!command.startsWith("/answerquestion")) return;
  161.         event.setCancelled(true);
  162.        
  163.         // Checks if the player is answering quiz questions
  164.         if(!currentQuestion.containsKey(p.getUniqueId())) return;
  165.        
  166.         // Splits the command so we can get the answer the player chose
  167.         String[] parse = command.split(" ");
  168.        
  169.         // Gets current question and answer from map using the players UUID
  170.         Question current = currentQuestion.get(p.getUniqueId());
  171.         boolean a = current.getAnswer();
  172.        
  173.         if(a == Boolean.valueOf(parse[1])) {
  174.             // Player chose the correct answer for the question and the next question is being retrieved
  175.             p.sendMessage("Correct!");
  176.             Question next = getNextQuestion(current);
  177.             if(next != null) {
  178.                 // A new question was found and is being sent to the player
  179.                 currentQuestion.put(p.getUniqueId(), next);
  180.                 p.sendMessage(next.getQuestion());
  181.                 sendTrueAndFalse(p);
  182.             }else {
  183.                 // The end of the quiz was reached and the player is being removed from the quiz map
  184.                 p.sendMessage("Completed quiz from category "+current.getCategory());
  185.                 currentQuestion.remove(p.getUniqueId());
  186.             }
  187.         }else {
  188.             p.sendMessage("Incorrect!");
  189.            
  190.             p.sendMessage("Go reclick the sign.");
  191.             /*p.sendMessage(current.getQuestion());
  192.             sendTrueAndFalse(p);*/
  193.            
  194.             currentQuestion.remove(p.getUniqueId());
  195.         }
  196.     }
  197.    
  198.     @EventHandler
  199.     public void onQuit(PlayerQuitEvent event) {
  200.         currentQuestion.remove(event.getPlayer().getUniqueId());
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement