so://phie

poppy

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

blob: 8647d47bf1a12c06a05de570d64156d5cd0651d3 (5643 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.entity.Entity;

12import org.bukkit.inventory.Inventory;

13import org.bukkit.inventory.ItemStack;

14import org.bukkit.plugin.Plugin;

15import org.bukkit.Location;

16import org.bukkit.GameMode;

17import org.bukkit.plugin.Plugin;

18

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

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

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

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

23

24

25public class Util

26{

27 public static boolean isEmpty(ItemStack item)

28 {

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

30 }

31

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

33 {

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

35 }

36

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

38 {

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

40 {

41 return false;

42 }

43

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

45 {

46 return false;

47 }

48

49 return true;

50 }

51

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

53 {

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

55 {

56 if(Util.isEmpty(item))

57 {

58 return false;

59 }

60

61 if(result.isSimilar(item))

62 {

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

64 {

65 return false;

66 }

67 }

68 }

69

70 return true;

71 }

72

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

74 {

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

76 {

77 ItemStack current = inventory.getItem(i);

78

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

80 {

81 amount -= current.getAmount();

82

83 if(amount <= 0)

84 {

85 return true;

86 }

87 }

88 }

89

90 return false;

91 }

92

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

94 {

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

96 }

97

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

99 {

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

101 {

102 int count = time;

103

104 @Override

105 public void run()

106 {

107 if(count == 0)

108 {

109 runnable.run();

110 return;

111 }

112

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

114

115 count--;

116 }

117 }, 0L, Constants.TICK);

118 }

119

120 public static Float convertFacingToYaw(BlockFace face)

121 {

122 switch(face)

123 {

124 case NORTH:

125 return 0F;

126 case EAST:

127 return 90F;

128 case SOUTH:

129 return 180F;

130 case WEST:

131 return -90F;

132 default:

133 break;

134 }

135

136 return null;

137 }

138

139 public static boolean isAir(Material material)

140 {

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

142 }

143

144 public static boolean isFarmable(Material material)

145 {

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

147 }

148

149 public static boolean isStem(Material material)

150 {

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

152 }

153

154 public static boolean hasStem(Block block)

155 {

156 Location blockLocation = block.getLocation();

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

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

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

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

161

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

163 }

164

165 public static Location calcBlockCenter(final Location location)

166 {

167 Location centered = location.clone();

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

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

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

171

172 return centered;

173 }

174

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

176 {

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

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

179

180 if(fee < Constants.MIN_FEE)

181 {

182 fee = Constants.MIN_FEE;

183 }

184

185 if(fee > Constants.MAX_FEE)

186 {

187 fee = Constants.MAX_FEE;

188 }

189

190 return fee;

191 }

192

193 public static boolean travelTo(final Plugin plugin, final Player player, final Location location)

194 {

195 final int fee = Util.calcTravelFee(player.getLocation(), location);

196 final String unit = fee == 1 ? "emerald" : "emeralds";

197

198 if(!payFee(player, Material.EMERALD, fee) && player.getGameMode() == GameMode.SURVIVAL)

199 {

200 player.sendMessage(ChatColor.RED + "Not enough emeralds to teleport! Travel fee: " + fee + " " + unit);

201 return false;

202 }

203

204 if(player.getVehicle() != null)

205 {

206 Entity vehicle = player.getVehicle();

207 vehicle.eject();

208 player.teleport(location);

209

210 Bukkit.getScheduler().runTaskLater(plugin, () -> {

211 vehicle.teleport(location);

212 }, 3);

213

214 Bukkit.getScheduler().runTaskLater(plugin, () -> {

215 vehicle.addPassenger(player);

216 }, 6);

217 }

218 else

219 {

220 player.teleport(location);

221 }

222

223 return true;

224 }

225

226 public static boolean payFee(final Player player, final Material unit, final int amount)

227 {

228 final ItemStack item = player.getInventory().getItemInMainHand();

229

230 if(item.getType() != unit || item.getAmount() < amount)

231 {

232 return false;

233 }

234

235 item.setAmount(item.getAmount() - amount);

236

237 return true;

238 }

239}