This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce native chat format support
Resolves #2
- Loading branch information
BGM
committed
Apr 3, 2020
1 parent
e975769
commit eda7781
Showing
7 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Commons/src/main/java/cl/bgmp/commons/Chat/ChatFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cl.bgmp.commons.Chat; | ||
|
||
import cl.bgmp.commons.Commons; | ||
import cl.bgmp.commons.Config; | ||
import java.util.logging.Logger; | ||
import net.milkbowl.vault.chat.Chat; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.AsyncPlayerChatEvent; | ||
import org.bukkit.event.server.ServiceRegisterEvent; | ||
import org.bukkit.event.server.ServiceUnregisterEvent; | ||
|
||
public class ChatFormatter implements Listener { | ||
|
||
// Placeholder constants | ||
private static final String NAME = "{name}"; | ||
private static final String DISPLAY_NAME = "{displayname}"; | ||
private static final String MESSAGE = "{message}"; | ||
private static final String PREFIX = "{prefix}"; | ||
private static final String SUFFIX = "{suffix}"; | ||
private static final String DEFAULT_FORMAT = "<" + PREFIX + NAME + SUFFIX + "> " + MESSAGE; | ||
|
||
private String format; | ||
private Chat vaultChat = null; | ||
private Logger logger; | ||
|
||
public ChatFormatter(Logger logger) { | ||
if (!Config.ChatFormat.isEnabled()) return; | ||
this.logger = logger; | ||
|
||
reloadConfigValues(); | ||
refreshVault(); | ||
Commons.get().registerEvents(this); | ||
} | ||
|
||
public void reloadConfigValues() { | ||
format = | ||
cl.bgmp.utilsbukkit.Chat.colourify( | ||
Config.ChatFormat.getFormat().replace(DISPLAY_NAME, "%1$s").replace(MESSAGE, "%2$s")); | ||
} | ||
|
||
public void refreshVault() { | ||
Chat vaultChat = Bukkit.getServer().getServicesManager().load(Chat.class); | ||
if (vaultChat != this.vaultChat) | ||
logger.info( | ||
"New Vault Chat implementation registered: " | ||
+ (vaultChat == null ? "null" : vaultChat.getName())); | ||
this.vaultChat = vaultChat; | ||
} | ||
|
||
@EventHandler | ||
public void onServiceChange(ServiceRegisterEvent e) { | ||
if (e.getProvider().getService() == Chat.class) { | ||
refreshVault(); | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onServiceChange(ServiceUnregisterEvent e) { | ||
if (e.getProvider().getService() == Chat.class) { | ||
refreshVault(); | ||
} | ||
} | ||
|
||
@EventHandler(priority = EventPriority.LOWEST) | ||
public void onChatLow(AsyncPlayerChatEvent e) { | ||
e.setFormat(format); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.HIGHEST) | ||
public void onChatHigh(AsyncPlayerChatEvent e) { | ||
String format = e.getFormat(); | ||
if (vaultChat != null && format.contains(PREFIX)) { | ||
format = | ||
format.replace( | ||
PREFIX, cl.bgmp.utilsbukkit.Chat.colourify(vaultChat.getPlayerPrefix(e.getPlayer()))); | ||
} | ||
if (vaultChat != null && format.contains(SUFFIX)) { | ||
format = | ||
format.replace( | ||
SUFFIX, cl.bgmp.utilsbukkit.Chat.colourify(vaultChat.getPlayerSuffix(e.getPlayer()))); | ||
} | ||
format = format.replace(NAME, e.getPlayer().getName()); | ||
e.setFormat(format); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Commons/src/main/java/cl/bgmp/commons/Commands/ChatFormatterCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cl.bgmp.commons.Commands; | ||
|
||
import cl.bgmp.commons.Commons; | ||
import com.sk89q.minecraft.util.commands.Command; | ||
import com.sk89q.minecraft.util.commands.CommandContext; | ||
import com.sk89q.minecraft.util.commands.CommandPermissions; | ||
import com.sk89q.minecraft.util.commands.NestedCommand; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.CommandSender; | ||
|
||
public class ChatFormatterCommand { | ||
@Command( | ||
aliases = {"reload"}, | ||
desc = "Chat format node command.", | ||
max = 1) | ||
@CommandPermissions("commons.chat.reload") | ||
public static void reload(final CommandContext args, final CommandSender sender) { | ||
Commons.get().getChatFormatter().refreshVault(); | ||
Commons.get().getChatFormatter().reloadConfigValues(); | ||
|
||
sender.sendMessage(ChatColor.GREEN + "Chat format successfully reloaded."); | ||
} | ||
|
||
public static class ChatFormatterParentCommand { | ||
@Command( | ||
aliases = {"chat", "chatformat"}, | ||
desc = "Chat format node command.", | ||
max = 1) | ||
@CommandPermissions("commons.chat") | ||
@NestedCommand(ChatFormatterCommand.class) | ||
public static void chat(final CommandContext args, final CommandSender sender) {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
name: Commons | ||
version: ${project.version} | ||
main: cl.bgmp.commons.Commons | ||
api-version: 1.13 | ||
api-version: '1.13' | ||
authors: [BGMP] | ||
softdepend: [Vault] |