Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vakiliner/chatmoderator/bukkit/command/UnmuteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String la
if (args.length < 1) return false;
ChatCommandSender sender = this.manager.toChatCommandSender(commandSender);
String targetName = args[0];
MutedPlayer mute = this.manager.mutes.getMutedPlayer(targetName);
MutedPlayer mute = this.manager.mutes.getByName(targetName);
if (mute != null && this.manager.mutes.unmute(mute.getUniqueId())) {
ChatTextComponent component = new ChatTextComponent();
component.append(new ChatTextComponent(mute.getName()));
Expand Down
41 changes: 27 additions & 14 deletions vakiliner/chatmoderator/core/MuteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MuteManager {
@SuppressWarnings("unused")
private final ChatModerator manager;
private final Map<UUID, MutedPlayer> map = new HashMap<>();
private final Map<String, MutedPlayer> byName = new HashMap<>();
private ThreadSaveConfig threadSaveConfig;
private File file;

Expand All @@ -32,13 +33,19 @@ public MutedPlayer get(UUID uuid) {
return this.map.get(uuid);
}

public MutedPlayer getByName(String name) {
return this.byName.get(name.toLowerCase());
}

@Deprecated
public MutedPlayer getMutedPlayer(String name) {
for (MutedPlayer mute : this.map.values()) {
if (mute.getName().equalsIgnoreCase(name)) return mute;
}
return null;
}

@Deprecated
public MutedPlayer getMutedPlayerExact(String name) {
for (MutedPlayer mute : this.map.values()) {
if (mute.getName().equals(name)) return mute;
Expand All @@ -50,31 +57,37 @@ public Map<UUID, MutedPlayer> map() {
return Collections.unmodifiableMap(this.map);
}

private synchronized boolean put(MutedPlayer mute) {
MutedPlayer put = this.map.putIfAbsent(mute.getUniqueId(), mute);
if (put == null) this.byName.putIfAbsent(mute.getName().toLowerCase(), mute);
return put == null;
}

private synchronized MutedPlayer remove(UUID uuid) {
MutedPlayer mute = this.map.remove(uuid);
if (mute != null) this.byName.remove(mute.getName().toLowerCase(), mute);
return mute;
}

public boolean mute(ChatOfflinePlayer player, String moderator, ModeratorType moderatorType, Integer duration, String reason) {
Date now = new Date();
synchronized (this.map) {
synchronized (this) {
MutedPlayer mute = this.map.get(player.getUniqueId());
if (mute != null && !mute.isExpired(now)) return false;
this.map.put(player.getUniqueId(), new MutedPlayer(player, moderator, moderatorType, now, duration, reason));
if (!this.put(new MutedPlayer(player, moderator, moderatorType, now, duration, reason))) return false;
}
this.threadSaveConfig.save();
return true;
}

@Deprecated
public boolean unmute(String name) {
MutedPlayer mute = this.getMutedPlayer(name);
if (mute != null) {
return this.unmute(mute.getUniqueId());
}
return false;
MutedPlayer mute = this.getByName(name);
return mute != null && this.unmute(mute.getUniqueId());
}

public boolean unmute(UUID uuid) {
MutedPlayer mute;
synchronized (this.map) {
mute = this.map.remove(uuid);
}
MutedPlayer mute = this.remove(uuid);
if (mute != null) this.threadSaveConfig.save();
return mute != null && !mute.isExpired();
}
Expand Down Expand Up @@ -103,10 +116,10 @@ public void reload() throws IOException {
public void reload(File file) throws IOException {
if (file.exists()) {
GsonMutes mutes = new Gson().fromJson(new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8), GsonMutes.class);
synchronized (this.map) {
synchronized (this) {
this.map.clear();
for (GsonMutedPlayer mute : mutes) {
this.map.put(mute.uuid, mute.toMutedPlayer());
this.put(mute.toMutedPlayer());
}
}
}
Expand All @@ -118,7 +131,7 @@ public void save() throws IOException {

public void save(File file) throws IOException {
GsonMutes mutes = new GsonMutes();
synchronized (this.map) {
synchronized (this) {
for (MutedPlayer mute : this.map.values()) {
mutes.add(GsonMutedPlayer.fromMutedPlayer(mute));
}
Expand Down