so://phie

poppy

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

blob: 48ab1c7bc7f7077d2189473be84631b07a0a1d0b (3623 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 arrow.setTicksLived(0);

71

72 event.setCancelled(true);

73

74 blockMap.put(block, arrow);

75 }

76 }

77 }

78 }

79 }

80 }

81

82 @EventHandler

83 public void onEntityDismount(EntityDismountEvent event)

84 {

85 if(event.getEntity() instanceof Player)

86 {

87 Entity entity = event.getDismounted();

88

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

90 {

91 if(blockMap.containsValue(entity))

92 {

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

94 {

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

96 {

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

98

99 break;

100 }

101 }

102 }

103

104 entity.remove();

105 }

106 }

107 }

108

109 @EventHandler

110 public void onBlockBreak(BlockBreakEvent event)

111 {

112 Block block = event.getBlock();

113

114 if(blockMap.containsKey(block))

115 {

116 Arrow arrow = blockMap.get(block);

117 arrow.eject();

118

119 blockMap.remove(block);

120 }

121 }

122

123 private boolean isChair(Material material)

124 {

125 for(Material chair : CHAIR_TYPE)

126 {

127 if(material == chair)

128 {

129 return true;

130 }

131 }

132

133 return false;

134 }

135

136 private boolean isCompatible(Stairs stairs)

137 {

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

139 {

140 return false;

141 }

142

143 if(stairs.isWaterlogged())

144 {

145 return false;

146 }

147

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

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

150 {

151 return false;

152 }

153

154 return true;

155 }

156}