Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some level of validation to /tempban command. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@
import java.util.Arrays;
import net.cubespace.geSuitBans.managers.BansManager;
import net.cubespace.geSuitBans.utils.StringUtils;
import net.cubespace.geSuitBans.utils.TimeParser;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;


public class TempBanCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {
@Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {

if(args.length > 1){
BansManager.tempBanPlayer(sender.getName(), args[0], args[1], (args.length > 2) ? StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " ") : "");
return true;
}
if(args.length > 1){
String player = args[0];
String timing = args[1];
String reason = (args.length > 2) ? StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " ") : "";
int seconds = TimeParser.parseString(timing);
if (seconds == 0) {
return false;
}
BansManager.tempBanPlayer(sender.getName(), player, seconds, reason);
return true;
}

return false;
}
return false;
}

}
15 changes: 12 additions & 3 deletions src/main/java/net/cubespace/geSuitBans/managers/BansManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public static void kickPlayer( String sender, String player, String msg ) {

}

public static void tempBanPlayer( String sender, String player, String timing, String reason ) {
int seconds = TimeParser.parseString(timing);

public static void tempBanPlayer( String sender, String player, int seconds, String reason ) {
ByteArrayOutputStream b = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream( b );
try {
Expand All @@ -71,6 +69,17 @@ public static void tempBanPlayer( String sender, String player, String timing, S
new PluginMessageTask( b ).runTaskAsynchronously( geSuitBans.instance );
}

/**
*
* @deprecated Does not validate timing is non-zero or a valid time.
*/
@Deprecated
public static void tempBanPlayer( String sender, String player, String timing, String reason ) {
int seconds = TimeParser.parseString(timing);
tempBanPlayer( sender, player, seconds, reason );
}


public static void unbanPlayer( String sender, String player ) {
ByteArrayOutputStream b = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream( b );
Expand Down
70 changes: 27 additions & 43 deletions src/main/java/net/cubespace/geSuitBans/utils/TimeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
* @author Chinwe
*/
public class TimeParser
{
private static final Pattern TIME_PATTERN = Pattern.compile("([0-9]+)([wdhms])");
private static final int SECOND = 1;
private static final int MINUTE = SECOND * 60;
private static final int HOUR = MINUTE * 60;
private static final int DAY = HOUR * 24;
private static final int WEEK = DAY * 7;

/**
* Parse a string input into milliseconds, using w(eeks), d(ays), h(ours), m(inutes) and s(econds) For example: 4d8m2s -> 4 days, 8 minutes and 2 seconds
Expand All @@ -22,52 +30,28 @@ public class TimeParser
*/
public static int parseString(String string)
{
List<String> list = new ArrayList<String>();

String c;
int goBack = 0;
for (int i = 0; i < string.length(); i++) {
c = String.valueOf(string.charAt(i));
if (c.matches("[a-zA-Z]")) {
list.add(string.substring(goBack, i + 1));
goBack = i + 1;

}
}
// Cleanse
int amount;
Matcher m = TIME_PATTERN.matcher(string);
int total = 0;
char ch;
for (String st : list) {
ch = st.charAt(st.length() - 1);
if (st.length() != 1 && String.valueOf(ch).matches("[M,w,d,h,m,s]")) {
// Total milliseconds
amount = Math.abs(Integer.parseInt(st.substring(0, st.length() - 1)));
switch (ch) {
case 's':
total += (amount);
break;
case 'm':
total += (amount * 60);
break;
case 'h':
total += (amount * 3600);
break;
case 'd':
total += (amount * 3600 * 24);
break;
case 'w':
total += (amount * 3600 * 24 * 7);
break;
}

while (m.find()) {
int amount = Integer.valueOf(m.group(1));
switch (m.group(2).charAt(0)) {
case 's':
total += amount * SECOND;
break;
case 'm':
total += amount * MINUTE;
break;
case 'h':
total += amount * HOUR;
break;
case 'd':
total += amount * DAY;
break;
case 'w':
total += amount * WEEK;
break;
}
}

if (total == 0) {
return -1;
}

return total;
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ commands:
permission: gesuit.bans.command.tempban
permission-message: §cYou do not have permission for this command
usage: |
"/<command> (player) (time) (reason)"
"/<command> (player) (time) (reason)"
"Example of a (time): 2d5h"
/<command> (player) (time) (reason)
/<command> (player) (time) (reason)
Example: /command bob 2d5h building things
Valid time units: s (seconds), m (minutes),
h (hours), d (days) and w (weeks)
unban:
description: Unbans a player from the proxy
aliases: [uban, removeban, pardon]
Expand Down Expand Up @@ -100,4 +102,4 @@ permissions:
gesuit.bans.command.tempban: true