Created
January 17, 2022 13:24
-
-
Save negativems/ef8b9aa3b9c897989aaa1e37094206e6 to your computer and use it in GitHub Desktop.
Bukkit Utils class from Icarus project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.zargum.plugin.icarus.utils; | |
import com.google.common.base.Preconditions; | |
import com.google.common.base.Strings; | |
import com.google.common.collect.ImmutableMap; | |
import com.google.common.collect.ImmutableSet; | |
import org.bukkit.*; | |
import org.bukkit.block.Block; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.Player; | |
import org.bukkit.entity.Projectile; | |
import org.bukkit.entity.ThrownPotion; | |
import org.bukkit.event.entity.EntityDamageByEntityEvent; | |
import org.bukkit.event.entity.EntityDamageEvent; | |
import org.bukkit.potion.PotionEffect; | |
import org.bukkit.potion.PotionEffectType; | |
import org.bukkit.projectiles.ProjectileSource; | |
import java.util.List; | |
import java.util.UUID; | |
import java.util.stream.Collectors; | |
public class BukkitUtils { | |
private static final ImmutableMap<ChatColor, DyeColor> CHAT_DYE_COLOUR_MAP = ImmutableMap.<ChatColor, DyeColor>builder().put(ChatColor.AQUA, DyeColor.LIGHT_BLUE).put(ChatColor.BLACK, DyeColor.BLACK).put(ChatColor.BLUE, DyeColor.LIGHT_BLUE).put(ChatColor.DARK_AQUA, DyeColor.CYAN).put(ChatColor.DARK_BLUE, DyeColor.BLUE).put(ChatColor.DARK_GRAY, DyeColor.GRAY).put(ChatColor.DARK_GREEN, DyeColor.GREEN).put(ChatColor.DARK_PURPLE, DyeColor.PURPLE).put(ChatColor.DARK_RED, DyeColor.RED).put(ChatColor.GOLD, DyeColor.ORANGE).put(ChatColor.GRAY, DyeColor.SILVER).put(ChatColor.GREEN, DyeColor.LIME).put(ChatColor.LIGHT_PURPLE, DyeColor.MAGENTA).put(ChatColor.RED, DyeColor.RED).put(ChatColor.WHITE, DyeColor.WHITE).put(ChatColor.YELLOW, DyeColor.YELLOW).build(); | |
private static final ImmutableSet<PotionEffectType> DEBUFF_TYPES = ImmutableSet.<PotionEffectType>builder().add(PotionEffectType.BLINDNESS).add(PotionEffectType.CONFUSION).add(PotionEffectType.HARM).add(PotionEffectType.HUNGER).add(PotionEffectType.POISON).add(PotionEffectType.SATURATION).add(PotionEffectType.SLOW).add(PotionEffectType.SLOW_DIGGING).add(PotionEffectType.WEAKNESS).add(PotionEffectType.WITHER).build(); | |
public static final String STRAIGHT_LINE_TEMPLATE = ChatColor.STRIKETHROUGH + Strings.repeat("-", 256); | |
public static List<String> getCompletions(String[] arguments, List<String> input) { | |
return getCompletions(arguments, input, 80); | |
} | |
public static List<String> getCompletions(String[] arguments, List<String> input, int limit) { | |
Preconditions.checkNotNull(arguments); | |
Preconditions.checkArgument(arguments.length != 0); | |
String argument = arguments[arguments.length - 1]; | |
return input.stream().filter(string -> string.regionMatches(true, 0, argument, 0, argument.length())).limit(limit).collect(Collectors.toList()); | |
} | |
public static String getDisplayName(CommandSender sender) { | |
Preconditions.checkNotNull(sender, "Sender cannot be null"); | |
return (sender instanceof Player) ? ((Player) sender).getDisplayName() : sender.getName(); | |
} | |
public static DyeColor toDyeColor(ChatColor colour) { | |
return CHAT_DYE_COLOUR_MAP.get(colour); | |
} | |
public static Player getFinalAttacker(EntityDamageEvent ede, boolean ignoreSelf) { | |
Player attacker = null; | |
if (ede instanceof EntityDamageByEntityEvent) { | |
EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) ede; | |
Entity damager = event.getDamager(); | |
if (event.getDamager() instanceof Player) { | |
attacker = (Player) damager; | |
} else if (event.getDamager() instanceof Projectile) { | |
Projectile projectile = (Projectile) damager; | |
ProjectileSource shooter = projectile.getShooter(); | |
if (shooter instanceof Player) { | |
attacker = (Player) shooter; | |
} | |
} | |
if (attacker != null && ignoreSelf && event.getEntity().equals(attacker)) { | |
attacker = null; | |
} | |
} | |
return attacker; | |
} | |
public static Player getPlayer(String string) { | |
if (string == null) return null; | |
return JavaUtils.isUUID(string) ? Bukkit.getPlayer(UUID.fromString(string)) : Bukkit.getPlayer(string); | |
} | |
public static String niceTime(int seconds) { | |
int hours = seconds / 3600; | |
seconds -= hours * 3600; | |
int minutes = seconds / 60; | |
seconds -= minutes * 60; | |
return niceTime(hours, minutes, seconds); | |
} | |
public static String niceTime(int seconds, boolean showEmptyHours) { | |
int hours = seconds / 3600; | |
seconds -= hours * 3600; | |
int minutes = seconds / 60; | |
seconds -= minutes * 60; | |
return niceTime(hours, minutes, seconds, showEmptyHours); | |
} | |
public static String niceTime(int hours, int minutes, int seconds) { | |
return niceTime(hours, minutes, seconds, true); | |
} | |
public static String niceTime(int hours, int minutes, int seconds, boolean showEmptyHours) { | |
StringBuilder builder = new StringBuilder(); | |
// Skip hours | |
if (hours > 0) { | |
if (hours < 10) { | |
builder.append('0'); | |
} | |
builder.append(hours); | |
builder.append(':'); | |
} else if (showEmptyHours) { | |
builder.append("00:"); | |
} | |
if (minutes < 10 && hours != -1) { | |
builder.append('0'); | |
} | |
builder.append(minutes); | |
builder.append(':'); | |
if (seconds < 10) { | |
builder.append('0'); | |
} | |
builder.append(seconds); | |
return builder.toString(); | |
} | |
@Deprecated | |
public static OfflinePlayer getOfflinePlayer(String string) { | |
if (string == null) { | |
return null; | |
} | |
return JavaUtils.isUUID(string) ? Bukkit.getOfflinePlayer(UUID.fromString(string)) : Bukkit.getOfflinePlayer(string); | |
} | |
public static boolean isWithinX(Location location, Location other, double distance) { | |
return location.getWorld().equals(other.getWorld()) && Math.abs(other.getX() - location.getX()) <= distance && Math.abs(other.getZ() - location.getZ()) <= distance; | |
} | |
public static Location getHighestLocation(Location origin) { | |
return getHighestLocation(origin, null); | |
} | |
public static Location getHighestLocation(Location origin, Location def) { | |
Location cloned = Preconditions.checkNotNull(origin, "The location cannot be null").clone(); | |
World world = cloned.getWorld(); | |
int x = cloned.getBlockX(); | |
int y = world.getMaxHeight(); | |
int z = cloned.getBlockZ(); | |
while (y > origin.getBlockY()) { | |
Block block = world.getBlockAt(x, --y, z); | |
if (!block.isEmpty()) { | |
Location next = block.getLocation(); | |
next.setPitch(origin.getPitch()); | |
next.setYaw(origin.getYaw()); | |
return next; | |
} | |
} | |
return def; | |
} | |
public static boolean isDebuff(PotionEffectType type) { | |
return DEBUFF_TYPES.contains(type); | |
} | |
public static boolean isDebuff(PotionEffect effect) { | |
return isDebuff(effect.getType()); | |
} | |
public static boolean isDebuff(ThrownPotion potion) { | |
for (PotionEffect effect : potion.getEffects()) { | |
if (isDebuff(effect)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment