Add debug for hologram trait

This commit is contained in:
fullwall 2021-07-04 13:07:15 +08:00
parent 2d80f763eb
commit ef4d746aee
3 changed files with 60 additions and 41 deletions

View File

@ -9,10 +9,12 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.hpastar.HPAGraph;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.editor.Editor;
@ -65,10 +67,29 @@ public class WaypointCommands {
Messaging.sendTr(sender, Messages.WAYPOINT_TELEPORTING_DISABLED);
}
@Command(
aliases = { "waypoints", "waypoint", "wp" },
usage = "hpa",
desc = "Debugging command",
modifiers = { "hpa" },
min = 1,
max = 1,
permission = "citizens.waypoints.hpa")
public void hpa(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (Messaging.isDebugging() && sender.isOp()) {
HPAGraph graph = new HPAGraph(new ChunkBlockSource(npc.getStoredLocation(), 16),
npc.getStoredLocation().getBlockX(), npc.getStoredLocation().getBlockY(),
npc.getStoredLocation().getBlockZ());
graph.addClusters(npc.getStoredLocation().getBlockX(), npc.getStoredLocation().getBlockZ());
System.out.println(graph.findPath(new Location(npc.getStoredLocation().getWorld(), 8, 68, -134),
new Location(npc.getStoredLocation().getWorld(), 11, 68, -131)));
}
}
@Command(
aliases = { "waypoints", "waypoint", "wp" },
usage = "opendoors",
desc = "Enables opening doors when pathfinding (temporary command)",
desc = "Enables opening doors when pathfinding",
modifiers = { "opendoors" },
min = 1,
max = 1,

View File

@ -283,8 +283,7 @@ public class Controllable extends Trait implements Toggleable, CommandConfigurab
double newSpeed = Math.sqrt(vel.getX() * vel.getX() + vel.getZ() * vel.getZ());
if (newSpeed > maxSpeed) {
double movementFactor = maxSpeed / newSpeed;
vel = vel.multiply(new Vector(movementFactor, 1, movementFactor));
vel = vel.multiply(new Vector(maxSpeed / newSpeed, 1, maxSpeed / newSpeed));
newSpeed = maxSpeed;
}
handle.setVelocity(vel);
@ -318,7 +317,8 @@ public class Controllable extends Trait implements Toggleable, CommandConfigurab
boolean onGround = NMS.isOnGround(npc.getEntity());
float speedMod = npc.getNavigator().getDefaultParameters()
.modifiedSpeed((onGround ? GROUND_SPEED : AIR_SPEED));
if (!Util.isHorse(npc.getEntity().getType()) || npc.getEntity().getType().name().equals("LLAMA") || npc.getEntity().getType().name().equals("TRADER_LLAMA")) {
if (!Util.isHorse(npc.getEntity().getType()) || npc.getEntity().getType().name().equals("LLAMA")
|| npc.getEntity().getType().name().equals("TRADER_LLAMA")) {
// just use minecraft horse physics
speed = updateHorizontalSpeed(npc.getEntity(), rider, speed, speedMod);
}

View File

@ -21,6 +21,7 @@ import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.util.NMS;
@ -52,15 +53,15 @@ public class HologramTrait extends Trait {
*/
public void addLine(String text) {
lines.add(text);
unload();
load();
onDespawn();
onSpawn();
}
/**
* Clears all hologram lines
*/
public void clear() {
unload();
onDespawn();
lines.clear();
}
@ -129,32 +130,34 @@ public class HologramTrait extends Trait {
return nameNPC != null && nameNPC.isSpawned() ? ((ArmorStand) npc.getEntity()) : null;
}
private void load() {
currentLoc = npc.getStoredLocation();
int i = 0;
if (npc.requiresNameHologram()
&& Boolean.parseBoolean(npc.data().<Object> get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString())) {
nameNPC = createHologram(npc.getFullName(), 0);
}
for (String line : lines) {
hologramNPCs.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
i++;
}
}
@Override
public void onDespawn() {
unload();
if (nameNPC != null) {
nameNPC.destroy();
nameNPC = null;
}
for (NPC npc : hologramNPCs) {
npc.destroy();
}
hologramNPCs.clear();
}
@Override
public void onRemove() {
unload();
onDespawn();
}
@Override
public void onSpawn() {
load();
currentLoc = npc.getStoredLocation();
if (npc.requiresNameHologram()
&& Boolean.parseBoolean(npc.data().<Object> get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString())) {
nameNPC = createHologram(npc.getFullName(), 0);
}
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
hologramNPCs.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
}
}
/**
@ -164,14 +167,14 @@ public class HologramTrait extends Trait {
*/
public void removeLine(int idx) {
lines.remove(idx);
unload();
load();
onDespawn();
onSpawn();
}
@Override
public void run() {
if (!npc.isSpawned()) {
unload();
onDespawn();
return;
}
if (npc.requiresNameHologram()) {
@ -203,6 +206,10 @@ public class HologramTrait extends Trait {
hologramNPC.teleport(currentLoc.clone().add(0, getEntityHeight() + getHeight(i), 0),
TeleportCause.PLUGIN);
}
if (i >= lines.size()) {
Messaging.severe("More hologram NPCs than lines for ID", npc.getId());
break;
}
String text = lines.get(i);
if (text != null && !ChatColor.stripColor(Colorizer.parseColors(text)).isEmpty()) {
hologramNPC.setName(Placeholders.replace(text, null, npc));
@ -221,8 +228,8 @@ public class HologramTrait extends Trait {
*/
public void setDirection(HologramDirection direction) {
this.direction = direction;
unload();
load();
onDespawn();
onSpawn();
}
/**
@ -239,6 +246,8 @@ public class HologramTrait extends Trait {
} else {
lines.set(idx, text);
}
onDespawn();
onSpawn();
}
/**
@ -250,19 +259,8 @@ public class HologramTrait extends Trait {
*/
public void setLineHeight(double height) {
lineHeight = height;
unload();
load();
}
private void unload() {
if (nameNPC != null) {
nameNPC.destroy();
nameNPC = null;
}
for (NPC npc : hologramNPCs) {
npc.destroy();
}
hologramNPCs.clear();
onDespawn();
onSpawn();
}
public enum HologramDirection {