Properly Support Shield Banners

This commit is contained in:
Olof Larsson 2016-06-02 10:45:06 +02:00
parent 9312bf860f
commit 4200155719
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
11 changed files with 197 additions and 261 deletions

View File

@ -250,7 +250,6 @@ public class DataItemStack implements Comparable<DataItemStack>
// SINCE: 1.8
// This should really be a list and not a set.
// The order matters and is explicitly assigned.
// String, Number, String, Number ...
@SerializedName("banner")
private MassiveListDef<DataBannerPattern> bannerPatterns = null;

View File

@ -1,6 +1,9 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.Banner;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
public abstract class WriterAbstractItemStackMeta<OB, CB, FA, FB> extends WriterAbstractItemStack<OB, CB, FA, FB>
@ -28,4 +31,46 @@ public abstract class WriterAbstractItemStackMeta<OB, CB, FA, FB> extends Writer
return itemStack.getItemMeta();
}
// -------------------------------------------- //
// BANNER
// -------------------------------------------- //
public static Banner getBanner(BlockStateMeta meta, boolean creative)
{
// Null
if (meta == null) return null;
// Creative
if (!meta.hasBlockState() && !creative) return null;
// Try
try
{
BlockState ret = meta.getBlockState();
if ( ! (ret instanceof Banner)) return null;
return (Banner)ret;
}
catch (Exception e)
{
// Catch errors such as: throw new IllegalStateException("Missing blockState for " + material);
return null;
}
}
public static void setBanner(BlockStateMeta meta, Banner banner)
{
// Null
if (meta == null) return;
if (banner == null) return;
// Dodge Update NPE
if (banner.getBaseColor() == null) return;
// Update with Force without Physics
banner.update(true, false);
// Apply
meta.setBlockState(banner);
}
}

View File

@ -1,46 +0,0 @@
package com.massivecraft.massivecore.item;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
public abstract class WriterAbstractItemStackMetaState<OB, CB, FA, FB> extends WriterAbstractItemStackMeta<OB, CB, FA, FB>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractItemStackMetaState(Class<CB> classCB)
{
super(classCB);
// For the setup to pass we must set a Material with a BlockStateMeta.
this.setMaterial(Material.SHIELD);
}
// -------------------------------------------- //
// CREATE INNER
// -------------------------------------------- //
public BlockState createItemMetaState()
{
return createItemMetaState(this.createItemMeta());
}
public static BlockState createItemMetaState(ItemMeta itemMeta)
{
if ( ! (itemMeta instanceof BlockStateMeta)) return null;
BlockStateMeta blockStateMeta = (BlockStateMeta)itemMeta;
try
{
return blockStateMeta.getBlockState();
}
catch (Exception e)
{
// Catch errors such as: throw new IllegalStateException("Missing blockState for " + material);
return null;
}
}
}

View File

@ -1,26 +0,0 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.BlockState;
public abstract class WriterAbstractItemStackMetaStateField<CB, FA, FB> extends WriterAbstractItemStackMetaState<BlockState, CB, FA, FB>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractItemStackMetaStateField(Class<CB> classCB)
{
super(classCB);
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public BlockState createOB()
{
return this.createItemMetaState();
}
}

View File

@ -1,56 +0,0 @@
package com.massivecraft.massivecore.item;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
public abstract class WriterAbstractItemStackMetaStateMorph<FA, FB> extends WriterAbstractItemStackMetaState<ItemMeta, BlockState, FA, FB>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public WriterAbstractItemStackMetaStateMorph()
{
super(BlockState.class);
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //
@Override
public ItemMeta createOB()
{
return this.createItemMeta();
}
// -------------------------------------------- //
// MORPH
// -------------------------------------------- //
@Override
public BlockState morphB(ItemMeta ob)
{
return createItemMetaState(ob);
}
// -------------------------------------------- //
// WRITE
// -------------------------------------------- //
@Override
public void writeInner(DataItemStack oa, ItemMeta ob, DataItemStack ca, BlockState cb, ItemStack d, boolean a2b)
{
super.writeInner(oa, ob, ca, cb, d, a2b);
// Write Back
if (a2b)
{
BlockStateMeta blockStateMeta = (BlockStateMeta)ob;
blockStateMeta.setBlockState(cb);
}
}
}

View File

@ -54,8 +54,9 @@ public class WriterItemStackMeta extends WriterAbstractItemStackMetaMorph<Object
WriterItemStackMetaBannerBase.class,
WriterItemStackMetaBannerPatterns.class,
// STATE
WriterItemStackMetaState.class,
// SHIELD
WriterItemStackMetaShieldBase.class,
WriterItemStackMetaShieldPatterns.class,
// POTION
WriterItemStackMetaPotion.class

View File

@ -0,0 +1,68 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
public class WriterItemStackMetaShieldBase extends WriterAbstractItemStackMetaField<BlockStateMeta, Integer, DyeColor>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaShieldBase i = new WriterItemStackMetaShieldBase();
public static WriterItemStackMetaShieldBase get() { return i; }
public WriterItemStackMetaShieldBase()
{
super(BlockStateMeta.class);
this.setMaterial(Material.SHIELD);
this.setConverterTo(ConverterToDyeColor.get());
this.setConverterFrom(ConverterFromDyeColor.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca, ItemStack d)
{
return ca.getBannerBase();
}
@Override
public void setA(DataItemStack ca, Integer fa, ItemStack d)
{
ca.setBannerBase(fa);
}
@Override
public DyeColor getB(BlockStateMeta cb, ItemStack d)
{
// Get
boolean creative = false;
Banner banner = getBanner(cb, creative);
if (banner == null) return null;
// Return
return banner.getBaseColor();
}
@Override
public void setB(BlockStateMeta cb, DyeColor fb, ItemStack d)
{
// Get
boolean creative = (fb != null);
Banner banner = getBanner(cb, creative);
if (banner == null) return;
// Change
banner.setBaseColor(fb);
// Set
setBanner(cb, banner);
}
}

