so://phie

poppy

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

blob: 0c196a75f82ba08ed52a30584276eb6868e24379 (5563 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.Location;

14import org.bukkit.GameMode;

15import org.bukkit.plugin.Plugin;

16

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

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

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

20

21import poppy.Constants;

22

23

24public class Utils

25{

26 public static boolean isEmpty(ItemStack item)

27 {

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

29 }

30

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

32 {

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

34 }

35

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

37 {

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

39 {

40 return false;

41 }

42

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

44 {

45 return false;

46 }

47

48 return true;

49 }

50

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

52 {

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

54 {

55 if(Utils.isEmpty(item))

56 {

57 return false;

58 }

59

60 if(result.isSimilar(item))

61 {

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

63 {

64 return false;

65 }

66 }

67 }

68

69 return true;

70 }

71

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

73 {

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

75 {

76 ItemStack current = inventory.getItem(i);

77

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

79 {

80 amount -= current.getAmount();

81

82 if(amount <= 0)

83 {

84 return true;

85 }

86 }

87 }

88

89 return false;

90 }

91

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

93 {

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

95 }

96

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

98 {

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

100 {

101 int count = time;

102

103 @Override

104 public void run()

105 {

106 if(count == 0)

107 {

108 runnable.run();

109 return;

110 }

111

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

113

114 count--;

115 }

116 }, 0L, Constants.TICK);

117 }

118

119 public static Float convertFacingToYaw(BlockFace face)

120 {

121 switch(face)

122 {

123 case NORTH:

124 return 0F;

125 case EAST:

126 return 90F;

127 case SOUTH:

128 return 180F;

129 case WEST:

130 return -90F;

131 default:

132 break;

133 }

134

135 return null;

136 }

137

138 public static boolean isAir(Material material)

139 {

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

141 }

142

143 public static boolean isFarmable(Material material)

144 {

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

146 }

147

148 public static boolean isStem(Material material)

149 {

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

151 }

152

153 public static boolean hasStem(Block block)

154 {

155 Location blockLocation = block.getLocation();

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

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

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

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

160

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

162 }

163

164 public static Location calcBlockCenter(final Location location)

165 {

166 Location centered = location.clone();

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

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

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

170

171 return centered;

172 }

173

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

175 {

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

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

178

179 if(fee < Constants.MIN_FEE)

180 {

181 fee = Constants.MIN_FEE;

182 }

183

184 if(fee > Constants.MAX_FEE)

185 {

186 fee = Constants.MAX_FEE;

187 }

188

189 return fee;

190 }

191

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

193 {

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

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

196

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

198 {

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

200 return false;

201 }

202

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

204 {

205 Entity vehicle = player.getVehicle();

206 vehicle.eject();

207 player.teleport(location);

208

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

210 vehicle.teleport(location);

211 }, 3);

212

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

214 vehicle.addPassenger(player);

215 }, 6);

216 }

217 else

218 {

219 player.teleport(location);

220 }

221

222 return true;

223 }

224

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

226 {

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

228

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

230 {

231 return false;

232 }

233

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

235

236 return true;

237 }

238}