Skip to content

Commit

Permalink
Fix player invisibility (note: they will appear on the tab list for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 1, 2014
1 parent 5f75a7c commit afba671
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
14 changes: 14 additions & 0 deletions src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_8_R1.EnumPlayerInfoAction;
import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerInfo;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -49,6 +52,7 @@
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.event.world.ChunkLoadEvent;
Expand Down Expand Up @@ -272,6 +276,16 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
Bukkit.getPluginManager().callEvent(rightClickEvent);
}

@EventHandler(ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
for (NPC npc : getAllNPCs()) {
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, ((CraftPlayer) npc
.getEntity()).getHandle()));
}
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ private void updatePackets(boolean navigating) {
break;
}
}
NMS.sendToOnline(getListPacket(getBukkitEntity(), true));
// NMS.sendToOnline(getListPacket(getBukkitEntity(), true));
if (otherOnline != null) {
NMS.sendToOnline(getListPacket(otherOnline, false));
// NMS.sendToOnline(getListPacket(otherOnline, false));
}
NMS.sendPacketsNearby(getBukkitEntity(), current, packets);
}
Expand Down
46 changes: 27 additions & 19 deletions src/main/java/net/citizensnpcs/npc/entity/HumanController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -83,8 +84,8 @@ public void run() {
npc.data().get("removefromplayerlist", removeFromPlayerList));
}
}, 1);
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, handle));
handle.getBukkitEntity().setSleepingIgnored(true);
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, handle));
return handle.getBukkitEntity();
}

Expand All @@ -93,6 +94,13 @@ public Player getBukkitEntity() {
return (Player) super.getBukkitEntity();
}

@Override
public void remove() {
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER,
((CraftPlayer) getBukkitEntity()).getHandle()));
super.remove();
}

private void updateSkin(final NPC npc, final WorldServer nmsWorld, GameProfile profile) {
String skinUUID = npc.data().get(NPC.PLAYER_SKIN_UUID_METADATA);
if (skinUUID == null) {
Expand Down Expand Up @@ -130,8 +138,8 @@ public SkinFetcher(Callable<String> uuid, MinecraftSessionService repo, NPC npc)
private GameProfile fillProfileProperties(YggdrasilAuthenticationService auth, GameProfile profile,
boolean requireSecure) throws Exception {
URL url = HttpAuthenticationService.constantURL(new StringBuilder()
.append("https://sessionserver.mojang.com/session/minecraft/profile/")
.append(UUIDTypeAdapter.fromUUID(profile.getId())).toString());
.append("https://sessionserver.mojang.com/session/minecraft/profile/")
.append(UUIDTypeAdapter.fromUUID(profile.getId())).toString());
url = HttpAuthenticationService.concatenateURL(url,
new StringBuilder().append("unsigned=").append(!requireSecure).toString());
MinecraftProfilePropertiesResponse response = (MinecraftProfilePropertiesResponse) MAKE_REQUEST.invoke(
Expand All @@ -158,7 +166,7 @@ public void run() {
if (cached != null) {
if (Messaging.isDebugging()) {
Messaging
.debug("Using cached skin texture for NPC " + npc.getName() + " UUID " + npc.getUniqueId());
.debug("Using cached skin texture for NPC " + npc.getName() + " UUID " + npc.getUniqueId());
}
skinProfile = new GameProfile(UUID.fromString(realUUID), "");
skinProfile.getProperties().put("textures", cached);
Expand All @@ -170,7 +178,7 @@ public void run() {
} catch (Exception e) {
if ((e.getMessage() != null && e.getMessage().contains("too many requests"))
|| (e.getCause() != null && e.getCause().getMessage() != null && e.getCause().getMessage()
.contains("too many requests"))) {
.contains("too many requests"))) {
SKIN_THREAD.delay();
SKIN_THREAD.addRunnable(this);
}
Expand Down Expand Up @@ -260,21 +268,21 @@ public String call() throws Exception {
.getGameProfileRepository();
repo.findProfilesByNames(new String[] { ChatColor.stripColor(reportedUUID) }, Agent.MINECRAFT,
new ProfileLookupCallback() {
@Override
public void onProfileLookupFailed(GameProfile arg0, Exception arg1) {
}
@Override
public void onProfileLookupFailed(GameProfile arg0, Exception arg1) {
}

@Override
public void onProfileLookupSucceeded(final GameProfile profile) {
UUID_CACHE.put(reportedUUID, profile.getId().toString());
if (Messaging.isDebugging()) {
Messaging.debug("Fetched UUID " + profile.getId() + " for NPC " + npc.getName()
+ " UUID " + npc.getUniqueId());
}
npc.data().setPersistent(CACHED_SKIN_UUID_METADATA, profile.getId().toString());
npc.data().setPersistent(CACHED_SKIN_UUID_NAME_METADATA, profile.getName());
}
});
@Override
public void onProfileLookupSucceeded(final GameProfile profile) {
UUID_CACHE.put(reportedUUID, profile.getId().toString());
if (Messaging.isDebugging()) {
Messaging.debug("Fetched UUID " + profile.getId() + " for NPC " + npc.getName()
+ " UUID " + npc.getUniqueId());
}
npc.data().setPersistent(CACHED_SKIN_UUID_METADATA, profile.getId().toString());
npc.data().setPersistent(CACHED_SKIN_UUID_NAME_METADATA, profile.getName());
}
});
return npc.data().get(CACHED_SKIN_UUID_METADATA, reportedUUID);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public void g(double x, double y, double z) {
return;
}
if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) {
if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true))
if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) {
super.g(x, y, z);
}
return;
}
Vector vector = new Vector(x, y, z);
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/net/citizensnpcs/trait/Controllable.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,18 @@ public boolean toggle() {
return enabled;
}

