so://phie

poppy

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

blob: e620f7f1442679aa9a91a18b5d897884d296978d (5477 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 final Double distance = from.distance(to);

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

174

175 if(fee < Constants.MIN_FEE)

176 {

177 fee = Constants.MIN_FEE;

178 }

179

180 if(fee > Constants.MAX_FEE)

181 {

182 fee = Constants.MAX_FEE;

183 }

184

185 return fee;

186 }

187

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

189 {

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

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

192

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

194 {

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

196 return false;

197 }

198

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

200 {

201 Entity vehicle = player.getVehicle();

202 vehicle.eject();

203 player.teleport(location);

204

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

206 vehicle.teleport(location);

207 }, 3);

208

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

210 vehicle.addPassenger(player);

211 }, 6);

212 }

213 else

214 {

215 player.teleport(location);

216 }

217

218 return true;

219 }

220

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

222 {

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

224

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

226 {

227 return false;

228 }

229

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

231

232 return true;

233 }

234}