Changes and fixes a lot of things
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good
Adds a viewSign command which allows a player to see the entirety of a sign, including any part that overflows the character limit. Changes the save structure for placeholder signs. This change was necessary to support storage of placeholders on both sides of signs, which is also implemented in this commit. Updates Spigot Probably fixes a few bugs
This commit is contained in:
@ -2,10 +2,14 @@ package net.knarcraft.placeholdersigns.handler;
|
||||
|
||||
import net.knarcraft.placeholdersigns.PlaceholderSigns;
|
||||
import net.knarcraft.placeholdersigns.container.PlaceholderSign;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.sign.Side;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -13,12 +17,13 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A handler for keeping track of placeholder signs
|
||||
*/
|
||||
public class PlaceholderSignHandler implements ConfigurationSerializable {
|
||||
public class PlaceholderSignHandler {
|
||||
|
||||
private static final File signsFile = new File(PlaceholderSigns.getInstance().getDataFolder(), "signs.yml");
|
||||
|
||||
@ -40,6 +45,7 @@ public class PlaceholderSignHandler implements ConfigurationSerializable {
|
||||
* @param location <p>The location of the sign</p>
|
||||
* @return <p>The sign at the location, or null if no such sign exists</p>
|
||||
*/
|
||||
@Nullable
|
||||
public PlaceholderSign getFromLocation(@NotNull Location location) {
|
||||
return locationLookup.get(location);
|
||||
}
|
||||
@ -70,10 +76,58 @@ public class PlaceholderSignHandler implements ConfigurationSerializable {
|
||||
* Loads all placeholder signs from disk
|
||||
*/
|
||||
public void load() {
|
||||
this.placeholderSigns = new HashSet<>();
|
||||
this.locationLookup = new HashMap<>();
|
||||
|
||||
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<>();
|
||||
ConfigurationSection signSection = configuration.getConfigurationSection("signs");
|
||||
if (signSection == null) {
|
||||
PlaceholderSigns.getInstance().getLogger().log(Level.INFO, "PlaceholderSigns found no signs to load");
|
||||
return;
|
||||
}
|
||||
|
||||
for (String key : signSection.getKeys(false)) {
|
||||
String[] locationInfo = key.split(",");
|
||||
World world = Bukkit.getWorld(UUID.fromString(locationInfo[0]));
|
||||
double x = Integer.parseInt(locationInfo[1]);
|
||||
double y = Integer.parseInt(locationInfo[2]);
|
||||
double z = Integer.parseInt(locationInfo[3]);
|
||||
Location signLocation = new Location(world, x, y, z);
|
||||
|
||||
Map<Side, Map<Integer, String>> allPlaceholders = new HashMap<>();
|
||||
Map<Integer, String> frontPlaceholders = new HashMap<>();
|
||||
Map<Integer, String> backPlaceholders = new HashMap<>();
|
||||
|
||||
loadPlaceholders(signSection, key + ".placeholders.front.", frontPlaceholders);
|
||||
loadPlaceholders(signSection, key + ".placeholders.back.", backPlaceholders);
|
||||
|
||||
allPlaceholders.put(Side.FRONT, frontPlaceholders);
|
||||
allPlaceholders.put(Side.BACK, backPlaceholders);
|
||||
|
||||
PlaceholderSign sign = new PlaceholderSign(signLocation, allPlaceholders);
|
||||
this.placeholderSigns.add(sign);
|
||||
this.locationLookup.put(signLocation, sign);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads placeholders from one side of a sign
|
||||
*
|
||||
* @param signSection <p>The configuration section to read</p>
|
||||
* @param configurationKey <p>The configuration key pointing to the placeholders</p>
|
||||
* @param placeholderMap <p>The map to add read placeholders to</p>
|
||||
*/
|
||||
private void loadPlaceholders(@NotNull ConfigurationSection signSection, @NotNull String configurationKey,
|
||||
@NotNull Map<Integer, String> placeholderMap) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
String placeholderKey = configurationKey + i;
|
||||
if (!signSection.contains(placeholderKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String placeholder = signSection.getString(placeholderKey);
|
||||
placeholderMap.put(i, placeholder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,37 +136,46 @@ public class PlaceholderSignHandler implements ConfigurationSerializable {
|
||||
public void save() {
|
||||
try {
|
||||
YamlConfiguration configuration = new YamlConfiguration();
|
||||
configuration.set("signHandler", this);
|
||||
ConfigurationSection signsSection = configuration.createSection("signs");
|
||||
for (PlaceholderSign sign : placeholderSigns) {
|
||||
saveSign(signsSection, sign);
|
||||
}
|
||||
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
|
||||
* Saves a sign to the given configuration section
|
||||
*
|
||||
* @param data <p>The data to deserialize</p>
|
||||
* @return <p>The deserialized sign handler</p>
|
||||
* @param section <p>The configuration section to save to</p>
|
||||
* @param sign <p>The sign to save</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);
|
||||
private void saveSign(@NotNull ConfigurationSection section, @NotNull PlaceholderSign sign) {
|
||||
Location location = sign.location();
|
||||
if (location.getWorld() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String key = location.getWorld().getUID() + "," + location.getBlockX() + "," + location.getBlockY() +
|
||||
"," + location.getBlockZ();
|
||||
String frontKey = key + ".placeholders.front";
|
||||
String backKey = key + ".placeholders.back";
|
||||
|
||||
Map<Integer, String> frontPlaceholders = sign.placeholders().get(Side.FRONT);
|
||||
if (frontPlaceholders != null) {
|
||||
for (Map.Entry<Integer, String> entry : frontPlaceholders.entrySet()) {
|
||||
section.set(frontKey + "." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
Map<Integer, String> backPlaceholders = sign.placeholders().get(Side.BACK);
|
||||
if (backPlaceholders != null) {
|
||||
for (Map.Entry<Integer, String> entry : backPlaceholders.entrySet()) {
|
||||
section.set(backKey + "." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
placeholderSignHandler.locationLookup = lookup;
|
||||
return placeholderSignHandler;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user