Skip to content

Commit

Permalink
Add /npc hitbox, WIP packet NPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 3, 2023
1 parent 26bf39c commit e21681f
Show file tree
Hide file tree
Showing 101 changed files with 1,070 additions and 291 deletions.
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/Citizens.java
Expand Up @@ -119,7 +119,7 @@ public String getTexture(SkullMeta meta) {
}

@Override
public void setTexture(String string, SkullMeta meta) {
public void setTexture(String texture, SkullMeta meta) {
GameProfile profile = NMS.getProfile(meta);
if (profile == null) {
if (SUPPORT_OWNER_PROFILE) {
Expand All @@ -134,7 +134,7 @@ public void setTexture(String string, SkullMeta meta) {
profile = new GameProfile(UUID.randomUUID(), null);
}
}
profile.getProperties().put("textures", new Property("textures", string));
profile.getProperties().put("textures", new Property("textures", texture));
NMS.setProfile(meta, profile);
}
};
Expand Down
51 changes: 16 additions & 35 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -55,6 +55,7 @@
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.scheduler.BukkitRunnable;

import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -349,14 +350,12 @@ public void onNeedsRespawn(NPCNeedsRespawnEvent event) {
public void onNPCDespawn(NPCDespawnEvent event) {
if (event.getReason() == DespawnReason.PLUGIN || event.getReason() == DespawnReason.REMOVAL
|| event.getReason() == DespawnReason.RELOAD) {
if (Messaging.isDebugging()) {
Messaging.debug("Preventing further respawns of", event.getNPC(),
"due to DespawnReason." + event.getReason());
}
Messaging.idebug(() -> Joiner.on(' ').join("Preventing further respawns of", event.getNPC(),
"due to DespawnReason." + event.getReason()));
toRespawn.values().remove(event.getNPC());
} else if (Messaging.isDebugging()) {
Messaging.debug("Removing", event.getNPC(),
"from skin tracker due to DespawnReason." + event.getReason().name());
} else {
Messaging.idebug(() -> Joiner.on(' ').join("Removing", event.getNPC(),
"from skin tracker due to DespawnReason." + event.getReason().name()));
}
skinUpdateTracker.onNPCDespawn(event.getNPC());
}
Expand All @@ -369,9 +368,8 @@ public void onNPCRemove(NPCRemoveEvent event) {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onNPCSpawn(NPCSpawnEvent event) {
skinUpdateTracker.onNPCSpawn(event.getNPC());
if (Messaging.isDebugging()) {
Messaging.debug("Removing respawns of", event.getNPC(), "due to SpawnReason." + event.getReason());
}
Messaging.idebug(() -> Joiner.on(' ').join("Removing respawns of", event.getNPC(),
"due to SpawnReason." + event.getReason()));
toRespawn.values().remove(event.getNPC());
}

Expand Down Expand Up @@ -631,28 +629,20 @@ private void respawnAllFromCoord(ChunkCoord coord, Event event) {
for (int i = 0; i < ids.size(); i++) {
NPC npc = ids.get(i);
if (npc.getOwningRegistry().getById(npc.getId()) != npc) {
if (Messaging.isDebugging()) {
Messaging.debug("Prevented deregistered NPC from respawning", npc);
}
Messaging.idebug(() -> "Prevented deregistered NPC from respawning " + npc);
continue;
}
if (npc.isSpawned()) {
if (Messaging.isDebugging()) {
Messaging.debug("Can't respawn NPC", npc, ": already spawned");
}
Messaging.idebug(() -> "Can't respawn NPC " + npc + ": already spawned");
continue;
}
boolean success = spawn(npc);
if (!success) {
ids.remove(i--);
if (Messaging.isDebugging()) {
Messaging.debug("Couldn't respawn", npc, "during", event, "at", coord);
}
Messaging.idebug(() -> Joiner.on(' ').join("Couldn't respawn", npc, "during", event, "at", coord));
continue;
}
if (Messaging.isDebugging()) {
Messaging.debug("Spawned", npc, "during", event, "at", coord);
}
Messaging.idebug(() -> Joiner.on(' ').join("Spawned", npc, "during", event, "at", coord));
}
for (NPC npc : ids) {
toRespawn.remove(coord, npc);
Expand All @@ -662,9 +652,7 @@ private void respawnAllFromCoord(ChunkCoord coord, Event event) {
private boolean spawn(NPC npc) {
Location spawn = npc.getOrAddTrait(CurrentLocation.class).getLocation();
if (spawn == null) {
if (Messaging.isDebugging()) {
Messaging.debug("Couldn't find a spawn location for despawned NPC", npc);
}
Messaging.idebug(() -> Joiner.on(' ').join("Couldn't find a spawn location for despawned NPC", npc));
return false;
}
return npc.spawn(spawn, SpawnReason.CHUNK_LOAD);
Expand All @@ -687,9 +675,7 @@ void unloadNPCs(ChunkEvent event, List<Entity> entities) {
for (NPC npc : toDespawn) {
if (!npc.despawn(DespawnReason.CHUNK_UNLOAD)) {
if (!(event instanceof Cancellable)) {
if (Messaging.isDebugging()) {
Messaging.debug("Reloading chunk because", npc, "couldn't despawn");
}
Messaging.idebug(() -> Joiner.on(' ').join("Reloading chunk because", npc, "couldn't despawn"));
loadChunk = true;
toRespawn.put(coord, npc);
continue;
Expand All @@ -700,17 +686,13 @@ void unloadNPCs(ChunkEvent event, List<Entity> entities) {
return;
}
toRespawn.put(coord, npc);
if (Messaging.isDebugging()) {
Messaging.debug("Despawned", npc, "due to chunk unload at", coord);
}
Messaging.idebug(() -> Joiner.on(' ').join("Despawned", npc, "due to chunk unload at", coord));
}
if (Messaging.isDebugging() && Setting.DEBUG_CHUNK_LOADS.asBoolean()) {
new Exception("CITIZENS CHUNK UNLOAD DEBUG " + coord).printStackTrace();
}
if (loadChunk) {
if (Messaging.isDebugging()) {
Messaging.debug("Loading chunk in 10 ticks due to forced chunk load at", coord);
}
Messaging.idebug(() -> Joiner.on(' ').join("Loading chunk in 10 ticks due to forced chunk load at", coord));
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Expand All @@ -722,6 +704,5 @@ public void run() {
}
}

private static boolean SUPPORT_OWNING_PLAYER = true;
private static boolean SUPPORT_STOP_USE_ITEM = true;
}
1 change: 0 additions & 1 deletion main/src/main/java/net/citizensnpcs/Settings.java
Expand Up @@ -29,7 +29,6 @@ public Settings(File folder) {
}
}
updateMessagingSettings();

save();
}

Expand Down
25 changes: 25 additions & 0 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -86,6 +86,7 @@
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.api.trait.trait.Speech;
import net.citizensnpcs.api.util.EntityDim;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.api.util.Placeholders;
Expand All @@ -100,6 +101,7 @@
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.BoundingBoxTrait;
import net.citizensnpcs.trait.ClickRedirectTrait;
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.CommandTrait.CommandTraitError;
Expand Down Expand Up @@ -1047,6 +1049,29 @@ public void gui(CommandContext args, Player sender, NPC npc) {
InventoryMenu.createSelfRegistered(new NPCConfigurator(npc)).present(sender);
}

@Command(
aliases = { "npc" },
usage = "hitbox --scale [scale] --width/height [value]",
desc = "Sets the NPC hitbox",
modifiers = { "hitbox" },
min = 1,
max = 1,
permission = "citizens.npc.hitbox")
public void hitbox(CommandContext args, CommandSender sender, NPC npc, @Flag("scale") Float scale,
@Flag("width") Float width, @Flag("height") Float height) {
if (scale != null) {
npc.getOrAddTrait(BoundingBoxTrait.class).setScale(scale);
}
if (width != null) {
npc.getOrAddTrait(BoundingBoxTrait.class).setWidth(width);
}
if (height != null) {
npc.getOrAddTrait(BoundingBoxTrait.class).setHeight(height);
}
EntityDim dim = npc.getOrAddTrait(BoundingBoxTrait.class).getAdjustedBoundingBox();
Messaging.sendTr(sender, Messages.BOUNDING_BOX_SET, "width " + dim.width + " height " + dim.height);
}

@Command(
aliases = { "npc" },
usage = "hologram add [text] | set [line #] [text] | remove [line #] | clear | lineheight [height] | direction [up|down]",
Expand Down
Expand Up @@ -3,9 +3,11 @@
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

public abstract class AbstractEntityController implements EntityController {
private Entity bukkitEntity;
Expand All @@ -17,8 +19,18 @@ public AbstractEntityController(Class<?> clazz) {
NMS.registerEntityClass(clazz);
}

@Override
public void create(Location at, NPC npc) {
bukkitEntity = createEntity(at, npc);
}

protected abstract Entity createEntity(Location at, NPC npc);

@Override
public void die() {
bukkitEntity = null;
}

@Override
public Entity getBukkitEntity() {
return bukkitEntity;
Expand All @@ -31,20 +43,15 @@ public void remove() {
if (bukkitEntity instanceof Player) {
NMS.removeFromWorld(bukkitEntity);
NMS.remove(bukkitEntity);
setEntity(null);
bukkitEntity = null;
} else {
bukkitEntity.remove();
bukkitEntity = null;
}
}

@Override
public void setEntity(Entity entity) {
this.bukkitEntity = entity;
}

@Override
public void spawn(Location at, NPC npc) {
bukkitEntity = createEntity(at, npc);
public boolean spawn(Location at) {
return !Util.isLoaded(at) ? false : NMS.addEntityToWorld(bukkitEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
}
24 changes: 13 additions & 11 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -12,7 +12,6 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scheduler.BukkitRunnable;
Expand Down Expand Up @@ -47,6 +46,7 @@
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.PacketNPC;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.trait.SitTrait;
import net.citizensnpcs.trait.SneakTrait;
Expand All @@ -63,10 +63,9 @@ public class CitizensNPC extends AbstractNPC {
private final CitizensNavigator navigator = new CitizensNavigator(this);
private int updateCounter = 0;

public CitizensNPC(UUID uuid, int id, String name, EntityController entityController, NPCRegistry registry) {
public CitizensNPC(UUID uuid, int id, String name, EntityController controller, NPCRegistry registry) {
super(uuid, id, name, registry);
Preconditions.checkNotNull(entityController);
this.entityController = entityController;
setEntityController(controller);
}

@Override
Expand Down Expand Up @@ -109,7 +108,7 @@ public boolean despawn(DespawnReason reason) {
}

if (reason == DespawnReason.DEATH) {
entityController.setEntity(null);
entityController.die();
} else {
entityController.remove();
}
Expand Down Expand Up @@ -139,6 +138,10 @@ public Entity getEntity() {
return entityController == null ? null : entityController.getBukkitEntity();
}

public EntityController getEntityController() {
return entityController;
}

@Override
public Navigator getNavigator() {
return navigator;
Expand All @@ -157,7 +160,7 @@ public boolean isFlyable() {

@Override
public boolean isSpawned() {
return getEntity() != null && NMS.isValid(getEntity());
return getEntity() != null && (hasTrait(PacketNPC.class) || NMS.isValid(getEntity()));
}

@Override
Expand Down Expand Up @@ -225,7 +228,7 @@ public void setBukkitEntityType(EntityType type) {

public void setEntityController(EntityController newController) {
Preconditions.checkNotNull(newController);
boolean wasSpawned = isSpawned();
boolean wasSpawned = entityController == null ? false : isSpawned();
Location prev = null;
if (wasSpawned) {
prev = getEntity().getLocation(CACHE_LOCATION);
Expand Down Expand Up @@ -287,7 +290,7 @@ public boolean spawn(Location at, SpawnReason reason) {
}

getOrAddTrait(CurrentLocation.class).setLocation(at);
entityController.spawn(at.clone(), this);
entityController.create(at.clone(), this);
getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true));

Collection<Trait> onPreSpawn = traits.values();
Expand All @@ -300,8 +303,8 @@ public boolean spawn(Location at, SpawnReason reason) {
}
}

boolean loaded = Util.isLoaded(at);
boolean couldSpawn = !loaded ? false : NMS.addEntityToWorld(getEntity(), CreatureSpawnEvent.SpawnReason.CUSTOM);
boolean loaded = Messaging.isDebugging() ? false : Util.isLoaded(at);
boolean couldSpawn = entityController.spawn(at);

if (!couldSpawn) {
if (Messaging.isDebugging()) {
Expand Down Expand Up @@ -598,7 +601,6 @@ private void updateUsingItemState(Player player) {
private static boolean SUPPORT_GLOWING = true;
private static boolean SUPPORT_NODAMAGE_TICKS = true;
private static boolean SUPPORT_PICKUP_ITEMS = true;

private static boolean SUPPORT_SILENT = true;

private static boolean SUPPORT_USE_ITEM = true;
Expand Down
Expand Up @@ -21,6 +21,7 @@
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.BoundingBoxTrait;
import net.citizensnpcs.trait.ClickRedirectTrait;
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.Controllable;
Expand All @@ -37,6 +38,7 @@
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.MountTrait;
import net.citizensnpcs.trait.OcelotModifiers;
import net.citizensnpcs.trait.PacketNPC;
import net.citizensnpcs.trait.Poses;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.RabbitType;
Expand Down Expand Up @@ -66,6 +68,7 @@ public CitizensTraitFactory() {
registerTrait(TraitInfo.create(Age.class));
registerTrait(TraitInfo.create(ArmorStandTrait.class));
registerTrait(TraitInfo.create(Anchors.class));
registerTrait(TraitInfo.create(BoundingBoxTrait.class));
registerTrait(TraitInfo.create(ClickRedirectTrait.class));
registerTrait(TraitInfo.create(CommandTrait.class));
registerTrait(TraitInfo.create(Controllable.class));
Expand All @@ -86,6 +89,7 @@ public CitizensTraitFactory() {
registerTrait(TraitInfo.create(MobType.class).asDefaultTrait());
registerTrait(TraitInfo.create(OcelotModifiers.class));
registerTrait(TraitInfo.create(Owner.class));
registerTrait(TraitInfo.create(PacketNPC.class));
registerTrait(TraitInfo.create(Poses.class));
registerTrait(TraitInfo.create(Powered.class));
registerTrait(TraitInfo.create(RabbitType.class));
Expand Down
8 changes: 5 additions & 3 deletions main/src/main/java/net/citizensnpcs/npc/EntityController.java
Expand Up @@ -6,11 +6,13 @@
import net.citizensnpcs.api.npc.NPC;

public interface EntityController {
void create(Location at, NPC npc);

void die();

Entity getBukkitEntity();

void remove();

void setEntity(Entity entity);

void spawn(Location at, NPC npc);
boolean spawn(Location at);
}

0 comments on commit e21681f

Please sign in to comment.