Essentials/Essentials/src/main/java/com/earth2me/essentials/Warps.java

182 lines
5.8 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
2012-03-15 03:12:43 +01:00
import com.earth2me.essentials.commands.WarpNotFoundException;
import com.earth2me.essentials.config.EssentialsConfiguration;
2013-10-11 04:44:41 +02:00
import com.earth2me.essentials.utils.StringUtil;
2015-04-15 06:06:16 +02:00
import net.ess3.api.InvalidNameException;
import net.ess3.api.InvalidWorldException;
import org.bukkit.Location;
2020-10-03 19:46:05 +02:00
import java.io.File;
2020-10-03 19:46:05 +02:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
2020-10-03 19:46:05 +02:00
import static com.earth2me.essentials.I18n.tl;
2015-04-15 06:06:16 +02:00
public class Warps implements IConf, net.ess3.api.IWarps {
private final Map<StringIgnoreCase, EssentialsConfiguration> warpPoints = new HashMap<>();
2015-04-15 06:06:16 +02:00
private final File warpsFolder;
public Warps(final File dataFolder) {
2015-04-15 06:06:16 +02:00
warpsFolder = new File(dataFolder, "warps");
if (!warpsFolder.exists()) {
warpsFolder.mkdirs();
}
reloadConfig();
}
@Override
public boolean isEmpty() {
return warpPoints.isEmpty();
}
@Override
public boolean isWarp(String name) {
return warpPoints.containsKey(new StringIgnoreCase(name));
}
2015-04-15 06:06:16 +02:00
@Override
public Collection<String> getList() {
final List<String> keys = new ArrayList<>();
2020-10-03 19:46:05 +02:00
for (final StringIgnoreCase stringIgnoreCase : warpPoints.keySet()) {
2015-04-15 06:06:16 +02:00
keys.add(stringIgnoreCase.getString());
}
keys.sort(String.CASE_INSENSITIVE_ORDER);
2015-04-15 06:06:16 +02:00
return keys;
}
@Override
2020-10-03 19:46:05 +02:00
public Location getWarp(final String warp) throws WarpNotFoundException, InvalidWorldException {
final EssentialsConfiguration conf = warpPoints.get(new StringIgnoreCase(warp));
2015-04-15 06:06:16 +02:00
if (conf == null) {
throw new WarpNotFoundException();
}
final Location loc = conf.getLocation(null).location();
if (loc == null) {
throw new WarpNotFoundException();
}
return loc;
2015-04-15 06:06:16 +02:00
}
@Override
2020-10-03 19:46:05 +02:00
public void setWarp(final String name, final Location loc) throws Exception {
setWarp(null, name, loc);
}
2020-10-03 19:46:05 +02:00
@Override
2020-10-03 19:46:05 +02:00
public void setWarp(final IUser user, final String name, final Location loc) throws Exception {
final String filename = StringUtil.sanitizeFileName(name);
EssentialsConfiguration conf = warpPoints.get(new StringIgnoreCase(name));
2015-04-15 06:06:16 +02:00
if (conf == null) {
2020-10-03 19:46:05 +02:00
final File confFile = new File(warpsFolder, filename + ".yml");
2015-04-15 06:06:16 +02:00
if (confFile.exists()) {
throw new Exception(tl("similarWarpExist"));
}
conf = new EssentialsConfiguration(confFile);
conf.load();
2015-04-15 06:06:16 +02:00
warpPoints.put(new StringIgnoreCase(name), conf);
}
conf.setProperty(null, loc);
conf.setProperty("name", name);
if (user != null) {
conf.setProperty("lastowner", user.getBase().getUniqueId().toString());
2015-04-15 06:06:16 +02:00
}
conf.save();
2015-04-15 06:06:16 +02:00
}
2020-10-03 19:46:05 +02:00
@Override
2020-10-03 19:46:05 +02:00
public UUID getLastOwner(final String warp) throws WarpNotFoundException {
final EssentialsConfiguration conf = warpPoints.get(new StringIgnoreCase(warp));
if (conf == null) {
throw new WarpNotFoundException();
}
UUID uuid = null;
try {
uuid = UUID.fromString(conf.getString("lastowner", null));
2020-10-03 19:46:05 +02:00
} catch (final Exception ignored) {
}
return uuid;
}
2020-10-03 19:46:05 +02:00
2015-04-15 06:06:16 +02:00
@Override
2020-10-03 19:46:05 +02:00
public void removeWarp(final String name) throws Exception {
final EssentialsConfiguration conf = warpPoints.get(new StringIgnoreCase(name));
2015-04-15 06:06:16 +02:00
if (conf == null) {
throw new Exception(tl("warpNotExist"));
}
if (!conf.getFile().delete()) {
throw new Exception(tl("warpDeleteError"));
}
warpPoints.remove(new StringIgnoreCase(name));
}
@Override
public final void reloadConfig() {
warpPoints.clear();
2020-10-03 19:46:05 +02:00
final File[] listOfFiles = warpsFolder.listFiles();
2015-04-15 06:06:16 +02:00
if (listOfFiles.length >= 1) {
2020-10-03 19:46:05 +02:00
for (final File listOfFile : listOfFiles) {
final String filename = listOfFile.getName();
if (listOfFile.isFile() && filename.endsWith(".yml")) {
2015-04-15 06:06:16 +02:00
try {
final EssentialsConfiguration conf = new EssentialsConfiguration(listOfFile);
2015-04-15 06:06:16 +02:00
conf.load();
final String name = conf.getString("name", null);
if (name != null && conf.hasProperty("world")) {
2015-04-15 06:06:16 +02:00
warpPoints.put(new StringIgnoreCase(name), conf);
}
2020-10-03 19:46:05 +02:00
} catch (final Exception ex) {
Essentials.getWrappedLogger().log(Level.WARNING, tl("loadWarpError", filename), ex);
2015-04-15 06:06:16 +02:00
}
}
}
}
}
2020-10-04 18:03:52 +02:00
/**
* @deprecated This method relates to the abandoned 3.x storage refactor and is not implemented.
*/
2015-04-15 06:06:16 +02:00
@Override
2020-10-04 18:03:52 +02:00
@Deprecated
2020-10-03 19:46:05 +02:00
public File getWarpFile(final String name) throws InvalidNameException {
2015-04-15 06:06:16 +02:00
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getCount() {
return getList().size();
}
private static class StringIgnoreCase {
private final String string;
2020-10-03 19:46:05 +02:00
StringIgnoreCase(final String string) {
2015-04-15 06:06:16 +02:00
this.string = string;
}
@Override
public int hashCode() {
return getString().toLowerCase(Locale.ENGLISH).hashCode();
}
@Override
2020-10-03 19:46:05 +02:00
public boolean equals(final Object o) {
2015-04-15 06:06:16 +02:00
if (o instanceof StringIgnoreCase) {
return getString().equalsIgnoreCase(((StringIgnoreCase) o).getString());
}
return false;
}
public String getString() {
return string;
}
}
}