001 package org.bukkit.material; 002 003 import org.bukkit.Material; 004 import org.bukkit.block.BlockFace; 005 006 /** 007 * Represents a trap door 008 */ 009 public class TrapDoor extends SimpleAttachableMaterialData implements Openable { 010 public TrapDoor() { 011 super(Material.TRAP_DOOR); 012 } 013 014 /** 015 * 016 * @deprecated Magic value 017 */ 018 @Deprecated 019 public TrapDoor(final int type) { 020 super(type); 021 } 022 023 public TrapDoor(final Material type) { 024 super(type); 025 } 026 027 /** 028 * 029 * @deprecated Magic value 030 */ 031 @Deprecated 032 public TrapDoor(final int type, final byte data) { 033 super(type, data); 034 } 035 036 /** 037 * 038 * @deprecated Magic value 039 */ 040 @Deprecated 041 public TrapDoor(final Material type, final byte data) { 042 super(type, data); 043 } 044 045 public boolean isOpen() { 046 return ((getData() & 0x4) == 0x4); 047 } 048 049 public void setOpen(boolean isOpen) { 050 byte data = getData(); 051 052 if (isOpen) { 053 data |= 0x4; 054 } else { 055 data &= ~0x4; 056 } 057 058 setData(data); 059 } 060 061 /** 062 * Test if trapdoor is inverted 063 * 064 * @return true if inverted (top half), false if normal (bottom half) 065 */ 066 public boolean isInverted() { 067 return ((getData() & 0x8) != 0); 068 } 069 070 /** 071 * Set trapdoor inverted state 072 * 073 * @param inv - true if inverted (top half), false if normal (bottom half) 074 */ 075 public void setInverted(boolean inv) { 076 int dat = getData() & 0x7; 077 if (inv) { 078 dat |= 0x8; 079 } 080 setData((byte) dat); 081 } 082 083 public BlockFace getAttachedFace() { 084 byte data = (byte) (getData() & 0x3); 085 086 switch (data) { 087 case 0x0: 088 return BlockFace.SOUTH; 089 090 case 0x1: 091 return BlockFace.NORTH; 092 093 case 0x2: 094 return BlockFace.EAST; 095 096 case 0x3: 097 return BlockFace.WEST; 098 } 099 100 return null; 101 102 } 103 104 public void setFacingDirection(BlockFace face) { 105 byte data = (byte) (getData() & 0xC); 106 107 switch (face) { 108 case SOUTH: 109 data |= 0x1; 110 break; 111 case WEST: 112 data |= 0x2; 113 break; 114 case EAST: 115 data |= 0x3; 116 break; 117 } 118 119 setData(data); 120 } 121 122 @Override 123 public String toString() { 124 return (isOpen() ? "OPEN " : "CLOSED ") + super.toString() + " with hinges set " + getAttachedFace() + (isInverted() ? " inverted" : ""); 125 } 126 127 @Override 128 public TrapDoor clone() { 129 return (TrapDoor) super.clone(); 130 } 131 }