Skip to content

Commit

Permalink
Fix NPC render on join
Browse files Browse the repository at this point in the history
  • Loading branch information
dandan2611 committed Jul 8, 2024
1 parent a7c1b75 commit e2d1fce
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions api/src/main/java/fr/codinbox/npclib/api/npc/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ default void renderFor(@NotNull UUID uuid) {
if (player.getWorld() != this.getWorld())
return; // Player is not in the same world as the NPC
if (getRenderLogic().shouldBeRendered(this, player, null)) {
this.addViewer(uuid);
this.addViewer(uuid).render();
}
} else {
if (player == null) // Player is not online, set the rendered property to false
Expand Down Expand Up @@ -230,7 +230,7 @@ default void renderFor(@NotNull Player player) {
*
* @param uuid the viewer UUID
*/
void addViewer(@NotNull UUID uuid);
@NotNull NpcViewer addViewer(@NotNull UUID uuid);

/**
* Remove a viewer from the NPC.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.codinbox.npclib.api.npc.NpcConfig;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.util.Set;
Expand Down Expand Up @@ -87,4 +88,11 @@ public interface NpcHolder {
*/
@NotNull Set<@NotNull UUID> getRenderedPlayers(@NotNull Npc npc);

/**
* Get the plugin of the holder.
*
* @return the plugin
*/
@NotNull Plugin getPlugin();

}
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
implementation(project(":api"))
implementation("commons-io:commons-io:2.11.0")
compileOnly("io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT")
compileOnly("com.comphenix.protocol:ProtocolLib:5.0.0")
compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import fr.codinbox.npclib.core.impl.npc.viewer.NpcViewerImpl;
import fr.codinbox.npclib.core.impl.npc.viewer.render.WorldDistanceRenderLogic;
import org.bukkit.Location;
import org.checkerframework.checker.units.qual.N;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
Expand Down Expand Up @@ -135,8 +136,10 @@ private void update() {
}

@Override
public void addViewer(@NotNull UUID uuid) {
this.viewers.put(uuid, new NpcViewerImpl(this, uuid));
public @NotNull NpcViewer addViewer(@NotNull UUID uuid) {
final NpcViewer viewer = new NpcViewerImpl(this, uuid);
this.viewers.put(uuid, viewer);
return viewer;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,9 @@ public void updateVisibility(@NotNull Npc npc, @NotNull Player player) {
return npc.getRenderers();
}

@Override
public @NotNull Plugin getPlugin() {
return this.plugin;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.comphenix.protocol.wrappers.*;
import fr.codinbox.npclib.api.npc.Npc;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -51,8 +52,9 @@ public interface NpcPacket {

NpcPacket PLAYER_SPAWN = (protocolManager, player, npc) -> {
var location = npc.getLocation();
var packet = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
var packet = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY);
packet.getIntegers().write(0, npc.getEntityId());
packet.getEntityTypeModifier().write(0, EntityType.PLAYER);
packet.getUUIDs().write(0, npc.getUUID());
packet.getDoubles().write(0 ,location.getX());
packet.getDoubles().write(1, location.getY());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.codinbox.npclib.core.listener;

import fr.codinbox.npclib.api.npc.holder.NpcHolder;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

Expand All @@ -13,7 +15,7 @@ public PlayerJoinListener(NpcHolder holder) {
this.holder = holder;
}

@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
private void onJoin(PlayerJoinEvent event) {
var player = event.getPlayer();
var location = player.getLocation();
Expand Down

0 comments on commit e2d1fce

Please sign in to comment.