mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-23 19:25:12 +01:00
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:
parent
0341515fc8
commit
fa41abc062
@ -25,6 +25,8 @@ import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.util.ItemParser;
|
||||
|
||||
|
||||
public class Flag implements Comparable<Flag> {
|
||||
|
||||
@ -315,6 +317,13 @@ public class Flag implements Comparable<Flag> {
|
||||
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
|
||||
*/
|
||||
@ -377,7 +386,7 @@ public class Flag implements Comparable<Flag> {
|
||||
}
|
||||
// Start the flag conversion
|
||||
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())))
|
||||
.clickHandler(clickHandler)
|
||||
.invisible(invisible);
|
||||
|
@ -14,6 +14,7 @@ import org.bukkit.potion.PotionType;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.MissingFormatArgumentException;
|
||||
import java.util.UUID;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
@ -26,16 +27,28 @@ import world.bentobox.bentobox.BentoBox;
|
||||
* @author tastybento, Poslovitch
|
||||
*/
|
||||
public class ItemParser {
|
||||
|
||||
/**
|
||||
* Parse given string to ItemStack.
|
||||
* @param text String value of item stack.
|
||||
* @return ItemStack of parsed item or null.
|
||||
*/
|
||||
@Nullable
|
||||
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()) {
|
||||
// Text does not exist or is empty.
|
||||
return null;
|
||||
return defaultItemStack;
|
||||
}
|
||||
|
||||
String[] part = text.split(":");
|
||||
@ -69,7 +82,7 @@ public class ItemParser {
|
||||
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));
|
||||
|
||||
if (reqItem == null) {
|
||||
return null;
|
||||
throw new IllegalArgumentException(part[0] + " is not a valid Material.");
|
||||
}
|
||||
|
||||
return new ItemStack(reqItem, reqAmount);
|
||||
@ -107,14 +120,12 @@ public class ItemParser {
|
||||
String[] parsable = {part[0], part[2]};
|
||||
ItemStack durability = parseItemQuantity(parsable);
|
||||
|
||||
if (durability != null) {
|
||||
ItemMeta meta = durability.getItemMeta();
|
||||
|
||||
if (meta instanceof Damageable) {
|
||||
((Damageable) meta).setDamage(Integer.parseInt(part[1]));
|
||||
durability.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
|
||||
return durability;
|
||||
}
|
||||
@ -131,7 +142,7 @@ public class ItemParser {
|
||||
*/
|
||||
private static ItemStack parsePotion(String[] part) {
|
||||
if (part.length != 6) {
|
||||
return null;
|
||||
throw new MissingFormatArgumentException("Potion parsing requires 6 parts.");
|
||||
}
|
||||
|
||||
/*
|
||||
@ -172,7 +183,6 @@ public class ItemParser {
|
||||
* @return Banner as item stack.
|
||||
*/
|
||||
private static ItemStack parseBanner(String[] part) {
|
||||
try {
|
||||
if (part.length >= 2) {
|
||||
Material bannerMat = Material.getMaterial(part[0]);
|
||||
if (bannerMat == null) {
|
||||
@ -191,10 +201,7 @@ public class ItemParser {
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
throw new MissingFormatArgumentException("Banner parsing requires at least 2 parts.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -228,11 +235,6 @@ public class ItemParser {
|
||||
playerHead = new ItemStack(Material.PLAYER_HEAD);
|
||||
}
|
||||
|
||||
if (playerHead == null) {
|
||||
// just a null-pointer check.
|
||||
return null;
|
||||
}
|
||||
|
||||
// Set correct Skull texture
|
||||
try {
|
||||
SkullMeta meta = (SkullMeta) playerHead.getItemMeta();
|
||||
|
@ -36,6 +36,7 @@ public class ItemParserTest {
|
||||
|
||||
private PotionMeta potionMeta;
|
||||
private BannerMeta bannerMeta;
|
||||
private ItemStack defaultItem;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
@ -67,6 +68,7 @@ public class ItemParserTest {
|
||||
}
|
||||
});
|
||||
|
||||
defaultItem = new ItemStack(Material.STONE);
|
||||
}
|
||||
|
||||
@After
|
||||
@ -77,16 +79,19 @@ public class ItemParserTest {
|
||||
@Test
|
||||
public void testParseNull() {
|
||||
assertNull(ItemParser.parse(null));
|
||||
assertEquals(defaultItem, ItemParser.parse(null, defaultItem));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseBlank() {
|
||||
assertNull(ItemParser.parse(""));
|
||||
assertEquals(defaultItem, ItemParser.parse("", defaultItem));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNoColons() {
|
||||
assertNull(ItemParser.parse("NOCOLONS"));
|
||||
assertEquals(defaultItem, ItemParser.parse("NOCOLONS", defaultItem));
|
||||
}
|
||||
|
||||
/*
|
||||
@ -258,6 +263,8 @@ public class ItemParserTest {
|
||||
@Test
|
||||
public void testParseBadTwoItem() {
|
||||
assertNull(ItemParser.parse("STNE:5"));
|
||||
assertEquals(defaultItem, ItemParser.parse("STNE:3", defaultItem));
|
||||
assertEquals(defaultItem, ItemParser.parse("STNE:Z", defaultItem));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -270,5 +277,8 @@ public class ItemParserTest {
|
||||
@Test
|
||||
public void testParseBadThreeItem() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user