so://phie

poppy

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

blob: 0af10f77e21a2baf70a47fc6fc9d298511ea5f77 (3592 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.data.Bisected.Half;

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

12import org.bukkit.entity.AbstractArrow.PickupStatus;

13import org.bukkit.entity.Arrow;

14import org.bukkit.entity.Entity;

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

24

25public class ChairListener implements Listener

26{

27 private static final Material[] CHAIR_TYPE = new Material[] { Material.OAK_STAIRS, Material.SPRUCE_STAIRS, Material.BIRCH_STAIRS, Material.JUNGLE_STAIRS, Material.ACACIA_STAIRS, Material.DARK_OAK_STAIRS, Material.ACACIA_STAIRS, Material.CRIMSON_STAIRS, Material.WARPED_STAIRS };

28

29 private Map<Block, Arrow> blockMap = new HashMap<>();

30

31 @SuppressWarnings("deprecation")

32 @EventHandler

33 public void onPlayerInteract(PlayerInteractEvent event)

34 {

35 Player player = event.getPlayer();

36

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

38 {

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

40 {

41 Block block = event.getClickedBlock();

42

43 if(isChair(block.getType()))

44 {

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

46 {

47 return;

48 }

49

50 if(blockMap.containsKey(block))

51 {

52 return;

53 }

54

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

56

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

58 {

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

60

61 if(isCompatible(stairs))

62 {

63 Location location = block.getLocation().add(0.5, 0, 0.5);

64 Arrow arrow = (Arrow) block.getWorld()

65 .spawnArrow(location, new Vector(), 0F, 0F);

66 arrow.setGravity(false);

67 arrow.setInvulnerable(true);

68 arrow.setPickupStatus(PickupStatus.DISALLOWED);

69 arrow.setPassenger((Entity) event.getPlayer());

70

71 event.setCancelled(true);

72

73 blockMap.put(block, arrow);

74 }

75 }

76 }

77 }

78 }

79 }

80

81 @EventHandler

82 public void onEntityDismount(EntityDismountEvent event)

83 {

84 if(event.getEntity() instanceof Player)

85 {

86 Entity entity = event.getDismounted();

87

88 if(entity != null && entity instanceof Arrow)

89 {

90 if(blockMap.containsValue(entity))

91 {

92 for(Entry<Block, Arrow> entry : blockMap.entrySet())

93 {

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

95 {

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

97

98 break;

99 }

100 }

101 }

102

103 entity.remove();

104 }

105 }

106 }

107

108 @EventHandler

109 public void onBlockBreak(BlockBreakEvent event)

110 {

111 Block block = event.getBlock();

112

113 if(blockMap.containsKey(block))

114 {

115 Arrow arrow = blockMap.get(block);

116 arrow.eject();

117

118 blockMap.remove(block);

119 }

120 }

121

122 private boolean isChair(Material material)

123 {

124 for(Material chair : CHAIR_TYPE)

125 {

126 if(material == chair)

127 {

128 return true;

129 }

130 }

131

132 return false;

133 }

134

135 private boolean isCompatible(Stairs stairs)

136 {

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

138 {

139 return false;

140 }

141

142 if(stairs.isWaterlogged())

143 {

144 return false;

145 }

146

147 if(stairs.getShape() == Stairs.Shape.INNER_RIGHT || stairs

148 .getShape() == Stairs.Shape.INNER_LEFT)

149 {

150 return false;

151 }

152

153 return true;

154 }

155}