so://phie

poppy

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

blob: b1784eb94e0c44c203b024c45b93a03f1c7ad4f0 (6049 bytes)[doc][blame][raw]

1package poppy;

2

3import org.bukkit.Bukkit;

4import org.bukkit.Location;

5import org.bukkit.Material;

6import org.bukkit.block.Block;

7import org.bukkit.block.BlockFace;

8import org.bukkit.entity.Player;

9import org.bukkit.entity.Entity;

10import org.bukkit.inventory.Inventory;

11import org.bukkit.inventory.ItemStack;

12import org.bukkit.plugin.Plugin;

13import org.bukkit.GameMode;

14

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

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

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

18

19

20public class Utils

21{

22 public static boolean isEmpty(ItemStack item)

23 {

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

25 }

26

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

28 {

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

30 }

31

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

33 {

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

35 {

36 return false;

37 }

38

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

40 {

41 return false;

42 }

43

44 return true;

45 }

46

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

48 {

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

50 {

51 if(Utils.isEmpty(item))

52 {

53 return false;

54 }

55

56 if(result.isSimilar(item))

57 {

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

59 {

60 return false;

61 }

62 }

63 }

64

65 return true;

66 }

67

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

69 {

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

71 {

72 ItemStack current = inventory.getItem(i);

73

74 if(Utils.compare(item, current))

75 {

76 amount -= current.getAmount();

77

78 if(amount <= 0)

79 {

80 return true;

81 }

82 }

83 }

84

85 return false;

86 }

87

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

89 {

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

91 }

92

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

94 {

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

96 {

97 int count = time;

98

99 @Override

100 public void run()

101 {

102 if(count == 0)

103 {

104 runnable.run();

105 return;

106 }

107

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

109

110 count--;

111 }

112 }, 0L, Constants.TICK);

113 }

114

115 public static Float convertFacingToYaw(BlockFace face)

116 {

117 switch(face)

118 {

119 case NORTH:

120 return 0F;

121 case EAST:

122 return 90F;

123 case SOUTH:

124 return 180F;

125 case WEST:

126 return -90F;

127 default:

128 break;

129 }

130

131 return null;

132 }

133

134 public static boolean isAir(Material material)

135 {

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

137 }

138

139 public static boolean isFarmable(Material material)

140 {

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

142 }

143

144 public static boolean isStem(Material material)

145 {

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

147 }

148

149 public static boolean hasStem(Block block)

150 {

151 Location blockLocation = block.getLocation();

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

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

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

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

156

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

158 }

159

160 public static Location calcBlockCenter(final Location location)

161 {

162 Location centered = location.clone();

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

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

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

166

167 return centered;

168 }

169

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

171 {

172 if(!isLocationInSameWorld(from, to))

173 {

174 return 0;

175 }

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 if(!isLocationInSameWorld(player.getLocation(), location))

196 {

197 player.sendMessage(ChatColor.RED + "The warppoint cannot be located in this world!");

198 return false;

199 }

200

201 final int fee = Utils.calcTravelFee(player.getLocation(), location);

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

203

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

205 // {

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

207 // return false;

208 // }

209

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

211 {

212 Entity vehicle = player.getVehicle();

213 vehicle.eject();

214 player.teleport(location);

215

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

217 vehicle.teleport(location);

218 }, 3);

219

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

221 vehicle.addPassenger(player);

222 }, 6);

223 }

224 else

225 {

226 player.teleport(location);

227 }

228

229 return true;

230 }

231

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

233 {

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

235

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

237 {

238 return false;

239 }

240

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

242

243 return true;

244 }

245

246 public static boolean isLocationInSameWorld(final Location from, final Location to)

247 {

248 return from.getWorld().getName().equals(to.getWorld().getName());

249 }

250

251 public static boolean randomlyReduceDurability(final int level)

252 {

253 final float chance = 100.0f / (level + 1);

254 return Math.random() * 100 <= chance;

255 }

256}