Add ability to edit string messages from StringListGUI via Chat.

This commit is contained in:
BONNe 2019-04-26 20:59:52 +03:00
parent ec099cee9c
commit 507c68d683
2 changed files with 142 additions and 17 deletions

View File

@ -2,13 +2,19 @@ package world.bentobox.challenges.panel.util;
import org.bukkit.Material;
import org.bukkit.conversations.*;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.wesjd.anvilgui.AnvilGUI;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -66,6 +72,8 @@ public class StringListGUI
panelBuilder.item(5, this.getButton(Button.REMOVE));
panelBuilder.item(6, this.getButton(Button.CLEAR));
panelBuilder.item(8, this.getButton(Button.MODE));
panelBuilder.item(44, this.getButton(Button.CANCEL));
int slot = 10;
@ -139,14 +147,25 @@ public class StringListGUI
description = Collections.emptyList();
icon = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
clickHandler = (panel, user, clickType, slot) -> {
new AnvilGUI(BentoBox.getInstance(),
this.user.getPlayer(),
" ",
(player, reply) -> {
this.value.add(reply);
this.build();
return reply;
});
if (this.useAnvil)
{
new AnvilGUI(BentoBox.getInstance(),
this.user.getPlayer(),
" ",
(player, reply) -> {
this.value.add(reply);
this.build();
return reply;
});
}
else
{
this.startConversion(value ->
this.value.add(value),
this.user.getTranslation("challenges.gui.descriptions.admin.add-text-line"));
}
return true;
};
break;
@ -176,6 +195,18 @@ public class StringListGUI
};
break;
}
case MODE:
{
name = this.user.getTranslation("challenges.gui.buttons.admin.input-mode");
description = Collections.singletonList(this.user.getTranslation("challenges.gui.descriptions.admin.input-mode"));
icon = this.useAnvil ? new ItemStack(Material.ANVIL) : new ItemStack(Material.MAP);
clickHandler = (panel, user, clickType, slot) -> {
this.useAnvil = !this.useAnvil;
panel.getInventory().setItem(slot, this.getButton(button).getItem());
return true;
};
break;
}
default:
return null;
}
@ -201,19 +232,102 @@ public class StringListGUI
name(element).
icon(Material.PAPER).
clickHandler((panel, user1, clickType, i) -> {
new AnvilGUI(BentoBox.getInstance(),
this.user.getPlayer(),
element,
(player, reply) -> {
this.value.set(stringIndex, reply);
this.build();
return reply;
});
if (this.useAnvil)
{
new AnvilGUI(BentoBox.getInstance(),
this.user.getPlayer(),
element,
(player, reply) -> {
this.value.set(stringIndex, reply);
this.build();
return reply;
});
}
else
{
this.startConversion(
value -> this.value.set(stringIndex, value),
this.user.getTranslation("challenges.gui.descriptions.admin.edit-text-line"),
element);
}
return true;
}).build();
}
/**
* 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.
* @param consumer Consumer that accepts player output text.
* @param question Message that will be displayed in chat when player triggers conversion.
*/
private void startConversion(Consumer<String> consumer, @NonNull String question)
{
this.startConversion(consumer, question, null);
}
/**
* 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.
* @param consumer Consumer that accepts player output text.
* @param question Message that will be displayed in chat when player triggers conversion.
* @param message Message that will be set in player text field when clicked on question.
*/
private void startConversion(Consumer<String> consumer, @NonNull String question, @Nullable String message)
{
final User user = this.user;
Conversation conversation =
new ConversationFactory(BentoBox.getInstance()).withFirstPrompt(
new StringPrompt()
{
/**
* @see Prompt#getPromptText(ConversationContext)
*/
@Override
public String getPromptText(ConversationContext conversationContext)
{
// Close input GUI.
user.closeInventory();
if (message != null)
{
// Create Edit Text message.
TextComponent component = new TextComponent(user.getTranslation("challenges.gui.descriptions.admin.click-to-edit"));
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message));
// Send question and message to player.
user.getPlayer().spigot().sendMessage(component);
}
// There are no editable message. Just return question.
return question;
}
/**
* @see Prompt#acceptInput(ConversationContext, String)
*/
@Override
public Prompt acceptInput(ConversationContext conversationContext, String answer)
{
// Add answer to consumer.
consumer.accept(answer);
// Reopen GUI
StringListGUI.this.build();
// End conversation
return Prompt.END_OF_CONVERSATION;
}
}).
withLocalEcho(false).
buildConversation(user.getPlayer());
conversation.begin();
}
// ---------------------------------------------------------------------
// Section: Enums
// ---------------------------------------------------------------------
@ -229,7 +343,8 @@ public class StringListGUI
REMOVE,
CANCEL,
CLEAR,
SAVE
SAVE,
MODE
}
@ -248,6 +363,11 @@ public class StringListGUI
*/
private User user;
/**
* Boolean that indicate if editing should happen in anvil.
*/
private boolean useAnvil;
/**
* Current value.
*/

View File

@ -141,6 +141,7 @@ challenges:
history-lifespan: 'History LifeSpan'
island-store: 'Store per Island'
default-locked-icon: 'Locked Level Icon'
input-mode: 'Switch input mode'
next: 'Next'
previous: 'Previous'
return: 'Return'
@ -228,6 +229,10 @@ challenges:
island-store: 'Allows to enable/disable challenges data string per island. This means that challenges will be the same on whole team, if this is enabled.|Will NOT convert data on click. PROGRESS WILL BE LOST.'
default-locked-icon: 'Allows to change default locked level icon.|This option can be overwritten by each level.'
gui-mode: 'Allows to enable/disable single challenges GUI.|&2Requires server restart.'
click-to-edit: '&4Click here to edit input.'
edit-text-line: '&6 Edit text message!'
add-text-line: '&6 Add new text message!'
input-mode: 'Switch between chat and anvil input modes.'
current-value: '|&6Current value: [value].'
enabled: 'Active'
disabled: 'Disabled'