Skip to content

Commit

Permalink
Split and Refactor how Holographs are populated.
Browse files Browse the repository at this point in the history
The getHologramConfig method was a bit long in the tooth, and most of the complexity was in the switch expression. This commit simply extracts the methods.

Creates four methods from existing code:
- `FlagWarConfig#populateHolo(ConfigurationSection, List, int)`
- `FlagWarConfig#addHoloItem(int, String, List)`
- `FlagWarConfig#addHoloText(int, String, List)`
- `FlagWarConfig#addHoloTimer(int, String, List)`
  • Loading branch information
TheFlagCourier committed Oct 28, 2021
1 parent d4f826d commit b08e1fa
Showing 1 changed file with 58 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NonNls;

import java.util.AbstractMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -185,59 +186,74 @@ public static List<Map.Entry<String, String>> getHologramConfig() {
LOGGER.severe("Hologram line indexes cannot be less than zero!");
LOGGER.severe("Ignoring invalid lines.");
}
for (int i = 0; i < maxIdx + 1; i++) {
String lineType = holoLines.getString(i + ".type", "empty");
String data = Colors.translateColorCodes(holoLines.getString(i + ".data", "null"));
populateHolo(holoLines, holoSettings, maxIdx);
return holoSettings;
}

private static void populateHolo(final ConfigurationSection hLines,
final List<Map.Entry<String, String>> holoSettings, final int maxIndex) {
for (int index = 0; index < maxIndex + 1; index++) {
String lineType = hLines.getString(index + ".type", "empty");
String data = Colors.translateColorCodes(hLines.getString(index + ".data", "null"));
switch (lineType.toLowerCase()) {
case "item" -> {
final var nItem = i;
if (Material.matchMaterial(data) == null) {
LOGGER.severe(() -> String.format("Invalid hologram material %s for line %s!", data, nItem));
setEmpty(holoSettings, i);
} else {
holoSettings.add(new AbstractMap.SimpleEntry<>("item", data));
}
}
case "text" -> {
final var nText = i;
if (data.equals("null")) {
LOGGER.severe(() -> String.format("Missing hologram text for line %s!", nText));
setEmpty(holoSettings, i);
} else {
holoSettings.add(new AbstractMap.SimpleEntry<>("text", data));
}
}
case "timer" -> {
final var nTimer = i;
if (!data.contains("%")) {
LOGGER.severe(() -> String.format("Missing time placeholder for hologram line %s!", nTimer));
setEmpty(holoSettings, i);
} else {
if (!hasTimerLine) {
hasTimerLine = true;
timerText = data;
holoSettings.add(new AbstractMap.SimpleEntry<>("timer", data));
} else {
LOGGER.severe(() -> String.format("Duplicate timer for hologram line %s!", nTimer));
setEmpty(holoSettings, i);
}
}
}
case "item" -> addHoloItem(index, data, holoSettings);
case "text" -> addHoloText(index, data, holoSettings);
case "timer" -> addHoloTimer(index, data, holoSettings);
case "empty" -> {
final var nEmpty = i;
final var nEmpty = index;
LOGGER.severe(() -> String.format("Missing hologram line type for line %s!", nEmpty));
setEmpty(holoSettings, i);
setEmpty(holoSettings, index);
}
default -> {
final var nDef = i;
final var nDef = index;
LOGGER.severe(() -> String.format("Invalid hologram line type %s for line %s!", lineType, nDef));
setEmpty(holoSettings, i);
setEmpty(holoSettings, index);
}
}
}
return holoSettings;
}

@NonNls
private static void addHoloItem(final int index, final String data,
final List<Map.Entry<String, String>> holoSettings) {
if (Material.matchMaterial(data) == null) {
LOGGER.severe(() -> String.format("Invalid hologram material %s for line %s!", data, index));
setEmpty(holoSettings, index);
} else {
holoSettings.add(new AbstractMap.SimpleEntry<>("item", data));
}
}

@NonNls
private static void addHoloText(final int index, final String data,
final List<Map.Entry<String, String>> holoSettings) {
if (data.equals("null")) {
LOGGER.severe(() -> String.format("Missing hologram text for line %s!", index));
setEmpty(holoSettings, index);
} else {
holoSettings.add(new AbstractMap.SimpleEntry<>("text", data));
}
}

@NonNls
private static void addHoloTimer(final int index, final String data,
final List<Map.Entry<String, String>> holoSettings) {
if (!data.contains("%")) {
LOGGER.severe(() -> String.format("Missing time placeholder for hologram line %s!", index));
setEmpty(holoSettings, index);
} else {
if (!hasTimerLine) {
hasTimerLine = true;
timerText = data;
holoSettings.add(new AbstractMap.SimpleEntry<>("timer", data));
} else {
LOGGER.severe(() -> String.format("Duplicate timer for hologram line %s!", index));
setEmpty(holoSettings, index);
}
}
}


/**
* Helper function for {@link #getHologramConfig()}. If the supplied line is the first line (i == 0), log that it
* will simply be ignored. Otherwise, log that the line will be set to empty, and add it to the hologram settings
Expand Down

0 comments on commit b08e1fa

Please sign in to comment.