package com.nicewuerfel.blockown; import org.bukkit.entity.EntityType; import org.bukkit.material.MaterialData; import java.io.Serializable; public final class Material implements Serializable { private static final long serialVersionUID = -3658668250059098095L; public static final String ANY_STRING = "ALL"; public static final Material ANY = new Material(); private final boolean isBlock; private final EntityType entityType; private final org.bukkit.Material material; private Material() { this.isBlock = true; this.entityType = null; this.material = null; } public Material(org.bukkit.Material material) { this.isBlock = true; this.material = material; this.entityType = null; } public Material(EntityType entityType) { this.isBlock = false; this.material = null; this.entityType = entityType; } public EntityType getEntityType() { return entityType; } public org.bukkit.Material getMaterial() { return material; } public Class<? extends MaterialData> getData() { return material.getData(); } public boolean isAny() { return (material == null && entityType == null); } public boolean isBlock() { return isBlock; } public String getName() { if (isAny()) { return ANY_STRING; } else { if (isBlock()) { return material.toString(); } else { return entityType.toString(); } } } @SuppressWarnings("deprecation") public static Material parseMaterial(String materialName) { if (materialName.equalsIgnoreCase(ANY_STRING)) { return ANY; } EntityType type = null; try { type = EntityType.valueOf(materialName.toUpperCase()); } catch (IllegalArgumentException ignored) { // Ignore this and try something different } if (type != null) { return new Material(type); } org.bukkit.Material material; try { int id = Integer.parseInt(materialName); material = org.bukkit.Material.getMaterial(id); } catch (NumberFormatException e) { material = org.bukkit.Material.matchMaterial(materialName); } if (material != null) { return new Material(material); } throw new IllegalArgumentException("Unknown material: " + materialName); } @Override public int hashCode() { final int prime = 31; int result = 7; result = prime * result + ((entityType == null) ? 0 : entityType.hashCode()); result = prime * result + (isBlock ? 1231 : 1237); result = prime * result + ((material == null) ? 0 : material.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Material)) { return false; } Material other = (Material) obj; if (isBlock != other.isBlock) { return false; } if (entityType != other.entityType) { return false; } return material == other.material; } @Override public String toString() { return getName(); } }