so://phie

poppy

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

blob: 3e1f26e1215e30d36219fd706bf0ee8feb74ad7e (4389 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;

14import org.bukkit.Location;

15

16import de.typable.minecrafthub.constant.Constants;

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

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

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

20

21

22public class Util

23{

24 public static boolean isEmpty(ItemStack item)

25 {

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

27 }

28

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

30 {

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

32 }

33

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

35 {

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

37 {

38 return false;

39 }

40

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

42 {

43 return false;

44 }

45

46 return true;

47 }

48

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

50 {

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

52 {

53 if(Util.isEmpty(item))

54 {

55 return false;

56 }

57

58 if(result.isSimilar(item))

59 {

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

61 {

62 return false;

63 }

64 }

65 }

66

67 return true;

68 }

69

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

71 {

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

73 {

74 ItemStack current = inventory.getItem(i);

75

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

77 {

78 amount -= current.getAmount();

79

80 if(amount <= 0)

81 {

82 return true;

83 }

84 }

85 }

86

87 return false;

88 }

89

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

91 {

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

93 }

94

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

96 {

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

98 {

99 int count = time;

100

101 @Override

102 public void run()

103 {

104 if(count == 0)

105 {

106 runnable.run();

107 return;

108 }

109

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

111

112 count--;

113 }

114 }, 0L, Constants.TICK);

115 }

116

117 public static Float convertFacingToYaw(BlockFace face)

118 {

119 switch(face)

120 {

121 case NORTH:

122 return 0F;

123 case EAST:

124 return 90F;

125 case SOUTH:

126 return 180F;

127 case WEST:

128 return -90F;

129 default:

130 break;

131 }

132

133 return null;

134 }

135

136 public static boolean isAir(Material material)

137 {

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

139 }

140

141 public static boolean isFarmable(Material material)

142 {

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

144 }

145

146 public static boolean isStem(Material material)

147 {

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

149 }

150

151 public static boolean hasStem(Block block)

152 {

153 Location blockLocation = block.getLocation();

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

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

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

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

158

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

160 }

161

162 public static Location calcBlockCenter(final Location location)

163 {

164 Location centered = location.clone();

165 centered.setX(Location.locToBlock(location.getX()) + 0.5);

166 centered.setY(Location.locToBlock(location.getY()));

167 centered.setZ(Location.locToBlock(location.getZ()) + 0.5);

168

169 return centered;

170 }

171

172 public static int calcTravelFee(final Location from, final Location to)

173 {

174 final Double distance = from.distance(to);

175 int fee = (int) Math.floor(distance / (Constants.BLOCKS_PER_CHUNK * Constants.CHUNKS_PER_EMERALD));

176

177 if(fee < Constants.MIN_FEE)

178 {

179 fee = Constants.MIN_FEE;

180 }

181

182 if(fee > Constants.MAX_FEE)

183 {

184 fee = Constants.MAX_FEE;

185 }

186

187 return fee;

188 }

189}