A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: 3e1f26e1215e30d36219fd706bf0ee8feb74ad7e (4389 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.inventory.Inventory;12
import org.bukkit.inventory.ItemStack;13
import org.bukkit.plugin.Plugin;14
import org.bukkit.Location;16
import de.typable.minecrafthub.constant.Constants;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;22
public class Util23
{24
public static boolean isEmpty(ItemStack item)25
{26
return item == null || item.getType() == Material.AIR;27
}29
public static boolean isType(ItemStack item, Material type)30
{31
return !isEmpty(item) && item.getType() == type;32
}34
public static boolean compare(ItemStack item, ItemStack item2)35
{36
if(item == null || item2 == null)37
{38
return false;39
}41
if(item.getType() != item2.getType())42
{43
return false;44
}46
return true;47
}49
public static boolean isInventoryFull(Inventory inventory, ItemStack result)50
{51
for(ItemStack item : inventory.getContents())52
{53
if(Util.isEmpty(item))54
{55
return false;56
}58
if(result.isSimilar(item))59
{60
if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())61
{62
return false;63
}64
}65
}67
return true;68
}70
public static boolean containsAtLeast(Inventory inventory, ItemStack item, int amount)71
{72
for(int i = 0; i < inventory.getSize(); i++)73
{74
ItemStack current = inventory.getItem(i);76
if(Util.compare(item, current))77
{78
amount -= current.getAmount();80
if(amount <= 0)81
{82
return true;83
}84
}85
}87
return false;88
}90
public static void sendActionMessage(Player player, String message)91
{92
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));93
}95
public static void sendCountdown(Plugin plugin, final String message, final int time, Runnable runnable)96
{97
Bukkit.getScheduler().runTaskTimer(plugin, new Runnable()98
{99
int count = time;101
@Override102
public void run()103
{104
if(count == 0)105
{106
runnable.run();107
return;108
}110
Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");112
count--;113
}114
}, 0L, Constants.TICK);115
}117
public static Float convertFacingToYaw(BlockFace face)118
{119
switch(face)120
{121
case NORTH:122
return 0F;123
case EAST:124
return 90F;125
case SOUTH:126
return 180F;127
case WEST:128
return -90F;129
default:130
break;131
}133
return null;134
}136
public static boolean isAir(Material material)137
{138
return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;139
}141
public static boolean isFarmable(Material material)142
{143
return material == Material.WHEAT || material == Material.CARROTS || material == Material.POTATOES || material == Material.BEETROOTS || material == Material.NETHER_WART || material == Material.COCOA;144
}146
public static boolean isStem(Material material)147
{148
return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;149
}151
public static boolean hasStem(Block block)152
{153
Location blockLocation = block.getLocation();154
Material blockNorth = blockLocation.clone().add(0, 0, -1).getBlock().getType();155
Material blockEast = blockLocation.clone().add(1, 0, 0).getBlock().getType();156
Material blockSouth = blockLocation.clone().add(0, 0, 1).getBlock().getType();157
Material blockWest = blockLocation.clone().add(-1, 0, 0).getBlock().getType();159
return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);160
}162
public static Location calcBlockCenter(final Location location)163
{164
Location centered = location.clone();165
centered.setX(Location.locToBlock(location.getX()) + 0.5);166
centered.setY(Location.locToBlock(location.getY()));167
centered.setZ(Location.locToBlock(location.getZ()) + 0.5);169
return centered;170
}172
public static int calcTravelFee(final Location from, final Location to)173
{174
final Double distance = from.distance(to);175
int fee = (int) Math.floor(distance / (Constants.BLOCKS_PER_CHUNK * Constants.CHUNKS_PER_EMERALD));177
if(fee < Constants.MIN_FEE)178
{179
fee = Constants.MIN_FEE;180
}182
if(fee > Constants.MAX_FEE)183
{184
fee = Constants.MAX_FEE;185
}187
return fee;188
}189
}