Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Menu {
- private boolean test = false;
- private Survey survey;
- private int question;
- public SurveyQuestionMenu(Player player, Survey survey, int question) {
- super(player, "§8" + survey.getName() + " (Survey)", 9);
- this.survey = survey;
- this.question = question;
- }
- @Override
- public void click(Profile profile, InventoryClickEvent event) {
- ItemStack item = event.getCurrentItem();
- if (item == null) {
- return;
- }
- event.setCancelled(true);
- switch(item.getType()) {
- case BOOK:
- int nextAnswer = (profile.getActiveSurvey().getCurrentAnswer() + 1 >= profile.getActiveSurvey().getQuestion().getAlternatives().size() ? 0
- : profile.getActiveSurvey().getCurrentAnswer() + 1);
- profile.getActiveSurvey().setCurrentAnswer(nextAnswer);
- event.setCurrentItem(getQuestionItem(profile));
- getPlayer().playSound(getPlayer().getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 1.0f);
- break;
- case EMERALD:
- profile.getActiveSurvey().submitAnswer(getPlayer());
- if(profile.getActiveSurvey() == null) {
- return;
- }
- test = true;
- new SurveyQuestionMenu(getPlayer(), this.survey, profile.getActiveSurvey().getQuestionIndex()).open(profile);
- getPlayer().playSound(getPlayer().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0f, 1.0f);
- break;
- }
- }
- @Override
- public void close(Profile profile, InventoryCloseEvent event) {
- if(!test && profile.getActiveSurvey() != null && !profile.getActiveSurvey().isCompleted()) {
- profile.setActiveSurvey(null);
- getPlayer().sendMessage("§c§lThe Survey has been cancelled!");
- }
- test = false;
- }
- @Override
- public void open(Profile profile) {
- Inventory inventory = Bukkit.createInventory(null, this.getSize(), this.getTitle());
- inventory.setItem(4, getQuestionItem(profile));
- if(survey.isFinalQuestion(this.question)) {
- 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());
- } else {
- 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());
- }
- getPlayer().openInventory(inventory);
- profile.setMenu(this);
- }
- private ItemStack getQuestionItem(Profile profile) {
- SurveyQuestion question = this.survey.getQuestion(this.question);
- ItemBuilder builder = new ItemBuilder(Material.BOOK).name("§6Question #" + (this.question + 1) + (survey.isFinalQuestion(this.question) ? " §7(Final)" : ""));
- builder.addLore(" ");
- builder.addLore("§7" + question.getQuestion());
- builder.addLore(" ");
- for (int i = 0; i < question.getAlternatives().size(); i++) {
- builder.addLore((profile.getActiveSurvey().getCurrentAnswer() == i ? " §6» " : "") + "§eAlt #" + (i + 1) + " §7| §f" + question.getAlternatives().get(i) + "");
- }
- builder.addLore(" ");
- builder.addLore("§aClick to switch alternative!");
- builder.addLore(" ");
- return builder.build();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement