mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-05 18:27:17 +01:00
Add a tab completion API for chat messages. Adds BUKKIT-2607
This implementation provides access to a (mutable) list and the base message. Also provided is a convenience method for getting the last 'token' in the provided string. By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
parent
14c7734fb1
commit
8848de9887
2 changed files with 96 additions and 0 deletions
|
@ -0,0 +1,68 @@
|
|||
package org.bukkit.event.player;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
/**
|
||||
* Called when a player attempts to tab-complete a chat message.
|
||||
*/
|
||||
public class PlayerChatTabCompleteEvent extends PlayerEvent {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private final String message;
|
||||
private final String lastToken;
|
||||
private final Collection<String> completions;
|
||||
|
||||
public PlayerChatTabCompleteEvent(final Player who, final String message, final Collection<String> completions) {
|
||||
super(who);
|
||||
Validate.notNull(message, "Message cannot be null");
|
||||
Validate.notNull(completions, "Completions cannot be null");
|
||||
this.message = message;
|
||||
int i = message.lastIndexOf(' ');
|
||||
if (i < 0) {
|
||||
this.lastToken = message;
|
||||
} else {
|
||||
this.lastToken = message.substring(i + 1);
|
||||
}
|
||||
this.completions = completions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the chat message being tab-completed.
|
||||
*
|
||||
* @return the chat message
|
||||
*/
|
||||
public String getChatMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last 'token' of the message being tab-completed.
|
||||
* The token is the substring starting with the character after the last space in the message.
|
||||
*
|
||||
* @return The last token for the chat message
|
||||
*/
|
||||
public String getLastToken() {
|
||||
return lastToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the collection of completions for this event.
|
||||
*
|
||||
* @return the current completions
|
||||
*/
|
||||
public Collection<String> getTabCompletions() {
|
||||
return completions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package org.bukkit.event;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.bukkit.event.player.PlayerChatTabCompleteEvent;
|
||||
import org.bukkit.plugin.messaging.TestPlayer;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class PlayerChatTabCompleteEventTest {
|
||||
|
||||
@Test
|
||||
public void testGetLastToken() {
|
||||
assertThat(getToken("Hello everyone!"), is("everyone!"));
|
||||
assertThat(getToken(" welcome to the show..."), is("show..."));
|
||||
assertThat(getToken("The whitespace is here "), is(""));
|
||||
assertThat(getToken("Too much whitespace is here "), is(""));
|
||||
assertThat(getToken("The_whitespace_is_missing"), is("The_whitespace_is_missing"));
|
||||
assertThat(getToken(""), is(""));
|
||||
assertThat(getToken(" "), is(""));
|
||||
}
|
||||
|
||||
private String getToken(String message) {
|
||||
return new PlayerChatTabCompleteEvent(TestPlayer.getInstance(), message, ImmutableList.<String>of()).getLastToken();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue