This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commands: add /warn, /unwarn, /warnlist
- Loading branch information
Showing
12 changed files
with
662 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package net.flectone.commands; | ||
|
||
import net.flectone.Main; | ||
import net.flectone.managers.FPlayerManager; | ||
import net.flectone.misc.commands.FCommand; | ||
import net.flectone.misc.commands.FTabCompleter; | ||
import net.flectone.misc.entity.FPlayer; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CommandUnwarn implements FTabCompleter { | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
Main.getDataThreadPool().execute(() -> command(commandSender, command, s, strings)); | ||
return true; | ||
} | ||
|
||
private void command(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
FCommand fCommand = new FCommand(commandSender, command.getName(), s, strings); | ||
|
||
if (fCommand.isInsufficientArgs(1)) return;; | ||
|
||
FPlayer fPlayer = FPlayerManager.getPlayerFromName(strings[0]); | ||
|
||
if (fPlayer == null) { | ||
fCommand.sendMeMessage("command.null-player"); | ||
return; | ||
} | ||
|
||
fPlayer.synchronizeDatabase(); | ||
|
||
if (fPlayer.getWarnList().isEmpty() || fPlayer.getRealWarnsCount() == 0) { | ||
fCommand.sendMeMessage("command.unwarn.not-warned"); | ||
return; | ||
} | ||
|
||
int index = strings.length == 1 || !StringUtils.isNumeric(strings[1]) | ||
? 1 | ||
: Integer.parseInt(strings[1]); | ||
|
||
if (index > fPlayer.getWarnList().size() || index < 1) { | ||
fCommand.sendMeMessage("command.long-number"); | ||
return; | ||
} | ||
|
||
if (fCommand.isHaveCD()) return; | ||
|
||
index--; | ||
fPlayer.unwarn(index); | ||
|
||
fCommand.sendMeMessage("command.unwarn.message", "<player>", fPlayer.getRealName()); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
wordsList.clear(); | ||
|
||
switch (strings.length) { | ||
case 1 -> { | ||
Main.getDatabase().getPlayerNameList("warns", "player").parallelStream() | ||
.forEach(playerName -> isStartsWith(strings[0], playerName)); | ||
} | ||
case 2 -> { | ||
String playerName = strings[0]; | ||
FPlayer fPlayer = FPlayerManager.getPlayerFromName(playerName); | ||
|
||
if (fPlayer == null) break; | ||
|
||
if (!fPlayer.isOnline() && fPlayer.getWarnList() == null) | ||
fPlayer.synchronizeDatabase(); | ||
|
||
if (fPlayer.getWarnList().isEmpty()) break; | ||
|
||
isDigitInArray(strings[1], "", 1, fPlayer.getWarnList().size() + 1); | ||
} | ||
} | ||
|
||
|
||
Collections.sort(wordsList); | ||
|
||
return wordsList; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getCommandName() { | ||
return "unwarn"; | ||
} | ||
} |
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,119 @@ | ||
package net.flectone.commands; | ||
|
||
import net.flectone.Main; | ||
import net.flectone.integrations.discordsrv.FDiscordSRV; | ||
import net.flectone.managers.FPlayerManager; | ||
import net.flectone.managers.HookManager; | ||
import net.flectone.misc.commands.FCommand; | ||
import net.flectone.misc.commands.FTabCompleter; | ||
import net.flectone.misc.entity.FPlayer; | ||
import net.flectone.utils.ObjectUtil; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static net.flectone.managers.FileManager.config; | ||
import static net.flectone.managers.FileManager.locale; | ||
|
||
public class CommandWarn implements FTabCompleter { | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
Main.getDataThreadPool().execute(() -> command(commandSender, command, s, strings)); | ||
|
||
return true; | ||
} | ||
|
||
private void command(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
FCommand fCommand = new FCommand(commandSender, command.getName(), s, strings); | ||
|
||
if (fCommand.isInsufficientArgs(2)) return; | ||
|
||
String stringTime = strings[1]; | ||
|
||
if (!fCommand.isStringTime(stringTime) || !StringUtils.isNumeric(stringTime.substring(0, stringTime.length() - 1))) { | ||
fCommand.sendUsageMessage(); | ||
return; | ||
} | ||
|
||
String playerName = strings[0]; | ||
FPlayer warnedFPlayer = FPlayerManager.getPlayerFromName(playerName); | ||
|
||
if (warnedFPlayer == null) { | ||
fCommand.sendMeMessage("command.null-player"); | ||
return; | ||
} | ||
|
||
int time = fCommand.getTimeFromString(stringTime); | ||
|
||
if (time < -1) { | ||
fCommand.sendMeMessage("command.long-number"); | ||
return; | ||
} | ||
|
||
if (fCommand.isHaveCD()) return; | ||
|
||
if (warnedFPlayer.getWarnList() == null) warnedFPlayer.synchronizeDatabase(); | ||
|
||
String reason = strings.length > 2 | ||
? ObjectUtil.toString(strings, 2) | ||
: locale.getString("command.warn.default-reason"); | ||
|
||
String formatString = locale.getString("command.warn.global-message") | ||
.replace("<player>", warnedFPlayer.getRealName()) | ||
.replace("<time>", ObjectUtil.convertTimeToString(time)) | ||
.replace("<count>", String.valueOf(warnedFPlayer.getRealWarnsCount() + 1)) | ||
.replace("<reason>", reason) | ||
.replace("<moderator>", commandSender.getName()); | ||
|
||
boolean announceModeration = config.getBoolean("command.warn.announce"); | ||
|
||
if (announceModeration && HookManager.enabledDiscordSRV) FDiscordSRV.sendModerationMessage(formatString); | ||
|
||
Set<Player> receivers = announceModeration | ||
? new HashSet<>(Bukkit.getOnlinePlayers()) | ||
: Bukkit.getOnlinePlayers().parallelStream() | ||
.filter(player -> player.hasPermission("flectonechat.warn") || player.equals(warnedFPlayer.getPlayer())) | ||
.collect(Collectors.toSet()); | ||
|
||
fCommand.sendFilterGlobalMessage(receivers, formatString, "", null, false); | ||
|
||
String moderator = (commandSender instanceof Player player) | ||
? player.getUniqueId().toString() | ||
: null; | ||
|
||
warnedFPlayer.warn(time, reason, moderator); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
wordsList.clear(); | ||
|
||
switch (strings.length){ | ||
case 1 -> isOfflinePlayer(strings[0]); | ||
case 2 -> isFormatString(strings[1]); | ||
case 3 -> isTabCompleteMessage(strings[2], "tab-complete.reason"); | ||
} | ||
|
||
Collections.sort(wordsList); | ||
|
||
return wordsList; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getCommandName() { | ||
return "warn"; | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
src/main/java/net/flectone/commands/CommandWarnlist.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,148 @@ | ||
package net.flectone.commands; | ||
|
||
import net.flectone.Main; | ||
import net.flectone.managers.FPlayerManager; | ||
import net.flectone.misc.commands.FCommand; | ||
import net.flectone.misc.commands.FTabCompleter; | ||
import net.flectone.misc.components.FComponent; | ||
import net.flectone.misc.components.FListComponent; | ||
import net.flectone.misc.entity.FPlayer; | ||
import net.flectone.utils.ObjectUtil; | ||
import net.md_5.bungee.api.chat.ComponentBuilder; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static net.flectone.managers.FileManager.config; | ||
import static net.flectone.managers.FileManager.locale; | ||
|
||
public class CommandWarnlist implements FTabCompleter { | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
Main.getDataThreadPool().execute(() -> command(commandSender, command, s, strings)); | ||
return true; | ||
} | ||
|
||
private void command(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
FCommand fCommand = new FCommand(commandSender, command.getName(), s, strings); | ||
|
||
if (fCommand.isInsufficientArgs(1)) return; | ||
|
||
FPlayer fPlayer = FPlayerManager.getPlayerFromName(strings[0]); | ||
|
||
if (fPlayer == null) { | ||
fCommand.sendMeMessage("command.null-player"); | ||
return; | ||
} | ||
|
||
if (fPlayer.getWarnList() == null) fPlayer.synchronizeDatabase(); | ||
|
||
int countMutes = fPlayer.getWarnList().size(); | ||
|
||
int perPage = config.getInt("command.warnlist.per-page"); | ||
|
||
int lastPage = (int) Math.ceil((double) countMutes / perPage); | ||
|
||
if (strings.length != 1 && | ||
(!StringUtils.isNumeric(strings[1]) | ||
|| Integer.parseInt(strings[1]) < 1 | ||
|| Integer.parseInt(strings[1]) > lastPage)) { | ||
|
||
fCommand.sendMeMessage("command.warnlist.page-not-exist"); | ||
return; | ||
} | ||
|
||
if (countMutes == 0) { | ||
fCommand.sendMeMessage("command.warnlist.empty"); | ||
return; | ||
} | ||
|
||
if (fCommand.isHaveCD()) return; | ||
|
||
ComponentBuilder componentBuilder = new ComponentBuilder(); | ||
|
||
String title = locale.getFormatString("command.warnlist.title", commandSender) | ||
.replace("<count>", String.valueOf(countMutes)); | ||
|
||
componentBuilder.append(FComponent.fromLegacyText(title)).append("\n\n"); | ||
|
||
String unmuteButton = locale.getFormatString("command.warnlist.unmute-button", commandSender); | ||
|
||
int page = strings.length > 1 ? Math.max(1, Integer.parseInt(strings[1])) : 1; | ||
|
||
page = Math.min(lastPage, page); | ||
|
||
AtomicInteger atomicInteger = new AtomicInteger((page - 1) * perPage + 1); | ||
|
||
fPlayer.getWarnList().stream().skip((long) (page - 1) * perPage).limit(perPage).forEach(playerWarn -> { | ||
|
||
String playerMuteFormat = locale.getFormatString("command.warnlist.player-warn", commandSender) | ||
.replace("<unwarn>", unmuteButton) | ||
.replace("<player>", playerWarn.getPlayerName()) | ||
.replace("<reason>", playerWarn.getReason()) | ||
.replace("<time>", ObjectUtil.convertTimeToString(playerWarn.getDifferenceTime())) | ||
.replace("<moderator>", playerWarn.getModeratorName()); | ||
|
||
String unbanHover = locale.getFormatString("command.warnlist.unmute-hover", commandSender) | ||
.replace("<player>", playerWarn.getPlayerName()); | ||
|
||
FComponent textComponent = new FComponent(playerMuteFormat) | ||
.addHoverText(unbanHover) | ||
.addRunCommand("/unwarn " + playerWarn.getPlayerName() + " " + atomicInteger.getAndIncrement()); | ||
|
||
componentBuilder | ||
.append(textComponent.get()) | ||
.append("\n\n"); | ||
}); | ||
|
||
componentBuilder.append(new FListComponent("warnlist " + fPlayer.getRealName() + " ", "warnlist", commandSender, page, lastPage).get()); | ||
|
||
commandSender.spigot().sendMessage(componentBuilder.create()); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { | ||
wordsList.clear(); | ||
|
||
|
||
switch (strings.length) { | ||
case 1 -> Main.getDatabase().getPlayerNameList("warns", "player") | ||
.parallelStream() | ||
.forEach(playerName -> isStartsWith(strings[0], playerName)); | ||
case 2 -> { | ||
String playerName = strings[0]; | ||
FPlayer fPlayer = FPlayerManager.getPlayerFromName(playerName); | ||
|
||
if (fPlayer == null) break; | ||
|
||
if (!fPlayer.isOnline() && fPlayer.getWarnList() == null) | ||
fPlayer.synchronizeDatabase(); | ||
|
||
if (fPlayer.getWarnList().isEmpty()) break; | ||
|
||
int perPage = config.getInt("command.warnlist.per-page"); | ||
|
||
int lastPage = (int) Math.ceil((double) fPlayer.getWarnList().size() / perPage); | ||
isDigitInArray(strings[1], "", 1, lastPage + 1); | ||
} | ||
} | ||
|
||
Collections.sort(wordsList); | ||
|
||
return wordsList; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getCommandName() { | ||
return "warnlist"; | ||
} | ||
} |
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
Oops, something went wrong.