Skip to content

Commit

Permalink
🔥 Started to implement API design
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Sep 11, 2024
1 parent 2e07f7a commit 290f137
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/mattmx/nametags/EventsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public void onChangeWorld(@NotNull PlayerChangedWorldEvent event) {

Location newLocation = SpigotConversionUtil.fromBukkitLocation(event.getPlayer().getLocation());

newLocation.setPitch(0f);
newLocation.setYaw(0f);

nameTagEntity.getPassenger().setLocation(newLocation);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/mattmx/nametags/NameTags.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.mattmx.nametags;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.util.Vector3f;
import com.mattmx.nametags.entity.NameTagEntityManager;
import me.tofaa.entitylib.APIConfig;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NameTags extends JavaPlugin {
private static final int TRANSPARENT = Color.fromARGB(0).asARGB();
private static @Nullable NameTags instance;

private NameTagEntityManager entityManager;
Expand All @@ -21,6 +26,18 @@ public class NameTags extends JavaPlugin {
public void onEnable() {
instance = this;
entityManager = new NameTagEntityManager();
saveDefaultConfig();

entityManager.setDefaultProvider((entity, meta) -> {
meta.setText(LegacyComponentSerializer.legacyAmpersand()
.deserialize(String.format("&f%s %s &#35A7FF0ms\n&#F3FFBDSome sub text", entity.getName(), "▪"))
);
meta.setBillboardConstraints(AbstractDisplayMeta.BillboardConstraints.VERTICAL);
meta.setTranslation(new Vector3f(0f, 0.2f, 0f));
meta.setBackgroundColor(TRANSPARENT);
meta.setShadow(true);
meta.setViewRange(50f);
});

SpigotEntityLibPlatform platform = new SpigotEntityLibPlatform(this);
APIConfig settings = new APIConfig(PacketEvents.getAPI())
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mattmx/nametags/OutgoingPacketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

public class OutgoingPacketListener extends PacketListenerAbstract {

private NameTags plugin;
private final @NotNull NameTags plugin;

public OutgoingPacketListener(NameTags plugin) {
public OutgoingPacketListener(@NotNull NameTags plugin) {
this.plugin = plugin;
}

Expand Down
42 changes: 27 additions & 15 deletions src/main/java/com/mattmx/nametags/entity/NameTagEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,54 @@

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.util.Vector3f;
import com.github.retrooper.packetevents.protocol.world.Location;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSetPassengers;
import com.mattmx.nametags.NameTags;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Color;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class NameTagEntity {
private final @NotNull Entity bukkitEntity;
private final @NotNull WrapperEntity passenger;

public NameTagEntity(@NotNull Entity entity) {
public NameTagEntity(@NotNull Entity entity, @NotNull BiConsumer<Entity, TextDisplayMeta> defaults) {
this.bukkitEntity = entity;
this.passenger = new WrapperEntity(EntityTypes.TEXT_DISPLAY);

applyDefaultMeta();
this.passenger.consumeEntityMeta(TextDisplayMeta.class, (meta) -> defaults.accept(entity, meta));

initialize();
}

public void applyDefaultMeta() {
this.passenger.consumeEntityMeta(TextDisplayMeta.class, (meta) -> {
meta.setText(bukkitEntity.name());
meta.setTranslation(new Vector3f(0f, 0.25f, 0f));
meta.setBackgroundColor(Color.RED.setAlpha(50).asARGB());
meta.setBillboardConstraints(AbstractDisplayMeta.BillboardConstraints.CENTER);
});
public void initialize() {
Location location = SpigotConversionUtil.fromBukkitLocation(this.bukkitEntity.getLocation());

this.passenger.spawn(SpigotConversionUtil.fromBukkitLocation(this.bukkitEntity.getLocation()));
location.setPitch(0f);
location.setYaw(0f);

this.passenger.spawn(location);

// TODO: Send packet to player if enabled in config
if (NameTags.getInstance().getConfig().getBoolean("show-self", false)) {

if (this.bukkitEntity instanceof Player self) {
this.passenger.addViewer(self.getUniqueId());
sendPassengerPacket(self);
}

}
}

public void modify(Consumer<TextDisplayMeta> consumer) {
this.passenger.consumeEntityMeta(TextDisplayMeta.class, consumer);
}

public void sendPassengerPacket(Player target) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/mattmx/nametags/entity/NameTagEntityManager.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package com.mattmx.nametags.entity;

import com.github.retrooper.packetevents.util.Vector3f;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

public class NameTagEntityManager {
private final ConcurrentHashMap<UUID, NameTagEntity> entityMap = new ConcurrentHashMap<>();
private final @NotNull ConcurrentHashMap<UUID, NameTagEntity> entityMap = new ConcurrentHashMap<>();
private @NotNull BiConsumer<Entity, TextDisplayMeta> defaultProvider = (entity, meta) -> {
// Default minecraft name-tag appearance
meta.setText(entity.name());
meta.setTranslation(new Vector3f(0f, 0.2f, 0f));
meta.setBillboardConstraints(AbstractDisplayMeta.BillboardConstraints.CENTER);
meta.setViewRange(50f);
};

public @NotNull NameTagEntity getOrCreateNameTagEntity(@NotNull Entity entity) {
return entityMap.computeIfAbsent(entity.getUniqueId(), (k) -> new NameTagEntity(entity));
return entityMap.computeIfAbsent(entity.getUniqueId(), (k) -> new NameTagEntity(entity, defaultProvider));
}

public @Nullable NameTagEntity removeEntity(@NotNull Entity entity) {
Expand All @@ -33,4 +44,8 @@ public class NameTagEntityManager {
.findFirst()
.orElse(null);
}

public void setDefaultProvider(@NotNull BiConsumer<Entity, TextDisplayMeta> consumer) {
this.defaultProvider = consumer;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Should the player see their own tag?
show-self: true

shift:
# Should we modify the opacity of the tag's background when the player shifts?
enabled: true
opacity: 80

0 comments on commit 290f137

Please sign in to comment.