Skip to main content

🖥️ API usage

Use the nChat API to develop other plugins.

Adding the API

Maven:

<repositories>
<repository>
<id>nickuc-repo</id>
<url>https://repo.nickuc.com/maven-releases/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.nickuc.chat</groupId>
<artifactId>api</artifactId>
<version>5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.nickuc.chat</groupId>
<artifactId>api</artifactId>
<version>5.6</version>
<scope>javadocs</scope>
</dependency>
</dependencies>

Gradle:

repositories {
maven {
url = uri('https://repo.nickuc.com/maven-releases/')
}
}

dependencies {
compileOnly('com.nickuc.chat:api:5.6')
}

JavaDocs

JavaDocs

Access JavaDocs

Listening to events

All events have the following characteristics:

  • They are asynchronous, they execute in parallel to the server thread.
  • Public or private
    • Public: if the message is seen by several players (e.g. global and local chat).
    • Private: if the message has a sender and a recipient (e.g. tell).
  • Real or Virtual:
    • Real: if the message was created by a player (e.g. global, local and tell chat).
    • Virtual: if the message was created without a player (e.g. message generated by a plugin).
  • Local or Remote (proxy):
    • Local: the message was only processed on one server.
    • Remote: the message was processed to be received on multiple servers.
Event Information:

There is a limitation when defining a tag in events: it will not be possible to use it as a placeholder in custom tags.

In that case...

  • Register the tag with PlaceholderAPI (or a placeholder plugin).
    • The tag will be in this format: %tag-name%.
  • Register a global tag with the nChat API.
    • The tag will be in this format: {tag-name}.

Examples:

Tycoon (using md5-chat)

// Message received by any public channel, real and local/remote (global, local, etc.)
@EventHandler
public void onPublicMessage(PublicMessageEvent event) {
if (event.isCancelled()) {
return;
}

TextComponent component = new TextComponent(TextComponent.fromLegacyText("§2[$]"));
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text(TextComponent.fromLegacyText("§7Server tycoon."))));

event.setTag("tycoon", Component.from(component));
}

// Message received by any private channel, real and local/remote (tell)
@EventHandler
public void onPrivateMessage(PrivateMessageEvent event) {
if (event.isCancelled()) {
return;
}

TextComponent component = new TextComponent(TextComponent.fromLegacyText("§2[$]"));
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text(TextComponent.fromLegacyText("§7Server tycoon."))));

event.setTag("tycoon", Component.from(component), PrivateMessageEvent.Type.ALL);
}

Tycoon (using adventure)

// Message received by any public channel, real and local/remote (global, local, etc.)
@EventHandler
public void onPublicMessage(PublicMessageEvent event) {
if (event.isCancelled()) {
return;
}

HoverEvent<Component> hoverEvent = HoverEvent.showText(
Component.text("Server tycoon.").color(NamedTextColor.GRAY));

TextComponent component = Component.text("[$]")
.color(NamedTextColor.DARK_GREEN)
.hoverEvent(hoverEvent);

event.setTag("tycoon", com.nickuc.chat.api.util.Component.from(component));
}

// Message received by any private channel, real and local/remote (tell)
@EventHandler
public void onPrivateMessage(PrivateMessageEvent event) {
if (event.isCancelled()) {
return;
}

HoverEvent<Component> hoverEvent = HoverEvent.showText(
Component.text("Server tycoon.").color(NamedTextColor.GRAY));

TextComponent component = Component.text("[$]")
.color(NamedTextColor.DARK_GREEN)
.hoverEvent(hoverEvent);

event.setTag("tycoon", com.nickuc.chat.api.util.Component.from(component), PrivateMessageEvent.Type.ALL);
}