so://phie

poppy

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

blob: d5543ce332db6cb577ffa3a6519b504b0a4f0d90 (4195 bytes)[doc][blame][raw]

1package de.typable.minecrafthub.event;

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.BlockFace;

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

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

13import org.bukkit.entity.ArmorStand;

14import org.bukkit.entity.Entity;

15import org.bukkit.entity.EntityType;

16import org.bukkit.entity.Player;

17import org.bukkit.event.EventHandler;

18import org.bukkit.event.Listener;

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

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

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

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

23

24

25public class ChairListener implements Listener

26{

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

28 {

29 Material.OAK_STAIRS,

30 Material.SPRUCE_STAIRS,

31 Material.BIRCH_STAIRS,

32 Material.JUNGLE_STAIRS,

33 Material.ACACIA_STAIRS,

34 Material.DARK_OAK_STAIRS,

35 Material.ACACIA_STAIRS,

36 Material.CRIMSON_STAIRS,

37 Material.WARPED_STAIRS

38 };

39

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

41

42 @SuppressWarnings("deprecation")

43 @EventHandler

44 public void onPlayerInteract(PlayerInteractEvent event)

45 {

46 Player player = event.getPlayer();

47

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

49 {

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

51 {

52 Block block = event.getClickedBlock();

53

54 if(isChair(block.getType()))

55 {

56 if(block.getLocation().add(0.5, 0.5, 0.5).distance(player.getLocation()) > 2)

57 {

58 return;

59 }

60

61 if(blockMap.containsKey(block))

62 {

63 return;

64 }

65

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

67

68 if(upperBlock != null && upperBlock.getType() == Material.AIR)

69 {

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

71

72 if(isCompatible(stairs))

73 {

74 Float yaw = convertFacingToYaw(stairs.getFacing());

75

76 if(yaw == null)

77 {

78 return;

79 }

80

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

82 location.setYaw(yaw);

83

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

85 armorStand.setPassenger((Entity) event.getPlayer());

86 armorStand.setSmall(true);

87 armorStand.setGravity(false);

88 armorStand.setInvulnerable(true);

89 armorStand.setVisible(false);

90

91 event.setCancelled(true);

92

93 blockMap.put(block, armorStand);

94 }

95 }

96 }

97 }

98 }

99 }

100

101 @EventHandler

102 public void onEntityDismount(EntityDismountEvent event)

103 {

104 if(event.getEntity() instanceof Player)

105 {

106 Entity entity = event.getDismounted();

107

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

109 {

110 if(blockMap.containsValue(entity))

111 {

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

113 {

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

115 {

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

117

118 break;

119 }

120 }

121 }

122

123 entity.remove();

124 }

125 }

126 }

127

128 @EventHandler

129 public void onBlockBreak(BlockBreakEvent event)

130 {

131 Block block = event.getBlock();

132

133 if(blockMap.containsKey(block))

134 {

135 ArmorStand armorStand = blockMap.get(block);

136 armorStand.eject();

137

138 blockMap.remove(block);

139 }

140 }

141

142 private boolean isChair(Material material)

143 {

144 for(Material chair : CHAIR_TYPE)

145 {

146 if(material == chair)

147 {

148 return true;

149 }

150 }

151

152 return false;

153 }

154

155 private boolean isCompatible(Stairs stairs)

156 {

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

158 {

159 return false;

160 }

161

162 if(stairs.isWaterlogged())

163 {

164 return false;

165 }

166

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

168 {

169 return false;

170 }

171

172 return true;

173 }

174

175 private Float convertFacingToYaw(BlockFace face)

176 {

177 switch(face)

178 {

179 case NORTH:

180 return 0F;

181 case EAST:

182 return 90F;

183 case SOUTH:

184 return 180F;

185 case WEST:

186 return -90F;

187 default:

188 break;

189 }

190

191 return null;

192 }

193

194 public void onDisable()

195 {

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

197 {

198 armorStand.eject();

199 armorStand.remove();

200 }

201 }

202}