A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: 0c196a75f82ba08ed52a30584276eb6868e24379 (5563 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.Location;14
import org.bukkit.GameMode;15
import org.bukkit.plugin.Plugin;17
import net.md_5.bungee.api.ChatColor;18
import net.md_5.bungee.api.ChatMessageType;19
import net.md_5.bungee.api.chat.TextComponent;21
import poppy.Constants;24
public class Utils25
{26
public static boolean isEmpty(ItemStack item)27
{28
return item == null || item.getType() == Material.AIR;29
}31
public static boolean isType(ItemStack item, Material type)32
{33
return !isEmpty(item) && item.getType() == type;34
}36
public static boolean compare(ItemStack item, ItemStack item2)37
{38
if(item == null || item2 == null)39
{40
return false;41
}43
if(item.getType() != item2.getType())44
{45
return false;46
}48
return true;49
}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
}60
if(result.isSimilar(item))61
{62
if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())63
{64
return false;65
}66
}67
}69
return true;70
}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);78
if(Utils.compare(item, current))79
{80
amount -= current.getAmount();82
if(amount <= 0)83
{84
return true;85
}86
}87
}89
return false;90
}92
public static void sendActionMessage(Player player, String message)93
{94
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));95
}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;103
@Override104
public void run()105
{106
if(count == 0)107
{108
runnable.run();109
return;110
}112
Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");114
count--;115
}116
}, 0L, Constants.TICK);117
}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
}135
return null;136
}138
public static boolean isAir(Material material)139
{140
return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;141
}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
}148
public static boolean isStem(Material material)149
{150
return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;151
}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();161
return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);162
}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);171
return centered;172
}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));179
if(fee < Constants.MIN_FEE)180
{181
fee = Constants.MIN_FEE;182
}184
if(fee > Constants.MAX_FEE)185
{186
fee = Constants.MAX_FEE;187
}189
return fee;190
}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";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
}203
if(player.getVehicle() != null)204
{205
Entity vehicle = player.getVehicle();206
vehicle.eject();207
player.teleport(location);209
Bukkit.getScheduler().runTaskLater(plugin, () -> {210
vehicle.teleport(location);211
}, 3);213
Bukkit.getScheduler().runTaskLater(plugin, () -> {214
vehicle.addPassenger(player);215
}, 6);216
}217
else218
{219
player.teleport(location);220
}222
return true;223
}225
public static boolean payFee(final Player player, final Material unit, final int amount)226
{227
final ItemStack item = player.getInventory().getItemInMainHand();229
if(item.getType() != unit || item.getAmount() < amount)230
{231
return false;232
}234
item.setAmount(item.getAmount() - amount);236
return true;237
}238
}