Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Added API for manipulating banners and banner patterns.
Browse files Browse the repository at this point in the history
Banner blocks have facing, base color, and pattern. Banner patterns are a
list of layers, which have a shape and color.
  • Loading branch information
ZephireNZ authored and SpaceManiac committed Nov 6, 2014
1 parent a073295 commit 162c1c3
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 2 deletions.
170 changes: 170 additions & 0 deletions src/main/java/org/bukkit/BannerPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package org.bukkit;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.apache.commons.lang.Validate;

import java.util.List;
import java.util.Map;

/**
* Represents a <i>complete</i> pattern for a banner.
*/
public class BannerPattern {

/**
* Represents an individual pattern texture on a banner.<p/>
*/
public static enum LayerTexture {
BORDER("bo"),
BOTTOM_HALF("hhb"),
BRICK("bri"),
CIRCLE("mc"),
CREEPER("cre"),
CURLY_BORDER("cbo"),
DIAGONAL_CROSS("cr"),
FLOWER("flo"),
GRADIENT("gra"),
GRADIENT_UP("gru"),
LEFT_HALF("vh"),
MOJANG("moj"),
RIGHT_HALF("vhr"),
RHOMBUS("mr"),
SAW_BOTTOM("bts"),
SAW_TOP("tts"),
SKULL("sku"),
STRIPES("ss"),
SQUARE_BOTTOM_LEFT("bl"),
SQUARE_BOTTOM_RIGHT("br"),
SQUARE_TOP_LEFT("tl"),
SQUARE_TOP_RIGHT("tr"),
STRIPE_BOTTOM("bs"),
STRAIGHT_CROSS("sc"),
STRIPE_DOWN_LEFT("dls"),
STRIPE_DOWN_RIGHT("drs"),
STRIPE_LEFT("ls"),
STRIPE_HORIZONTAL("ms"),
STRIPE_RIGHT("rs"),
STRIPE_TOP("ts"),
STRIPE_VERTICAL("cs"),
TOP_HALF("hh"),
TRIANGLE_BOTTOM("bt"),
TRIANGLE_BOTTOM_LEFT("lud"),
TRIANGLE_BOTTOM_RIGHT("rd"),
TRIANGLE_TOP("tt"),
TRIANGLE_TOP_LEFT("ld"),
TRIANGLE_TOP_RIGHT("rud");

private final String code;
private final static Map<String, LayerTexture> BY_CODE = Maps.newHashMap();

private LayerTexture(String code) {
this.code = code;
}

/**
* Gets the internal code of this banner texture
*
* @return The 2 or 3 character pattern code
* @deprecated Magic value
*/
@Deprecated
public String getCode() {
return code;
}

/**
* Gets a texture by the code
*
* @param code 2 or 3 character pattern code
* @return Banner pattern
* @deprecated Magic value
*/
@Deprecated
public static LayerTexture getByCode(String code) {
return BY_CODE.get(code);
}

static {
for (LayerTexture type : LayerTexture.values()) {
BY_CODE.put(type.code, type);
}
}
}

/**
* Representation of a single colored layer on a banner.
*/
public static class BannerLayer {

private LayerTexture texture;
private DyeColor color;

private BannerLayer(LayerTexture texture, DyeColor color) {
this.texture = texture;
this.color = color;
}

public LayerTexture getTexture() {
return texture;
}

public DyeColor getColor() {
return color;
}
}

/**
* Construct a builder for a BannerPattern.
*
* @return BannerPattern Builder
*/
public static Builder builder() {
return new Builder();
}

public static final class Builder {
ImmutableList.Builder<BannerLayer> layers = ImmutableList.builder();

/**
* Adds a colored layer to the banner.
*
* @param texture Pattern for this layer
* @param color Color of the layer
* @return This object, for chaining
* @throws IllegalArgumentException If texture or color is null
*/
public Builder layer(LayerTexture texture, DyeColor color) {
Validate.notNull(texture, "Cannot have null texture");
Validate.notNull(color, "Cannot have null color");
layers.add(new BannerLayer(texture, color));

return this;
}

/**
* Creates a {@link BannerPattern} from the contents of this builder.
*
* @return The built BannerPattern
*/
public BannerPattern build() {
return new BannerPattern(layers.build());
}
}

private final ImmutableList<BannerLayer> layers;

private BannerPattern(ImmutableList<BannerLayer> layers) {
this.layers = layers;
}

/**
* Get the layers of this pattern.
*
* @return The layers of this pattern
*/
public List<BannerLayer> getLayers() {
return layers;
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/bukkit/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ public enum Material {
COAL_BLOCK(173),
PACKED_ICE(174),
DOUBLE_PLANT(175),
STANDING_BANNER(176),
WALL_BANNER(177),
STANDING_BANNER(176, Banner.class),
WALL_BANNER(177, Banner.class),
DAYLIGHT_DETECTOR_INVERTED(178),
RED_SANDSTONE(179, Sandstone.class),
RED_SANDSTONE_STAIRS(180, Stairs.class),
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/bukkit/block/Banner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.bukkit.block;

import org.bukkit.BannerPattern;
import org.bukkit.DyeColor;

/**
* Represents either a standing or hanging banner.
*/
public interface Banner extends BlockState {

/**
* Sets the base color of the banner.
*
* @param color Base color, must not be null.
*/
public void setBase(DyeColor color);

/**
* Get the base color of the banner.
*
* @return Base color
*/
public DyeColor getBase();

/**
* Sets the pattern of the banner. The old pattern is removed completely.
*
* @param pattern Pattern to change to, must not be null.
*/
public void setPattern(BannerPattern pattern);

/**
* Gets the banner's pattern.
*
* @return Pattern currently on this banner
*/
public BannerPattern getPattern();
}
24 changes: 24 additions & 0 deletions src/main/java/org/bukkit/inventory/meta/BannerMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.bukkit.inventory.meta;

import org.bukkit.BannerPattern;

/**
* Represents a banner item.
*/
public interface BannerMeta extends ItemMeta {

/**
* Sets the pattern of the banner. The old pattern is removed completely.
*
* @param pattern Pattern to change to, must not be null
*/
public void setPattern(BannerPattern pattern);

/**
* Gets the banner's pattern.
*
* @return Pattern currently on this banner
*/
public BannerPattern getPattern();

}
Loading

0 comments on commit 162c1c3

Please sign in to comment.