Skip to content

Commit

Permalink
Workaround for horse pathfinding in minecraft - assume that it fits o…
Browse files Browse the repository at this point in the history
…n a single block
  • Loading branch information
fullwall committed Jul 2, 2016
1 parent 8027fc7 commit a4b199c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/citizensnpcs/Citizens.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.citizensnpcs.api.command.Injector;
import net.citizensnpcs.api.event.CitizensDisableEvent;
import net.citizensnpcs.api.event.CitizensEnableEvent;
import net.citizensnpcs.api.event.CitizensPreReloadEvent;
import net.citizensnpcs.api.event.CitizensReloadEvent;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.exception.NPCLoadException;
Expand Down Expand Up @@ -344,6 +345,8 @@ public void reload() throws NPCLoadException {
despawnNPCs();
ProfileFetcher.reset();
Skin.clearCache();
getServer().getPluginManager().callEvent(new CitizensPreReloadEvent());

saves = createStorage(getDataFolder());
saves.loadInto(npcRegistry);

Expand Down
50 changes: 26 additions & 24 deletions src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import net.citizensnpcs.api.ai.event.NavigationBeginEvent;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.event.CitizensDeserialiseMetaEvent;
import net.citizensnpcs.api.event.CitizensReloadEvent;
import net.citizensnpcs.api.event.CitizensPreReloadEvent;
import net.citizensnpcs.api.event.CitizensSerialiseMetaEvent;
import net.citizensnpcs.api.event.CommandSenderCreateNPCEvent;
import net.citizensnpcs.api.event.DespawnReason;
Expand Down Expand Up @@ -164,7 +164,7 @@ public void onChunkUnload(ChunkUnloadEvent event) {
}

@EventHandler(priority = EventPriority.MONITOR)
public void onCitizensReload(CitizensReloadEvent event) {
public void onCitizensReload(CitizensPreReloadEvent event) {
skinUpdateTracker.reset();
toRespawn.clear();
}
Expand Down Expand Up @@ -306,29 +306,29 @@ public void onMetaDeserialise(CitizensDeserialiseMetaEvent event) {

@EventHandler
public void onMetaSerialise(CitizensSerialiseMetaEvent event) {
if (event.getMeta() instanceof SkullMeta) {
SkullMeta meta = (SkullMeta) event.getMeta();
GameProfile profile = NMS.getProfile(meta);
if (profile == null)
return;
if (profile.getName() != null) {
event.getKey().setString("skull.owner", profile.getName());
}
if (profile.getId() != null) {
event.getKey().setString("skull.uuid", profile.getId().toString());
}
if (profile.getProperties() != null) {
for (Entry<String, Collection<Property>> entry : profile.getProperties().asMap().entrySet()) {
DataKey relative = event.getKey().getRelative("skull.properties." + entry.getKey());
int i = 0;
for (Property value : entry.getValue()) {
relative.getRelative(i).setString("name", value.getName());
if (value.getSignature() != null) {
relative.getRelative(i).setString("signature", value.getSignature());
}
relative.getRelative(i).setString("value", value.getValue());
i++;
if (!(event.getMeta() instanceof SkullMeta))
return;
SkullMeta meta = (SkullMeta) event.getMeta();
GameProfile profile = NMS.getProfile(meta);
if (profile == null)
return;
if (profile.getName() != null) {
event.getKey().setString("skull.owner", profile.getName());
}
if (profile.getId() != null) {
event.getKey().setString("skull.uuid", profile.getId().toString());
}
if (profile.getProperties() != null) {
for (Entry<String, Collection<Property>> entry : profile.getProperties().asMap().entrySet()) {
DataKey relative = event.getKey().getRelative("skull.properties." + entry.getKey());
int i = 0;
for (Property value : entry.getValue()) {
relative.getRelative(i).setString("name", value.getName());
if (value.getSignature() != null) {
relative.getRelative(i).setString("signature", value.getSignature());
}
relative.getRelative(i).setString("value", value.getValue());
i++;
}
}
}
Expand All @@ -349,6 +349,7 @@ public void onNeedsRespawn(NPCNeedsRespawnEvent event) {
ChunkCoord coord = toCoord(event.getSpawnLocation());
if (toRespawn.containsEntry(coord, event.getNPC()))
return;
Messaging.debug("Stored", event.getNPC().getId(), "for respawn from NPCNeedsRespawnEvent");
toRespawn.put(coord, event.getNPC());
}

Expand Down Expand Up @@ -516,6 +517,7 @@ public void onWorldUnload(WorldUnloadEvent event) {
respawnAllFromCoord(coord);
}
}
event.setCancelled(true);
return;
}
if (npc.isSpawned()) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public boolean despawn(DespawnReason reason) {
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
getEntity().getLocation().getChunk();
Messaging.debug("Couldn't despawn", getId(), "due to despawn event cancellation. Force loaded chunk.");
Messaging.debug("Couldn't despawn", getId(), "due to despawn event cancellation. Force loaded chunk.",
getEntity().isValid());
return false;
}
boolean keepSelected = getTrait(Spawned.class).shouldSpawn();
Expand All @@ -93,6 +94,7 @@ public boolean despawn(DespawnReason reason) {
for (Trait trait : new ArrayList<Trait>(traits.values())) {
trait.onDespawn();
}
Messaging.debug("Despawned", getId(), "DespawnReason.", reason);
entityController.remove();

return true;
Expand Down Expand Up @@ -140,6 +142,9 @@ public void load(final DataKey root) {
if (getTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() != null) {
spawn(spawnLocation.getLocation());
}
if (getTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() == null) {
Messaging.debug("Tried to spawn", getId(), "on load but world was null");
}

navigator.load(root.getRelative("navigator"));
}
Expand Down Expand Up @@ -261,6 +266,7 @@ public boolean spawn(Location at) {
NMS.replaceTrackerEntry(player);
}
}
Messaging.debug("Spawned", getId(), at, mcEntity.valid);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import net.citizensnpcs.api.ai.TargetType;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_10_R1.EntityHorse;
import net.minecraft.server.v1_10_R1.EntityLiving;
import net.minecraft.server.v1_10_R1.NavigationAbstract;

Expand All @@ -29,7 +31,13 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
navigation = NMS.getNavigation(handle);
float oldWidth = handle.width;
if (handle instanceof EntityHorse) {
handle.width = Math.min(0.99f, oldWidth);
}
navigation.a(dest.getX(), dest.getY(), dest.getZ(), parameters.speed());
handle.width = oldWidth; // minecraft requires that an entity fit onto both blocks if width >= 1f, but we'd
// prefer to make it just fit on 1 so hack around it a bit.
if (NMS.isNavigationFinished(navigation)) {
setCancelReason(CancelReason.STUCK);
}
Expand Down Expand Up @@ -64,6 +72,7 @@ public boolean update() {
if (getCancelReason() != null)
return true;
if (parameters.speed() != lastSpeed) {
Messaging.debug("Repathfinding " + ((NPCHolder) handle).getNPC().getId() + " due to speed change");
navigation.a(target.getX(), target.getY(), target.getZ(), parameters.speed());
lastSpeed = parameters.speed();
}
Expand Down

0 comments on commit a4b199c

Please sign in to comment.