Allow all messages to be formatted by external formatters and add PAPI support

This commit is contained in:
Alexander Söderberg
2020-02-20 16:03:17 +01:00
parent 17e4bde720
commit 464f5e09ae
25 changed files with 171 additions and 84 deletions

View File

@ -7,6 +7,7 @@ import com.github.intellectualsites.plotsquared.bukkit.listeners.PlayerEvents;
import com.github.intellectualsites.plotsquared.bukkit.listeners.PlotPlusListener;
import com.github.intellectualsites.plotsquared.bukkit.listeners.SingleWorldListener;
import com.github.intellectualsites.plotsquared.bukkit.listeners.WorldEvents;
import com.github.intellectualsites.plotsquared.bukkit.placeholders.PlaceholderFormatter;
import com.github.intellectualsites.plotsquared.bukkit.placeholders.Placeholders;
import com.github.intellectualsites.plotsquared.bukkit.util.BukkitChatManager;
import com.github.intellectualsites.plotsquared.bukkit.util.BukkitChunkManager;
@ -31,6 +32,7 @@ import com.github.intellectualsites.plotsquared.configuration.ConfigurationSecti
import com.github.intellectualsites.plotsquared.plot.IPlotMain;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.ChatFormatter;
import com.github.intellectualsites.plotsquared.plot.config.ConfigurationNode;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.generator.GeneratorWrapper;
@ -160,6 +162,9 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new Placeholders(this).register();
if (Settings.Enabled_Components.EXTERNAL_PLACEHOLDERS) {
ChatFormatter.formatters.add(new PlaceholderFormatter());
}
PlotSquared.log(Captions.PREFIX + "&6PlaceholderAPI found! Hook activated.");
} else {
PlotSquared.log(Captions.PREFIX + "&6PlaceholderAPI is not in use. Hook deactivated.");

View File

@ -187,7 +187,9 @@ public class BukkitPlayer extends PlotPlayer {
return this.player.isPermissionSet(permission);
}
@Override public void sendMessage(final String message) {
@Override public void sendMessage(String message) {
message = message.replace('\u2010', '%')
.replace('\u2020', '&').replace('\u2030', '&');
if (!StringMan.isEqual(this.getMeta("lastMessage"), message) || (
System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000)) {
setMeta("lastMessage", message);

View File

@ -0,0 +1,23 @@
package com.github.intellectualsites.plotsquared.bukkit.placeholders;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.plot.config.ChatFormatter;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
public class PlaceholderFormatter implements ChatFormatter {
@Override public void format(final ChatContext context) {
final PlotPlayer recipient = context.getRecipient();
if (recipient instanceof BukkitPlayer) {
if (context.isRawOutput()) {
context.setMessage(context.getMessage().replace('%', '\u2010'));
} else {
final Player player = ((BukkitPlayer) recipient).player;
context.setMessage(PlaceholderAPI.setPlaceholders(player, context.getMessage()));
}
}
}
}