mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-09-28 22:48:00 +02:00
Add ability to edit string messages from StringListGUI via Chat.
This commit is contained in:
parent
ec099cee9c
commit
507c68d683
@ -2,13 +2,19 @@ package world.bentobox.challenges.panel.util;
|
|||||||
|
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.conversations.*;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.BiConsumer;
|
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 net.wesjd.anvilgui.AnvilGUI;
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -66,6 +72,8 @@ public class StringListGUI
|
|||||||
panelBuilder.item(5, this.getButton(Button.REMOVE));
|
panelBuilder.item(5, this.getButton(Button.REMOVE));
|
||||||
panelBuilder.item(6, this.getButton(Button.CLEAR));
|
panelBuilder.item(6, this.getButton(Button.CLEAR));
|
||||||
|
|
||||||
|
panelBuilder.item(8, this.getButton(Button.MODE));
|
||||||
|
|
||||||
panelBuilder.item(44, this.getButton(Button.CANCEL));
|
panelBuilder.item(44, this.getButton(Button.CANCEL));
|
||||||
|
|
||||||
int slot = 10;
|
int slot = 10;
|
||||||
@ -139,6 +147,9 @@ public class StringListGUI
|
|||||||
description = Collections.emptyList();
|
description = Collections.emptyList();
|
||||||
icon = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
|
icon = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
|
||||||
clickHandler = (panel, user, clickType, slot) -> {
|
clickHandler = (panel, user, clickType, slot) -> {
|
||||||
|
|
||||||
|
if (this.useAnvil)
|
||||||
|
{
|
||||||
new AnvilGUI(BentoBox.getInstance(),
|
new AnvilGUI(BentoBox.getInstance(),
|
||||||
this.user.getPlayer(),
|
this.user.getPlayer(),
|
||||||
" ",
|
" ",
|
||||||
@ -147,6 +158,14 @@ public class StringListGUI
|
|||||||
this.build();
|
this.build();
|
||||||
return reply;
|
return reply;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.startConversion(value ->
|
||||||
|
this.value.add(value),
|
||||||
|
this.user.getTranslation("challenges.gui.descriptions.admin.add-text-line"));
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
@ -176,6 +195,18 @@ public class StringListGUI
|
|||||||
};
|
};
|
||||||
break;
|
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:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -201,6 +232,9 @@ public class StringListGUI
|
|||||||
name(element).
|
name(element).
|
||||||
icon(Material.PAPER).
|
icon(Material.PAPER).
|
||||||
clickHandler((panel, user1, clickType, i) -> {
|
clickHandler((panel, user1, clickType, i) -> {
|
||||||
|
|
||||||
|
if (this.useAnvil)
|
||||||
|
{
|
||||||
new AnvilGUI(BentoBox.getInstance(),
|
new AnvilGUI(BentoBox.getInstance(),
|
||||||
this.user.getPlayer(),
|
this.user.getPlayer(),
|
||||||
element,
|
element,
|
||||||
@ -209,11 +243,91 @@ public class StringListGUI
|
|||||||
this.build();
|
this.build();
|
||||||
return reply;
|
return reply;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.startConversion(
|
||||||
|
value -> this.value.set(stringIndex, value),
|
||||||
|
this.user.getTranslation("challenges.gui.descriptions.admin.edit-text-line"),
|
||||||
|
element);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}).build();
|
}).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
|
// Section: Enums
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
@ -229,7 +343,8 @@ public class StringListGUI
|
|||||||
REMOVE,
|
REMOVE,
|
||||||
CANCEL,
|
CANCEL,
|
||||||
CLEAR,
|
CLEAR,
|
||||||
SAVE
|
SAVE,
|
||||||
|
MODE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -248,6 +363,11 @@ public class StringListGUI
|
|||||||
*/
|
*/
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boolean that indicate if editing should happen in anvil.
|
||||||
|
*/
|
||||||
|
private boolean useAnvil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current value.
|
* Current value.
|
||||||
*/
|
*/
|
||||||
|
@ -141,6 +141,7 @@ challenges:
|
|||||||
history-lifespan: 'History LifeSpan'
|
history-lifespan: 'History LifeSpan'
|
||||||
island-store: 'Store per Island'
|
island-store: 'Store per Island'
|
||||||
default-locked-icon: 'Locked Level Icon'
|
default-locked-icon: 'Locked Level Icon'
|
||||||
|
input-mode: 'Switch input mode'
|
||||||
next: 'Next'
|
next: 'Next'
|
||||||
previous: 'Previous'
|
previous: 'Previous'
|
||||||
return: 'Return'
|
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.'
|
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.'
|
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.'
|
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].'
|
current-value: '|&6Current value: [value].'
|
||||||
enabled: 'Active'
|
enabled: 'Active'
|
||||||
disabled: 'Disabled'
|
disabled: 'Disabled'
|
||||||
|
Loading…
Reference in New Issue
Block a user