A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: 8647d47bf1a12c06a05de570d64156d5cd0651d3 (5643 bytes)[doc][blame][raw]
1
package de.typable.minecrafthub.util;3
import javax.swing.text.Position;5
import org.bukkit.Bukkit;6
import org.bukkit.Location;7
import org.bukkit.Material;8
import org.bukkit.block.Block;9
import org.bukkit.block.BlockFace;10
import org.bukkit.entity.Player;11
import org.bukkit.entity.Entity;12
import org.bukkit.inventory.Inventory;13
import org.bukkit.inventory.ItemStack;14
import org.bukkit.plugin.Plugin;15
import org.bukkit.Location;16
import org.bukkit.GameMode;17
import org.bukkit.plugin.Plugin;19
import de.typable.minecrafthub.constant.Constants;20
import net.md_5.bungee.api.ChatColor;21
import net.md_5.bungee.api.ChatMessageType;22
import net.md_5.bungee.api.chat.TextComponent;25
public class Util26
{27
public static boolean isEmpty(ItemStack item)28
{29
return item == null || item.getType() == Material.AIR;30
}32
public static boolean isType(ItemStack item, Material type)33
{34
return !isEmpty(item) && item.getType() == type;35
}37
public static boolean compare(ItemStack item, ItemStack item2)38
{39
if(item == null || item2 == null)40
{41
return false;42
}44
if(item.getType() != item2.getType())45
{46
return false;47
}49
return true;50
}52
public static boolean isInventoryFull(Inventory inventory, ItemStack result)53
{54
for(ItemStack item : inventory.getContents())55
{56
if(Util.isEmpty(item))57
{58
return false;59
}61
if(result.isSimilar(item))62
{63
if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())64
{65
return false;66
}67
}68
}70
return true;71
}73
public static boolean containsAtLeast(Inventory inventory, ItemStack item, int amount)74
{75
for(int i = 0; i < inventory.getSize(); i++)76
{77
ItemStack current = inventory.getItem(i);79
if(Util.compare(item, current))80
{81
amount -= current.getAmount();83
if(amount <= 0)84
{85
return true;86
}87
}88
}90
return false;91
}93
public static void sendActionMessage(Player player, String message)94
{95
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));96
}98
public static void sendCountdown(Plugin plugin, final String message, final int time, Runnable runnable)99
{100
Bukkit.getScheduler().runTaskTimer(plugin, new Runnable()101
{102
int count = time;104
@Override105
public void run()106
{107
if(count == 0)108
{109
runnable.run();110
return;111
}113
Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");115
count--;116
}117
}, 0L, Constants.TICK);118
}120
public static Float convertFacingToYaw(BlockFace face)121
{122
switch(face)123
{124
case NORTH:125
return 0F;126
case EAST:127
return 90F;128
case SOUTH:129
return 180F;130
case WEST:131
return -90F;132
default:133
break;134
}136
return null;137
}139
public static boolean isAir(Material material)140
{141
return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;142
}144
public static boolean isFarmable(Material material)145
{146
return material == Material.WHEAT || material == Material.CARROTS || material == Material.POTATOES || material == Material.BEETROOTS || material == Material.NETHER_WART || material == Material.COCOA;147
}149
public static boolean isStem(Material material)150
{151
return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;152
}154
public static boolean hasStem(Block block)155
{156
Location blockLocation = block.getLocation();157
Material blockNorth = blockLocation.clone().add(0, 0, -1).getBlock().getType();158
Material blockEast = blockLocation.clone().add(1, 0, 0).getBlock().getType();159
Material blockSouth = blockLocation.clone().add(0, 0, 1).getBlock().getType();160
Material blockWest = blockLocation.clone().add(-1, 0, 0).getBlock().getType();162
return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);163
}165
public static Location calcBlockCenter(final Location location)166
{167
Location centered = location.clone();168
centered.setX(Location.locToBlock(location.getX()) + 0.5);169
centered.setY(Location.locToBlock(location.getY()));170
centered.setZ(Location.locToBlock(location.getZ()) + 0.5);172
return centered;173
}175
public static int calcTravelFee(final Location from, final Location to)176
{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
final int fee = Util.calcTravelFee(player.getLocation(), location);196
final String unit = fee == 1 ? "emerald" : "emeralds";198
if(!payFee(player, Material.EMERALD, fee) && player.getGameMode() == GameMode.SURVIVAL)199
{200
player.sendMessage(ChatColor.RED + "Not enough emeralds to teleport! Travel fee: " + fee + " " + unit);201
return false;202
}204
if(player.getVehicle() != null)205
{206
Entity vehicle = player.getVehicle();207
vehicle.eject();208
player.teleport(location);210
Bukkit.getScheduler().runTaskLater(plugin, () -> {211
vehicle.teleport(location);212
}, 3);214
Bukkit.getScheduler().runTaskLater(plugin, () -> {215
vehicle.addPassenger(player);216
}, 6);217
}218
else219
{220
player.teleport(location);221
}223
return true;224
}226
public static boolean payFee(final Player player, final Material unit, final int amount)227
{228
final ItemStack item = player.getInventory().getItemInMainHand();230
if(item.getType() != unit || item.getAmount() < amount)231
{232
return false;233
}235
item.setAmount(item.getAmount() - amount);237
return true;238
}239
}