-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add prompts * Add missing permission test
- Loading branch information
1 parent
af6310f
commit 3593f45
Showing
5 changed files
with
159 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/com/palmergames/bukkit/towny/conversation/ResidentConversation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.palmergames.bukkit.towny.conversation; | ||
|
||
import com.palmergames.bukkit.towny.Towny; | ||
import com.palmergames.bukkit.towny.TownyAPI; | ||
import com.palmergames.bukkit.towny.TownyMessaging; | ||
import com.palmergames.bukkit.towny.object.Resident; | ||
import com.palmergames.bukkit.towny.object.Translatable; | ||
import org.bukkit.conversations.ConversationContext; | ||
import org.bukkit.conversations.ConversationFactory; | ||
import org.bukkit.conversations.Prompt; | ||
import org.bukkit.conversations.StringPrompt; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* A conversation that is used for getting resident input via chat. | ||
*/ | ||
public class ResidentConversation extends TownyConversation { | ||
public ResidentConversation(Player player) { | ||
new ConversationFactory(Towny.getPlugin()) | ||
.withPrefix(this) | ||
.addConversationAbandonedListener(this) | ||
.withFirstPrompt(new ResidentPrompt()) | ||
.withEscapeSequence("q") | ||
.withTimeout(30) | ||
.withLocalEcho(false) | ||
.withModality(false) | ||
.buildConversation(player) | ||
.begin(); | ||
} | ||
|
||
private class ResidentPrompt extends StringPrompt { | ||
public ResidentPrompt() { | ||
super(); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getPromptText(@NotNull ConversationContext context) { | ||
return Translatable.of("msg_resident_prompt").forLocale(getPlayer(context)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Prompt acceptInput(@NotNull ConversationContext context, @Nullable String string) { | ||
if (string != null) { | ||
Resident resident = TownyAPI.getInstance().getResident(string); | ||
|
||
if (resident == null) | ||
TownyMessaging.sendErrorMsg(getPlayer(context), Translatable.of("msg_err_not_registered_1", string)); | ||
else if (consumer != null) | ||
consumer.accept(resident); | ||
} | ||
|
||
return Prompt.END_OF_CONVERSATION; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/com/palmergames/bukkit/towny/conversation/TownyConversation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.palmergames.bukkit.towny.conversation; | ||
|
||
import com.palmergames.bukkit.towny.TownyMessaging; | ||
import com.palmergames.bukkit.towny.object.Translatable; | ||
import org.bukkit.conversations.ConversationAbandonedEvent; | ||
import org.bukkit.conversations.ConversationAbandonedListener; | ||
import org.bukkit.conversations.ConversationContext; | ||
import org.bukkit.conversations.ConversationPrefix; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class TownyConversation implements ConversationPrefix, ConversationAbandonedListener { | ||
|
||
public Consumer<Object> consumer; | ||
|
||
@NotNull | ||
@Override | ||
public String getPrefix(@NotNull ConversationContext context) { | ||
return Translatable.of("default_towny_prefix").forLocale(getPlayer(context)); | ||
} | ||
|
||
public Player getPlayer(@NotNull ConversationContext context) { | ||
return (Player) context.getForWhom(); | ||
} | ||
|
||
public void runOnResponse(Consumer<Object> consumer) { | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public void conversationAbandoned(@NotNull ConversationAbandonedEvent event) { | ||
if (!event.gracefulExit()) | ||
TownyMessaging.sendMsg(getPlayer(event.getContext()), Translatable.of("msg_prompt_cancel")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters