Initial commit
This commit is contained in:
@ -0,0 +1,118 @@
|
||||
package net.knarcraft.placeholdersigns.handler;
|
||||
|
||||
import net.knarcraft.placeholdersigns.PlaceholderSigns;
|
||||
import net.knarcraft.placeholdersigns.container.PlaceholderSign;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A handler for keeping track of placeholder signs
|
||||
*/
|
||||
public class PlaceholderSignHandler implements ConfigurationSerializable {
|
||||
|
||||
private static final File signsFile = new File(PlaceholderSigns.getInstance().getDataFolder(), "signs.yml");
|
||||
|
||||
private Set<PlaceholderSign> placeholderSigns;
|
||||
private Map<Location, PlaceholderSign> locationLookup;
|
||||
|
||||
/**
|
||||
* Gets all registered signs
|
||||
*
|
||||
* @return <p>All registered signs</p>
|
||||
*/
|
||||
public @NotNull Set<PlaceholderSign> getSigns() {
|
||||
return new HashSet<>(placeholderSigns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a placeholder sign from the given location
|
||||
*
|
||||
* @param location <p>The location of the sign</p>
|
||||
* @return <p>The sign at the location, or null if no such sign exists</p>
|
||||
*/
|
||||
public PlaceholderSign getFromLocation(@NotNull Location location) {
|
||||
return locationLookup.get(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new placeholder sign
|
||||
*
|
||||
* @param sign <p>The sign to register</p>
|
||||
*/
|
||||
public void registerSign(@NotNull PlaceholderSign sign) {
|
||||
this.placeholderSigns.add(sign);
|
||||
locationLookup.put(sign.location(), sign);
|
||||
save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Un-registers a placeholder sign
|
||||
*
|
||||
* @param sign <p>The sign to un-register</p>
|
||||
*/
|
||||
public void unregisterSign(@NotNull PlaceholderSign sign) {
|
||||
locationLookup.remove(sign.location());
|
||||
this.placeholderSigns.remove(sign);
|
||||
save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all placeholder signs from disk
|
||||
*/
|
||||
public void load() {
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
|
||||
PlaceholderSignHandler loadedHandler = (PlaceholderSignHandler) configuration.get("signHandler");
|
||||
this.placeholderSigns = loadedHandler != null ? loadedHandler.placeholderSigns : new HashSet<>();
|
||||
this.locationLookup = loadedHandler != null ? loadedHandler.locationLookup : new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves all current placeholder signs
|
||||
*/
|
||||
public void save() {
|
||||
try {
|
||||
YamlConfiguration configuration = new YamlConfiguration();
|
||||
configuration.set("signHandler", this);
|
||||
configuration.save(signsFile);
|
||||
} catch (IOException exception) {
|
||||
PlaceholderSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to save placeholder signs!");
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("signs", this.placeholderSigns);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the given placeholder sign handler data
|
||||
*
|
||||
* @param data <p>The data to deserialize</p>
|
||||
* @return <p>The deserialized sign handler</p>
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "unused"})
|
||||
public static PlaceholderSignHandler deserialize(@NotNull Map<String, Object> data) {
|
||||
PlaceholderSignHandler placeholderSignHandler = new PlaceholderSignHandler();
|
||||
placeholderSignHandler.placeholderSigns = (Set<PlaceholderSign>) data.get("signs");
|
||||
Map<Location, PlaceholderSign> lookup = new HashMap<>();
|
||||
for (PlaceholderSign sign : placeholderSignHandler.placeholderSigns) {
|
||||
lookup.put(sign.location(), sign);
|
||||
}
|
||||
placeholderSignHandler.locationLookup = lookup;
|
||||
return placeholderSignHandler;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user