Implements #1
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good

This commit is contained in:
2024-04-30 03:09:47 +02:00
parent a34c63b7e2
commit f8ca5705a5
18 changed files with 489 additions and 76 deletions

View File

@ -3,6 +3,7 @@ package net.knarcraft.placeholdersigns.handler;
import net.knarcraft.placeholdersigns.PlaceholderSigns;
import net.knarcraft.placeholdersigns.container.PlaceholderSign;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.sign.Side;
@ -29,15 +30,7 @@ public class PlaceholderSignHandler {
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);
}
private Map<Chunk, Set<PlaceholderSign>> signsInChunk;
/**
* Gets a placeholder sign from the given location
@ -57,8 +50,11 @@ public class PlaceholderSignHandler {
*/
public void registerSign(@NotNull PlaceholderSign sign) {
this.placeholderSigns.add(sign);
locationLookup.put(sign.location(), sign);
save();
this.locationLookup.put(sign.getLocation(), sign);
Chunk chunk = sign.getLocation().getChunk();
this.signsInChunk.putIfAbsent(chunk, new HashSet<>());
this.signsInChunk.get(chunk).add(sign);
}
/**
@ -67,9 +63,24 @@ public class PlaceholderSignHandler {
* @param sign <p>The sign to un-register</p>
*/
public void unregisterSign(@NotNull PlaceholderSign sign) {
locationLookup.remove(sign.location());
this.locationLookup.remove(sign.getLocation());
this.placeholderSigns.remove(sign);
save();
this.signsInChunk.get(sign.getLocation().getChunk()).remove(sign);
}
/**
* Gets all placeholder signs in the given chunk
*
* @param chunk <p>The chunk to check</p>
* @return <p>All placeholder signs in the chunk</p>
*/
@NotNull
public Set<PlaceholderSign> getFromChunk(@NotNull Chunk chunk) {
if (this.signsInChunk.containsKey(chunk)) {
return this.signsInChunk.get(chunk);
} else {
return new HashSet<>();
}
}
/**
@ -78,6 +89,7 @@ public class PlaceholderSignHandler {
public void load() {
this.placeholderSigns = new HashSet<>();
this.locationLookup = new HashMap<>();
this.signsInChunk = new HashMap<>();
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile);
ConfigurationSection signSection = configuration.getConfigurationSection("signs");
@ -104,9 +116,16 @@ public class PlaceholderSignHandler {
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);
String updateDelayKey = key + ".updateDelay";
int updateDelay = -1;
if (signSection.contains(updateDelayKey)) {
updateDelay = signSection.getInt(updateDelayKey, -1);
}
if (updateDelay < 1) {
updateDelay = PlaceholderSigns.getInstance().getSignUpdateDelay();
}
registerSign(new PlaceholderSign(signLocation, allPlaceholders, updateDelay));
}
}
@ -153,7 +172,7 @@ public class PlaceholderSignHandler {
* @param sign <p>The sign to save</p>
*/
private void saveSign(@NotNull ConfigurationSection section, @NotNull PlaceholderSign sign) {
Location location = sign.location();
Location location = sign.getLocation();
if (location.getWorld() == null) {
return;
}
@ -163,19 +182,21 @@ public class PlaceholderSignHandler {
String frontKey = key + ".placeholders.front";
String backKey = key + ".placeholders.back";
Map<Integer, String> frontPlaceholders = sign.placeholders().get(Side.FRONT);
Map<Integer, String> frontPlaceholders = sign.getPlaceholders().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);
Map<Integer, String> backPlaceholders = sign.getPlaceholders().get(Side.BACK);
if (backPlaceholders != null) {
for (Map.Entry<Integer, String> entry : backPlaceholders.entrySet()) {
section.set(backKey + "." + entry.getKey(), entry.getValue());
}
}
section.set(key + ".updateDelay", sign.getUpdateDelay());
}
}