1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Started to refactor holograms

This commit is contained in:
TheBusyBiscuit 2021-01-03 18:48:18 +01:00
parent f2dd3a0125
commit debf201846
2 changed files with 98 additions and 1 deletions

View File

@ -0,0 +1,96 @@
package io.github.thebusybiscuit.slimefun4.core.attributes;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.HologramProjector;
/**
*
*
* @author TheBusyBiscuit
*
* @see HologramProjector
*
*/
public interface HologramOwner extends ItemAttribute {
default void updateHologram(@Nonnull Block b, @Nonnull String text) {
ArmorStand hologram = getHologram(b, true);
hologram.setCustomName(ChatColors.color(text));
hologram.setCustomNameVisible(true);
}
@Nonnull
default Vector getHologramOffset() {
return new Vector(0.5, 0.75, 0.5);
}
@Nullable
default ArmorStand getHologram(@Nonnull Block b, boolean createIfNoneExists) {
Location loc = b.getLocation().add(getHologramOffset());
Collection<Entity> holograms = b.getWorld().getNearbyEntities(loc, 0.4, 0.4, 0.4, n -> n instanceof ArmorStand && isHologram(n));
for (Entity n : holograms) {
if (n instanceof ArmorStand) {
PersistentDataContainer container = n.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(SlimefunPlugin.instance(), "hologram_id");
String value = b.getX() + ";" + b.getY() + ";" + b.getZ();
if (container.has(key, PersistentDataType.STRING)) {
if (container.get(key, PersistentDataType.STRING).equals(value)) {
return (ArmorStand) n;
}
} else {
container.set(key, PersistentDataType.STRING, value);
return (ArmorStand) n;
}
}
}
if (!createIfNoneExists) {
return null;
} else {
ArmorStand hologram = (ArmorStand) b.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
hologram.setSilent(true);
hologram.setMarker(true);
hologram.setAI(false);
hologram.setGravity(false);
return hologram;
}
}
default boolean isHologram(@Nonnull Entity n) {
if (n instanceof ArmorStand) {
ArmorStand armorstand = (ArmorStand) n;
return armorstand.isValid() && armorstand.isSilent() && armorstand.isMarker() && !armorstand.hasGravity();
} else {
return false;
}
}
default void removeHologram(@Nonnull Block b) {
ArmorStand hologram = getHologram(b, false);
if (hologram != null) {
hologram.remove();
}
}
}

View File

@ -12,6 +12,7 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.math.DoubleHandler; import io.github.thebusybiscuit.cscorelib2.math.DoubleHandler;
import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
@ -25,7 +26,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
public class HologramProjector extends SlimefunItem { public class HologramProjector extends SlimefunItem implements HologramOwner {
private static final String OFFSET_PARAMETER = "offset"; private static final String OFFSET_PARAMETER = "offset";