so://phie

poppy

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

blob: b589957f6cdb41c4d922c950f4d3c1df357bad71 (2731 bytes)[doc][blame][raw]

1package de.typable.minecrafthub.event;

2

3import org.bukkit.Location;

4import org.bukkit.Material;

5import org.bukkit.block.Block;

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

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

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

9import org.bukkit.entity.Arrow;

10import org.bukkit.entity.Entity;

11import org.bukkit.entity.Player;

12import org.bukkit.event.EventHandler;

13import org.bukkit.event.Listener;

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

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

16import org.bukkit.util.Vector;

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

18

19

20public class ChairListener implements Listener

21{

22 private static final Material[] CHAIR_TYPE = new Material[] {

23 Material.OAK_STAIRS,

24 Material.SPRUCE_STAIRS,

25 Material.BIRCH_STAIRS,

26 Material.JUNGLE_STAIRS,

27 Material.ACACIA_STAIRS,

28 Material.DARK_OAK_STAIRS,

29 Material.ACACIA_STAIRS,

30 Material.CRIMSON_STAIRS,

31 Material.WARPED_STAIRS

32 };

33

34 @SuppressWarnings("deprecation")

35 @EventHandler

36 public void onPlayerInteract(PlayerInteractEvent event)

37 {

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

39 {

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

41 {

42 Block block = event.getClickedBlock();

43

44 if(isChair(block.getType()))

45 {

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

47

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

49 {

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

51

52 if(isCompatible(stairs))

53 {

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

55 Arrow arrow = (Arrow) block.getWorld().spawnArrow(location, new Vector(), 0F, 0F);

56 arrow.setGravity(false);

57 arrow.setInvulnerable(true);

58 arrow.setPickupStatus(PickupStatus.DISALLOWED);

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

60

61 event.setCancelled(true);

62 }

63 }

64 }

65 }

66 }

67 }

68

69 @EventHandler

70 public void onEntityDismount(EntityDismountEvent event)

71 {

72 if(event.getEntity() instanceof Player)

73 {

74 Entity entity = event.getDismounted();

75

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

77 {

78 entity.remove();

79 }

80 }

81 }

82

83 private boolean isChair(Material material)

84 {

85 for(Material chair : CHAIR_TYPE)

86 {

87 if(material == chair)

88 {

89 return true;

90 }

91 }

92

93 return false;

94 }

95

96 private boolean isCompatible(Stairs stairs)

97 {

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

99 {

100 return false;

101 }

102

103 if(stairs.isWaterlogged())

104 {

105 return false;

106 }

107

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

109 {

110 return false;

111 }

112

113 return true;

114 }

115}