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

not done yet

This commit is contained in:
TheBusyBiscuit 2021-01-14 20:44:56 +01:00
parent 029bc2ae78
commit 84d2d99097
3 changed files with 152 additions and 66 deletions

View File

@ -1,18 +1,9 @@
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;
@ -20,7 +11,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.HologramProjector;
/**
*
* This {@link ItemAttribute} manages holograms.
*
* @author TheBusyBiscuit
*
@ -30,9 +21,11 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.HologramPr
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);
Location loc = b.getLocation().add(getHologramOffset());
SlimefunPlugin.getHologramsService().updateHologram(loc, hologram -> {
hologram.setCustomName(ChatColors.color(text));
hologram.setCustomNameVisible(true);
});
}
@Nonnull
@ -40,57 +33,4 @@ public interface HologramOwner extends ItemAttribute {
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

@ -0,0 +1,138 @@
package io.github.thebusybiscuit.slimefun4.core.services;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
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.plugin.Plugin;
import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner;
/**
* This service is responsible for handling holograms.
* This includes error management when something goes wrong.
*
* @author TheBusyBiscuit
*
* @see HologramOwner
*/
public class HologramsService {
/**
* The radius in which we scan for holograms.
*/
private static final double RADIUS = 0.45;
/**
* The {@link NamespacedKey} used to store data on a hologram
*/
private final NamespacedKey persistentDataKey;
/**
* Our cache to save {@link Entity} lookups
*/
private final Map<BlockPosition, UUID> entityCache = new HashMap<>();
public HologramsService(@Nonnull Plugin plugin) {
// Null-Validation is performed in the NamespacedKey constructor
persistentDataKey = new NamespacedKey(plugin, "hologram_id");
}
@Nullable
public ArmorStand getHologram(@Nonnull Location loc, boolean createIfNoneExists) {
Validate.notNull(loc, "Location cannot be null");
BlockPosition position = new BlockPosition(loc);
UUID uuid = entityCache.get(position);
if (uuid != null) {
Entity entity = Bukkit.getEntity(uuid);
if (entity instanceof ArmorStand) {
return (ArmorStand) entity;
}
}
Collection<Entity> holograms = loc.getWorld().getNearbyEntities(loc, RADIUS, RADIUS, RADIUS, this::isHologram);
for (Entity n : holograms) {
if (n instanceof ArmorStand) {
PersistentDataContainer container = n.getPersistentDataContainer();
if (container.has(persistentDataKey, PersistentDataType.LONG)) {
// Check if it is ours or a different one.
if (container.get(persistentDataKey, PersistentDataType.LONG).equals(position.getPosition())) {
return (ArmorStand) n;
}
} else {
// Set a persistent tag to re-identify the correct hologram later
container.set(persistentDataKey, PersistentDataType.LONG, position.getPosition());
return (ArmorStand) n;
}
}
}
if (createIfNoneExists) {
// Spawn a new ArmorStand
ArmorStand hologram = (ArmorStand) loc.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
// Set a persistent tag to re-identify the correct hologram later
PersistentDataContainer container = hologram.getPersistentDataContainer();
container.set(persistentDataKey, PersistentDataType.LONG, position.getPosition());
hologram.setSilent(true);
hologram.setMarker(true);
hologram.setAI(false);
hologram.setGravity(false);
return hologram;
} else {
return null;
}
}
public boolean removeHologram(@Nonnull Location loc) {
Validate.notNull(loc, "Location cannot be null");
ArmorStand hologram = getHologram(loc, false);
if (hologram != null) {
hologram.remove();
return true;
} else {
return false;
}
}
private boolean isHologram(@Nonnull Entity n) {
if (n instanceof ArmorStand) {
ArmorStand armorstand = (ArmorStand) n;
return armorstand.isValid() && armorstand.isSilent() && armorstand.isMarker() && !armorstand.hasAI() && !armorstand.hasGravity();
} else {
return false;
}
}
public void updateHologram(@Nonnull Location loc, @Nonnull Consumer<ArmorStand> consumer) {
Validate.notNull(loc, "Location cannot be null");
Validate.notNull(consumer, "Callbacks must not be null");
consumer.accept(getHologram(loc, true));
}
}

View File

@ -43,6 +43,7 @@ import io.github.thebusybiscuit.slimefun4.core.services.BackupService;
import io.github.thebusybiscuit.slimefun4.core.services.BlockDataService;
import io.github.thebusybiscuit.slimefun4.core.services.CustomItemDataService;
import io.github.thebusybiscuit.slimefun4.core.services.CustomTextureService;
import io.github.thebusybiscuit.slimefun4.core.services.HologramsService;
import io.github.thebusybiscuit.slimefun4.core.services.LocalizationService;
import io.github.thebusybiscuit.slimefun4.core.services.MetricsService;
import io.github.thebusybiscuit.slimefun4.core.services.MinecraftRecipeService;
@ -149,6 +150,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
private final PermissionsService permissionsService = new PermissionsService(this);
private final PerWorldSettingsService worldSettingsService = new PerWorldSettingsService(this);
private final MinecraftRecipeService recipeService = new MinecraftRecipeService(this);
private final HologramsService hologramsService = new HologramsService(this);
private final IntegrationsManager integrations = new IntegrationsManager(this);
private final SlimefunProfiler profiler = new SlimefunProfiler();
@ -835,6 +837,12 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
return instance.worldSettingsService;
}
@Nonnull
public static HologramsService getHologramsService() {
validateInstance();
return instance.hologramsService;
}
/**
* This returns our instance of {@link IntegrationsManager}.
* This is responsible for managing any integrations with third party {@link Plugin plugins}.