Skip to content

Commit

Permalink
v0.85:
Browse files Browse the repository at this point in the history
  - Add persistant leaving for channels.
    - Uses Towny resident metadata, you will be able to see ignored
channels on your /res status screen.
    - Normal joining/leaving commands are used just like before.
    - Closes TownyAdvanced/Towny#3133.
  - Bump minimum Towny version to 0.96.3.0.
  • Loading branch information
LlmDl committed Dec 19, 2020
1 parent fecf303 commit 4497832
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 29 deletions.
4 changes: 2 additions & 2 deletions 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.84</version>
<version>0.85</version>

<licenses>
<license>
Expand Down Expand Up @@ -74,7 +74,7 @@
<dependency>
<groupId>com.github.TownyAdvanced</groupId>
<artifactId>Towny</artifactId>
<version>0.96.3.0</version>
<version>0.96.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
8 changes: 7 additions & 1 deletion resources/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,10 @@ v0.84:
- Allow hex colour codes to be used in the messagecolour options channels.yml.
- Closes https://github.com/TownyAdvanced/Towny/issues/4087.
- Implemented PlayerJoinChatChannelEvent, courtesy of Glare with PR #32.
- Closes https://github.com/TownyAdvanced/Towny/issues/4575.
- Closes https://github.com/TownyAdvanced/Towny/issues/4575.
v0.85:
- Add persistant leaving for channels.
- Uses Towny resident metadata, you will be able to see ignored channels on your /res status screen.
- Normal joining/leaving commands are used just like before.
- Closes https://github.com/TownyAdvanced/Towny/issues/3133.
- Bump minimum Towny version to 0.96.3.0.
4 changes: 2 additions & 2 deletions src/com/palmergames/bukkit/TownyChat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Chat extends JavaPlugin {
private Towny towny = null;
private DynmapAPI dynMap = null;

private static Version requiredTownyVersion = Version.fromString("0.96.2.17");
private static Version requiredTownyVersion = Version.fromString("0.96.3.0");
public static boolean usingPlaceholderAPI = false;
boolean chatConfigError = false;
boolean channelsConfigError = false;
Expand All @@ -62,7 +62,7 @@ public void onEnable() {
loadConfigs();

if (!townyVersionCheck(towny.getDescription().getVersion())) {
getLogger().severe("Towny version does not meet required version: " + requiredTownyVersion.toString());
getLogger().severe("Towny version does not meet required minimum version: " + requiredTownyVersion.toString());
this.getServer().getPluginManager().disablePlugin(this);
return;
} else {
Expand Down
14 changes: 4 additions & 10 deletions src/com/palmergames/bukkit/TownyChat/Command/ChannelCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,10 @@ public static void parseChannelList(Player player) {
TownyMessaging.sendMessage(player, Colors.Gold + "Channel" + Colors.Gray + " - " + Colors.LightBlue + Translation.of("tc_channel_list_status"));
for (Map.Entry<String, Channel> channel : chanList.entrySet()) {
if (player.hasPermission(channel.getValue().getPermission()))
if (channel.getValue().isPresent(player.getName())) {
if (channel.getValue().isPresent(player.getName()))
TownyMessaging.sendMessage(player, Colors.Gold + channel.getKey() + Colors.Gray + " - " + Colors.LightBlue + Translation.of("tc_channel_list_in"));
} else {
/*if (!plugin.getTowny().isPermissions()
|| ( (plugin.getTowny().isPermissions())
&& (TownyUniverse.getPermissionSource().has(player, channel.getValue().getPermission()))
|| (channel.getValue().getPermission().isEmpty()))) {*/
else
TownyMessaging.sendMessage(player, Colors.Gold + channel.getKey() + Colors.Gray + " - " + Colors.LightBlue + Translation.of("tc_channel_list_out"));
//}
}
}
}

Expand Down Expand Up @@ -319,7 +313,7 @@ public static void parseChannelLeave(Player player, String[] split) {
}

// If we fail you weren't in there to start with
if (!chan.leave(player.getName())) {
if (!chan.leave(player)) {
TownyMessaging.sendMessage(player, Translation.of("tc_you_already_left_channel", chan.getName()));
return;
}
Expand Down Expand Up @@ -385,7 +379,7 @@ public static void parseChannelJoin(Player player, String[] split) {
return;
}

if (!chan.join(player.getName())) {
if (!chan.join(player)) {
TownyMessaging.sendMessage(player, Translation.of("tc_you_are_already_in_channel", chan.getName()));
return;
}
Expand Down
87 changes: 79 additions & 8 deletions src/com/palmergames/bukkit/TownyChat/channels/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import org.bukkit.event.player.AsyncPlayerChatEvent;

import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Translation;
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
import com.palmergames.bukkit.towny.object.metadata.StringDataField;

public abstract class Channel {

Expand Down Expand Up @@ -120,33 +124,38 @@ public void setRange(double range) {
/*
* Used to reset channel settings for a given player
*/
public void forgetPlayer(String name) {
public void forgetPlayer(Player player) {
// If the channel is auto join, they will be added
// If the channel is not auto join, they will marked as absent
if (playerIgnoringThisChannel(player))
return;

if (autojoin) {
join(name);
join(player);
} else {
leave(name);
leave(player);
}
}

/*
* Mark a player as having left chat
*/
public boolean leave(String name) {
public boolean leave(Player player) {
if (absentPlayers == null) {
absentPlayers = new ConcurrentHashMap<String, Integer> ();
}
Integer res = absentPlayers.put(name, 1);
Integer res = absentPlayers.put(player.getName(), 1);
playerAddIgnoreMeta(player);
return (res == null || res == 0);
}

/*
* Mark a player has having joined the chat
*/
public boolean join(String name) {
public boolean join(Player player) {
if (absentPlayers == null) return false;
Integer res = absentPlayers.remove(name);
Integer res = absentPlayers.remove(player.getName());
playerRemoveIgnoreMeta(player);
return (res != null && res == 1);
}

Expand Down Expand Up @@ -274,5 +283,67 @@ public boolean isSpam(Player player) {
}
return false;
}

private void playerAddIgnoreMeta(Player player) {
StringDataField icdf = new StringDataField("townychat_ignoredChannels", "", "Ignored TownyChat Channels");
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident == null)
return;

if (resident.hasMeta(icdf.getKey())) {
CustomDataField<?> cdf = resident.getMetadata(icdf.getKey());
if (cdf instanceof StringDataField) {
StringDataField sdf = (StringDataField) cdf;
sdf.setValue(sdf.getValue().concat("\uFF0C " + this.getName()));
TownyUniverse.getInstance().getDataSource().saveResident(resident);
}

} else {
resident.addMetaData(new StringDataField("townychat_ignoredChannels", this.getName(), "Ignored TownyChat Channels"));
}
}

private void playerRemoveIgnoreMeta(Player player) {
StringDataField icdf = new StringDataField("townychat_ignoredChannels", "", "Ignored TownyChat Channels");
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident == null || !resident.hasMeta(icdf.getKey()))
return;

CustomDataField<?> cdf = resident.getMetadata(icdf.getKey());
if (cdf instanceof StringDataField) {
StringDataField sdf = (StringDataField) cdf;
String newValues = "";
String[] values = sdf.getValue().split("\uFF0C ");
for (String chanName : values)
if (!chanName.equalsIgnoreCase(this.getName()))
if (newValues.isEmpty())
newValues = chanName;
else
newValues += "\uFF0C " + chanName;

if (!newValues.isEmpty()) {
sdf.setValue(newValues);
TownyUniverse.getInstance().getDataSource().saveResident(resident);
} else {
resident.removeMetaData(icdf);
}
}

}

private boolean playerIgnoringThisChannel(Player player) {
StringDataField idf = new StringDataField("townychat_ignoredChannels", "", "Ignored TownyChat Channels");
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident != null && resident.hasMeta(idf.getKey())) {
CustomDataField<?> cdf = resident.getMetadata(idf.getKey());
if (cdf instanceof StringDataField) {
StringDataField sdf = (StringDataField) cdf;
String[] split = sdf.getValue().split("\uFF0C ");
for (String string : split)
if (string.equalsIgnoreCase(this.getName()))
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void chatProcess(AsyncPlayerChatEvent event) {
// If player sends a message to a channel it had left
// tell the channel to add the player back
if (isAbsent(player.getName())) {
join(player.getName());
join(player);
notifyjoin = true;
Bukkit.getPluginManager().callEvent(new PlayerJoinChatChannelEvent(player, this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public TownyChatPlayerListener(Chat instance) {
@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(final PlayerJoinEvent event) {
Player player = event.getPlayer();
String name = player.getName();

for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
if (!TownyUniverse.getInstance().getPermissionSource().testPermission(player, channel.getPermission()))
continue;

// If the channel is auto join, they will be added
// If the channel is not auto join, they will marked as absent
// TODO: Only do this for channels the user has permissions for
channel.forgetPlayer(name);
channel.forgetPlayer(player);
}
Channel channel = plugin.getChannelsHandler().getDefaultChannel();
if (channel != null) {
Expand All @@ -58,11 +60,10 @@ public void onPlayerJoin(final PlayerJoinEvent event) {

@EventHandler(priority = EventPriority.LOW)
public void onPlayerQuit(final PlayerQuitEvent event) {
String name = event.getPlayer().getName();
for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) {
// If the channel is auto join, they will be added
// If the channel is not auto join, they will marked as absent
channel.forgetPlayer(name);
channel.forgetPlayer(event.getPlayer());
}
}

Expand Down

0 comments on commit 4497832

Please sign in to comment.