Skip to content

Commit

Permalink
Merge pull request #44 from TownyAdvanced/fix/parse_placeholders_always
Browse files Browse the repository at this point in the history
Parse placeholders when we're unable to find a channel to speak into, but modify_chat is true.
  • Loading branch information
LlmDl authored Jan 21, 2023
2 parents b0f1863 + a4a053d commit 915285a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.palmergames.bukkit</groupId>
<artifactId>TownyChat</artifactId>
<packaging>jar</packaging>
<version>0.101</version>
<version>0.102</version>

<licenses>
<license>
Expand Down
4 changes: 3 additions & 1 deletion resources/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,6 @@ v0.100:
- Fix a player who has lost their town being stuck in /tc or /nc.
v0.101:
- Update min. Towny version to 0.98.5.0 & replace deprecated methods.
- Fix permission test backing /channel join {channelname}.
- Fix permission test backing /channel join {channelname}.
v0.102:
- Parse placeholders when we're unable to find a channel to speak into, but modify_chat is true.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.palmergames.bukkit.towny.object.metadata.StringDataField;
import com.palmergames.bukkit.towny.utils.MetaDataUtil;
import com.palmergames.bukkit.util.Colors;

import me.clip.placeholderapi.PlaceholderAPI;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -21,6 +24,7 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.UnknownFormatConversionException;
import java.util.WeakHashMap;

public class TownyChatPlayerListener implements Listener {
Expand Down Expand Up @@ -122,9 +126,33 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
* We found no channels available so modify the chat (if enabled) and exit.
*/
if (ChatSettings.isModify_chat()) {
Resident resident = TownyAPI.getInstance().getResident(player);
if (resident == null)
return;
// Nuke the channeltag and message colouring, but apply the remaining format.
String format = ChatSettings.getChannelFormat(player, channelTypes.GLOBAL).replace("{channelTag}", "").replace("{msgcolour}", "");

// format is left to store the original non-PAPI-parsed chat format.
String newFormat = format;

// Parse any PAPI placeholders.
if (Chat.usingPlaceholderAPI)
newFormat = PlaceholderAPI.setPlaceholders(player, format);

// Attempt to apply the new format.
catchFormatConversionException(event, format, newFormat);

// Fire the LocalTownyChatEvent.
LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident);

// Format the chat line, replacing the TownyChat chat tags.
newFormat = TownyChatFormatter.getChatFormat(chatEvent);

// Attempt to apply the new format.
catchFormatConversionException(event, format, newFormat);

// Set the format based on the global channelformat, with channeltag and msgcolour removed.
event.setFormat(ChatSettings.getChannelFormat(player, channelTypes.GLOBAL).replace("{channelTag}", "").replace("{msgcolour}", ""));
event.setFormat(TownyChatFormatter.getChatFormat(new LocalTownyChatEvent(event, TownyAPI.getInstance().getResident(player))));
event.setFormat(newFormat);
}
}

Expand Down Expand Up @@ -243,4 +271,31 @@ private String replaceSymbol(String meta) {
}
return new String(charray);
}

private void catchFormatConversionException(AsyncPlayerChatEvent event, String format, String newFormat) {
try {
event.setFormat(newFormat);
} catch (UnknownFormatConversionException e) {
// This exception is usually thrown when a PAPI placeholder did not get parsed
// and has left behind a % symbol followed by something that String#format
// cannot handle.
boolean percentSymbol = format.contains("%" + e.getConversion());
String errmsg = "TownyChat tried to apply a chat format that is not allowed: '" +
newFormat + "', because of the " + e.getConversion() + " symbol" +
(percentSymbol ? ", found after a %. There is probably a PAPIPlaceholder that could not be parsed." : "." +
" You should attempt to correct this in your towny\\settings\\chatconfig.yml file and use /townychat reload.");
Chat.getTownyChat().getLogger().severe(errmsg);

if (percentSymbol)
// Attempt to remove the unparsed placeholder and send this right back.
catchFormatConversionException(event, format, purgePAPI(newFormat, "%" + e.getConversion()));
else
// Just let the chat go, this results in an error in the log, and TownyChat not being able to format chat.
event.setFormat(format);
}
}

private String purgePAPI(String format, String startOfPlaceholder) {
return format.replaceAll(startOfPlaceholder + ".*%", "");
}
}

0 comments on commit 915285a

Please sign in to comment.