Skip to content

Commit

Permalink
feat: add title & toast api
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Feb 28, 2025
1 parent 3a37433 commit 7804f03
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Unless otherwise specified, any version comparison below is the comparison of se
history for more details.
- (API) Introduced `WorldStorage#readEntities`, `WorldStorage#writeEntities` and their correspond sync methods. These methods are used
to read and write entities in a specified chunk area.
- (API) Introduced a variety of methods for sending toast, title, subtitle and actionbar text to player, and new command `/title` is added.
- Add support for the new entity storage format used in 1.18.30+. Now entities in newer vanilla maps can be loaded correctly.
- Implemented reeds (also called sugar cane) and cactus.
- Implemented `UpdateSubChunkBlocksPacket` related logic, which will make client load large range block updates much quicker (e.g.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,57 @@ default void setFlying(boolean flying) {
*/
void sendPopup(String message);

/**
* Send a toast to the player. The toast will be displayed at the top of the screen
* with a title and content in a period of time.
*
* @param title the title of the toast.
* @param content the content of the toast.
*/
void sendToast(String title, String content);

/**
* Send a title to the player which will be shown in the middle of the screen.
*
* @param title the title to send.
*/
void sendTitle(String title);

/**
* Send a subtitle to the player which will be shown below the main title.
* If no title is set, the subtitle won't be shown, in other words title must
* be set before set subtitle.
*
* @param subtitle the subtitle to send.
*/
void sendSubtitle(String subtitle);

/**
* Send an action bar message to the player which will be shown above player's hot bar.
*
* @param actionBar the action bar message to send.
*/
void sendActionBar(String actionBar);

/**
* Set the title settings of this player. These settings will be used for the next title, subtitle and actionbar.
*
* @param fadeInTime the time in ticks for the title to fade in.
* @param duration the time in ticks for the title to stay.
* @param fadeOutTime the time in ticks for the title to fade out.
*/
void setTitleSettings(int fadeInTime, int duration, int fadeOutTime);

/**
* Reset the title settings of this player to the default values.
*/
void resetTitleSettings();

/**
* Clear the title that is being displayed on the player's screen
*/
void clearTitle();

/**
* Save the player's data.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.allaymc.server.command.defaults;

import org.allaymc.api.command.SimpleCommand;
import org.allaymc.api.command.tree.CommandTree;
import org.allaymc.api.entity.component.player.EntityPlayerBaseComponent;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.i18n.TrKeys;

import java.util.Collection;

/**
* @author daoge_cmd
*/
public class TitleCommand extends SimpleCommand {
public TitleCommand() {
super("title", TrKeys.M_COMMANDS_TITLE_DESCRIPTION);
}

@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.key("set")
.playerTarget("players")
.enumClass("type", TitleType.class)
.str("text")
.exec(context -> {
Collection<EntityPlayer> players = context.getResult(1);
TitleType type = context.getResult(2);
String text = context.getResult(3);
players.forEach(p -> sendTitle(p, text, type));
context.addOutput(TrKeys.M_COMMANDS_TITLE_SUCCESS);
return context.success();
})
.root()
.key("clear")
.playerTarget("players")
.exec(context -> {
Collection<EntityPlayer> players = context.getResult(1);
players.forEach(EntityPlayerBaseComponent::clearTitle);
context.addOutput(TrKeys.M_COMMANDS_TITLE_SUCCESS);
return context.success();
})
.root()
.key("times")
.playerTarget("players")
.intNum("fadeInTime")
.intNum("duration")
.intNum("fadeOutTime")
.exec(context -> {
Collection<EntityPlayer> players = context.getResult(1);
int fadeInTime = context.getResult(2);
int duration = context.getResult(3);
int fadeOutTime = context.getResult(4);
players.forEach(p -> p.setTitleSettings(fadeInTime, duration, fadeOutTime));
context.addOutput(TrKeys.M_COMMANDS_TITLE_SUCCESS);
return context.success();
})
.root()
.key("reset")
.playerTarget("players")
.exec(context -> {
Collection<EntityPlayer> players = context.getResult(1);
players.forEach(EntityPlayerBaseComponent::resetTitleSettings);
context.addOutput(TrKeys.M_COMMANDS_TITLE_SUCCESS);
return context.success();
});
}

protected void sendTitle(EntityPlayer player, String text, TitleType type) {
switch (type) {
case TITLE -> player.sendTitle(text);
case SUBTITLE -> player.sendSubtitle(text);
case ACTIONBAR -> player.sendActionBar(text);
}
}

protected enum TitleType {
TITLE,
SUBTITLE,
ACTIONBAR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,7 @@ public void sendCommandOutputs(CommandSender sender, int status, TrContainer...
@Override
public CommandOriginData getCommandOriginData() {
if (commandOriginData == null) {
commandOriginData = new CommandOriginData(
CommandOriginType.PLAYER,
networkComponent.getLoginData().getUuid(),
"",
-1
);
commandOriginData = new CommandOriginData(CommandOriginType.PLAYER, networkComponent.getLoginData().getUuid(), "", -1);
}
return commandOriginData;
}
Expand All @@ -596,6 +591,68 @@ public void sendPopup(String message) {
sendSimpleMessage(message, TextPacket.Type.POPUP);
}

@Override
public void sendToast(String title, String content) {
ToastRequestPacket pk = new ToastRequestPacket();
pk.setTitle(title);
pk.setContent(content);
this.sendPacket(pk);
}

@Override
public void sendTitle(String title) {
var pk = new SetTitlePacket();
pk.setText(title);
pk.setType(SetTitlePacket.Type.TITLE);
pk.setXuid("");
pk.setPlatformOnlineId("");
this.sendPacket(pk);
}

@Override
public void sendSubtitle(String subtitle) {
var pk = new SetTitlePacket();
pk.setText(subtitle);
pk.setType(SetTitlePacket.Type.SUBTITLE);
pk.setXuid("");
pk.setPlatformOnlineId("");
this.sendPacket(pk);
}

@Override
public void sendActionBar(String actionBar) {
var pk = new SetTitlePacket();
pk.setText(actionBar);
pk.setType(SetTitlePacket.Type.ACTIONBAR);
pk.setXuid("");
pk.setPlatformOnlineId("");
this.sendPacket(pk);
}

@Override
public void setTitleSettings(int fadeInTime, int duration, int fadeOutTime) {
var pk = new SetTitlePacket();
pk.setType(SetTitlePacket.Type.TIMES);
pk.setFadeInTime(fadeInTime);
pk.setFadeOutTime(fadeOutTime);
pk.setStayTime(duration);
this.sendPacket(pk);
}

@Override
public void resetTitleSettings() {
var pk = new SetTitlePacket();
pk.setType(SetTitlePacket.Type.RESET);
this.sendPacket(pk);
}

@Override
public void clearTitle() {
var pk = new SetTitlePacket();
pk.setType(SetTitlePacket.Type.CLEAR);
this.sendPacket(pk);
}

@Override
public PlayerData savePlayerData() {
return PlayerData.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private void registerDefaultCommands() {
register(new HelpCommand());
register(new StructureCommand());
register(new FillCommand());
register(new TitleCommand());
if (AllayAPI.getInstance().isDevBuild()) {
register(new GameTestCommand());
}
Expand Down

0 comments on commit 7804f03

Please sign in to comment.