Skip to content

Commit

Permalink
Implement base_color and patterns for shields (currently only half-fu…
Browse files Browse the repository at this point in the history
…nctional because of Spigot)

Hopefully they'll update it soon.
  • Loading branch information
Morphan1 committed Mar 9, 2016
1 parent dcfb927 commit 06a48d6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
Expand Up @@ -8,7 +8,11 @@
import net.aufdemrand.denizencore.tags.Attribute;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;

public class ItemBaseColor implements Property {

Expand All @@ -17,7 +21,8 @@ public static boolean describes(dObject item) {
Material material = ((dItem) item).getItemStack().getType();
return material == Material.BANNER
|| material == Material.WALL_BANNER
|| material == Material.STANDING_BANNER;
|| material == Material.STANDING_BANNER
|| material == Material.SHIELD;
}
return false;
}
Expand All @@ -38,12 +43,28 @@ private ItemBaseColor(dItem item) {

dItem item;

private Element getBaseColor() {
DyeColor baseColor = ((BannerMeta) item.getItemStack().getItemMeta()).getBaseColor();
if (baseColor != null) {
return new Element(baseColor.name());
private DyeColor getBaseColor() {
ItemMeta itemMeta = item.getItemStack().getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
return ((Banner) ((BlockStateMeta) itemMeta).getBlockState()).getBaseColor();
}
return null;
else {
return ((BannerMeta) itemMeta).getBaseColor();
}
}

private void setBaseColor(DyeColor color) {
ItemStack itemStack = item.getItemStack();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
banner.setBaseColor(color);
((BlockStateMeta) itemMeta).setBlockState(banner);
}
else {
((BannerMeta) itemMeta).setBaseColor(color);
}
itemStack.setItemMeta(itemMeta);
}

@Override
Expand All @@ -63,11 +84,11 @@ public String getAttribute(Attribute attribute) {
// For the list of possible colors, see <@link url http://bit.ly/1dydq12>.
// -->
if (attribute.startsWith("base_color")) {
Element baseColor = getBaseColor();
DyeColor baseColor = getBaseColor();
if (baseColor != null) {
return getBaseColor().getAttribute(attribute.fulfill(1));
return new Element(baseColor.name()).getAttribute(attribute.fulfill(1));
}
return new Element("BLACK").getAttribute(attribute.fulfill(1));
return null;
}

return null;
Expand All @@ -76,9 +97,9 @@ public String getAttribute(Attribute attribute) {

@Override
public String getPropertyString() {
Element baseColor = getBaseColor();
DyeColor baseColor = getBaseColor();
if (baseColor != null) {
return getBaseColor().identify();
return baseColor.name();
}
return null;
}
Expand All @@ -103,9 +124,7 @@ public void adjust(Mechanism mechanism) {
// -->

if (mechanism.matches("base_color")) {
BannerMeta bannerMeta = (BannerMeta) item.getItemStack().getItemMeta();
bannerMeta.setBaseColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
item.getItemStack().setItemMeta(bannerMeta);
setBaseColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
}
}
}
Expand Up @@ -10,9 +10,13 @@
import net.aufdemrand.denizencore.utilities.CoreUtilities;
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.BannerMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,7 +28,8 @@ public static boolean describes(dObject item) {
Material material = ((dItem) item).getItemStack().getType();
return material == Material.BANNER
|| material == Material.WALL_BANNER
|| material == Material.STANDING_BANNER;
|| material == Material.STANDING_BANNER
|| material == Material.SHIELD;
}
return false;
}
Expand All @@ -47,12 +52,36 @@ private ItemPatterns(dItem item) {

private dList listPatterns() {
dList list = new dList();
for (Pattern pattern : ((BannerMeta) item.getItemStack().getItemMeta()).getPatterns()) {
for (Pattern pattern : getPatterns()) {
list.add(pattern.getColor().name() + "/" + pattern.getPattern().name());
}
return list;
}

private List<Pattern> getPatterns() {
ItemMeta itemMeta = item.getItemStack().getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
return ((Banner) ((BlockStateMeta) itemMeta).getBlockState()).getPatterns();
}
else {
return ((BannerMeta) itemMeta).getPatterns();
}
}

private void setPatterns(List<Pattern> patterns) {
ItemStack itemStack = item.getItemStack();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
banner.setPatterns(patterns);
((BlockStateMeta) itemMeta).setBlockState(banner);
}
else {
((BannerMeta) itemMeta).setPatterns(patterns);
}
itemStack.setItemMeta(itemMeta);
}

@Override
public String getAttribute(Attribute attribute) {

Expand Down Expand Up @@ -123,9 +152,7 @@ public void adjust(Mechanism mechanism) {
dB.echoError("Could not apply pattern to banner: " + string);
}
}
BannerMeta bannerMeta = (BannerMeta) item.getItemStack().getItemMeta();
bannerMeta.setPatterns(patterns);
item.getItemStack().setItemMeta(bannerMeta);
setPatterns(patterns);
}
}
}

0 comments on commit 06a48d6

Please sign in to comment.