- Update java to 17 and adopt jabel.

- More StandardChannel refactoring.
This commit is contained in:
LlmDl committed 2022-09-30 12:01:44 -05:00
1 parent 93f8bd846e
commit 0e6544c52f
4 files changed
+165 -158

No files matched your search

+13 -3
View File
@@ -23,8 +23,8 @@
</organization> </organization>
<properties> <properties>
<java.version>1.8</java.version> <java.version>17</java.version>
<project.bukkitAPIVersion>1.13</project.bukkitAPIVersion> <project.bukkitAPIVersion>1.16</project.bukkitAPIVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
@@ -118,6 +118,12 @@
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>16.0.2</version> <version>16.0.2</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.bsideup.jabel</groupId>
<artifactId>jabel-javac-plugin</artifactId>
<version>0.4.2</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -126,10 +132,14 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <version>3.10.1</version>
<configuration> <configuration>
<release>8</release>
<source>${java.version}</source> <source>${java.version}</source>
<target>${java.version}</target> <target>${java.version}</target>
<compilerArgs>
<arg>-Xplugin:jabel</arg>
</compilerArgs>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
+1
View File
@@ -414,3 +414,4 @@ v0.97:
- Closes https://github.com/TownyAdvanced/Towny/issues/6194 - Closes https://github.com/TownyAdvanced/Towny/issues/6194
- Fix channel sounds being played at the sender's location, fix muting channel sounds. - Fix channel sounds being played at the sender's location, fix muting channel sounds.
- Remove built-in HexFormatter. - Remove built-in HexFormatter.
- Update java to 17 and adopt jabel.
@@ -430,4 +430,8 @@ public abstract class Channel {
} }
return false; return false;
} }
public boolean isGovernmentChannel() {
return type.equals(channelTypes.TOWN) || type.equals(channelTypes.NATION) || type.equals(channelTypes.ALLIANCE);
}
} }
@@ -15,7 +15,6 @@ import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.util.Colors; import com.palmergames.bukkit.util.Colors;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Sound; import org.bukkit.Sound;
@@ -42,10 +41,8 @@ public class StandardChannel extends Channel {
@Override @Override
public void chatProcess(AsyncPlayerChatEvent event) { public void chatProcess(AsyncPlayerChatEvent event) {
channelTypes channelType = channelTypes.valueOf(getType().name()); channelTypes channelType = this.getType();
Player player = event.getPlayer(); Player player = event.getPlayer();
Set<Player> recipients = null;
String format = "";
boolean notifyjoin = false; boolean notifyjoin = false;
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId()); Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
@@ -54,7 +51,14 @@ public class StandardChannel extends Channel {
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident); Town town = TownyAPI.getInstance().getResidentTownOrNull(resident);
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(resident); Nation nation = TownyAPI.getInstance().getResidentNationOrNull(resident);
// If player sends a message to a channel it had left // If the channel would require a town/nation which is null, cancel and fail early.
if (town == null && channelType.equals(channelTypes.TOWN) ||
nation == null && (channelType.equals(channelTypes.NATION) || channelType.equals(channelTypes.ALLIANCE))) {
event.setCancelled(true);
return;
}
// If player sends a message to a channel they have left
// tell the channel to add the player back // tell the channel to add the player back
if (isAbsent(player.getName())) { if (isAbsent(player.getName())) {
join(player); join(player);
@@ -62,60 +66,16 @@ public class StandardChannel extends Channel {
Bukkit.getPluginManager().callEvent(new PlayerJoinChatChannelEvent(player, this)); Bukkit.getPluginManager().callEvent(new PlayerJoinChatChannelEvent(player, this));
} }
/* // Set the channel specific format
* Retrieve the channel specific format String format = getFormat(player, channelType);
* and compile a set of recipients
*/
switch (channelType) {
case TOWN: // Get the list of message recipients.
if (town == null) { Set<Player> recipients = getRecipients(player, town, nation, channelType, event.getRecipients());
event.setCancelled(true);
return;
}
format = ChatSettings.getRelevantFormatGroup(player).getTOWN();
recipients = new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(town)));
break;
case NATION:
if (nation == null) {
event.setCancelled(true);
return;
}
format = ChatSettings.getRelevantFormatGroup(player).getNATION();
recipients = new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(nation)));
break;
case ALLIANCE:
if (nation == null) {
event.setCancelled(true);
return;
}
format = ChatSettings.getRelevantFormatGroup(player).getALLIANCE();
recipients = new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayersAlliance(nation)));
break;
case DEFAULT:
format = ChatSettings.getRelevantFormatGroup(player).getDEFAULT();
recipients = new HashSet<>(findRecipients(player, new ArrayList<>(event.getRecipients())));
break;
case GLOBAL:
format = ChatSettings.getRelevantFormatGroup(player).getGLOBAL();
recipients = new HashSet<>(findRecipients(player, new ArrayList<>(event.getRecipients())));
break;
case PRIVATE:
format = ChatSettings.getRelevantFormatGroup(player).getGLOBAL();
recipients = new HashSet<>(findRecipients(player, new ArrayList<>(event.getRecipients())));
break;
}
// Try sending an alone message if it is called for.
trySendingAloneMessage(player, recipients); trySendingAloneMessage(player, recipients);
/* // Parse any PAPI placeholders.
* Perform all replace functions on this format
*/
if (Chat.usingPlaceholderAPI) if (Chat.usingPlaceholderAPI)
format = PlaceholderAPI.setPlaceholders(player, format); format = PlaceholderAPI.setPlaceholders(player, format);
@@ -123,7 +83,7 @@ public class StandardChannel extends Channel {
* Only modify GLOBAL channelType chat (general and local chat channels) if isModifyChat() is true. * Only modify GLOBAL channelType chat (general and local chat channels) if isModifyChat() is true.
*/ */
if (!(channelType.equals(channelTypes.GLOBAL) && !ChatSettings.isModify_chat())) { if (!(channelType.equals(channelTypes.GLOBAL) && !ChatSettings.isModify_chat())) {
event.setFormat(getFormat(format)); event.setFormat(parseTagAndMsgColour(format));
LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident); LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident);
event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent));
} }
@@ -135,85 +95,82 @@ public class StandardChannel extends Channel {
event.getRecipients().addAll(recipients); event.getRecipients().addAll(recipients);
// If the server has marked this Channel as hooked, fire the AsyncChatHookEvent. // If the server has marked this Channel as hooked, fire the AsyncChatHookEvent.
if (isHooked()) { // If the event is cancelled, cancel the chat entirely.
AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(event, this, !Bukkit.getServer().isPrimaryThread()); // Fires its own sendSpyMessage().
Bukkit.getServer().getPluginManager().callEvent(hookEvent); if (isHooked())
if (hookEvent.isCancelled()) { if (!sendOffHookedMessage(event, channelType)) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
/*
* Send spy message before another plugin changes any of the recipients, // Send spy message if this was never hooked.
* so we know which people can see it. if (!isHooked())
*/
sendSpyMessage(event, channelType); sendSpyMessage(event, channelType);
if (hookEvent.isChanged()) { // Play the channel sound, if used.
event.setMessage(hookEvent.getMessage());
event.setFormat(hookEvent.getFormat());
event.getRecipients().clear();
event.getRecipients().addAll(hookEvent.getRecipients());
}
} else {
/*
* Send spy message.
*/
sendSpyMessage(event, channelType);
}
tryPlayChannelSound(event.getRecipients()); tryPlayChannelSound(event.getRecipients());
if (notifyjoin) if (notifyjoin)
TownyMessaging.sendMessage(player, "You join " + Colors.White + getName()); TownyMessaging.sendMessage(player, "You join " + Colors.White + getName());
/* /*
* Perform any last channel specific functions * Perform any last channel specific functions like logging this chat and
* like logging this chat and relaying to Dynmap. * relaying to Dynmap.
*/ */
switch (channelType) { switch (channelType) {
case TOWN: case TOWN:
case NATION: case NATION:
case ALLIANCE: case ALLIANCE:
case DEFAULT: case DEFAULT:
break; break;
case PRIVATE: case PRIVATE:
case GLOBAL: case GLOBAL:
tryPostToDynmap(player, event.getMessage()); tryPostToDynmap(player, event.getMessage());
break; break;
} }
} }
private void tryPlayChannelSound(Set<Player> recipients) { private String getFormat(Player player, channelTypes channelType) {
if (getChannelSound() != null) { return switch(channelType) {
for (Player recipient : recipients) { case TOWN -> ChatSettings.getRelevantFormatGroup(player).getTOWN();
if (!isSoundMuted(recipient)) { case NATION -> ChatSettings.getRelevantFormatGroup(player).getNATION();
try { case ALLIANCE -> ChatSettings.getRelevantFormatGroup(player).getALLIANCE();
recipient.playSound(recipient.getLocation(), Sound.valueOf(getChannelSound()), 1.0f, 1.0f); case DEFAULT -> ChatSettings.getRelevantFormatGroup(player).getDEFAULT();
} catch (IllegalArgumentException ex) { case GLOBAL, PRIVATE -> ChatSettings.getRelevantFormatGroup(player).getGLOBAL();
plugin.getLogger().warning("Channel " + this.getName() + " has an invalid sound configured."); };
setChannelSound(null);
break;
}
}
}
}
} }
private void tryPostToDynmap(Player player, String message) { private Set<Player> getRecipients(Player player, Town town, Nation nation, channelTypes channelType, Set<Player> recipients) {
if (super.getRange() > 0) return switch (channelType) {
return; case TOWN -> new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(town)));
DynmapAPI dynMap = plugin.getDynmap(); case NATION -> new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(nation)));
if (dynMap != null) case ALLIANCE -> new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayersAlliance(nation)));
dynMap.postPlayerMessageToWeb(player, message); case DEFAULT -> new HashSet<>(findRecipients(player, new ArrayList<>(recipients)));
case GLOBAL, PRIVATE -> new HashSet<>(findRecipients(player, new ArrayList<>(recipients)));
};
} }
private String getFormat(String format) { /**
return format * Compile a list of valid recipients for this message.
.replace("{channelTag}", Colors.translateColorCodes(getChannelTag() != null ? getChannelTag() : "")) *
.replace("{msgcolour}", Colors.translateColorCodes(getMessageColour() != null ? getMessageColour() : "")); * @param sender
* @param playerList
* @return Set containing a list of players for this message.
*/
private Set<Player> findRecipients(Player sender, List<Player> playerList) {
// Refresh the potential channels a player can see, if they are not currently in the channel.
playerList.stream().forEach(p -> refreshPlayer(this, p));
return playerList.stream()
.filter(p -> TownyUniverse.getInstance().getPermissionSource().has(p, getPermission())) // Check permission.
.filter(p -> testDistance(sender, p, getRange())) // Within range.
.filter(p -> !plugin.isIgnoredByEssentials(sender, p)) // Check essentials ignore.
.filter(p -> !isAbsent(p.getName())) // Check if player is purposefully absent.
.collect(Collectors.toSet());
}
private void refreshPlayer(Channel channel, Player player) {
if (!channel.isPresent(player.getName()))
channel.forgetPlayer(player);
} }
/** /**
@@ -242,35 +199,46 @@ public class StandardChannel extends Channel {
player1.getLocation().distance(player2.getLocation()) < range; player1.getLocation().distance(player2.getLocation()) < range;
} }
/**
* Compile a list of valid recipients for this message.
*
* @param sender
* @param playerList
* @return Set containing a list of players for this message.
*/
private Set<Player> findRecipients(Player sender, List<Player> playerList) {
// Refresh the potential channels a player can see, if they are not currently in the channel.
playerList.stream().forEach(p -> refreshPlayer(this, p));
return playerList.stream()
.filter(p -> TownyUniverse.getInstance().getPermissionSource().has(p, getPermission())) // Check permission.
.filter(p -> testDistance(sender, p, getRange())) // Within range.
.filter(p -> !plugin.isIgnoredByEssentials(sender, p)) // Check essentials ignore.
.filter(p -> !isAbsent(p.getName())) // Check if player is purposefully absent.
.collect(Collectors.toSet());
}
private void refreshPlayer(Channel channel, Player player) {
if (!channel.isPresent(player.getName()))
channel.forgetPlayer(player);
}
private void trySendingAloneMessage(Player sender, Set<Player> recipients) { private void trySendingAloneMessage(Player sender, Set<Player> recipients) {
if (ChatSettings.isUsingAloneMessage() && if (ChatSettings.isUsingAloneMessage() &&
recipients.stream().filter(p -> sender.canSee(p)).count() < 2) // sender will usually be a recipient of their own message. recipients.stream().filter(p -> sender.canSee(p)).count() < 2) // sender will usually be a recipient of their own message.
sender.sendMessage(Colors.translateColorCodes(ChatSettings.getUsingAloneMessageString())); sender.sendMessage(Colors.translateColorCodes(ChatSettings.getUsingAloneMessageString()));
} }
private String parseTagAndMsgColour(String format) {
return format
.replace("{channelTag}", Colors.translateColorCodes(getChannelTag() != null ? getChannelTag() : ""))
.replace("{msgcolour}", Colors.translateColorCodes(getMessageColour() != null ? getMessageColour() : ""));
}
/**
* Send off TownyChat's {@link AsyncChatHookEvent} which allows other plugins to
* cancel or modify TownyChat's messaging.
*
* @param event {@link AsyncPlayerChatEvent} that has caused a message.
* @param channelType {@link channelTypes} which this message is being sent through.
* @return false if the AsyncChatHookEvent is cancelled.
*/
private boolean sendOffHookedMessage(AsyncPlayerChatEvent event, channelTypes channelType) {
AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(event, this, !Bukkit.getServer().isPrimaryThread());
Bukkit.getServer().getPluginManager().callEvent(hookEvent);
if (hookEvent.isCancelled())
return false;
/*
* Send spy message before another plugin changes any of the recipients, so we
* know which people can see it.
*/
sendSpyMessage(event, channelType);
if (hookEvent.isChanged()) {
event.setMessage(hookEvent.getMessage());
event.setFormat(hookEvent.getFormat());
event.getRecipients().clear();
event.getRecipients().addAll(hookEvent.getRecipients());
}
return true;
}
/** /**
* Sends messages to spies who have not already seen the message naturally. * Sends messages to spies who have not already seen the message naturally.
* *
@@ -286,7 +254,17 @@ public class StandardChannel extends Channel {
// Remove spies who've already seen the message naturally. // Remove spies who've already seen the message naturally.
spies.stream() spies.stream()
.filter(spy -> !recipients.contains(spy)) .filter(spy -> !recipients.contains(spy))
.forEach(spy -> spy.sendMessage(ChatColor.GOLD + "[SPY] " + ChatColor.WHITE + format + ": " + event.getMessage())); .forEach(spy -> spy.sendMessage(format + event.getMessage()));
}
/**
* @return A Set of online players who are spying.
*/
private Set<Player> getSpies() {
// Compile the list of recipients with spy perms
return plugin.getServer().getOnlinePlayers().stream()
.filter(p -> plugin.getTowny().hasPlayerMode(p, "spy"))
.collect(Collectors.toSet());
} }
/** /**
@@ -301,36 +279,50 @@ public class StandardChannel extends Channel {
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId()); Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident == null) if (resident == null)
return null; return null;
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident); String channelPrefix = Colors.translateColorCodes(getChannelTag() != null ? getChannelTag() : getName()) + " ";
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(resident); if (isGovernmentChannel()) // Town, Nation, Alliance channels get an extra [Name] added after the channelPrefix.
channelPrefix = getGovtChannelSpyingPrefix(resident, type, channelPrefix);
String format = Colors.translateColorCodes(getChannelTag() != null ? getChannelTag() : getName()); return ChatColor.GOLD + "[SPY] " + ChatColor.WHITE + channelPrefix + resident.getName() + ": ";
switch (type) {
case TOWN:
format = format + " [" + town.getName() + "] " + resident.getName();
break;
case NATION:
case ALLIANCE:
format = format + " [" + nation.getName() + "] " + resident.getName();
break;
case GLOBAL:
case PRIVATE:
case DEFAULT:
format = format + " " + resident.getName();
break;
} }
return format;
private String getGovtChannelSpyingPrefix(Resident resident, channelTypes type, String channelPrefix) {
String slug = type.equals(channelTypes.TOWN)
? TownyAPI.getInstance().getResidentTownOrNull(resident).getName() // Town chat.
: TownyAPI.getInstance().getResidentNationOrNull(resident).getName(); // Nation or Alliance chat.
return channelPrefix + "[" + slug + "] ";
} }
/** /**
* @return A Set of players online who are spying. * Try to send a channel sound, if enabled.
*
* @param recipients Set of Players that will receive the message and potentially the sound.
*/ */
private Set<Player> getSpies() { private void tryPlayChannelSound(Set<Player> recipients) {
// Compile the list of recipients with spy perms if (getChannelSound() == null)
return plugin.getServer().getOnlinePlayers().stream() return;
.filter(p -> plugin.getTowny().hasPlayerMode(p, "spy")) for (Player recipient : recipients) {
.collect(Collectors.toSet()); if (!isSoundMuted(recipient)) {
try {
recipient.playSound(recipient, Sound.valueOf(getChannelSound()), 1.0f, 1.0f);
} catch (IllegalArgumentException ex) {
plugin.getLogger().warning("Channel " + this.getName() + " has an invalid sound configured.");
setChannelSound(null);
break;
}
}
}
} }
/**
* Try to send a message to dynmap's web chat.
* @param player Player which has spoken.
* @param message Message being spoken.
*/
private void tryPostToDynmap(Player player, String message) {
if (super.getRange() > 0)
return;
DynmapAPI dynMap = plugin.getDynmap();
if (dynMap != null)
dynMap.postPlayerMessageToWeb(player, message);
}
} }