0

Basically what I am trying to do is have the player respond to a message in which they are required to input numbers only. From that point, I could parse the String into an int and use it towards the rest of my code. Also, I am trying to make it so this occurs in my event method. Any help is greatly appreciated!

J Alex
  • 25
  • 7

2 Answers2

0

What you essentially want to do is store the player in a container until the next time they talk, then remove them. This, represented in pseudocode, would look like the following:

on your condition:
    add player to collection

on player chat:
    does the player exist in the collection?
        yes: is input a valid number?
            yes: proceed with execution, remove player from collection after
            no: print error
        no: ignore, let event pass

Since the MineCraft protocol does not allow input verifying, there will be cases where the user may submit non-numerical characters. Integer.parseInt, or its sibling valueOf will throw an exception if this is the case.

To prevent memory leaks, you should remove the player from the collection when they log off. Alternatively, you could store them in a weak reference container. A good one for this scenario would be a WeakSet, which you can essentially obtain via Collections.newSetFromMap(new WeakHashMap()). Weak references get garbage-collected if all other references are eliminated, so this reduces the risk of a memory leak.

Xyene
  • 2,304
  • 20
  • 36
0

You should look into the bukkit conversation API. It for doing exactly this. You can find tutorials online, but basically to set it up you do this.

  1. Build a conversation with the ConversationFactory

    ConversationFactory HudConvo = new ConversationFactory(plugin)
    .withModality(true)
    .withEscapeSequence("exit")
    .withFirstPrompt(new HudConversationMain(plugin, player, 0))
    .withLocalEcho(false);
    
    
    Conversation conversation = HudConvo.buildConversation((Conversable) player);
    
  2. Begin the conversation

    conversation.begin();
    
  3. Make the first prompt as a class that either extends one of the input type prompts (i.e. StringPrompt) or implements the Prompt abstract class.

  4. Fill in the methods getPromptText() and acceptInput(). getPromptText() constructs the message to be displayed to the player and acceptInput() takes what the player types and reacts to it with a new prompt.

I hope this helped. If you have questions, feel free to ask.

Kammeot
  • 469
  • 7
  • 18