Some changes to the Quality StringParser

This commit is contained in:
Sn0wStorm 2020-11-09 16:14:20 +01:00
parent 9553aa9fce
commit 390f7fe765
4 changed files with 45 additions and 78 deletions

View File

@ -47,7 +47,6 @@ public class WorldListener implements Listener {
public void onWorldUnload(WorldUnloadEvent event) { public void onWorldUnload(WorldUnloadEvent event) {
String worldName = event.getWorld().getName(); String worldName = event.getWorld().getName();
if (Barrel.hasDataInWorld(worldName) || BCauldron.hasDataInWorld(worldName)) { if (Barrel.hasDataInWorld(worldName) || BCauldron.hasDataInWorld(worldName)) {
P.p.log("Saving due to data in unloading world");
DataSave.save(true); DataSave.save(true);
Barrel.onUnload(worldName); Barrel.onUnload(worldName);
BCauldron.onUnload(worldName); BCauldron.onUnload(worldName);

View File

@ -103,7 +103,7 @@ public class BCauldronRecipe {
} }
List<Tuple<Integer,String>> lore = BRecipe.loadQualityStringList(cfg, id + ".lore", StringParser.loreParser); List<Tuple<Integer,String>> lore = BRecipe.loadQualityStringList(cfg, id + ".lore", StringParser.ParseType.LORE);
if (lore != null && !lore.isEmpty()) { if (lore != null && !lore.isEmpty()) {
recipe.lore = lore.stream().map(Tuple::second).collect(Collectors.toList()); recipe.lore = lore.stream().map(Tuple::second).collect(Collectors.toList());
} }

View File

@ -19,7 +19,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.stream.Collectors;
/** /**
* A Recipe used to Brew a Brewery Potion. * A Recipe used to Brew a Brewery Potion.
@ -128,10 +128,10 @@ public class BRecipe {
return null; return null;
} }
recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", StringParser.loreParser); recipe.lore = loadQualityStringList(configSectionRecipes, recipeId + ".lore", StringParser.ParseType.LORE);
recipe.servercmds = loadQualityStringList(configSectionRecipes, recipeId + ".servercommands", StringParser.cmdParser); recipe.servercmds = loadQualityStringList(configSectionRecipes, recipeId + ".servercommands", StringParser.ParseType.CMD);
recipe.playercmds = loadQualityStringList(configSectionRecipes, recipeId + ".playercommands", StringParser.cmdParser); recipe.playercmds = loadQualityStringList(configSectionRecipes, recipeId + ".playercommands", StringParser.ParseType.CMD);
recipe.drinkMsg = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinkmessage")); recipe.drinkMsg = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinkmessage"));
recipe.drinkTitle = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinktitle")); recipe.drinkTitle = P.p.color(BUtil.loadCfgString(configSectionRecipes, recipeId + ".drinktitle"));
@ -292,26 +292,13 @@ public class BRecipe {
} }
/** /**
* Load a list of strings from a ConfigurationSection and parse it accordingly using a parser. * Load a list of strings from a ConfigurationSection and parse the quality
*/ */
@Nullable @Nullable
public static List<Tuple<Integer, String>> loadQualityStringList(ConfigurationSection cfg, String path, StringParser p) { public static List<Tuple<Integer, String>> loadQualityStringList(ConfigurationSection cfg, String path, StringParser.ParseType parseType) {
List<String> load = BUtil.loadCfgStringList(cfg, path); List<String> load = BUtil.loadCfgStringList(cfg, path);
if (load != null) { if (load != null) {
List<Tuple<Integer, String>> list = new ArrayList<>(load.size()); return load.stream().map(line -> StringParser.parseQuality(line, parseType)).collect(Collectors.toList());
// create fallback parser, so passing null will convert the String to a Touple without furter processing.
if (p == null){
p = new StringParser() {
@Override
public Object parse(String line) {
return new Tuple<Integer, String>(0, line);
}
};
}
for (String line : load) {
list.add((Tuple<Integer, String>) p.parse(line));
}
return list;
} }
return null; return null;
} }
@ -606,24 +593,24 @@ public class BRecipe {
@Nullable @Nullable
public List<String> getLoreForQuality(int quality) { public List<String> getLoreForQuality(int quality) {
return getStringForQuality(quality, lore); return getStringsForQuality(quality, lore);
} }
@Nullable @Nullable
public List<String> getPlayercmdsForQuality(int quality) { public List<String> getPlayercmdsForQuality(int quality) {
return getStringForQuality(quality, playercmds); return getStringsForQuality(quality, playercmds);
} }
@Nullable @Nullable
public List<String> getServercmdsForQuality(int quality) { public List<String> getServercmdsForQuality(int quality) {
return getStringForQuality(quality, servercmds); return getStringsForQuality(quality, servercmds);
} }
/** /**
* Get a quality filtered list of supported attributes * Get a quality filtered list of supported attributes
*/ */
@Nullable @Nullable
public List<String> getStringForQuality(int quality, List<Tuple<Integer, String>> source) { public List<String> getStringsForQuality(int quality, List<Tuple<Integer, String>> source) {
if (source == null) return null; if (source == null) return null;
int plus; int plus;
if (quality <= 3) { if (quality <= 3) {
@ -881,7 +868,7 @@ public class BRecipe {
ArrayList<Tuple<Integer,String>> playercmds = new ArrayList<Tuple<Integer, String>>(cmds.length); ArrayList<Tuple<Integer,String>> playercmds = new ArrayList<Tuple<Integer, String>>(cmds.length);
for (String cmd : cmds) { for (String cmd : cmds) {
playercmds.add((Tuple<Integer, String>) StringParser.cmdParser.parse(cmd)); playercmds.add(StringParser.parseQuality(cmd, StringParser.ParseType.CMD));
} }
if (recipe.playercmds == null) { if (recipe.playercmds == null) {
recipe.playercmds = playercmds; recipe.playercmds = playercmds;
@ -898,7 +885,7 @@ public class BRecipe {
ArrayList<Tuple<Integer,String>> servercmds = new ArrayList<Tuple<Integer, String>>(cmds.length); ArrayList<Tuple<Integer,String>> servercmds = new ArrayList<Tuple<Integer, String>>(cmds.length);
for (String cmd : cmds) { for (String cmd : cmds) {
servercmds.add((Tuple<Integer, String>) StringParser.cmdParser.parse(cmd)); servercmds.add(StringParser.parseQuality(cmd, StringParser.ParseType.CMD));
} }
if (recipe.servercmds == null) { if (recipe.servercmds == null) {
recipe.servercmds = servercmds; recipe.servercmds = servercmds;

View File

@ -2,13 +2,9 @@ package com.dre.brewery.utility;
import com.dre.brewery.P; import com.dre.brewery.P;
public interface StringParser { public class StringParser {
public Object parse(String line); public static Tuple<Integer, String> parseQuality(String line, ParseType type) {
public static StringParser cmdParser = new StringParser() {
@Override
public Object parse(String line) {
line = P.p.color(line); line = P.p.color(line);
int plus = 0; int plus = 0;
if (line.startsWith("+++")) { if (line.startsWith("+++")) {
@ -24,35 +20,20 @@ public interface StringParser {
if (line.startsWith(" ")) { if (line.startsWith(" ")) {
line = line.substring(1); line = line.substring(1);
} }
if (line.startsWith("/")) {
line = line.substring(1);
}
return new Tuple<Integer,String>(plus, line);
}
};
public static StringParser loreParser = new StringParser() { if (type == ParseType.CMD && line.startsWith("/")) {
@Override
public Object parse(String line) {
line = P.p.color(line);
int plus = 0;
if (line.startsWith("+++")) {
plus = 3;
line = line.substring(3);
} else if (line.startsWith("++")) {
plus = 2;
line = line.substring(2);
} else if (line.startsWith("+")) {
plus = 1;
line = line.substring(1); line = line.substring(1);
} }
if (line.startsWith(" ")) {
line = line.substring(1); if (type == ParseType.LORE && !line.startsWith("§")) {
}
if (!line.startsWith("§")) {
line = "§9" + line; line = "§9" + line;
} }
return new Tuple<Integer,String>(plus, line); return new Tuple<Integer,String>(plus, line);
} }
};
public enum ParseType {
LORE,
CMD,
OTHER
}
} }