Implement Flag icon changing via Locales file. (#1829)

This commit contains 2 changes:
- An option for Flag to use icon that is defined in locales after "icon" string.
- An option for ItemParser to parse icon or return given value, if parsing was not successful.

The flag option is not ideal, but it is simpler and easier to maintain then adding new config section where icons can be changed, as the locales file already contains a lot of info about each flag.
This commit is contained in:
BONNe 2021-08-29 23:30:45 +03:00 committed by GitHub
parent 0341515fc8
commit fa41abc062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 39 deletions

View File

@ -25,6 +25,8 @@ import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.ItemParser;
public class Flag implements Comparable<Flag> { public class Flag implements Comparable<Flag> {
@ -315,6 +317,13 @@ public class Flag implements Comparable<Flag> {
return PROTECTION_FLAGS + this.id + ".name"; return PROTECTION_FLAGS + this.id + ".name";
} }
/**
* @return a locale reference for the icon of this protection flag
*/
public String getIconReference() {
return PROTECTION_FLAGS + this.id + ".icon";
}
/** /**
* @return a locale reference for the description of this protection flag * @return a locale reference for the description of this protection flag
*/ */
@ -377,7 +386,7 @@ public class Flag implements Comparable<Flag> {
} }
// Start the flag conversion // Start the flag conversion
PanelItemBuilder pib = new PanelItemBuilder() PanelItemBuilder pib = new PanelItemBuilder()
.icon(new ItemStack(icon)) .icon(ItemParser.parse(user.getTranslation(this.getIconReference()), new ItemStack(icon)))
.name(user.getTranslation("protection.panel.flag-item.name-layout", TextVariables.NAME, user.getTranslation(getNameReference()))) .name(user.getTranslation("protection.panel.flag-item.name-layout", TextVariables.NAME, user.getTranslation(getNameReference())))
.clickHandler(clickHandler) .clickHandler(clickHandler)
.invisible(invisible); .invisible(invisible);

View File

@ -14,6 +14,7 @@ import org.bukkit.potion.PotionType;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.MissingFormatArgumentException;
import java.util.UUID; import java.util.UUID;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
@ -26,16 +27,28 @@ import world.bentobox.bentobox.BentoBox;
* @author tastybento, Poslovitch * @author tastybento, Poslovitch
*/ */
public class ItemParser { public class ItemParser {
/** /**
* Parse given string to ItemStack. * Parse given string to ItemStack.
* @param text String value of item stack. * @param text String value of item stack.
* @return ItemStack of parsed item or null. * @return ItemStack of parsed item or null.
*/ */
@Nullable
public static ItemStack parse(String text) { public static ItemStack parse(String text) {
return ItemParser.parse(text, null);
}
/**
* Parse given string to ItemStack.
* @param text String value of item stack.
* @param defaultItemStack Material that should be returned if parsing failed.
* @return ItemStack of parsed item or defaultItemStack.
*/
@Nullable
public static ItemStack parse(@Nullable String text, @Nullable ItemStack defaultItemStack) {
if (text == null || text.isBlank()) { if (text == null || text.isBlank()) {
// Text does not exist or is empty. // Text does not exist or is empty.
return null; return defaultItemStack;
} }
String[] part = text.split(":"); String[] part = text.split(":");
@ -69,7 +82,7 @@ public class ItemParser {
BentoBox.getInstance().logError("Could not parse item " + text); BentoBox.getInstance().logError("Could not parse item " + text);
} }
return null; return defaultItemStack;
} }
@ -86,7 +99,7 @@ public class ItemParser {
Material reqItem = Material.getMaterial(part[0].toUpperCase(java.util.Locale.ENGLISH)); Material reqItem = Material.getMaterial(part[0].toUpperCase(java.util.Locale.ENGLISH));
if (reqItem == null) { if (reqItem == null) {
return null; throw new IllegalArgumentException(part[0] + " is not a valid Material.");
} }
return new ItemStack(reqItem, reqAmount); return new ItemStack(reqItem, reqAmount);
@ -107,13 +120,11 @@ public class ItemParser {
String[] parsable = {part[0], part[2]}; String[] parsable = {part[0], part[2]};
ItemStack durability = parseItemQuantity(parsable); ItemStack durability = parseItemQuantity(parsable);
if (durability != null) { ItemMeta meta = durability.getItemMeta();
ItemMeta meta = durability.getItemMeta();
if (meta instanceof Damageable) { if (meta instanceof Damageable) {
((Damageable) meta).setDamage(Integer.parseInt(part[1])); ((Damageable) meta).setDamage(Integer.parseInt(part[1]));
durability.setItemMeta(meta); durability.setItemMeta(meta);
}
} }
return durability; return durability;
@ -131,7 +142,7 @@ public class ItemParser {
*/ */
private static ItemStack parsePotion(String[] part) { private static ItemStack parsePotion(String[] part) {
if (part.length != 6) { if (part.length != 6) {
return null; throw new MissingFormatArgumentException("Potion parsing requires 6 parts.");
} }
/* /*
@ -172,29 +183,25 @@ public class ItemParser {
* @return Banner as item stack. * @return Banner as item stack.
*/ */
private static ItemStack parseBanner(String[] part) { private static ItemStack parseBanner(String[] part) {
try { if (part.length >= 2) {
if (part.length >= 2) { Material bannerMat = Material.getMaterial(part[0]);
Material bannerMat = Material.getMaterial(part[0]); if (bannerMat == null) {
if (bannerMat == null) { BentoBox.getInstance().logError("Could not parse banner item " + part[0] + " so using a white banner.");
BentoBox.getInstance().logError("Could not parse banner item " + part[0] + " so using a white banner."); bannerMat = Material.WHITE_BANNER;
bannerMat = Material.WHITE_BANNER;
}
ItemStack result = new ItemStack(bannerMat, Integer.parseInt(part[1]));
BannerMeta meta = (BannerMeta) result.getItemMeta();
if (meta != null) {
for (int i = 2; i < part.length; i += 2) {
meta.addPattern(new Pattern(DyeColor.valueOf(part[i + 1]), PatternType.valueOf(part[i])));
}
result.setItemMeta(meta);
}
return result;
} else {
return null;
} }
} catch (Exception e) { ItemStack result = new ItemStack(bannerMat, Integer.parseInt(part[1]));
return null;
BannerMeta meta = (BannerMeta) result.getItemMeta();
if (meta != null) {
for (int i = 2; i < part.length; i += 2) {
meta.addPattern(new Pattern(DyeColor.valueOf(part[i + 1]), PatternType.valueOf(part[i])));
}
result.setItemMeta(meta);
}
return result;
} else {
throw new MissingFormatArgumentException("Banner parsing requires at least 2 parts.");
} }
} }
@ -228,11 +235,6 @@ public class ItemParser {
playerHead = new ItemStack(Material.PLAYER_HEAD); playerHead = new ItemStack(Material.PLAYER_HEAD);
} }
if (playerHead == null) {
// just a null-pointer check.
return null;
}
// Set correct Skull texture // Set correct Skull texture
try { try {
SkullMeta meta = (SkullMeta) playerHead.getItemMeta(); SkullMeta meta = (SkullMeta) playerHead.getItemMeta();

View File

@ -36,6 +36,7 @@ public class ItemParserTest {
private PotionMeta potionMeta; private PotionMeta potionMeta;
private BannerMeta bannerMeta; private BannerMeta bannerMeta;
private ItemStack defaultItem;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -67,6 +68,7 @@ public class ItemParserTest {
} }
}); });
defaultItem = new ItemStack(Material.STONE);
} }
@After @After
@ -77,16 +79,19 @@ public class ItemParserTest {
@Test @Test
public void testParseNull() { public void testParseNull() {
assertNull(ItemParser.parse(null)); assertNull(ItemParser.parse(null));
assertEquals(defaultItem, ItemParser.parse(null, defaultItem));
} }
@Test @Test
public void testParseBlank() { public void testParseBlank() {
assertNull(ItemParser.parse("")); assertNull(ItemParser.parse(""));
assertEquals(defaultItem, ItemParser.parse("", defaultItem));
} }
@Test @Test
public void testParseNoColons() { public void testParseNoColons() {
assertNull(ItemParser.parse("NOCOLONS")); assertNull(ItemParser.parse("NOCOLONS"));
assertEquals(defaultItem, ItemParser.parse("NOCOLONS", defaultItem));
} }
/* /*
@ -258,6 +263,8 @@ public class ItemParserTest {
@Test @Test
public void testParseBadTwoItem() { public void testParseBadTwoItem() {
assertNull(ItemParser.parse("STNE:5")); assertNull(ItemParser.parse("STNE:5"));
assertEquals(defaultItem, ItemParser.parse("STNE:3", defaultItem));
assertEquals(defaultItem, ItemParser.parse("STNE:Z", defaultItem));
} }
@Test @Test
@ -270,5 +277,8 @@ public class ItemParserTest {
@Test @Test
public void testParseBadThreeItem() { public void testParseBadThreeItem() {
assertNull(ItemParser.parse("STNE:5:5")); assertNull(ItemParser.parse("STNE:5:5"));
assertEquals(defaultItem, ItemParser.parse("STNE:5:5", defaultItem));
assertEquals(defaultItem, ItemParser.parse("STNE:AA:5", defaultItem));
assertEquals(defaultItem, ItemParser.parse("WOODEN_SWORD:4:AA", defaultItem));
} }
} }