so://phie

poppy

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

blob: 3b8fa9923caca6c4fecda195d94bd607a27f4ed5 (2535 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.type.Stairs;

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

8import org.bukkit.entity.Arrow;

9import org.bukkit.entity.Entity;

10import org.bukkit.entity.Player;

11import org.bukkit.event.EventHandler;

12import org.bukkit.event.Listener;

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

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

15import org.bukkit.util.Vector;

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

17

18

19public class ChairListener implements Listener

20{

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

22 Material.OAK_STAIRS,

23 Material.SPRUCE_STAIRS,

24 Material.BIRCH_STAIRS,

25 Material.JUNGLE_STAIRS,

26 Material.ACACIA_STAIRS,

27 Material.DARK_OAK_STAIRS,

28 Material.ACACIA_STAIRS,

29 Material.CRIMSON_STAIRS,

30 Material.WARPED_STAIRS

31 };

32

33 @SuppressWarnings("deprecation")

34 @EventHandler

35 public void onPlayerInteract(PlayerInteractEvent event)

36 {

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

38 {

39 if(event.getClickedBlock() != null)

40 {

41 Block block = event.getClickedBlock();

42

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

44 {

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

46

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

48 {

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

50

51 if(isCompatible(stairs))

52 {

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

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

55 arrow.setGravity(false);

56 arrow.setInvulnerable(true);

57 arrow.setPickupStatus(PickupStatus.DISALLOWED);

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

59

60 event.setCancelled(true);

61 }

62 }

63 }

64 }

65 }

66 }

67

68 @EventHandler

69 public void onEntityDismount(EntityDismountEvent event)

70 {

71 if(event.getEntity() instanceof Player)

72 {

73 Entity entity = event.getDismounted();

74

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

76 {

77 entity.remove();

78 }

79 }

80 }

81

82 private boolean isChair(Material material)

83 {

84 for(Material chair : CHAIR_TYPE)

85 {

86 if(material == chair)

87 {

88 return true;

89 }

90 }

91

92 return false;

93 }

94

95 private boolean isCompatible(Stairs stairs)

96 {

97 if(stairs.isWaterlogged())

98 {

99 return false;

100 }

101

102 if(stairs.getShape() != Stairs.Shape.STRAIGHT)

103 {

104 return false;

105 }

106

107 return true;

108 }

109}