View File

@ -0,0 +1,81 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.block.banner.Pattern;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
public class WriterItemStackMetaShieldPatterns extends WriterAbstractItemStackMetaField<BlockStateMeta, List<DataBannerPattern>, List<Pattern>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaShieldPatterns i = new WriterItemStackMetaShieldPatterns();
public static WriterItemStackMetaShieldPatterns get() { return i; }
public WriterItemStackMetaShieldPatterns()
{
super(BlockStateMeta.class);
this.setMaterial(Material.SHIELD);
this.setConverterTo(ConverterToBannerPatterns.get());
this.setConverterFrom(ConverterFromBannerPatterns.get());
this.addDependencyClasses(
WriterBannerPattern.class
);
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public List<DataBannerPattern> getA(DataItemStack ca, ItemStack d)
{
return ca.getBannerPatterns();
}
@Override
public void setA(DataItemStack ca, List<DataBannerPattern> fa, ItemStack d)
{
ca.setBannerPatterns(fa);
}
@Override
public List<Pattern> getB(BlockStateMeta cb, ItemStack d)
{
// Get
boolean creative = false;
Banner banner = getBanner(cb, creative);
if (banner == null) return null;
// Return
return banner.getPatterns();
}
@Override
public void setB(BlockStateMeta cb, List<Pattern> fb, ItemStack d)
{
// Get
boolean creative = (fb != null && !fb.isEmpty());
Banner banner = getBanner(cb, creative);
if (banner == null) return;
// Change
banner.setPatterns(fb);
// Ensure Base
if (banner.getBaseColor() == null && !banner.getPatterns().isEmpty())
{
banner.setBaseColor(DyeColor.BLACK);
}
// Set
setBanner(cb, banner);
}
}

View File

@ -1,20 +0,0 @@
package com.massivecraft.massivecore.item;
public class WriterItemStackMetaState extends WriterAbstractItemStackMetaStateMorph<Object, Object>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaState i = new WriterItemStackMetaState();
public static WriterItemStackMetaState get() { return i; }
public WriterItemStackMetaState()
{
this.addWriterClasses(
// SHIELD
WriterItemStackMetaStateShieldBase.class,
WriterItemStackMetaStateShieldPatterns.class
);
}
}

View File

@ -1,52 +0,0 @@
package com.massivecraft.massivecore.item;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.inventory.ItemStack;
public class WriterItemStackMetaStateShieldBase extends WriterAbstractItemStackMetaStateField<Banner, Integer, DyeColor>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaStateShieldBase i = new WriterItemStackMetaStateShieldBase();
public static WriterItemStackMetaStateShieldBase get() { return i; }
public WriterItemStackMetaStateShieldBase()
{
super(Banner.class);
this.setMaterial(Material.SHIELD);
this.setConverterTo(ConverterToDyeColor.get());
this.setConverterFrom(ConverterFromDyeColor.get());
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public Integer getA(DataItemStack ca, ItemStack d)
{
return ca.getBannerBase();
}
@Override
public void setA(DataItemStack ca, Integer fa, ItemStack d)
{
ca.setBannerBase(fa);
}
@Override
public DyeColor getB(Banner cb, ItemStack d)
{
return cb.getBaseColor();
}
@Override
public void setB(Banner cb, DyeColor fb, ItemStack d)
{
cb.setBaseColor(fb);
}
}

View File

@ -1,58 +0,0 @@
package com.massivecraft.massivecore.item;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.block.banner.Pattern;
import org.bukkit.inventory.ItemStack;
public class WriterItemStackMetaStateShieldPatterns extends WriterAbstractItemStackMetaStateField<Banner, List<DataBannerPattern>, List<Pattern>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final WriterItemStackMetaStateShieldPatterns i = new WriterItemStackMetaStateShieldPatterns();
public static WriterItemStackMetaStateShieldPatterns get() { return i; }
public WriterItemStackMetaStateShieldPatterns()
{
super(Banner.class);
this.setMaterial(Material.SHIELD);
this.setConverterTo(ConverterToBannerPatterns.get());
this.setConverterFrom(ConverterFromBannerPatterns.get());
this.addDependencyClasses(
WriterBannerPattern.class
);
}
// -------------------------------------------- //
// ACCESS
// -------------------------------------------- //
@Override
public List<DataBannerPattern> getA(DataItemStack ca, ItemStack d)
{
return ca.getBannerPatterns();
}
@Override
public void setA(DataItemStack ca, List<DataBannerPattern> fa, ItemStack d)
{
ca.setBannerPatterns(fa);
}
@Override
public List<Pattern> getB(Banner cb, ItemStack d)
{
return cb.getPatterns();
}
@Override
public void setB(Banner cb, List<Pattern> fb, ItemStack d)
{
cb.setPatterns(fb);
}
}