so://phie

poppy

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

blob: 4e8bbae8d4af37d2b54a9ba0dd8c51d7c6f12ba2 (3689 bytes)[doc][blame][raw]

1package de.typable.minecrafthub.util;

2

3import javax.swing.text.Position;

4

5import org.bukkit.Bukkit;

6import org.bukkit.Location;

7import org.bukkit.Material;

8import org.bukkit.block.Block;

9import org.bukkit.block.BlockFace;

10import org.bukkit.entity.Player;

11import org.bukkit.inventory.Inventory;

12import org.bukkit.inventory.ItemStack;

13import org.bukkit.plugin.Plugin;

14

15import de.typable.minecrafthub.constant.DefaultConstants;

16import net.md_5.bungee.api.ChatColor;

17import net.md_5.bungee.api.ChatMessageType;

18import net.md_5.bungee.api.chat.TextComponent;

19

20

21public class Util

22{

23 public static boolean isEmpty(ItemStack item)

24 {

25 return item == null || item.getType() == Material.AIR;

26 }

27

28 public static boolean isType(ItemStack item, Material type)

29 {

30 return !isEmpty(item) && item.getType() == type;

31 }

32

33 public static boolean compare(ItemStack item, ItemStack item2)

34 {

35 if(item == null || item2 == null)

36 {

37 return false;

38 }

39

40 if(item.getType() != item2.getType())

41 {

42 return false;

43 }

44

45 return true;

46 }

47

48 public static boolean isInventoryFull(Inventory inventory, ItemStack result)

49 {

50 for(ItemStack item : inventory.getContents())

51 {

52 if(Util.isEmpty(item))

53 {

54 return false;

55 }

56

57 if(result.isSimilar(item))

58 {

59 if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())

60 {

61 return false;

62 }

63 }

64 }

65

66 return true;

67 }

68

69 public static boolean containsAtLeast(Inventory inventory, ItemStack item, int amount)

70 {

71 for(int i = 0; i < inventory.getSize(); i++)

72 {

73 ItemStack current = inventory.getItem(i);

74

75 if(Util.compare(item, current))

76 {

77 amount -= current.getAmount();

78

79 if(amount <= 0)

80 {

81 return true;

82 }

83 }

84 }

85

86 return false;

87 }

88

89 public static void sendActionMessage(Player player, String message)

90 {

91 player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));

92 }

93

94 public static void sendCountdown(Plugin plugin, final String message, final int time, Runnable runnable)

95 {

96 Bukkit.getScheduler().runTaskTimer(plugin, new Runnable()

97 {

98 int count = time;

99

100 @Override

101 public void run()

102 {

103 if(count == 0)

104 {

105 runnable.run();

106 return;

107 }

108

109 Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");

110

111 count--;

112 }

113 }, 0L, DefaultConstants.TICK);

114 }

115

116 public static Float convertFacingToYaw(BlockFace face)

117 {

118 switch(face)

119 {

120 case NORTH:

121 return 0F;

122 case EAST:

123 return 90F;

124 case SOUTH:

125 return 180F;

126 case WEST:

127 return -90F;

128 default:

129 break;

130 }

131

132 return null;

133 }

134

135 public static boolean isAir(Material material)

136 {

137 return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;

138 }

139

140 public static boolean isFarmable(Material material)

141 {

142 return material == Material.WHEAT || material == Material.CARROTS || material == Material.POTATOES || material == Material.BEETROOTS || material == Material.NETHER_WART || material == Material.COCOA;

143 }

144

145 public static boolean isStem(Material material)

146 {

147 return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;

148 }

149

150 public static boolean hasStem(Block block)

151 {

152 Location blockLocation = block.getLocation();

153 Material blockNorth = blockLocation.clone().add(0, 0, -1).getBlock().getType();

154 Material blockEast = blockLocation.clone().add(1, 0, 0).getBlock().getType();

155 Material blockSouth = blockLocation.clone().add(0, 0, 1).getBlock().getType();

156 Material blockWest = blockLocation.clone().add(-1, 0, 0).getBlock().getType();

157

158 return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);

159 }

160}