so://phie

poppy

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

blob: 52dde979175a3def12316b0b05f5cc55dcc934e0 (3435 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 if(event.getAction() == Action.RIGHT_CLICK_BLOCK)

36 {

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

38 {

39 Block block = event.getClickedBlock();

40

41 if(isChair(block.getType()))

42 {

43 if(blockMap.containsKey(block))

44 {

45 return;

46 }

47

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

49

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

51 {

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

53

54 if(isCompatible(stairs))

55 {

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

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

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

59 arrow.setGravity(false);

60 arrow.setInvulnerable(true);

61 arrow.setPickupStatus(PickupStatus.DISALLOWED);

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

63

64 event.setCancelled(true);

65

66 blockMap.put(block, arrow);

67 }

68 }

69 }

70 }

71 }

72 }

73

74 @EventHandler

75 public void onEntityDismount(EntityDismountEvent event)

76 {

77 if(event.getEntity() instanceof Player)

78 {

79 Entity entity = event.getDismounted();

80

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

82 {

83 if(blockMap.containsValue(entity))

84 {

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

86 {

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

88 {

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

90

91 break;

92 }

93 }

94 }

95

96 entity.remove();

97 }

98 }

99 }

100

101 @EventHandler

102 public void onBlockBreak(BlockBreakEvent event)

103 {

104 Block block = event.getBlock();

105

106 if(blockMap.containsKey(block))

107 {

108 Arrow arrow = blockMap.get(block);

109 arrow.eject();

110

111 blockMap.remove(block);

112 }

113 }

114

115 private boolean isChair(Material material)

116 {

117 for(Material chair : CHAIR_TYPE)

118 {

119 if(material == chair)

120 {

121 return true;

122 }

123 }

124

125 return false;

126 }

127

128 private boolean isCompatible(Stairs stairs)

129 {

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

131 {

132 return false;

133 }

134

135 if(stairs.isWaterlogged())

136 {

137 return false;

138 }

139

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

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

142 {

143 return false;

144 }

145

146 return true;

147 }

148}