A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: f5ca029f90ef2b86ed66fee7b1d4051fd81427d0 (5877 bytes)[doc][blame][raw]
1
package poppy;3
import org.bukkit.Bukkit;4
import org.bukkit.Location;5
import org.bukkit.Material;6
import org.bukkit.block.Block;7
import org.bukkit.block.BlockFace;8
import org.bukkit.entity.Player;9
import org.bukkit.entity.Entity;10
import org.bukkit.inventory.Inventory;11
import org.bukkit.inventory.ItemStack;12
import org.bukkit.plugin.Plugin;13
import org.bukkit.GameMode;15
import net.md_5.bungee.api.ChatColor;16
import net.md_5.bungee.api.ChatMessageType;17
import net.md_5.bungee.api.chat.TextComponent;20
public class Utils21
{22
public static boolean isEmpty(ItemStack item)23
{24
return item == null || item.getType() == Material.AIR;25
}27
public static boolean isType(ItemStack item, Material type)28
{29
return !isEmpty(item) && item.getType() == type;30
}32
public static boolean compare(ItemStack item, ItemStack item2)33
{34
if(item == null || item2 == null)35
{36
return false;37
}39
if(item.getType() != item2.getType())40
{41
return false;42
}44
return true;45
}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
}56
if(result.isSimilar(item))57
{58
if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())59
{60
return false;61
}62
}63
}65
return true;66
}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);74
if(Utils.compare(item, current))75
{76
amount -= current.getAmount();78
if(amount <= 0)79
{80
return true;81
}82
}83
}85
return false;86
}88
public static void sendActionMessage(Player player, String message)89
{90
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));91
}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;99
@Override100
public void run()101
{102
if(count == 0)103
{104
runnable.run();105
return;106
}108
Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");110
count--;111
}112
}, 0L, Constants.TICK);113
}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
}131
return null;132
}134
public static boolean isAir(Material material)135
{136
return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;137
}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
}144
public static boolean isStem(Material material)145
{146
return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;147
}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();157
return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);158
}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);167
return centered;168
}170
public static int calcTravelFee(final Location from, final Location to)171
{172
if(!isLocationInSameWorld(from, to))173
{174
return 0;175
}177
final Double distance = from.distance(to);178
int fee = (int) Math.floor(distance / (Constants.BLOCKS_PER_CHUNK * Constants.CHUNKS_PER_EMERALD));180
if(fee < Constants.MIN_FEE)181
{182
fee = Constants.MIN_FEE;183
}185
if(fee > Constants.MAX_FEE)186
{187
fee = Constants.MAX_FEE;188
}190
return fee;191
}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
}201
final int fee = Utils.calcTravelFee(player.getLocation(), location);202
final String unit = fee == 1 ? "emerald" : "emeralds";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
}210
if(player.getVehicle() != null)211
{212
Entity vehicle = player.getVehicle();213
vehicle.eject();214
player.teleport(location);216
Bukkit.getScheduler().runTaskLater(plugin, () -> {217
vehicle.teleport(location);218
}, 3);220
Bukkit.getScheduler().runTaskLater(plugin, () -> {221
vehicle.addPassenger(player);222
}, 6);223
}224
else225
{226
player.teleport(location);227
}229
return true;230
}232
public static boolean payFee(final Player player, final Material unit, final int amount)233
{234
final ItemStack item = player.getInventory().getItemInMainHand();236
if(item.getType() != unit || item.getAmount() < amount)237
{238
return false;239
}241
item.setAmount(item.getAmount() - amount);243
return true;244
}246
public static boolean isLocationInSameWorld(final Location from, final Location to)247
{248
return from.getWorld().getName().equals(to.getWorld().getName());249
}250
}