Skip to content

Commit 2e24268

Browse files
authored
Merge pull request #1200 from PlaceholderAPI/feat/update-hytale-example
update hytale example
2 parents a1f1430 + 199cd2d commit 2e24268

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

docs/developers/using-placeholderapi.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,47 +323,71 @@ public class JoinExample extends JavaPlugin implements Listener {
323323

324324
/// tab | Hytale
325325

326-
The following is an example plugin that sends `Welcome %player_name%!` as the Join message, having the placeholders be replaced by PlaceholderAPI.
327-
Keeping in mind the [Hytale Player Expansion](https://ecloud.placeholderapi.com/expansions/player-hytale/) needs to be installed to make use of `%player_<identifier>%` placeholders.
326+
The following is an example plugin that modifies the chat to include a format before player messages. It relies on the [Player Expansion](https://ecloud.placeholderapi.com/expansions/player-hytale/), [Luckperms Expansion](https://ecloud.placeholderapi.com/expansions/luckperms-hytale/), and [HyFaction Expansion](https://ecloud.placeholderapi.com/expansions/hyfaction/).
328327

329328
//// info | Thread Safety Warning
330329
Due to Hytale's api design, certain components can only be accessed by specific threads. For full compatibility with placeholderapi, you need to call setPlaceholders on the world thread the player is in. `player.getWorld().execute(() -> )`
331-
By default for player events and player commands, you're already going to be on the world thread so it's not so much of an issue, but you do need to consider it if you're trying to do async work and then call PAPI, e.g. async player chat.
330+
By default for player commands, you're already going to be on the world thread so it's not so much of an issue, but you do need to consider it if you're trying to do async work and then call PAPI, e.g. async player chat.
332331
////
333332

334-
``` { .java .annotate title="JoinExample.java" }
333+
``` { .java .annotate title="ChatExample.java" }
335334
packate com.example.plugin;
336335

337336
import at.helpch.placeholderapi.PlaceholderAPI;
338337

339-
import com.hypixel.hytale.server.core.event.events.player.PlayerReadyEvent;
338+
import com.hypixel.hytale.server.core.event.events.player.PlayerChatEvent;
340339
import com.hypixel.hytale.server.core.Message;
341340
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
342341
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
342+
import com.hypixel.hytale.server.core.universe.PlayerRef;
343343
import com.hypixel.hytale.server.core.universe.Universe;
344+
import com.hypixel.hytale.server.core.universe.world.World;
344345

345-
public class JoinExample extends JavaPlugin {
346+
import java.util.UUID;
346347

347-
public JoinExample(JavaPluginInit init) {
348+
public class ChatExample extends JavaPlugin {
349+
private static final String FORMAT = "%rel_factions_relation% [%luckperms_prefix%] %player_name% > "
350+
351+
public ChatExample(JavaPluginInit init) {
348352
super(init)
349353
}
350354

351355
@Override
352356
protected void setup() {
353357
// (1)
354-
getEventRegistry().registerGlobal(PlayerReadyEvent.class, this::onPlayerReady));
358+
getEventRegistry().registerGlobal(PlayerChatEvent.class, this::onPlayerChat));
355359
}
356360

357-
public void onPlayerReady(PlayerReadyEvent event) {
358-
Player player = event.getPlayer();
361+
public void onPlayerChat(PlayerChatEvent event) {
359362
// (2)
360-
player.sendMessage(PlaceholderAPI.setPlaceholders(player.getPlayerRef(), Message.raw("Welcome %player_name%!")))
361-
363+
PlayerRef sender = event.getSender();
364+
final UUID worldUuid = sender.getWorldUuid();
365+
366+
if (worldUuid == null) {
367+
return;
368+
}
369+
370+
final World world = Universe.get().getWorld(worldUuid);
371+
372+
if (world == null) {
373+
return;
374+
} else {
375+
event.setCancelled(true); // cancel event and run our own sending logic
376+
// call your own chat event perhaps?
377+
// HytaleServer.get().getEventBus().dispatchForAsync(CustomChatEvent.class);
378+
}
379+
380+
world.execute(() -> {
381+
String replacedFormat = PlaceholderAPI.setPlaceholders(sender, FORMAT);
382+
for (PlayerRef recipient : event.getTargets()) {
383+
recipient.sendMessage(Message.raw(PlaceholderAPI.setRelationalPlaceholders(sender, recipient, replacedFormat) + event.getContent()));
384+
}
385+
});
362386
}
363387
}
364388
```
365389

366-
1. We tell the server to call `onPlayerReady` whenever a `PlayerReadyEvent` fires.
390+
1. We tell the server to call `onPlayerChat` whenever a `PlayerChatEvent` fires.
367391
2. PlaceholderAPI offers multiple `setPlaceholders` methods that can either return a `String` or a `Message` object, depending on your needs.
368392
Note that these methods require input of the same type: `setPlaceholders(PlayerRef, String)` for String and `setPlaceholders(PlayerRef, Message)` for Messages.
369393

0 commit comments

Comments
 (0)