Skip to content

Commit

Permalink
no type variable, more version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAHuman-xD committed Nov 2, 2024
1 parent f594e19 commit 7abd04e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.util.Objects;

public class ArmorStandHologram extends Hologram<ArmorStand> {
public class ArmorStandHologram extends Hologram {
private ArmorStandHologram(ArmorStand entity) {
super(entity.getUniqueId());
}
Expand All @@ -22,7 +22,7 @@ public void setText(String text) {
return;
}

ArmorStand entity = getEntity();
Entity entity = getEntity();
if (entity != null) {
entity.setCustomName(text);
entity.setCustomNameVisible(text != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author TheBusyBiscuit, JustAHuman
*/
abstract class Hologram<E extends Entity> {
abstract class Hologram {
private static final long EXPIRES_AFTER = TimeUnit.MINUTES.toMillis(10);

protected final UUID uniqueId;
Expand All @@ -35,7 +35,7 @@ protected Hologram(@Nonnull UUID uniqueId) {
}

public abstract void setText(@Nullable String text);
public abstract Class<E> getEntityType();
public abstract Class<? extends Entity> getEntityType();

/**
* @return Whether the associated {@link Entity} has despawned.
Expand Down Expand Up @@ -64,7 +64,7 @@ public boolean hasExpired() {
* @return The {@link Entity} or null.
*/
@Nullable
public E getEntity() {
public Entity getEntity() {
Entity entity = Bukkit.getEntity(uniqueId);
if (getEntityType().isInstance(entity) && entity.isValid()) {
this.lastAccess = System.currentTimeMillis();
Expand All @@ -76,7 +76,7 @@ public E getEntity() {
}

public void teleport(Location location) {
E getEntity = getEntity();
Entity getEntity = getEntity();
if (getEntity != null) {
getEntity.teleport(location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.bukkit.Server;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.TextDisplay;
import org.bukkit.entity.EntityType;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;

Expand All @@ -42,7 +42,7 @@ public class HologramsService {

private final Plugin plugin;
private final NamespacedKey key;
private final Map<BlockPosition, Hologram<? extends Entity>> cache = new HashMap<>();
private final Map<BlockPosition, Hologram> cache = new HashMap<>();
protected boolean started = false;

public HologramsService(@Nonnull Plugin plugin) {
Expand Down Expand Up @@ -98,12 +98,12 @@ private void purge() {
* @return The existing (or newly created) hologram
*/
@Nullable
private Hologram<?> getHologram(@Nonnull Location loc, boolean createIfNoneExists) {
private Hologram getHologram(@Nonnull Location loc, boolean createIfNoneExists) {
Validate.notNull(loc, "Location cannot be null");
Validate.notNull(loc.getWorld(), "The Location's World cannot be null");

BlockPosition position = new BlockPosition(loc);
Hologram<?> hologram = cache.get(position);
Hologram hologram = cache.get(position);

// Check if the Hologram was cached and still exists
if (hologram != null && !hologram.hasDespawned()) {
Expand Down Expand Up @@ -137,8 +137,8 @@ private boolean hasHologramData(Entity entity, BlockPosition position) {
}

/**
* This checks if a given {@link Entity} is an {@link TextDisplay} or {@link ArmorStand}
* and whether it has the correct attributes to be considered a {@link Hologram}.
* This checks if a given {@link Entity} is an is the right type of entity
* with the right properties to be a {@link Hologram}.
*
* @param entity
* The {@link Entity} to check
Expand All @@ -152,8 +152,8 @@ private boolean isHologram(@Nonnull Entity entity, BlockPosition position) {
&& armorStand.isSilent()
&& !armorStand.hasGravity()
&& hasHologramData(armorStand, position);
} else if (entity instanceof TextDisplay textDisplay) {
return hasHologramData(textDisplay, position);
} else if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_19_4) && entity.getType() == EntityType.TEXT_DISPLAY) {
return hasHologramData(entity, position);
}
return false;
}
Expand All @@ -169,8 +169,11 @@ private boolean isHologram(@Nonnull Entity entity, BlockPosition position) {
* @return The {@link Hologram}
*/
@ParametersAreNonnullByDefault
private @Nullable Hologram<?> getAsHologram(Entity entity, BlockPosition position) {
Hologram<?> hologram = TextDisplayHologram.of(entity, position);
private @Nullable Hologram getAsHologram(Entity entity, BlockPosition position) {
Hologram hologram = null;
if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_19_4)) {
hologram = TextDisplayHologram.of(entity, position);
}
if (hologram == null) {
hologram = ArmorStandHologram.of(entity, position);
}
Expand All @@ -188,7 +191,7 @@ private boolean isHologram(@Nonnull Entity entity, BlockPosition position) {
* @param consumer
* The callback to run
*/
private void updateHologram(@Nonnull Location loc, @Nonnull Consumer<Hologram<?>> consumer) {
private void updateHologram(@Nonnull Location loc, @Nonnull Consumer<Hologram> consumer) {
Validate.notNull(loc, "Location must not be null");
Validate.notNull(consumer, "Callbacks must not be null");
if (!Bukkit.isPrimaryThread()) {
Expand All @@ -197,7 +200,7 @@ private void updateHologram(@Nonnull Location loc, @Nonnull Consumer<Hologram<?>
}

try {
Hologram<?> hologram = getHologram(loc, true);
Hologram hologram = getHologram(loc, true);
if (hologram != null) {
consumer.accept(hologram);
}
Expand Down Expand Up @@ -253,7 +256,7 @@ public boolean removeHologram(@Nonnull Location location) {
}

try {
Hologram<?> hologram = getHologram(location, false);
Hologram hologram = getHologram(location, false);
if (hologram == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.annotation.Nullable;
import java.util.Objects;

public class TextDisplayHologram extends Hologram<TextDisplay> {
public class TextDisplayHologram extends Hologram {
private TextDisplayHologram(@Nonnull TextDisplay textDisplay) {
super(textDisplay.getUniqueId());
}
Expand All @@ -23,8 +23,8 @@ public void setText(@Nullable String text) {
return;
}

TextDisplay textDisplay = getEntity();
if (textDisplay != null) {
Entity entity = getEntity();
if (entity instanceof TextDisplay textDisplay) {
textDisplay.setText(text);
}
}
Expand Down

0 comments on commit 7abd04e

Please sign in to comment.