private double updateHorizontalSpeed(net.minecraft.server.v1_8_R1.Entity handle, double speed, float speedMod) {
private double updateHorizontalSpeed(net.minecraft.server.v1_8_R1.Entity handle,
net.minecraft.server.v1_8_R1.Entity passenger, double speed, float speedMod) {
double oldSpeed = Math.sqrt(handle.motX * handle.motX + handle.motZ * handle.motZ);
double horizontal = ((EntityLiving) handle.passenger).aY;
double horizontal = ((EntityLiving) passenger).aY;
if (horizontal > 0.0D) {
double dXcos = -Math.sin(handle.passenger.yaw * Math.PI / 180.0F);
double dXsin = Math.cos(handle.passenger.yaw * Math.PI / 180.0F);
double dXcos = -Math.sin(passenger.yaw * Math.PI / 180.0F);
double dXsin = Math.cos(passenger.yaw * Math.PI / 180.0F);
handle.motX += dXcos * speed * 0.5;
handle.motZ += dXsin * speed * 0.5;
}
handle.motX += handle.passenger.motX * speedMod;
handle.motZ += handle.passenger.motZ * speedMod;
handle.motX += passenger.motX * speedMod;
handle.motZ += passenger.motZ * speedMod;

double newSpeed = Math.sqrt(handle.motX * handle.motX + handle.motZ * handle.motZ);
if (newSpeed > 0.35D) {
Expand Down Expand Up @@ -259,12 +260,13 @@ public void rightClickEntity(NPCRightClickEvent event) {
@Override
public void run(Player rider) {
net.minecraft.server.v1_8_R1.Entity handle = getHandle();
net.minecraft.server.v1_8_R1.Entity passenger = ((CraftPlayer) rider).getHandle();
boolean onGround = handle.onGround;
float speedMod = npc.getNavigator().getDefaultParameters()
.modifiedSpeed((onGround ? GROUND_SPEED : AIR_SPEED));
this.speed = updateHorizontalSpeed(handle, speed, speedMod);
speed = updateHorizontalSpeed(handle, passenger, speed, speedMod);

boolean shouldJump = NMS.shouldJump(handle.passenger);
boolean shouldJump = NMS.shouldJump(passenger);
if (shouldJump) {
if (handle.onGround && jumpTicks == 0) {
getHandle().motY = JUMP_VELOCITY;
Expand Down Expand Up @@ -353,8 +355,10 @@ public void run(Player rider) {
return;
}
net.minecraft.server.v1_8_R1.Entity handle = getHandle();
this.speed = updateHorizontalSpeed(handle, this.speed, 1F);
boolean shouldJump = NMS.shouldJump(handle.passenger);
net.minecraft.server.v1_8_R1.Entity passenger = ((CraftPlayer) rider).getHandle();

speed = updateHorizontalSpeed(handle, passenger, speed, 1F);
boolean shouldJump = NMS.shouldJump(passenger);
if (shouldJump) {
handle.motY = 0.3F;
}
Expand Down

0 comments on commit afba671

Please sign in to comment.