dev/rework
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
package com.alttd.playershops.conversation;
|
||||
|
||||
import com.alttd.playershops.gui.ShopManagementGui;
|
||||
import com.alttd.playershops.shop.PlayerShop;
|
||||
import com.alttd.playershops.shop.ShopType;
|
||||
import com.alttd.playershops.utils.EconomyUtils;
|
||||
import com.google.common.base.Joiner;
|
||||
import org.bukkit.conversations.*;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ConversationManager implements ConversationAbandonedListener {
|
||||
|
||||
PlayerShop playerShop;
|
||||
Player player;
|
||||
Conversation conversation;
|
||||
|
||||
public ConversationManager(JavaPlugin plugin, Player player, ConversationType conversationType, PlayerShop playerShop) {
|
||||
this.player = player;
|
||||
this.playerShop = playerShop;
|
||||
ConversationFactory conversationFactory = new ConversationFactory(plugin)
|
||||
.withModality(true)
|
||||
.withFirstPrompt(getPrompt(conversationType))
|
||||
.withEscapeSequence("cancel");
|
||||
conversation = conversationFactory.buildConversation(player);
|
||||
conversation.begin();
|
||||
}
|
||||
|
||||
Prompt getPrompt(ConversationType conversationType) {
|
||||
switch (conversationType) {
|
||||
case CHANGE_ITEM -> {
|
||||
return new ChangeItemPrompt();
|
||||
}
|
||||
case CHANGE_AMOUNT -> {
|
||||
return new ChangeAmountPrompt();
|
||||
}
|
||||
case CHANGE_TYPE -> {
|
||||
return new ChangeTypePrompt();
|
||||
}
|
||||
case WITHDRAW_BALANCE -> {
|
||||
return new WithdrawBalancePrompt();
|
||||
}
|
||||
case ADD_BALANCE -> {
|
||||
return new AddBalancePrompt();
|
||||
}
|
||||
case CHANGE_PRICE -> {
|
||||
return new ChangePricePrompt();
|
||||
}
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
private void openGui() {
|
||||
player.abandonConversation(conversation);
|
||||
ShopManagementGui shopManagementGui = new ShopManagementGui(player.getUniqueId(), playerShop);
|
||||
shopManagementGui.open();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void conversationAbandoned(@NotNull ConversationAbandonedEvent abandonedEvent) {
|
||||
// abandonedEvent.getContext().getForWhom().sendRawMessage("Conversation ended.");
|
||||
}
|
||||
|
||||
private class ChangeTypePrompt extends FixedSetPrompt {
|
||||
|
||||
public ChangeTypePrompt() {
|
||||
super("buy", "sell", "none"); // todo can this be automated shoptype.values()
|
||||
}
|
||||
|
||||
public @NotNull String getPromptText(ConversationContext context) {
|
||||
return "What shoptype would you like to use: " + Joiner.on(", ").join(fixedSet) + " Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, String input) {
|
||||
ShopType newType = ShopType.fromString(input);
|
||||
if (playerShop.getType() == newType) {
|
||||
context.getForWhom().sendRawMessage("Shoptype was already set to " + newType.toString() + " Type cancel to cancel this action.");
|
||||
return new ChangeTypePrompt();
|
||||
}
|
||||
playerShop.setShopType(newType);
|
||||
openGui();
|
||||
return END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class ChangePricePrompt extends NumericPrompt {
|
||||
|
||||
public @NotNull String getPromptText(ConversationContext context) {
|
||||
return "What should the price be? Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isNumberValid(ConversationContext context, Number input) {
|
||||
return input.doubleValue() >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
|
||||
return "Input must 0 or higher. Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, Number number) {
|
||||
playerShop.setPrice(number.doubleValue());
|
||||
openGui();
|
||||
return END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class ChangeAmountPrompt extends NumericPrompt {
|
||||
|
||||
public @NotNull String getPromptText(ConversationContext context) {
|
||||
return "How many items would you like to sell? Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isNumberValid(ConversationContext context, Number input) {
|
||||
return input.intValue() > 0 && input.intValue() <= 3456;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
|
||||
return "Input must be between 1 and 3456. Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, Number number) {
|
||||
playerShop.setAmount(number.intValue());
|
||||
openGui();
|
||||
return END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class AddBalancePrompt extends NumericPrompt {
|
||||
|
||||
public @NotNull String getPromptText(ConversationContext context) {
|
||||
return "How much money would you like to add? Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isNumberValid(ConversationContext context, Number input) {
|
||||
if (input.doubleValue() < 0)
|
||||
return false;
|
||||
return EconomyUtils.hasSufficientFunds(player, input.doubleValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
|
||||
return "You do not have enough balance to deposit this amount. Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, Number number) {
|
||||
playerShop.addBalance(number.doubleValue());
|
||||
EconomyUtils.removeFunds(player, number.doubleValue());
|
||||
openGui();
|
||||
return END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class WithdrawBalancePrompt extends NumericPrompt {
|
||||
|
||||
public @NotNull String getPromptText(ConversationContext context) {
|
||||
return "How much money would you like to withdraw? Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isNumberValid(ConversationContext context, Number input) {
|
||||
if (input.doubleValue() < 0)
|
||||
return false;
|
||||
return EconomyUtils.hasSufficientFunds(playerShop, input.doubleValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
|
||||
return "Your shop does not have enough balance to withdraw this amount. Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, Number number) {
|
||||
playerShop.removeBalance(number.doubleValue());
|
||||
EconomyUtils.addFunds(player, number.doubleValue());
|
||||
openGui();
|
||||
return END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class ChangeItemPrompt extends FixedSetPrompt {
|
||||
|
||||
ChangeItemPrompt() {
|
||||
super("continue");
|
||||
}
|
||||
|
||||
public @NotNull String getPromptText(ConversationContext context) {
|
||||
return "Hold the item you would like to sell and type continue? Type cancel to cancel this action.";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, String input) {
|
||||
playerShop.setItemStack(player.getInventory().getItemInMainHand().clone());
|
||||
openGui();
|
||||
return END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.alttd.playershops.conversation;
|
||||
|
||||
public enum ConversationType {
|
||||
CHANGE_ITEM,
|
||||
CHANGE_AMOUNT,
|
||||
CHANGE_TYPE,
|
||||
CHANGE_PRICE,
|
||||
WITHDRAW_BALANCE,
|
||||
ADD_BALANCE;
|
||||
}
|
||||
Reference in New Issue
Block a user