-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
320 additions
and
0 deletions.
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
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,42 @@ | ||
package me.hsgamer.muteplus.config; | ||
|
||
import me.hsgamer.hscore.config.annotation.ConfigPath; | ||
import me.hsgamer.muteplus.config.converter.CommandFetcherConverter; | ||
import me.hsgamer.muteplus.config.converter.StringListConverter; | ||
import me.hsgamer.muteplus.fetcher.CommandFetcher; | ||
import me.hsgamer.muteplus.fetcher.impl.SimpleCommandFetcher; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public interface MainConfig { | ||
@ConfigPath({"filter-command", "enabled"}) | ||
default boolean isFilterCommandEnabled() { | ||
return true; | ||
} | ||
|
||
@ConfigPath(value = {"filter-command", "player"}, converter = CommandFetcherConverter.class) | ||
default List<CommandFetcher> getPlayerCommandFetcher() { | ||
return Arrays.asList( | ||
new SimpleCommandFetcher("/tpa"), | ||
new SimpleCommandFetcher("/tpahere"), | ||
new SimpleCommandFetcher("/w"), | ||
new SimpleCommandFetcher("/whisper"), | ||
new SimpleCommandFetcher("/msg"), | ||
new SimpleCommandFetcher("/tell") | ||
); | ||
} | ||
|
||
@ConfigPath(value = {"filter-command", "global"}, converter = StringListConverter.class) | ||
default List<String> getGlobalCommand() { | ||
return Arrays.asList( | ||
"/r", | ||
"/reply" | ||
); | ||
} | ||
|
||
default boolean matchGlobalCommand(String command) { | ||
String[] split = command.split(" "); | ||
return getGlobalCommand().stream().anyMatch(s -> s.equalsIgnoreCase(split[0])); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/main/java/me/hsgamer/muteplus/config/converter/CommandFetcherConverter.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,70 @@ | ||
package me.hsgamer.muteplus.config.converter; | ||
|
||
import me.hsgamer.hscore.common.CollectionUtils; | ||
import me.hsgamer.hscore.config.annotation.converter.Converter; | ||
import me.hsgamer.muteplus.fetcher.impl.PatternCommandFetcher; | ||
import me.hsgamer.muteplus.fetcher.impl.SimpleCommandFetcher; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class CommandFetcherConverter implements Converter { | ||
private final Pattern regexFetcherPattern = Pattern.compile("pattern(\\(\\d+\\))?\\s*:\\s*(.+)\\s*"); | ||
|
||
@Override | ||
public Object convert(Object raw) { | ||
if (raw == null) return null; | ||
return CollectionUtils.createStringListFromObject(raw) | ||
.stream() | ||
.flatMap(string -> { | ||
Matcher matcher = regexFetcherPattern.matcher(string); | ||
if (matcher.matches()) { | ||
String patternString = matcher.group(2); | ||
String targetGroup = matcher.group(1); | ||
if (targetGroup == null || targetGroup.isEmpty()) { | ||
targetGroup = "1"; | ||
} | ||
|
||
Pattern pattern; | ||
try { | ||
pattern = Pattern.compile(patternString); | ||
} catch (Exception e) { | ||
return Stream.empty(); | ||
} | ||
|
||
try { | ||
int parsedTargetGroup = Integer.parseInt(targetGroup); | ||
return Stream.of(new PatternCommandFetcher(pattern, parsedTargetGroup)); | ||
} catch (Exception e) { | ||
return Stream.empty(); | ||
} | ||
} else { | ||
return Stream.of(new SimpleCommandFetcher(string)); | ||
} | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Object convertToRaw(Object value) { | ||
if (value instanceof List) { | ||
List<?> list = (List<?>) value; | ||
return list.stream() | ||
.flatMap(fetcher -> { | ||
if (fetcher instanceof PatternCommandFetcher) { | ||
PatternCommandFetcher patternCommandFetcher = (PatternCommandFetcher) fetcher; | ||
return Stream.of("pattern(" + patternCommandFetcher.getTargetGroup() + "): " + patternCommandFetcher.getPatternString()); | ||
} else if (fetcher instanceof SimpleCommandFetcher) { | ||
SimpleCommandFetcher simpleCommandFetcher = (SimpleCommandFetcher) fetcher; | ||
return Stream.of(simpleCommandFetcher.getCommand()); | ||
} | ||
return Stream.empty(); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
return null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/me/hsgamer/muteplus/config/converter/StringListConverter.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,17 @@ | ||
package me.hsgamer.muteplus.config.converter; | ||
|
||
import me.hsgamer.hscore.common.CollectionUtils; | ||
import me.hsgamer.hscore.config.annotation.converter.Converter; | ||
|
||
public class StringListConverter implements Converter { | ||
@Override | ||
public Object convert(Object raw) { | ||
if (raw == null) return null; | ||
return CollectionUtils.createStringListFromObject(raw); | ||
} | ||
|
||
@Override | ||
public Object convertToRaw(Object value) { | ||
return value; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/hsgamer/muteplus/fetcher/CommandFetcher.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,7 @@ | ||
package me.hsgamer.muteplus.fetcher; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CommandFetcher { | ||
Optional<CommandResult> getPlayer(String command); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/me/hsgamer/muteplus/fetcher/CommandResult.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,34 @@ | ||
package me.hsgamer.muteplus.fetcher; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.OfflinePlayer; | ||
|
||
import java.util.Optional; | ||
|
||
public class CommandResult { | ||
private final boolean present; | ||
private final String playerName; | ||
|
||
public CommandResult(String playerName) { | ||
this.present = !playerName.isEmpty(); | ||
this.playerName = playerName; | ||
} | ||
|
||
public boolean isPresent() { | ||
return present; | ||
} | ||
|
||
public String getPlayerName() { | ||
return playerName; | ||
} | ||
|
||
public Optional<OfflinePlayer> getPlayer() { | ||
//noinspection deprecation | ||
OfflinePlayer player = Bukkit.getOfflinePlayer(playerName); | ||
if (player.hasPlayedBefore()) { | ||
return Optional.of(player); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/me/hsgamer/muteplus/fetcher/impl/PatternCommandFetcher.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,48 @@ | ||
package me.hsgamer.muteplus.fetcher.impl; | ||
|
||
import me.hsgamer.muteplus.fetcher.CommandFetcher; | ||
import me.hsgamer.muteplus.fetcher.CommandResult; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class PatternCommandFetcher implements CommandFetcher { | ||
private final Pattern pattern; | ||
private final int targetGroup; | ||
|
||
public PatternCommandFetcher(Pattern pattern, int targetGroup) { | ||
this.pattern = pattern; | ||
this.targetGroup = targetGroup; | ||
} | ||
|
||
@Override | ||
public Optional<CommandResult> getPlayer(String command) { | ||
Matcher matcher = pattern.matcher(command); | ||
if (!matcher.matches()) { | ||
return Optional.empty(); | ||
} | ||
|
||
String player = ""; | ||
try { | ||
player = matcher.group(targetGroup); | ||
player = player == null ? "" : player; | ||
} catch (Exception ignored) { | ||
// IGNORED | ||
} | ||
|
||
return Optional.of(new CommandResult(player)); | ||
} | ||
|
||
public Pattern getPattern() { | ||
return pattern; | ||
} | ||
|
||
public String getPatternString() { | ||
return pattern.pattern(); | ||
} | ||
|
||
public int getTargetGroup() { | ||
return targetGroup; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/me/hsgamer/muteplus/fetcher/impl/SimpleCommandFetcher.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,31 @@ | ||
package me.hsgamer.muteplus.fetcher.impl; | ||
|
||
import me.hsgamer.muteplus.fetcher.CommandFetcher; | ||
import me.hsgamer.muteplus.fetcher.CommandResult; | ||
|
||
import java.util.Optional; | ||
|
||
public class SimpleCommandFetcher implements CommandFetcher { | ||
private final String command; | ||
|
||
public SimpleCommandFetcher(String command) { | ||
this.command = command; | ||
} | ||
|
||
@Override | ||
public Optional<CommandResult> getPlayer(String command) { | ||
if (!command.startsWith(this.command)) return Optional.empty(); | ||
|
||
String playerName = ""; | ||
String[] args = command.split(" "); | ||
if (args.length >= 2) { | ||
playerName = args[1]; | ||
} | ||
|
||
return Optional.of(new CommandResult(playerName)); | ||
} | ||
|
||
public String getCommand() { | ||
return command; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/me/hsgamer/muteplus/listener/CommandListener.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,58 @@ | ||
package me.hsgamer.muteplus.listener; | ||
|
||
import me.hsgamer.hscore.bukkit.utils.MessageUtils; | ||
import me.hsgamer.muteplus.MutePlus; | ||
import me.hsgamer.muteplus.fetcher.CommandResult; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerCommandPreprocessEvent; | ||
|
||
import java.util.Locale; | ||
import java.util.Optional; | ||
|
||
public class CommandListener implements Listener { | ||
private final MutePlus plugin; | ||
|
||
public CommandListener(MutePlus plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@EventHandler | ||
public void onCommand(PlayerCommandPreprocessEvent event) { | ||
Player player = event.getPlayer(); | ||
String command = event.getMessage().toLowerCase(Locale.ROOT); | ||
|
||
if (!plugin.getMainConfig().isFilterCommandEnabled()) { | ||
return; | ||
} | ||
|
||
if (plugin.getGlobalMuteManager().isMuted(player.getUniqueId()) && plugin.getMainConfig().matchGlobalCommand(command)) { | ||
MessageUtils.sendMessage(player.getUniqueId(), plugin.getMessageConfig().commandDenied()); | ||
event.setCancelled(true); | ||
return; | ||
} | ||
|
||
Optional<CommandResult> optionalCommandResult = plugin.getMainConfig().getPlayerCommandFetcher().stream() | ||
.map(fetcher -> fetcher.getPlayer(command)) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.findFirst(); | ||
if (!optionalCommandResult.isPresent()) { | ||
return; | ||
} | ||
CommandResult result = optionalCommandResult.get(); | ||
|
||
Optional<OfflinePlayer> optionalPlayer = result.getPlayer(); | ||
if (!optionalPlayer.isPresent()) { | ||
return; | ||
} | ||
|
||
OfflinePlayer target = optionalPlayer.get(); | ||
if (plugin.getGlobalMuteManager().isMuted(target.getUniqueId()) || plugin.getPlayerMuteManager().isMuted(player.getUniqueId(), target.getUniqueId())) { | ||
MessageUtils.sendMessage(player.getUniqueId(), plugin.getMessageConfig().commandDenied()); | ||
event.setCancelled(true); | ||
} | ||
} | ||
} |