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

Fix support for Chinese and other characters in setwarp and sethome filenames; ensure backward compatibility with old safe string names replaced by underscores #5890

Open
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
26 changes: 16 additions & 10 deletions Essentials/src/main/java/com/earth2me/essentials/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ private String getHomeName(String search) {
}
return search;
}

//Optimize character compatibility
public Location getHome(final String name) {
final String search = getHomeName(name);
final LazyLocation loc = holder.homes().get(search);
LazyLocation loc = holder.homes().get(getHomeName(name));
if (loc == null) {
loc = holder.homes().get(getHomeName(StringUtil.safeString(name)));
}
return loc != null ? loc.location() : null;
}

Expand Down Expand Up @@ -179,28 +181,32 @@ public List<String> getHomes() {

public void setHome(String name, final Location loc) {
//Invalid names will corrupt the yaml
name = StringUtil.safeString(name);
name = StringUtil.new_safeString(name);
holder.homes().put(name, LazyLocation.fromLocation(loc));
config.save();
}

public void delHome(final String name) throws Exception {
String search = getHomeName(name);
if (!holder.homes().containsKey(search)) {
search = StringUtil.safeString(search);
}
if (holder.homes().containsKey(search)) {
holder.homes().remove(search);
String safeSearch = StringUtil.new_safeString(search);
String oldSafeSearch = StringUtil.safeString(search);
if (holder.homes().containsKey(safeSearch)) {
holder.homes().remove(safeSearch);
config.save();
} else if (holder.homes().containsKey(oldSafeSearch)) {
holder.homes().remove(oldSafeSearch);
config.save();
} else {
throw new TranslatableException("invalidHome", search);
}
}


public void renameHome(final String name, final String newName) throws Exception {
final LazyLocation location = holder.homes().remove(name);
//If possible, it is necessary to improve the compatibility here to make it support underscores
if (location != null) {
holder.homes().put(StringUtil.safeString(newName), location);
holder.homes().put(StringUtil.new_safeString(newName), location);
config.save();
} else {
throw new TranslatableException("invalidHome", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Worth(final File dataFolder) {
public BigDecimal getPrice(final IEssentials ess, final ItemStack itemStack) {
BigDecimal result = BigDecimal.ONE.negate();


final String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");

if (VersionUtil.PRE_FLATTENING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class Commandhome extends EssentialsCommand {
Expand Down Expand Up @@ -124,7 +125,9 @@ private void goHome(final User user, final User player, final String home, final
if (home.length() < 1) {
throw new NotEnoughArgumentsException();
}
final Location loc = player.getHome(home);

final Location loc = player.getHome(home);

if (loc == null) {
throw new NotEnoughArgumentsException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
import java.util.regex.Pattern;

public final class StringUtil {
private static final Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9-]");
private static final Pattern INVALIDCHARACTER = Pattern.compile("[/\\\\:*?\"<>|\\x00-\\x1F]");
private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]");
@SuppressWarnings("CheckStyle")
private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]");
private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]");


private StringUtil() {
}

//Used to clean file names before saving to disk
public static String sanitizeFileName(final String name) {
return INVALIDFILECHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_");
return INVALIDCHARACTER.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_");
}

//Used to clean strings/names before saving as filenames/permissions
Expand All @@ -30,6 +31,12 @@ public static String safeString(final String string) {
}
return STRICTINVALIDCHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_");
}
public static String new_safeString(final String string) {
if (string == null) {
return null;
}
return INVALIDCHARACTER.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_");
}

//Less restrictive string sanitizing, when not used as perm or filename
public static String sanitizeString(final String string) {
Expand Down