public static boolean placeStanding(Location location, BannerMeta meta) { Block block = location.getBlock(); block.setType(Material.STANDING_BANNER, false); final BlockState state = block.getState(); if(state instanceof Banner) { Banner banner = (Banner) block.getState(); applyToBlock(banner, meta); org.bukkit.material.Banner material = (org.bukkit.material.Banner) banner.getData(); material.setFacingDirection(BlockFaces.yawToFace(location.getYaw())); banner.setData(material); banner.update(true); return true; } return false; }
@Override public void change(Event e, Object[] delta, @NotNull Changer.ChangeMode mode) { ItemStack i = item.getSingle(e); if (i == null) { return; } if (i.getType() == Material.BANNER || i.getType() == Material.SHIELD) { BannerMeta meta = ((BannerMeta) i.getItemMeta()); switch (mode) { case ADD: Arrays.asList(((Pattern[]) delta)).forEach(meta::addPattern); break; case SET: meta.setPatterns(Arrays.asList(((Pattern[]) delta))); break; case DELETE: case RESET: for (int j = 1; j <= meta.numberOfPatterns(); j++) { meta.removePattern(j); } break; } i.setItemMeta(meta); } }
@Override public void change(Event e, Object[] delta, @NotNull Changer.ChangeMode mode) { ItemStack i = item.getSingle(e); if (i == null) { return; } if (i.getType() == Material.BANNER || i.getType() == Material.SHIELD) { BannerMeta meta = ((BannerMeta) i.getItemMeta()); switch (mode) { case SET: Color c = (Color) delta[0]; meta.setBaseColor(c.getWoolColor()); break; case RESET: meta.setBaseColor(DyeColor.WHITE); break; } i.setItemMeta(meta); } }
public static BannerMeta parseMncPattern(@NotNull String mnc) { if (mnc.matches("[a-p]a([a-p][b-zA-M])+")) { List<String> groups = StringUtils.sizedSplitString(mnc, 2, true); BannerMeta meta = emptyBannerMeta(); meta.setBaseColor(colorFromMnc(mnc.charAt(0))); for (String str : groups.subList(1, groups.size())) { if (!str.isEmpty()) { meta.addPattern(new Pattern(colorFromMnc(str.toCharArray()[0]), patternTypeFromMnc(str.toCharArray()[1]) )); } } return meta; } return null; }
public static ItemMeta parse(Element xml, ItemMeta source) { if (source instanceof BannerMeta) { return parseBanner(xml, (BannerMeta) source); } else if (source instanceof BookMeta) { return parseBook(xml, (BookMeta) source); } else if (source instanceof EnchantmentStorageMeta) { return parseEnchantmentStorage(xml, (EnchantmentStorageMeta) source); } else if (source instanceof FireworkMeta) { return parseFirework(xml, (FireworkMeta) source); } else if (source instanceof FireworkEffectMeta) { return parseFireworkEffect(xml, (FireworkEffectMeta) source); } else if (source instanceof LeatherArmorMeta) { return parseLeatherArmor(xml, (LeatherArmorMeta) source); } else if (source instanceof MapMeta) { return parseMap(xml, (MapMeta) source); } else if (source instanceof PotionMeta) { return parsePotion(xml, (PotionMeta) source); } else if (source instanceof SkullMeta) { return parseSkull(xml, (SkullMeta) source); } else if (source instanceof SpawnEggMeta) { return parseSpawnEgg(xml, (SpawnEggMeta) source); } return source; }
/** * Create a flag representing a banner item's layers. Does nothing if it is not * a banner * * @param item * the item. Does nothing if it isnt a banner */ @SuppressWarnings("deprecation") public Flag(ItemStack item) { this(DyeColor.WHITE); if(item.getType().equals(Material.BANNER)) { BannerMeta bm = ((BannerMeta) item.getItemMeta()); baseColor = bm.getBaseColor(); for(int i = 0; i < bm.numberOfPatterns(); i++) { if(bm.getPattern(i).getPattern().equals(PatternType.BASE)) { continue; } addLayer(new FlagLayer(bm.getPattern(i))); } } }
static public void saveBanner(Player player, ItemStack banner) { //只處理旗幟 if (!BannerUtil.isBanner(banner)) { player.sendMessage(MessageUtil.format(true, "&c" + tl("io.save-failed"))); return; } //設定檔 String fileName = getFileName(player); FileConfiguration config = KConfigManager.get(fileName); //索引值(時間戳記,不會重複) String key = String.valueOf(System.currentTimeMillis()); //旗幟資訊 BannerMeta bm = (BannerMeta) banner.getItemMeta(); //儲存 config.set(key + ".color", banner.getDurability()); List<String> patternList = new ArrayList<>(); for (Pattern pattern : bm.getPatterns()) { patternList.add(pattern.getPattern().getIdentifier() + ":" + pattern.getColor().toString()); } if (patternList.size() > 0) { config.set(key + ".patterns", patternList); } KConfigManager.save(fileName); //訊息 player.sendMessage(MessageUtil.format(true, "&a" + tl("io.save-success"))); }
@Override public boolean executeCommand(CommandSender sender, Command cmd, String label, String[] args) { BannerMaker bm = (BannerMaker) plugin; Player player = (Player) sender; ItemStack itemStack = bm.getVersionHandler().getItemInMainHand(player); if (!BannerUtil.isBanner(itemStack)) { player.sendMessage(MessageUtil.format(true, "&c" + tl("command.not-banner-hand"))); return true; } //複製旗幟,僅保留底色與樣式 BannerMeta originalBannerMeta = (BannerMeta) itemStack.getItemMeta(); KItemStack banner = new KItemStack(Material.BANNER) .durability(itemStack.getDurability()) .setPatterns(originalBannerMeta.getPatterns()); //顯示旗幟 InventoryMenuUtil.showBannerInfo(player, banner); return true; }
/** * Adds {@link Pattern}s to this banner, assuming it is a banner. * <p /> * <b>UNSAFE</b> * * @param patterns patterns to add * * @return this item builder instance, for chaining */ public ItemBuilder bannerAdd(Pattern... patterns) { if (patterns == null) { if (!this.failSilently) { throw new IllegalArgumentException("patterns cannot be null."); } return this; } if (isBannerMeta()) { try { for (Pattern pattern : patterns) { if (pattern != null) { ((BannerMeta) this.itemMeta).addPattern(pattern); } else { throw new NullPointerException("pattern element is null."); } } } catch (Exception e) { if (!this.failSilently) { e.printStackTrace(); } } } return this; }
/** * A method to serialize banner meta to a JSONObject. Saves the base color, each pattern color, and each * pattern type. * * @param banner The BannerMeta to serialize * @return The JSONObject representation of the BannerMeta, or null if an exception occurred */ public static JSONObject serializeBanner(BannerMeta banner) { try { JSONObject root = new JSONObject(); if (banner.getBaseColor() != null) root.put("base-color", banner.getBaseColor().getDyeData()); JSONArray colors = new JSONArray(); JSONArray patternTypes = new JSONArray(); for (Pattern pattern : banner.getPatterns()) { colors.put(pattern.getColor().getDyeData()); patternTypes.put(pattern.getPattern().getIdentifier()); } root.put("colors", colors); root.put("pattern-types", patternTypes); return root; } catch (JSONException ex) { ex.printStackTrace(); return null; } }
/** * A method to deserialize a BannerMeta object from a JSONObject. This method assumes that the JSONArrays containing * the colors and pattern types are the same length. * * @param json The JSONObject of the BannerMeta to deserialize * @return The BannerMeta or null if an exception occurs */ public static BannerMeta getBannerMeta(JSONObject json) { try { BannerMeta dummy = (BannerMeta) new ItemStack(Material.BANNER).getItemMeta(); if (json.has("base-color")) dummy.setBaseColor(DyeColor.getByDyeData(Byte.parseByte("" + json.get("base-color")))); JSONArray colors = json.getJSONArray("colors"); JSONArray patternTypes = json.getJSONArray("pattern-types"); for (int i = 0; i < colors.length(); i++) { dummy.addPattern(new Pattern(DyeColor.getByDyeData(Integer.valueOf((int) colors.get(i)).byteValue()), PatternType.getByIdentifier(patternTypes.getString(i)))); } return dummy; } catch (JSONException ex) { ex.printStackTrace(); return null; } }
@Override public void processMeta(Player player, ItemMeta m) { super.processMeta(player, m); BannerMeta meta = (BannerMeta) m; meta.setBaseColor(baseColor); meta.setPatterns(patterns); }
public static ItemStack createBanner(String name, List<String> lore, DyeColor color){ ItemStack banner = new ItemStack(Material.BANNER); BannerMeta itemMeta = (BannerMeta) banner.getItemMeta(); itemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_DESTROYS, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_PLACED_ON, ItemFlag.HIDE_UNBREAKABLE); ArrayList<String> colorLore = new ArrayList<>(); if (lore != null) { lore.forEach(str -> colorLore.add(Utils.colorize(str))); itemMeta.setLore(colorLore); } itemMeta.setBaseColor(color); itemMeta.setDisplayName(name); banner.setItemMeta(itemMeta); return banner; }
/** * @param banner * the banner to set */ public void setBanner(ItemStack banner) { if (banner.getType() != Material.BANNER && banner.getType() != Material.SHIELD) { return; } this.banner = (BannerMeta) banner.getItemMeta(); bannerColor = banner.getDurability(); }
@NotNull @Override protected ItemStack[] get(Event event) { ItemStack banner = new ItemStack(type); BannerMeta meta = (BannerMeta) banner.getItemMeta(); meta.setBaseColor(CollectionUtils.getRandom(DyeColor.values())); for (int i = 0; i < new Random().nextInt(7); i++) meta.addPattern(new Pattern(CollectionUtils.getRandom(DyeColor.values()), CollectionUtils.getRandom(PatternType.values()) )); banner.setItemMeta(meta); return new ItemStack[]{banner}; }
@Nullable @Override protected Pattern[] get(Event e) { ItemStack i = item.getSingle(e); if (i == null) { return null; } if (i.getType() == Material.BANNER || i.getType() == Material.SHIELD) { return ((BannerMeta) i.getItemMeta()).getPatterns().stream().toArray(Pattern[]::new); } return null; }
@Override public ItemStack convert(Block block) { if (block.getType() == Material.STANDING_BANNER || block.getType() == Material.WALL_BANNER) { Banner banner = (Banner) block.getState(); ItemStack item = new ItemStack(Material.BANNER); BannerMeta meta = (BannerMeta) item.getItemMeta(); meta.setPatterns(banner.getPatterns()); meta.setBaseColor(banner.getBaseColor()); item.setItemMeta(meta); return item; } return null; }
@Nullable @Override protected ItemStack[] get(Event e) { Color c = color.getSingle(e); if (c == null) return null; ItemStack returnItem = new ItemStack(mat); BannerMeta meta = ((BannerMeta) returnItem.getItemMeta()); meta.setPatterns(Arrays.asList(patterns.getAll(e))); meta.setBaseColor(c.getWoolColor()); returnItem.setItemMeta(meta); return new ItemStack[]{returnItem}; }
@Nullable @Override protected String[] get(Event e) { ItemStack i = item.getSingle(e); if (i == null) { return null; } if (i.getType() == Material.BANNER || i.getType() == Material.SHIELD) { return new String[]{BannerUtils.toMncPattern((BannerMeta) i.getItemMeta())}; } return null; }
@Override protected void execute(Event e) { ItemStack i = item.getSingle(e); Block b = block.getSingle(e); if (i == null || b == null) { return; } if ((i.getType() == Material.BANNER || i.getType() == Material.SHIELD) && b.getType() == Material.BANNER) { BannerMeta itemMeta = ((BannerMeta) i.getItemMeta()); Banner blockMeta = ((Banner) b.getState()); blockMeta.setPatterns(itemMeta.getPatterns()); blockMeta.setBaseColor(itemMeta.getBaseColor()); blockMeta.update(true, false); } }
public static String toMncPattern(@NotNull BannerMeta meta) { List<String> stringList = new ArrayList<>(); stringList.add(colorToMnc(meta.getBaseColor()) + "a"); for (Pattern pattern : meta.getPatterns()) { stringList.add(new String(new char[]{colorToMnc(pattern.getColor()), patternTypeToMnc(pattern.getPattern())})); } return StringUtils.join(stringList.toArray(new String[stringList.size()])); }
public static ItemStack createBanner(String name, List<String> lore, DyeColor color) { ItemStack banner = new ItemStack(Material.BANNER); BannerMeta itemMeta = (BannerMeta) banner.getItemMeta(); itemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_DESTROYS, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_PLACED_ON, ItemFlag.HIDE_UNBREAKABLE); ArrayList<String> colorLore = new ArrayList<>(); if (lore != null) { lore.forEach(str -> colorLore.add(Utils.colorize(str))); itemMeta.setLore(colorLore); } itemMeta.setBaseColor(color); itemMeta.setDisplayName(name); banner.setItemMeta(itemMeta); return banner; }
@Override public boolean setItemMeta(ItemMeta meta) { if (meta instanceof BannerMeta) { return super.setItemMeta(meta); } throw new IllegalArgumentException("Flag's item meta must be an instance of the " + BannerMeta.class.getSimpleName() + " class."); }
public static BannerMeta meta(ItemStack item) { if (isBanner(item)) { return (BannerMeta) item.getItemMeta(); } return null; }
public static ItemStack toItem(ItemStack item, Banner block) { BannerMeta meta = meta(item); if (meta != null) { item.setItemMeta(toMeta(meta, block)); } return item; }
private static ItemStack loadBanner(Player player, String key) { //設定檔 String fileName = getFileName(player); FileConfiguration config = KConfigManager.get(fileName); //檢查是否為物品 ItemStack banner = null; //檢查是否為正確格式 if (config.isInt(key + ".color") && (!config.contains(key + ".patterns") || config.isList(key + ".patterns"))) { //嘗試以新格式讀取 try { //建立旗幟 banner = new ItemStack(Material.BANNER, 1, (short) config.getInt(key + ".color")); BannerMeta bm = (BannerMeta) banner.getItemMeta(); //新增Patterns if (config.contains(key + ".patterns")) { List<String> patternsList = config.getStringList(key + ".patterns"); for (String str : patternsList) { String strPattern = str.split(":")[0]; String strColor = str.split(":")[1]; Pattern pattern = new Pattern(DyeColor.valueOf(strColor), PatternType.getByIdentifier(strPattern)); bm.addPattern(pattern); } banner.setItemMeta(bm); } //將key藏於Lore bm.setLore(Collections.singletonList(HiddenStringUtil.encodeString(key))); banner.setItemMeta(bm); } catch (Exception e) { banner = null; } } //只處理旗幟 if (!BannerUtil.isBanner(banner)) { return null; } return banner; }
/** * 是否可以在生存模式合成(不超過6個pattern) * * @param banner 旗幟 * @return 是否可以合成 */ static public boolean isCraftableInSurvival(ItemStack banner) { //只檢查旗幟 if (!isBanner(banner)) { return false; } int patternCount = ((BannerMeta) banner.getItemMeta()).numberOfPatterns(); return patternCount <= 6; }
@Override public boolean apply(@Nullable ItemStack item1, @Nullable ItemStack item2) { Boolean b = precondition(item1, item2, true); if (b != null) { return b; } if (item1.getItemMeta() instanceof BannerMeta && item2.getItemMeta() instanceof BannerMeta) { return ((BannerMeta) item1.getItemMeta()).getBaseColor() .equals(((BannerMeta) item2.getItemMeta()).getBaseColor()); } return false; }
@Override public boolean apply(@Nullable ItemStack item1, @Nullable ItemStack item2) { Boolean b = precondition(item1, item2, true); if (b != null) { return b; } if (item1.getItemMeta() instanceof BannerMeta && item2.getItemMeta() instanceof BannerMeta) { return ((BannerMeta) item1.getItemMeta()).getPatterns() .equals(((BannerMeta) item2.getItemMeta()).getPatterns()); } return false; }
/** * Sets this banner's base color, assuming it is a banner. * <p /> * <b>UNSAFE</b> * * @param color base color to set * * @return this item builder instance, for chaining */ public ItemBuilder bannerColor(DyeColor color) { if (isBannerMeta()) { try { ((BannerMeta) this.itemMeta).setBaseColor(color); } catch (Exception e) { if (!this.failSilently) { e.printStackTrace(); } } } return this; }
/** * Adds a {@link Pattern} to a specific index to this banner, assuming it is a banner. * <p /> * <b>UNSAFE</b> * * @param index index to set * @param pattern pattern to add * * @return this item builder instance, for chaining */ public ItemBuilder bannerSet(int index, Pattern pattern) { if (isBannerMeta()) { try { ((BannerMeta) this.itemMeta).setPattern(index, pattern); } catch (Exception e) { if (!this.failSilently) { e.printStackTrace(); } } } return this; }
/** * Sets this banner's patterns, assuming it is a banner. * <p /> * <b>UNSAFE</b> * * @param patterns patterns to set * * @return this item builder instance, for chaining */ public ItemBuilder bannerSet(List<Pattern> patterns) { if (isBannerMeta()) { try { ((BannerMeta) this.itemMeta).setPatterns(patterns); } catch (Exception e) { if (!this.failSilently) { e.printStackTrace(); } } } return this; }
/** * Removes a pattern by index from this banner, assuming it is a banner. * <p /> * <b>UNSAFE</b> * * @param index index of pattern to remove * * @return this item builder instance, for chaining */ public ItemBuilder bannerRemove(int index) { if (isBannerMeta()) { try { ((BannerMeta) this.itemMeta).removePattern(index); } catch (Exception e) { if (!this.failSilently) { e.printStackTrace(); } } } return this; }
private boolean isBannerMeta() { if (!(this.itemMeta instanceof BannerMeta)) { if (!this.failSilently) { throw new IllegalStateException("ItemMeta is not of BannerMeta."); } return false; } return true; }
/** * Gets random banner meta * * @return banner meta */ public static BannerMeta getRandomMeta() { if(ConfigManager.getServerVersion().isOlderThan(ConfigManager.ServerVersion.MINECRAFT_1_8_R2)) { return null; } BannerMeta meta = (BannerMeta) Bukkit.getItemFactory().getItemMeta(Material.BANNER); meta.setBaseColor(randomDyeColor()); for(int i = NumberUtils.randInt(0, PatternType.values().length) + 2; i > 0; i--) { meta.addPattern(new Pattern(randomDyeColor(), randomPatternType())); } return meta; }
/** * Deserializes a string to banner meta * * @param string serialized meta * @return banner meta */ public static BannerMeta deserialize(String string) { if(ConfigManager.getServerVersion().isOlderThan(ConfigManager.ServerVersion.MINECRAFT_1_8_R2)) { return null; } return getSerializer().deserialize(string); }
/** * Serializes banner meta into a string * * @param bannerMeta banner meta * @return serialized meta */ public static String serialize(BannerMeta bannerMeta) { if(ConfigManager.getServerVersion().isOlderThan(ConfigManager.ServerVersion.MINECRAFT_1_8_R2)) { return ""; } return getSerializer().serialize(bannerMeta); }
/** * Applies meta to a shield * * @param itemStack shield item * @param bannerMeta banner meta * @return shield item */ public static ItemStack applyMeta(ItemStack itemStack, BannerMeta bannerMeta) { if(itemStack.getType() != Material.SHIELD && itemStack.getType() != Material.BANNER) { throw new IllegalArgumentException("Passed ItemStack is not a shield nor a banner"); } ItemMeta meta = itemStack.getItemMeta(); BlockStateMeta blockStateMeta = (BlockStateMeta) meta; Banner banner = (Banner) blockStateMeta.getBlockState(); applyMeta(banner, bannerMeta); banner.update(); blockStateMeta.setBlockState(banner); itemStack.setItemMeta(blockStateMeta); return itemStack; }
/** * Turns a banner into banner meta * * @param banner banner block * @return banner meta */ public static BannerMeta getBannerMeta(Banner banner) { if(ConfigManager.getServerVersion().isOlderThan(ConfigManager.ServerVersion.MINECRAFT_1_8_R2)) { return null; } BannerMeta meta = (BannerMeta) Bukkit.getItemFactory().getItemMeta(Material.BANNER); meta.setBaseColor(banner.getBaseColor()); for(Pattern pattern : banner.getPatterns()) { meta.addPattern(pattern); } return meta; }
@Override public String serialize(BannerMeta bannerMeta) { if(bannerMeta == null) { return ""; } StringBuilder builder = new StringBuilder(); builder.append((bannerMeta.getBaseColor() == null ? DyeColor.BLACK : bannerMeta.getBaseColor()).name()); if(bannerMeta.numberOfPatterns() > 0) { builder.append(':'); } int index = 1; for(Pattern pattern : bannerMeta.getPatterns()) { builder.append(pattern.getColor().name()); builder.append('-'); builder.append(pattern.getPattern().getIdentifier()); if(index < bannerMeta.numberOfPatterns()) { builder.append("|"); } index++; } return builder.toString(); }