Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. extends Menu {
  2.  
  3. private boolean test = false;
  4. private Survey survey;
  5. private int question;
  6.  
  7. public SurveyQuestionMenu(Player player, Survey survey, int question) {
  8. super(player, "§8" + survey.getName() + " (Survey)", 9);
  9. this.survey = survey;
  10. this.question = question;
  11. }
  12.  
  13. @Override
  14. public void click(Profile profile, InventoryClickEvent event) {
  15. ItemStack item = event.getCurrentItem();
  16.  
  17. if (item == null) {
  18. return;
  19. }
  20.  
  21. event.setCancelled(true);
  22.  
  23. switch(item.getType()) {
  24. case BOOK:
  25. int nextAnswer = (profile.getActiveSurvey().getCurrentAnswer() + 1 >= profile.getActiveSurvey().getQuestion().getAlternatives().size() ? 0
  26. : profile.getActiveSurvey().getCurrentAnswer() + 1);
  27.  
  28. profile.getActiveSurvey().setCurrentAnswer(nextAnswer);
  29. event.setCurrentItem(getQuestionItem(profile));
  30.  
  31. getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 1.0f);
  32.  
  33. break;
  34. case EMERALD:
  35. profile.getActiveSurvey().submitAnswer(getPlayer());
  36.  
  37. if(profile.getActiveSurvey() == null) {
  38. return;
  39. }
  40.  
  41. test = true;
  42. new SurveyQuestionMenu(getPlayer(), this.survey, profile.getActiveSurvey().getQuestionIndex()).open(profile);
  43. getPlayer().playSound(getPlayer().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0f, 1.0f);
  44.  
  45. break;
  46. }
  47. }
  48.  
  49. @Override
  50. public void close(Profile profile, InventoryCloseEvent event) {
  51. if(!test && profile.getActiveSurvey() != null && !profile.getActiveSurvey().isCompleted()) {
  52. profile.setActiveSurvey(null);
  53. getPlayer().sendMessage("§c§lThe Survey has been cancelled!");
  54. }
  55. test = false;
  56. }
  57.  
  58. @Override
  59. public void open(Profile profile) {
  60. Inventory inventory = Bukkit.createInventory(null, this.getSize(), this.getTitle());
  61.  
  62. inventory.setItem(4, getQuestionItem(profile));
  63.  
  64. if(survey.isFinalQuestion(this.question)) {
  65. inventory.setItem(8, new ItemBuilder(Material.EMERALD).name("§aFinish Survey").lore(" ", "§7By clicking this, you submit your Answer", "§7and finalize this Survey", " ", "§4WARNING: §cThis action cannot be undone.", " ").build());
  66. } else {
  67. inventory.setItem(8, new ItemBuilder(Material.EMERALD).name("§eNext Question").lore(" ", "§7By clicking this, you submit your Answer", "§7and get progressed to the next question!", " ", "§4WARNING: §cYour answer cannot be undone.", " ").build());
  68. }
  69.  
  70. getPlayer().openInventory(inventory);
  71. profile.setMenu(this);
  72. }
  73.  
  74. private ItemStack getQuestionItem(Profile profile) {
  75. SurveyQuestion question = this.survey.getQuestion(this.question);
  76.  
  77. ItemBuilder builder = new ItemBuilder(Material.BOOK).name("§6Question #" + (this.question + 1) + (survey.isFinalQuestion(this.question) ? " §7(Final)" : ""));
  78. builder.addLore(" ");
  79. builder.addLore("§7" + question.getQuestion());
  80. builder.addLore(" ");
  81.  
  82. for (int i = 0; i < question.getAlternatives().size(); i++) {
  83. builder.addLore((profile.getActiveSurvey().getCurrentAnswer() == i ? " §6» " : "") + "§eAlt #" + (i + 1) + " §7| §f" + question.getAlternatives().get(i) + "");
  84. }
  85.  
  86. builder.addLore(" ");
  87. builder.addLore("§aClick to switch alternative!");
  88. builder.addLore(" ");
  89.  
  90. return builder.build();
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement