Use single error message for invalid attributes which are required

This commit is contained in:
filoghost 2020-11-03 20:57:38 +01:00
parent 741446c638
commit 1511bd3348
2 changed files with 40 additions and 21 deletions

View File

@ -5,9 +5,6 @@
*/
package me.filoghost.chestcommands.parsing.icon;
import java.nio.file.Path;
import java.util.EnumMap;
import java.util.Map;
import me.filoghost.chestcommands.attribute.AttributeErrorHandler;
import me.filoghost.chestcommands.attribute.IconAttribute;
import me.filoghost.chestcommands.icon.InternalConfigurableIcon;
@ -19,22 +16,30 @@ import me.filoghost.fcommons.config.exception.ConfigValueException;
import me.filoghost.fcommons.logging.ErrorCollector;
import org.bukkit.Material;
import java.nio.file.Path;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class IconSettings {
private final Path menuFile;
private final String iconName;
private final Map<AttributeType, IconAttribute> attributes;
private final Map<AttributeType, IconAttribute> validAttributes;
private final Set<AttributeType> invalidAttributes;
public IconSettings(Path menuFile, String iconName) {
this.menuFile = menuFile;
this.iconName = iconName;
this.attributes = new EnumMap<>(AttributeType.class);
this.validAttributes = new EnumMap<>(AttributeType.class);
this.invalidAttributes = new HashSet<>();
}
public InternalConfigurableIcon createIcon() {
InternalConfigurableIcon icon = new InternalConfigurableIcon(Material.BEDROCK);
for (IconAttribute attribute : attributes.values()) {
for (IconAttribute attribute : validAttributes.values()) {
attribute.apply(icon);
}
@ -42,13 +47,18 @@ public class IconSettings {
}
public IconAttribute getAttributeValue(AttributeType attributeType) {
return attributes.get(attributeType);
return validAttributes.get(attributeType);
}
public boolean isMissingAttribute(AttributeType attributeType) {
return !validAttributes.containsKey(attributeType) && !invalidAttributes.contains(attributeType);
}
public void loadFrom(ConfigSection config, ErrorCollector errorCollector) {
for (String attributeName : config.getKeys()) {
AttributeType attributeType = null;
try {
AttributeType attributeType = AttributeType.fromAttributeName(attributeName);
attributeType = AttributeType.fromAttributeName(attributeName);
if (attributeType == null) {
throw new ParseException(Errors.Parsing.unknownAttribute);
}
@ -59,10 +69,13 @@ public class IconSettings {
ConfigValue configValue = config.get(attributeName);
IconAttribute iconAttribute = attributeType.getParser().parse(configValue, errorHandler);
attributes.put(attributeType, iconAttribute);
validAttributes.put(attributeType, iconAttribute);
} catch (ParseException | ConfigValueException e) {
errorCollector.add(e, Errors.Menu.invalidAttribute(this, attributeName));
if (attributeType != null) {
invalidAttributes.add(attributeType);
}
}
}
}

View File

@ -47,32 +47,42 @@ public class MenuParser {
private static void tryAddIconToMenu(InternalMenu menu, IconSettings iconSettings, ErrorCollector errorCollector) {
if (iconSettings.isMissingAttribute(AttributeType.POSITION_X)) {
errorCollector.add(Errors.Menu.missingAttribute(iconSettings, AttributeType.POSITION_X));
}
if (iconSettings.isMissingAttribute(AttributeType.POSITION_Y)) {
errorCollector.add(Errors.Menu.missingAttribute(iconSettings, AttributeType.POSITION_Y));
}
if (iconSettings.isMissingAttribute(AttributeType.MATERIAL)) {
errorCollector.add(Errors.Menu.missingAttribute(iconSettings, AttributeType.MATERIAL));
}
PositionAttribute positionX = (PositionAttribute) iconSettings.getAttributeValue(AttributeType.POSITION_X);
PositionAttribute positionY = (PositionAttribute) iconSettings.getAttributeValue(AttributeType.POSITION_Y);
if (positionX == null) {
errorCollector.add(Errors.Menu.missingAttribute(iconSettings, AttributeType.POSITION_X));
return;
}
if (positionY == null) {
errorCollector.add(Errors.Menu.missingAttribute(iconSettings, AttributeType.POSITION_Y));
if (positionX == null || positionY == null) {
return;
}
int row = positionY.getPosition() - 1;
int column = positionX.getPosition() - 1;
boolean invalidPosition = false;
if (row < 0 || row >= menu.getRows()) {
errorCollector.add(
Errors.Menu.invalidAttribute(iconSettings, AttributeType.POSITION_Y),
"it must be between 1 and " + menu.getRows());
return;
invalidPosition = true;
}
if (column < 0 || column >= menu.getColumns()) {
errorCollector.add(
Errors.Menu.invalidAttribute(iconSettings, AttributeType.POSITION_X),
"it must be between 1 and " + menu.getColumns());
invalidPosition = true;
}
if (invalidPosition) {
return;
}
@ -80,10 +90,6 @@ public class MenuParser {
errorCollector.add(Errors.Menu.iconOverridesAnother(iconSettings));
}
if (iconSettings.getAttributeValue(AttributeType.MATERIAL) == null) {
errorCollector.add(Errors.Menu.missingAttribute(iconSettings, AttributeType.MATERIAL));
}
menu.setIcon(row, column, iconSettings.createIcon());
}