Move message sending into PlotPlayer to make it platform independent

This commit is contained in:
Alexander Söderberg 2020-07-08 15:35:58 +02:00
parent 9792d4cc4b
commit 3fa532a3c0
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
5 changed files with 67 additions and 155 deletions

View File

@ -26,13 +26,10 @@
package com.plotsquared.bukkit.player;
import com.google.common.base.Charsets;
import com.google.common.base.Objects;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.TeleportCause;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer;
@ -45,10 +42,6 @@ import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.title.Title;
import org.bukkit.GameMode;
import org.bukkit.Sound;
import org.bukkit.WeatherType;
@ -60,10 +53,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.RegisteredListener;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@ -75,8 +65,6 @@ import static com.sk89q.worldedit.world.gamemode.GameModes.SURVIVAL;
public class BukkitPlayer extends PlotPlayer<Player> {
private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
private static boolean CHECK_EFFECTIVE = true;
public final Player player;
private boolean offline;
@ -232,34 +220,6 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return this.player.isPermissionSet(permission);
}
@Override
public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle,
final int fadeIn, final int stay, final int fadeOut, @NotNull final Template ... replacements) {
final Component titleComponent = MINI_MESSAGE.parse(title.getComponent(this), replacements);
final Component subtitleComponent = MINI_MESSAGE.parse(subtitle.getComponent(this), replacements);
final Audience audience = BukkitUtil.BUKKIT_AUDIENCES.player(this.player);
audience.showTitle(Title.of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50,
ChronoUnit.MILLIS), Duration.of(stay * 50, ChronoUnit.MILLIS), Duration.of(fadeOut * 50, ChronoUnit.MILLIS)));
}
@Override public void sendMessage(@NotNull final Caption caption,
@NotNull final Template... replacements) {
final String message = caption.getComponent(this);
if (message.isEmpty()) {
return;
}
// Create the template list, and add the prefix as a replacement
final List<Template> templates = Arrays.asList(replacements);
templates.add(Template.of("prefix", MINI_MESSAGE.parse(TranslatableCaption.of("core.prefix").getComponent(this))));
// Parse the message
final Component component = MINI_MESSAGE.parse(message, templates);
if (!Objects.equal(component, this.getMeta("lastMessage")) || System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000) {
setMeta("lastMessage", component);
setMeta("lastMessageTime", System.currentTimeMillis());
BukkitUtil.BUKKIT_AUDIENCES.player(player).sendMessage(component);
}
}
@Override
public void teleport(@NotNull final Location location, @NotNull final TeleportCause cause) {
if (Math.abs(location.getX()) >= 30000000 || Math.abs(location.getZ()) >= 30000000) {
@ -379,6 +339,10 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return this.player.isBanned();
}
@Override @NotNull public Audience getAudience() {
return BukkitUtil.BUKKIT_AUDIENCES.player(this.player);
}
public PlayerTeleportEvent.TeleportCause getTeleportCause(@NotNull final TeleportCause cause) {
switch (cause) {

View File

@ -25,10 +25,9 @@
*/
package com.plotsquared.core.command;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.task.TaskManager;
@CommandDeclaration(command = "confirm",
@ -40,13 +39,13 @@ public class Confirm extends SubCommand {
@Override public boolean onCommand(PlotPlayer<?> player, String[] args) {
CmdInstance command = CmdConfirm.getPending(player);
if (command == null) {
MainUtil.sendMessage(player, Captions.FAILED_CONFIRM);
player.sendMessage(TranslatableCaption.of("confirm.failed_confirm"));
return false;
}
CmdConfirm.removePending(player);
if ((System.currentTimeMillis() - command.timestamp)
> Settings.Confirmation.CONFIRMATION_TIMEOUT_SECONDS * 1000) {
MainUtil.sendMessage(player, Captions.EXPIRED_CONFIRM);
player.sendMessage(TranslatableCaption.of("confirm.expired_confirm"));
return false;
}
TaskManager.runTask(command.command);

View File

@ -39,6 +39,7 @@ import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import org.jetbrains.annotations.NotNull;
@ -191,4 +192,8 @@ public class ConsolePlayer extends PlotPlayer<Actor> {
return false;
}
@Override @NotNull public Audience getAudience() {
return PlotSquared.imp().getConsoleAudience();
}
}

View File

@ -25,15 +25,16 @@
*/
package com.plotsquared.core.player;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.command.CommandCaller;
import com.plotsquared.core.command.RequiredType;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.CaptionUtility;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.LocaleHolder;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.database.DBFunc;
import com.plotsquared.core.events.TeleportCause;
import com.plotsquared.core.location.Location;
@ -55,15 +56,23 @@ import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.item.ItemType;
import lombok.NonNull;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.title.Title;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@ -77,6 +86,7 @@ import java.util.stream.Collectors;
*/
public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer, LocaleHolder {
private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
public static final String META_LAST_PLOT = "lastplot";
public static final String META_LOCATION = "location";
@ -411,32 +421,6 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
return result;
}
/**
* Send a title to the player that fades in, in 10 ticks, stays for 50 ticks and fades
* out in 20 ticks
*
* @param title Title text
* @param subtitle Subtitle text
* @param replacements Variable replacements
*/
public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle,
@NotNull final Template ... replacements) {
sendTitle(title, subtitle, 10, 50, 10, replacements);
}
/**
* Send a title to the player
*
* @param title Title text
* @param subtitle Subtitle text
* @param fadeIn time in ticks for titles to fade in. Defaults to 10
* @param stay time in ticks for titles to stay. Defaults to 70
* @param fadeOut time in ticks for titles to fade out. Defaults to 20
* @param replacements Variable replacements
*/
public abstract void sendTitle(@NotNull Caption title, @NotNull Caption subtitle, int fadeIn,
int stay, int fadeOut, @NotNull final Template ... replacements);
/**
* Teleport this player to a location.
*
@ -704,9 +688,7 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
TaskManager.runTask(() -> {
if (getMeta("teleportOnLogin", true)) {
teleport(location);
sendMessage(CaptionUtility.format(PlotPlayer.this,
Captions.TELEPORTED_TO_PLOT.getTranslated())
+ " (quitLoc) (" + plotX + "," + plotZ + ")");
sendMessage(TranslatableCaption.of("teleport.teleported_to_plot"));
}
});
} else if (!PlotSquared.get().isMainThread(Thread.currentThread())) {
@ -716,10 +698,7 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
if (getMeta("teleportOnLogin", true)) {
if (plot.isLoaded()) {
teleport(location);
sendMessage(CaptionUtility.format(PlotPlayer.this,
Captions.TELEPORTED_TO_PLOT.getTranslated())
+ " (quitLoc-unloaded) (" + plotX + "," + plotZ
+ ")");
sendMessage(TranslatableCaption.of("teleport.teleported_to_plot"));
}
}
}));
@ -752,6 +731,41 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
}
}
/**
* Send a title to the player that fades in, in 10 ticks, stays for 50 ticks and fades
* out in 20 ticks
*
* @param title Title text
* @param subtitle Subtitle text
* @param replacements Variable replacements
*/
public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle,
final int fadeIn, final int stay, final int fadeOut, @NotNull final Template ... replacements) {
final Component titleComponent = MINI_MESSAGE.parse(title.getComponent(this), replacements);
final Component subtitleComponent = MINI_MESSAGE.parse(subtitle.getComponent(this), replacements);
getAudience().showTitle(Title.of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50,
ChronoUnit.MILLIS), Duration.of(stay * 50, ChronoUnit.MILLIS), Duration.of(fadeOut * 50, ChronoUnit.MILLIS)));
}
@Override public void sendMessage(@NotNull final Caption caption,
@NotNull final Template... replacements) {
final String message = caption.getComponent(this);
if (message.isEmpty()) {
return;
}
// Create the template list, and add the prefix as a replacement
final List<Template> templates = Arrays.asList(replacements);
templates.add(Template.of("prefix", MINI_MESSAGE.parse(
TranslatableCaption.of("core.prefix").getComponent(this))));
// Parse the message
final Component component = MINI_MESSAGE.parse(message, templates);
if (!Objects.equal(component, this.getMeta("lastMessage")) || System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000) {
setMeta("lastMessage", component);
setMeta("lastMessageTime", System.currentTimeMillis());
getAudience().sendMessage(component);
}
}
public boolean hasPersistentMeta(String key) {
return this.metaMap.containsKey(key);
}
@ -792,6 +806,13 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
return this.getUUID().equals(other.getUUID());
}
/**
* Get the {@link Audience} that represents this plot player
*
* @return Player audience
*/
@NotNull public abstract Audience getAudience();
/**
* The amount of money this Player has.
*/

