A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: 9f9ecd9f1dba1538a19a85616cfe7ca086d625b6 (5648 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;14
import net.md_5.bungee.api.ChatColor;15
import net.md_5.bungee.api.ChatMessageType;16
import net.md_5.bungee.api.chat.TextComponent;19
public class Utils20
{21
public static boolean isEmpty(ItemStack item)22
{23
return item == null || item.getType() == Material.AIR;24
}26
public static boolean isType(ItemStack item, Material type)27
{28
return !isEmpty(item) && item.getType() == type;29
}31
public static boolean compare(ItemStack item, ItemStack item2)32
{33
if(item == null || item2 == null)34
{35
return false;36
}38
if(item.getType() != item2.getType())39
{40
return false;41
}43
return true;44
}46
public static boolean isInventoryFull(Inventory inventory, ItemStack result)47
{48
for(ItemStack item : inventory.getContents())49
{50
if(Utils.isEmpty(item))51
{52
return false;53
}55
if(result.isSimilar(item))56
{57
if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())58
{59
return false;60
}61
}62
}64
return true;65
}67
public static boolean containsAtLeast(Inventory inventory, ItemStack item, int amount)68
{69
for(int i = 0; i < inventory.getSize(); i++)70
{71
ItemStack current = inventory.getItem(i);73
if(Utils.compare(item, current))74
{75
amount -= current.getAmount();77
if(amount <= 0)78
{79
return true;80
}81
}82
}84
return false;85
}87
public static void sendActionMessage(Player player, String message)88
{89
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacy(message));90
}92
public static void sendCountdown(Plugin plugin, final String message, final int time, Runnable runnable)93
{94
Bukkit.getScheduler().runTaskTimer(plugin, new Runnable()95
{96
int count = time;98
@Override99
public void run()100
{101
if(count == 0)102
{103
runnable.run();104
return;105
}107
Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");109
count--;110
}111
}, 0L, Constants.TICK);112
}114
public static Float convertFacingToYaw(BlockFace face)115
{116
switch(face)117
{118
case NORTH:119
return 0F;120
case EAST:121
return 90F;122
case SOUTH:123
return 180F;124
case WEST:125
return -90F;126
default:127
break;128
}130
return null;131
}133
public static boolean isAir(Material material)134
{135
return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;136
}138
public static boolean isFarmable(Material material)139
{140
return material == Material.WHEAT || material == Material.CARROTS || material == Material.POTATOES || material == Material.BEETROOTS || material == Material.NETHER_WART || material == Material.COCOA;141
}143
public static boolean isStem(Material material)144
{145
return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;146
}148
public static boolean hasStem(Block block)149
{150
Location blockLocation = block.getLocation();151
Material blockNorth = blockLocation.clone().add(0, 0, -1).getBlock().getType();152
Material blockEast = blockLocation.clone().add(1, 0, 0).getBlock().getType();153
Material blockSouth = blockLocation.clone().add(0, 0, 1).getBlock().getType();154
Material blockWest = blockLocation.clone().add(-1, 0, 0).getBlock().getType();156
return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);157
}159
public static Location calcBlockCenter(final Location location)160
{161
Location centered = location.clone();162
centered.setX(Location.locToBlock(location.getX()) + 0.5);163
centered.setY(Location.locToBlock(location.getY()));164
centered.setZ(Location.locToBlock(location.getZ()) + 0.5);166
return centered;167
}169
public static int calcTravelFee(final Location from, final Location to)170
{171
if(!isLocationInSameWorld(from, to))172
{173
return 0;174
}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
if(!isLocationInSameWorld(player.getLocation(), location))195
{196
player.sendMessage(ChatColor.RED + "The warppoint cannot be located in this world!");197
return false;198
}200
if(player.getVehicle() != null)201
{202
Entity vehicle = player.getVehicle();203
vehicle.eject();204
player.teleport(location);206
Bukkit.getScheduler().runTaskLater(plugin, () -> {207
vehicle.teleport(location);208
}, 3);210
Bukkit.getScheduler().runTaskLater(plugin, () -> {211
vehicle.addPassenger(player);212
}, 6);213
}214
else215
{216
player.teleport(location);217
}219
return true;220
}222
public static boolean payFee(final Player player, final Material unit, final int amount)223
{224
final ItemStack item = player.getInventory().getItemInMainHand();226
if(item.getType() != unit || item.getAmount() < amount)227
{228
return false;229
}231
item.setAmount(item.getAmount() - amount);233
return true;234
}236
public static boolean isLocationInSameWorld(final Location from, final Location to)237
{238
return from.getWorld().getName().equals(to.getWorld().getName());239
}241
public static boolean randomlyReduceDurability(final int level)242
{243
final float chance = 100.0f / (level + 1);244
return Math.random() * 100 <= chance;245
}246
}