so://phie

poppy

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

blob: ece0ab3a948c9d7e0fd9e06c30c5fd5e4988b0c5 (4532 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.player.PlayerInteractEvent;

21import org.bukkit.util.Vector;

22import org.spigotmc.event.entity.EntityDismountEvent;

23

24import poppy.Utils;

25

26

27public class ChairModule implements Listener

28{

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

30 {

31 Material.OAK_STAIRS,

32 Material.SPRUCE_STAIRS,

33 Material.BIRCH_STAIRS,

34 Material.JUNGLE_STAIRS,

35 Material.ACACIA_STAIRS,

36 Material.DARK_OAK_STAIRS,

37 Material.ACACIA_STAIRS,

38 Material.CRIMSON_STAIRS,

39 Material.WARPED_STAIRS

40 };

41

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

43

44 public void onDisable()

45 {

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

47 {

48 armorStand.eject();

49 armorStand.remove();

50 }

51 }

52

53 @EventHandler

54 public void onPlayerInteract(PlayerInteractEvent event)

55 {

56 Player player = event.getPlayer();

57

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

59 {

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

61 {

62 Block block = event.getClickedBlock();

63

64 if(isChair(block.getType()))

65 {

66 if(!isInRange(block, player))

67 {

68 return;

69 }

70

71 if(blockMap.containsKey(block))

72 {

73 return;

74 }

75

76 if(isCompatible(block))

77 {

78 ArmorStand armorStand = createMountableChair(block, player);

79

80 if(armorStand == null)

81 {

82 return;

83 }

84

85 event.setCancelled(true);

86

87 blockMap.put(block, armorStand);

88 }

89 }

90 }

91 }

92 }

93

94 @EventHandler

95 public void onEntityDismount(EntityDismountEvent event)

96 {

97 if(event.getEntity() instanceof Player)

98 {

99 Entity entity = event.getDismounted();

100

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

102 {

103 if(blockMap.containsValue(entity))

104 {

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

106 {

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

108 {

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

110

111 break;

112 }

113 }

114 }

115

116 Location location = entity.getLocation();

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

118

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

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

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

122 passenger.teleport(location);

123 }

124

125 entity.remove();

126 }

127 }

128 }

129

130 @EventHandler

131 public void onBlockBreak(BlockBreakEvent event)

132 {

133 Block block = event.getBlock();

134

135 if(blockMap.containsKey(block))

136 {

137 ArmorStand armorStand = blockMap.get(block);

138 armorStand.eject();

139

140 blockMap.remove(block);

141 }

142 }

143

144 private boolean isInRange(Block block, Player player)

145 {

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

147 }

148

149 private boolean isChair(Material material)

150 {

151 for(Material chair : CHAIR_TYPE)

152 {

153 if(material == chair)

154 {

155 return true;

156 }

157 }

158

159 return false;

160 }

161

162 private boolean isCompatible(Block block)

163 {

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

165

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

167 {

168 return false;

169 }

170

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

172

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

174 {

175 return false;

176 }

177

178 if(stairs.isWaterlogged())

179 {

180 return false;

181 }

182

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

184 {

185 return false;

186 }

187

188 return true;

189 }

190

191 @SuppressWarnings("deprecation")

192 private ArmorStand createMountableChair(Block block, Player player)

193 {

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

195

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

197

198 if(yaw == null)

199 {

200 return null;

201 }

202

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

204 location.setYaw(yaw);

205

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

207 armorStand.setPassenger((Entity) player);

208 armorStand.setSmall(true);

209 armorStand.setGravity(false);

210 armorStand.setInvulnerable(true);

211 armorStand.setVisible(false);

212

213 return armorStand;

214 }

215}