View File

@ -609,83 +609,6 @@ public class MainUtil {
});
}
/**
* Send a message to the player.
*
* @param player Player to receive message
* @param message Message to send
* @return true Can be used in things such as commands (return PlayerFunctions.sendMessage(...))
*/
public static boolean sendMessage(PlotPlayer player, String message) {
return sendMessage(player, message, true);
}
/**
* Send a message to console.
*
* @param caption
* @param args
*/
public static void sendConsoleMessage(Captions caption, String... args) {
sendMessage(null, caption, args);
}
/**
* Send a message to a player.
*
* @param player Can be null to represent console, or use ConsolePlayer.getConsole()
* @param msg
* @param prefix If the message should be prefixed with the configured prefix
* @return
*/
public static boolean sendMessage(PlotPlayer player, @NotNull String msg, boolean prefix) {
if (!msg.isEmpty()) {
if (player == null) {
String message = CaptionUtility
.format(null, (prefix ? Captions.PREFIX.getTranslated() : "") + msg);
PlotSquared.log(message);
} else {
player.sendMessage(CaptionUtility.format(player,
(prefix ? Captions.PREFIX.getTranslated() : "") + Captions.color(msg)));
}
}
return true;
}
/**
* Send a message to the player.
*
* @param player the recipient of the message
* @param caption the message to send
* @return boolean success
*/
public static boolean sendMessage(PlotPlayer player, Caption caption, String... args) {
return sendMessage(player, caption, (Object[]) args);
}
/**
* Send a message to the player
*
* @param player the recipient of the message
* @param caption the message to send
* @return boolean success
*/
public static boolean sendMessage(final PlotPlayer player, final Caption caption,
final Object... args) {
if (caption.getTranslated().isEmpty()) {
return true;
}
TaskManager.runTaskAsync(() -> {
String m = CaptionUtility.format(player, caption, args);
if (player == null) {
PlotSquared.log(m);
} else {
player.sendMessage(m);
}
});
return true;
}
/**
* If rating categories are enabled, get the average rating by category.<br>
* - The index corresponds to the index of the category in the config