|
| 1 | +package me.ricglz.discoords; |
| 2 | + |
| 3 | +import me.ricglz.discoords.exceptions.CoordinatesExistException; |
| 4 | +import me.ricglz.discoords.exceptions.CoordinatesNotFoundException; |
| 5 | +import me.ricglz.discoords.exceptions.InvalidWorldException; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Locale; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +import org.bukkit.Location; |
| 18 | +import org.bukkit.Server; |
| 19 | + |
| 20 | +public class Coordinates { |
| 21 | + private final Map<StringIgnoreCase, DiscoordsConf> coordMap = new HashMap<>(); |
| 22 | + private final File coordsFolder; |
| 23 | + private final Server server; |
| 24 | + |
| 25 | + public Coordinates(final Server server, final File dataFolder) { |
| 26 | + this.server = server; |
| 27 | + coordsFolder = new File(dataFolder, "coords"); |
| 28 | + if (!coordsFolder.exists()) { |
| 29 | + coordsFolder.mkdirs(); |
| 30 | + } |
| 31 | + reloadConfig(); |
| 32 | + } |
| 33 | + |
| 34 | + public boolean isEmpty() { |
| 35 | + return coordMap.isEmpty(); |
| 36 | + } |
| 37 | + |
| 38 | + public Collection<String> getList() { |
| 39 | + final List<String> keys = new ArrayList<>(); |
| 40 | + for (final StringIgnoreCase filename : coordMap.keySet()) { |
| 41 | + keys.add(filename.getString()); |
| 42 | + } |
| 43 | + keys.sort(String.CASE_INSENSITIVE_ORDER); |
| 44 | + return keys; |
| 45 | + } |
| 46 | + |
| 47 | + public Location getCoordinates(final String coords) throws CoordinatesNotFoundException, InvalidWorldException { |
| 48 | + final DiscoordsConf conf = coordMap.get(new StringIgnoreCase(coords)); |
| 49 | + if (conf == null) { |
| 50 | + throw new CoordinatesNotFoundException(); |
| 51 | + } |
| 52 | + return conf.getLocation(null, server); |
| 53 | + } |
| 54 | + |
| 55 | + private String sanitizeFileName(final String name) { |
| 56 | + final Pattern invalidFileChars = Pattern.compile("[^a-z0-9-]"); |
| 57 | + return invalidFileChars.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_"); |
| 58 | + } |
| 59 | + |
| 60 | + public void setCoordinates(final String name, final Location loc) throws CoordinatesExistException, IOException { |
| 61 | + final String filename = sanitizeFileName(name); |
| 62 | + final StringIgnoreCase ignoreCaseName = new StringIgnoreCase(name); |
| 63 | + DiscoordsConf conf = coordMap.get(ignoreCaseName); |
| 64 | + if (conf == null) { |
| 65 | + final File confFile = new File(coordsFolder, filename + ".yml"); |
| 66 | + if (confFile.exists()) { |
| 67 | + throw new CoordinatesExistException(); |
| 68 | + } |
| 69 | + conf = new DiscoordsConf(confFile); |
| 70 | + coordMap.put(ignoreCaseName, conf); |
| 71 | + } |
| 72 | + conf.setProperty(null, loc); |
| 73 | + conf.setProperty("name", name); |
| 74 | + try { |
| 75 | + conf.saveWithError(); |
| 76 | + } catch (final IOException ex) { |
| 77 | + throw new IOException("That was an invalid coordinates label"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public final void reloadConfig() { |
| 82 | + coordMap.clear(); |
| 83 | + final File[] listOfFiles = coordsFolder.listFiles(); |
| 84 | + if (listOfFiles.length >= 1) { |
| 85 | + for (final File listOfFile : listOfFiles) { |
| 86 | + final String filename = listOfFile.getName(); |
| 87 | + if (listOfFile.isFile() && filename.endsWith(".yml")) { |
| 88 | + try { |
| 89 | + final DiscoordsConf conf = new DiscoordsConf(listOfFile); |
| 90 | + conf.load(); |
| 91 | + final String name = conf.getString("name"); |
| 92 | + if (name != null) { |
| 93 | + coordMap.put(new StringIgnoreCase(name), conf); |
| 94 | + } |
| 95 | + } catch (final Exception ex) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public int getCount() { |
| 104 | + return getList().size(); |
| 105 | + } |
| 106 | + |
| 107 | + private static class StringIgnoreCase { |
| 108 | + private final String string; |
| 109 | + |
| 110 | + StringIgnoreCase(final String string) { |
| 111 | + this.string = string; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int hashCode() { |
| 116 | + return getString().toLowerCase(Locale.ENGLISH).hashCode(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean equals(final Object o) { |
| 121 | + if (o instanceof StringIgnoreCase) { |
| 122 | + return getString().equalsIgnoreCase(((StringIgnoreCase) o).getString()); |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + public String getString() { |
| 128 | + return string; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments