so://phie

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy

blob: 444bdff8acf0b7af258c42a330757f0de72d6b15 (4499 bytes)[doc][blame][raw]

1package poppy.modules;

2

3import java.util.HashMap;

4import java.util.Map;

5import java.util.Map.Entry;

6

7import org.bukkit.Location;

8import org.bukkit.Material;

9import org.bukkit.block.Block;

10import org.bukkit.block.data.Bisected.Half;

11import org.bukkit.block.data.type.Stairs;

12import org.bukkit.entity.ArmorStand;

13import org.bukkit.entity.Entity;

14import org.bukkit.entity.EntityType;

15import org.bukkit.entity.Player;

16import org.bukkit.event.EventHandler;

17import org.bukkit.event.Listener;

18import org.bukkit.event.block.Action;

19import org.bukkit.event.block.BlockBreakEvent;

20import org.bukkit.event.entity.EntityDismountEvent;

21import org.bukkit.event.player.PlayerInteractEvent;

22

23import poppy.Utils;

24

25

26public class ChairModule implements Listener

27{

28 private static final Material[] CHAIR_TYPE = new Material[]

29 {

30 Material.OAK_STAIRS,

31 Material.SPRUCE_STAIRS,

32 Material.BIRCH_STAIRS,

33 Material.JUNGLE_STAIRS,

34 Material.ACACIA_STAIRS,

35 Material.DARK_OAK_STAIRS,

36 Material.ACACIA_STAIRS,

37 Material.CRIMSON_STAIRS,

38 Material.WARPED_STAIRS

39 };

40

41 private Map<Block, ArmorStand> blockMap = new HashMap<>();

42

43 public void onDisable()

44 {

45 for(ArmorStand armorStand : blockMap.values())

46 {

47 armorStand.eject();

48 armorStand.remove();

49 }

50 }

51

52 @EventHandler

53 public void onPlayerInteract(PlayerInteractEvent event)

54 {

55 Player player = event.getPlayer();

56

57 if(event.getAction() == Action.RIGHT_CLICK_BLOCK)

58 {

59 if(event.getClickedBlock() != null && !event.getPlayer().isSneaking())

60 {

61 Block block = event.getClickedBlock();

62

63 if(isChair(block.getType()))

64 {

65 if(!isInRange(block, player))

66 {

67 return;

68 }

69

70 if(blockMap.containsKey(block))

71 {

72 return;

73 }

74

75 if(isCompatible(block))

76 {

77 ArmorStand armorStand = createMountableChair(block, player);

78

79 if(armorStand == null)

80 {

81 return;

82 }

83

84 event.setCancelled(true);

85

86 blockMap.put(block, armorStand);

87 }

88 }

89 }

90 }

91 }

92

93 @EventHandler

94 public void onEntityDismount(EntityDismountEvent event)

95 {

96 if(event.getEntity() instanceof Player)

97 {

98 Entity entity = event.getDismounted();

99

100 if(entity != null && entity instanceof ArmorStand)

101 {

102 if(blockMap.containsValue(entity))

103 {

104 for(Entry<Block, ArmorStand> entry : blockMap.entrySet())

105 {

106 if(entry.getValue() == entity)

107 {

108 blockMap.remove(entry.getKey());

109

110 break;

111 }

112 }

113 }

114

115 Location location = entity.getLocation();

116 location.add(0, 1, 0);

117

118 for(Entity passenger : entity.getPassengers()) {

119 location.setPitch(passenger.getLocation().getPitch());

120 location.setYaw(passenger.getLocation().getYaw());

121 passenger.teleport(location);

122 }

123

124 entity.remove();

125 }

126 }

127 }

128

129 @EventHandler

130 public void onBlockBreak(BlockBreakEvent event)

131 {

132 Block block = event.getBlock();

133

134 if(blockMap.containsKey(block))

135 {

136 ArmorStand armorStand = blockMap.get(block);

137 armorStand.eject();

138

139 blockMap.remove(block);

140 }

141 }

142

143 private boolean isInRange(Block block, Player player)

144 {

145 return block.getLocation().add(0.5, 0.5, 0.5).distance(player.getLocation()) <= 2;

146 }

147

148 private boolean isChair(Material material)

149 {

150 for(Material chair : CHAIR_TYPE)

151 {

152 if(material == chair)

153 {

154 return true;

155 }

156 }

157

158 return false;

159 }

160

161 private boolean isCompatible(Block block)

162 {

163 Block upperBlock = block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0));

164

165 if(!Utils.isAir(upperBlock.getType()))

166 {

167 return false;

168 }

169

170 Stairs stairs = (Stairs) block.getState().getBlockData();

171

172 if(stairs.getHalf() != Half.BOTTOM)

173 {

174 return false;

175 }

176

177 if(stairs.isWaterlogged())

178 {

179 return false;

180 }

181

182 if(stairs.getShape() == Stairs.Shape.INNER_RIGHT || stairs.getShape() == Stairs.Shape.INNER_LEFT)

183 {

184 return false;

185 }

186

187 return true;

188 }

189

190 @SuppressWarnings("deprecation")

191 private ArmorStand createMountableChair(Block block, Player player)

192 {

193 Stairs stairs = (Stairs) block.getState().getBlockData();

194

195 Float yaw = Utils.convertFacingToYaw(stairs.getFacing());

196

197 if(yaw == null)

198 {

199 return null;

200 }

201

202 Location location = block.getLocation().add(0.5, -0.4, 0.5);

203 location.setYaw(yaw);

204

205 ArmorStand armorStand = (ArmorStand) block.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);

206 armorStand.setPassenger((Entity) player);

207 armorStand.setSmall(true);

208 armorStand.setGravity(false);

209 armorStand.setInvulnerable(true);

210 armorStand.setVisible(false);

211

212 return armorStand;

213 }

214}