001 package org.bukkit.material; 002 003 import org.bukkit.block.BlockFace; 004 import org.bukkit.Material; 005 006 /** 007 * Represents a lever 008 */ 009 public class Lever extends SimpleAttachableMaterialData implements Redstone { 010 public Lever() { 011 super(Material.LEVER); 012 } 013 014 /** 015 * 016 * @deprecated Magic value 017 */ 018 @Deprecated 019 public Lever(final int type) { 020 super(type); 021 } 022 023 public Lever(final Material type) { 024 super(type); 025 } 026 027 /** 028 * 029 * @deprecated Magic value 030 */ 031 @Deprecated 032 public Lever(final int type, final byte data) { 033 super(type, data); 034 } 035 036 /** 037 * 038 * @deprecated Magic value 039 */ 040 @Deprecated 041 public Lever(final Material type, final byte data) { 042 super(type, data); 043 } 044 045 /** 046 * Gets the current state of this Material, indicating if it's powered or 047 * unpowered 048 * 049 * @return true if powered, otherwise false 050 */ 051 public boolean isPowered() { 052 return (getData() & 0x8) == 0x8; 053 } 054 055 /** 056 * Set this lever to be powered or not. 057 * 058 * @param isPowered whether the lever should be powered or not 059 */ 060 public void setPowered(boolean isPowered) { 061 setData((byte) (isPowered ? (getData() | 0x8) : (getData() & ~0x8))); 062 } 063 064 /** 065 * Gets the face that this block is attached on 066 * 067 * @return BlockFace attached to 068 */ 069 public BlockFace getAttachedFace() { 070 byte data = (byte) (getData() & 0x7); 071 072 switch (data) { 073 case 0x1: 074 return BlockFace.WEST; 075 076 case 0x2: 077 return BlockFace.EAST; 078 079 case 0x3: 080 return BlockFace.NORTH; 081 082 case 0x4: 083 return BlockFace.SOUTH; 084 085 case 0x5: 086 case 0x6: 087 return BlockFace.DOWN; 088 089 case 0x0: 090 case 0x7: 091 return BlockFace.UP; 092 093 } 094 095 return null; 096 } 097 098 /** 099 * Sets the direction this lever is pointing in 100 */ 101 public void setFacingDirection(BlockFace face) { 102 byte data = (byte) (getData() & 0x8); 103 BlockFace attach = getAttachedFace(); 104 105 if (attach == BlockFace.DOWN) { 106 switch (face) { 107 case SOUTH: 108 case NORTH: 109 data |= 0x5; 110 break; 111 112 case EAST: 113 case WEST: 114 data |= 0x6; 115 break; 116 } 117 } else if (attach == BlockFace.UP) { 118 switch (face) { 119 case SOUTH: 120 case NORTH: 121 data |= 0x7; 122 break; 123 124 case EAST: 125 case WEST: 126 data |= 0x0; 127 break; 128 } 129 } else { 130 switch (face) { 131 case EAST: 132 data |= 0x1; 133 break; 134 135 case WEST: 136 data |= 0x2; 137 break; 138 139 case SOUTH: 140 data |= 0x3; 141 break; 142 143 case NORTH: 144 data |= 0x4; 145 break; 146 } 147 } 148 setData(data); 149 } 150 151 @Override 152 public String toString() { 153 return super.toString() + " facing " + getFacing() + " " + (isPowered() ? "" : "NOT ") + "POWERED"; 154 } 155 156 @Override 157 public Lever clone() { 158 return (Lever) super.clone(); 159 } 160 }