TF-EssentialsX/Essentials/src/main/java/com/earth2me/essentials/commands/Commandunban.java

45 lines
1.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.Console;
2011-07-15 21:58:03 +00:00
import com.earth2me.essentials.User;
2014-07-12 16:37:25 +00:00
import org.bukkit.BanList;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
2015-04-15 04:06:16 +00:00
import java.util.logging.Level;
import static com.earth2me.essentials.I18n.tl;
public class Commandunban extends EssentialsCommand {
public Commandunban() {
super("unban");
}
@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
if (args.length < 1) {
throw new NotEnoughArgumentsException();
}
Use playerNeverOnServer message where appropriate (#3489) ### Use `playerNeverOnServer` message where it should be used: ### https://github.com/EssentialsX/Essentials/pull/3489/commits/309e1c470d4bcfe2a2186f9720ce33f26441b802 `playerNeverOnServer=\u00a74Player\u00a7c {0} \u00a74was never on this server.` is currently used in the **Commandmail** class at [Line 61](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L61), [Line 116](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L116) and [Line 129](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L129) however is **not called** as PlayerNotFoundException is thrown by `#getPlayer` breaking current execution (below). ``` Commandmail#run() throws Exception { ... User u = getPlayer(server, args[1], true, true); // throws PlayerNotFoundException if (u == null) { throw new Exception(tl("playerNeverOnServer", args[1])); } ... } ``` Before changes: ![bm](https://user-images.githubusercontent.com/24858857/87236993-6b679180-c3e8-11ea-83a7-002194f5c467.png) After changes: ![mailafter](https://user-images.githubusercontent.com/24858857/87237060-3576dd00-c3e9-11ea-8020-d5a80a958ca0.png) -------------------------- **Commandseen** currently throws the default PlayerNotFoundException `playerNotFound` message for players that have not logged on to the server where it would be more appropriate to use the `playerNeverOnServer` message. ``` Commandseen#run throws Exception { ... AsyncRunnable#run() { User userFromBukkit = ess.getUserMap().getUserFromBukkit(args[0]); <-- *** try { if (userFromBukkit != null) { <--- *** showUserSeen(userFromBukkit); } else { showUserSeen(getPlayer(server, sender, args, 0)); <--- *** } } catch (Exception e) { ess.showError(sender, e, commandLabel); } } private void showUserSeen(User user) throws Exception { if (user == null) { <--- *** throw new PlayerNotFoundException(); } showSeenMessage(server, sender, user, showBan, showIp, showLocation); } ... } ``` **`<-- ***`:** `usersFromBukkit` null check is performed before `#showUserSeen` so there is no need for another null check. `EssentialsCommand#getPlayer` throws **NotEnoughArguementsException** or **PlayerNotFoundException** after [arg checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L88) and [player checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L103). https://github.com/EssentialsX/Essentials/pull/3489#issuecomment-657138524 Before changes: ![sb](https://user-images.githubusercontent.com/24858857/87237038-e9c43380-c3e8-11ea-8294-8f91e8b6f25d.png) After changes: ![seenafter](https://user-images.githubusercontent.com/24858857/87237067-53444200-c3e9-11ea-92c5-1784b4dcd739.png) ---------------- ### 725128e Catch more specfic exception `PlayerNotFoundException`. Before changes: ![banbefore](https://user-images.githubusercontent.com/24858857/87237021-c1d4d000-c3e8-11ea-99e4-eb97b5a5ba6d.png) After changes: ![afterunban](https://user-images.githubusercontent.com/24858857/87237081-8d154880-c3e9-11ea-9d35-a25b8c105969.png)
2020-08-04 14:30:05 +00:00
2015-04-15 04:06:16 +00:00
String name;
try {
final User user = getPlayer(server, args, 0, true, true);
name = user.getName();
ess.getServer().getBanList(BanList.Type.NAME).pardon(name);
2020-10-03 17:46:05 +00:00
} catch (final PlayerNotFoundException e) {
2015-04-15 04:06:16 +00:00
final OfflinePlayer player = server.getOfflinePlayer(args[0]);
name = player.getName();
if (!player.isBanned()) {
Use playerNeverOnServer message where appropriate (#3489) ### Use `playerNeverOnServer` message where it should be used: ### https://github.com/EssentialsX/Essentials/pull/3489/commits/309e1c470d4bcfe2a2186f9720ce33f26441b802 `playerNeverOnServer=\u00a74Player\u00a7c {0} \u00a74was never on this server.` is currently used in the **Commandmail** class at [Line 61](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L61), [Line 116](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L116) and [Line 129](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L129) however is **not called** as PlayerNotFoundException is thrown by `#getPlayer` breaking current execution (below). ``` Commandmail#run() throws Exception { ... User u = getPlayer(server, args[1], true, true); // throws PlayerNotFoundException if (u == null) { throw new Exception(tl("playerNeverOnServer", args[1])); } ... } ``` Before changes: ![bm](https://user-images.githubusercontent.com/24858857/87236993-6b679180-c3e8-11ea-83a7-002194f5c467.png) After changes: ![mailafter](https://user-images.githubusercontent.com/24858857/87237060-3576dd00-c3e9-11ea-8020-d5a80a958ca0.png) -------------------------- **Commandseen** currently throws the default PlayerNotFoundException `playerNotFound` message for players that have not logged on to the server where it would be more appropriate to use the `playerNeverOnServer` message. ``` Commandseen#run throws Exception { ... AsyncRunnable#run() { User userFromBukkit = ess.getUserMap().getUserFromBukkit(args[0]); <-- *** try { if (userFromBukkit != null) { <--- *** showUserSeen(userFromBukkit); } else { showUserSeen(getPlayer(server, sender, args, 0)); <--- *** } } catch (Exception e) { ess.showError(sender, e, commandLabel); } } private void showUserSeen(User user) throws Exception { if (user == null) { <--- *** throw new PlayerNotFoundException(); } showSeenMessage(server, sender, user, showBan, showIp, showLocation); } ... } ``` **`<-- ***`:** `usersFromBukkit` null check is performed before `#showUserSeen` so there is no need for another null check. `EssentialsCommand#getPlayer` throws **NotEnoughArguementsException** or **PlayerNotFoundException** after [arg checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L88) and [player checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L103). https://github.com/EssentialsX/Essentials/pull/3489#issuecomment-657138524 Before changes: ![sb](https://user-images.githubusercontent.com/24858857/87237038-e9c43380-c3e8-11ea-8294-8f91e8b6f25d.png) After changes: ![seenafter](https://user-images.githubusercontent.com/24858857/87237067-53444200-c3e9-11ea-92c5-1784b4dcd739.png) ---------------- ### 725128e Catch more specfic exception `PlayerNotFoundException`. Before changes: ![banbefore](https://user-images.githubusercontent.com/24858857/87237021-c1d4d000-c3e8-11ea-99e4-eb97b5a5ba6d.png) After changes: ![afterunban](https://user-images.githubusercontent.com/24858857/87237081-8d154880-c3e9-11ea-9d35-a25b8c105969.png)
2020-08-04 14:30:05 +00:00
throw new Exception(tl("playerNeverOnServer", args[0]));
2015-04-15 04:06:16 +00:00
}
ess.getServer().getBanList(BanList.Type.NAME).pardon(name);
}
final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
server.getLogger().log(Level.INFO, tl("playerUnbanned", senderName, name));
2015-04-15 04:06:16 +00:00
ess.broadcastMessage("essentials.ban.notify", tl("playerUnbanned", senderName, name));
}
}