< Add a banner to a shield />

5
(1)

First we need to create our shield, by using the ItemStack.class

ItemStack stack = new ItemStack(Material.SHIELD); 

Then cast it to a BlockStateMeta

BlockStateMeta blockStateMeta = (BlockStateMeta) stack.getItemMeta(); 

Last of all, create a Banner object. Which is required to set the patterns like how you normally make a banner.

Banner banner = (Banner) blockStateMeta.getBlockState();

banner.addPattern(new Pattern(DyeColor.RED, PatternType.MOJANG));
banner.update(); 

Assign the objects to the itemstack. Assign the banner to the blockStateMeta and then to the ItemStack.

blockStateMeta.setBlockState(banner);
stack.setItemMeta(blockStateMeta); 

Final piece of code:

import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
  
public ItemStack createShieldWithBanner(){
  ItemStack stack = new ItemStack(Material.SHIELD);
  BlockStateMeta blockStateMeta = (BlockStateMeta) stack.getItemMeta();
  Banner banner = (Banner) blockStateMeta.getBlockState();
  
  banner.addPattern(new Pattern(DyeColor.RED, PatternType.MOJANG));
  banner.update();
  blockStateMeta.setBlockState(banner);
  stack.setItemMeta(blockStateMeta);

  return stack
} 

Did this help you?

Average rating 5 / 5. Vote count: 1

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on telegram
Telegram
Share on whatsapp
WhatsApp
Share on print
Print

Did this help you?

Average rating 5 / 5. Vote count: 1

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Justin van lier

Justin van lier

I am a self taught Software Engineer with experience in managing and developing game servers, websites, applications & backend systems. I began to work on a professional level since April, 2014. I'm always excited to teach people about Computer Science and the advantages that come along.