Skip to content

Commit

Permalink
First Admin Panel implementation...
Browse files Browse the repository at this point in the history
Add all buttons (empty) that will be in the Panel.

Implement confirmation Conversation that requires user input for certain operations.
  • Loading branch information
BONNe committed Oct 7, 2020
1 parent f1cc45f commit 6c84381
Show file tree
Hide file tree
Showing 4 changed files with 545 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;
import world.bentobox.magiccobblestonegenerator.panels.admin.GeneratorAdminPanel;
import world.bentobox.magiccobblestonegenerator.utils.Constants;
import world.bentobox.magiccobblestonegenerator.utils.Utils;

Expand Down Expand Up @@ -73,15 +74,14 @@ public void setup()
@Override
public boolean execute(User user, String label, List<String> args)
{
// if (args.isEmpty())
// {
// TODO: Need to create and implement admin command.
// AdminPanel.openPanel(this.getAddon(), this.getWorld(), user);
// }
// else
// {
// this.showHelp(this, user);
// }
if (args.isEmpty())
{
GeneratorAdminPanel.openPanel(this.getAddon(), this.getWorld(), user);
}
else
{
this.showHelp(this, user);
}

this.showHelp(this, user);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
//
// Created by BONNe
// Copyright - 2020
//


package world.bentobox.magiccobblestonegenerator.panels;


import org.apache.commons.lang.ArrayUtils;
import org.bukkit.conversations.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.magiccobblestonegenerator.utils.Constants;
import world.bentobox.magiccobblestonegenerator.utils.Utils;


public class ConversationUtils
{
// ---------------------------------------------------------------------
// Section: Conversation API implementation
// ---------------------------------------------------------------------


/**
* This method will close opened gui and writes inputText in chat. After players answers on
* inputText in chat, message will trigger consumer and gui will reopen.
* Success and fail messages can be implemented like that, as user's chat options are disabled
* while it is in conversation.
* @param consumer Consumer that accepts player output text.
* @param question Message that will be displayed in chat when player triggers conversion.
* @param successMessage Message that will be displayed on successful operation.
* @param failMessage Message that will be displayed on failed operation.
* @param user User who is targeted with current confirmation.
*/
public static void createConfirmation(Consumer<Boolean> consumer,
@NotNull String question,
@Nullable String successMessage,
@Nullable String failMessage,
User user)
{
ValidatingPrompt confirmationPrompt = new ValidatingPrompt()
{
/**
* Is input valid boolean.
*
* @param context the context
* @param input the input
* @return the boolean
*/
protected boolean isInputValid(@NotNull ConversationContext context, @NotNull String input)
{
String[] accepted = new String[]{
"true", "false",
"on", "off",
"yes", "no",
"y", "n",
"1", "0",
"right", "wrong",
"correct", "incorrect",
"valid", "invalid",
"confirm", "deny",
"cancel",
"exit"
};
return ArrayUtils.contains(accepted, input.toLowerCase());
}


/**
* Accept validated input prompt.
*
* @param context the context
* @param input the input
* @return the prompt
*/
@Nullable
protected Prompt acceptValidatedInput(@NotNull ConversationContext context, @NotNull String input)
{
if (input.equalsIgnoreCase("true") ||
input.equalsIgnoreCase("on") ||
input.equalsIgnoreCase("yes") ||
input.equalsIgnoreCase("y") ||
input.equals("1") ||
input.equalsIgnoreCase("right") ||
input.equalsIgnoreCase("correct") ||
input.equalsIgnoreCase("valid") ||
input.equalsIgnoreCase("confirm"))
{
// Add answer to consumer.
consumer.accept(true);

if (successMessage != null)
{
// Return message about successful operation.
return new MessagePrompt()
{
@Override
protected @Nullable Prompt getNextPrompt(@NotNull ConversationContext context)
{
return Prompt.END_OF_CONVERSATION;
}


@Override
public @NotNull String getPromptText(@NotNull ConversationContext context)
{
return successMessage;
}
};
}
}
else
{
// Add answer to consumer.
consumer.accept(false);

// Return message about failed operation.
if (failMessage != null)
{
return new MessagePrompt()
{
@Override
protected @Nullable Prompt getNextPrompt(@NotNull ConversationContext context)
{
return Prompt.END_OF_CONVERSATION;
}


@Override
public @NotNull String getPromptText(@NotNull ConversationContext context)
{
return failMessage;
}
};
}
}

return Prompt.END_OF_CONVERSATION;
}


/**
* @see Prompt#getPromptText(ConversationContext)
*/
@Override
public @NotNull String getPromptText(@NotNull ConversationContext conversationContext)
{
// Close input GUI.
user.closeInventory();
// There are no editable message. Just return question.
return question;
}
};

Conversation conversation = new ConversationFactory(BentoBox.getInstance()).
withFirstPrompt(confirmationPrompt).
withLocalEcho(false).
withPrefix(context -> user.getTranslation(Constants.QUESTIONS + "prefix")).
buildConversation(user.getPlayer());

conversation.begin();
}
}

0 comments on commit 6c84381

Please sign in to comment.