1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 19:55:48 +00:00
Slimefun4/src/me/mrCookieSlime/Slimefun/holograms/EnergyHologram.java

48 lines
1.7 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.holograms;
import org.bukkit.Bukkit;
2016-11-23 17:21:19 +00:00
import org.bukkit.ChatColor;
2016-04-14 16:24:03 +00:00
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
2017-06-14 10:35:25 +00:00
import me.mrCookieSlime.CSCoreLibPlugin.general.Math.DoubleHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.World.ArmorStandFactory;
2019-08-31 09:36:45 +00:00
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
2017-06-14 10:35:25 +00:00
2019-08-29 22:43:17 +00:00
public final class EnergyHologram {
private EnergyHologram() {}
2016-04-14 16:24:03 +00:00
public static void update(Block b, double supply, double demand) {
2016-11-23 15:51:57 +00:00
update(b, demand > supply ? ("&4&l- &c" + DoubleHandler.getFancyDouble(Math.abs(supply - demand)) + " &7J &e\u26A1"): ("&2&l+ &a" + DoubleHandler.getFancyDouble(supply - demand) + " &7J &e\u26A1"));
2016-04-14 16:24:03 +00:00
}
public static void update(final Block b, final String name) {
2019-08-31 09:36:45 +00:00
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
2019-08-29 22:43:17 +00:00
ArmorStand hologram = getArmorStand(b, true);
2019-03-30 12:32:13 +00:00
hologram.setCustomName(ChatColor.translateAlternateColorCodes('&', name));
2016-04-14 16:24:03 +00:00
});
2017-06-14 10:35:25 +00:00
}
2016-04-14 16:24:03 +00:00
public static void remove(final Block b) {
2019-08-31 09:36:45 +00:00
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
2019-08-29 22:43:17 +00:00
ArmorStand hologram = getArmorStand(b, false);
if (hologram != null) hologram.remove();
2016-04-14 16:24:03 +00:00
});
}
2019-09-25 12:17:06 +00:00
private static ArmorStand getArmorStand(Block b, boolean createIfNoneExists) {
2019-06-19 13:32:33 +00:00
Location l = new Location(b.getWorld(), b.getX() + 0.5, b.getY() + 0.7F, b.getZ() + 0.5);
2016-04-14 16:24:03 +00:00
2019-03-30 12:32:13 +00:00
for (Entity n : l.getChunk().getEntities()) {
2019-08-29 20:04:31 +00:00
if (n instanceof ArmorStand && n.getCustomName() != null && l.distanceSquared(n.getLocation()) < 0.4D) return (ArmorStand) n;
2016-04-14 16:24:03 +00:00
}
2019-09-25 12:17:06 +00:00
if (!createIfNoneExists) return null;
2019-08-29 23:11:33 +00:00
else return ArmorStandFactory.createHidden(l);
2016-04-14 16:24:03 +00:00
}
}