Skip to content

Commit

Permalink
Use new spawnreason
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Aug 8, 2018
1 parent 50cc442 commit 505ef8a
Show file tree
Hide file tree
Showing 24 changed files with 206 additions and 162 deletions.
6 changes: 3 additions & 3 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.trait.Owner;
Expand Down Expand Up @@ -262,7 +263,7 @@ public void onEntityDeath(EntityDeathEvent event) {
@Override
public void run() {
if (!npc.isSpawned() && npc.getOwningRegistry().getByUniqueId(npc.getUniqueId()) == npc) {
npc.spawn(location);
npc.spawn(location, SpawnReason.TIMED_RESPAWN);
}
}
}, delay + 2);
Expand Down Expand Up @@ -530,8 +531,7 @@ public void onVehicleEnter(final VehicleEnterEvent event) {
@EventHandler(ignoreCancelled = true)
public void onWorldLoad(WorldLoadEvent event) {
for (ChunkCoord chunk : toRespawn.keySet()) {
if (!chunk.worldUUID.equals(event.getWorld().getUID())
|| !event.getWorld().isChunkLoaded(chunk.x, chunk.z))
if (!chunk.worldUUID.equals(event.getWorld().getUID()) || !event.getWorld().isChunkLoaded(chunk.x, chunk.z))
continue;
respawnAllFromCoord(chunk);
}
Expand Down
17 changes: 9 additions & 8 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import net.citizensnpcs.api.event.CommandSenderCreateNPCEvent;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
Expand Down Expand Up @@ -416,7 +417,7 @@ public void create(CommandContext args, CommandSender sender, NPC npc) throws Co
}

if (!args.hasFlag('u')) {
npc.spawn(spawnLoc);
npc.spawn(spawnLoc, SpawnReason.CREATE);
}

if (args.hasValueFlag("trait")) {
Expand Down Expand Up @@ -687,8 +688,8 @@ public void item(CommandContext args, CommandSender sender, NPC npc) throws Comm
break;
}
if (npc.isSpawned()) {
npc.despawn();
npc.spawn(npc.getStoredLocation());
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.spawn(npc.getStoredLocation(), SpawnReason.RESPAWN);
}
Messaging.sendTr(sender, Messages.ITEM_SET, Util.prettyEnum(mat));
}
Expand Down Expand Up @@ -915,7 +916,7 @@ public void mount(CommandContext args, CommandSender sender, NPC npc) throws Com
public void moveto(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned()) {
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation(), SpawnReason.COMMAND);
}
if (!npc.isSpawned()) {
throw new CommandException("NPC could not be spawned.");
Expand Down Expand Up @@ -1144,7 +1145,7 @@ public void playerlist(CommandContext args, CommandSender sender, NPC npc) {
npc.data().setPersistent("removefromplayerlist", remove);
if (npc.isSpawned()) {
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation(), SpawnReason.RESPAWN);
NMS.addOrRemoveFromPlayerList(npc.getEntity(), remove);
}
Messaging.sendTr(sender, remove ? Messages.REMOVED_FROM_PLAYERLIST : Messages.ADDED_TO_PLAYERLIST,
Expand Down Expand Up @@ -1333,7 +1334,7 @@ public void rename(CommandContext args, CommandSender sender, NPC npc) {
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.setName(newName);
if (prev != null) {
npc.spawn(prev);
npc.spawn(prev, SpawnReason.RESPAWN);
}

Messaging.sendTr(sender, Messages.NPC_RENAMED, oldName, newName);
Expand Down Expand Up @@ -1664,7 +1665,7 @@ public void run(NPC respawn) throws CommandException {
if (args.hasFlag('l') && !Util.isLoaded(location)) {
location.getChunk().load();
}
if (respawn.spawn(location)) {
if (respawn.spawn(location, SpawnReason.COMMAND)) {
selector.select(sender, respawn);
Messaging.sendTr(sender, Messages.NPC_SPAWNED, respawn.getName());
}
Expand Down Expand Up @@ -1799,7 +1800,7 @@ public void tphere(CommandContext args, CommandSender sender, NPC npc) throws Co
throw new ServerCommandException();
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned()) {
npc.spawn(args.getSenderLocation());
npc.spawn(args.getSenderLocation(), SpawnReason.COMMAND);
if (!sender.hasPermission("citizens.npc.tphere.multiworld")
&& npc.getEntity().getLocation().getWorld() != args.getSenderLocation().getWorld()) {
npc.despawn(DespawnReason.REMOVAL);
Expand Down
18 changes: 13 additions & 5 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
Expand All @@ -28,6 +28,7 @@
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.AbstractNPC;
import net.citizensnpcs.api.npc.BlockBreaker;
import net.citizensnpcs.api.npc.BlockBreaker.BlockBreakerConfiguration;
Expand Down Expand Up @@ -139,7 +140,7 @@ public void load(final DataKey root) {
// Spawn the NPC
CurrentLocation spawnLocation = getTrait(CurrentLocation.class);
if (getTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() != null) {
spawn(spawnLocation.getLocation());
spawn(spawnLocation.getLocation(), SpawnReason.RESPAWN);
}
if (getTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() == null) {
Messaging.debug("Tried to spawn", getId(), "on load but world was null");
Expand Down Expand Up @@ -174,7 +175,7 @@ public void setEntityController(EntityController newController) {
}
entityController = newController;
if (wasSpawned) {
spawn(prev);
spawn(prev, SpawnReason.RESPAWN);
}
}

Expand All @@ -186,7 +187,13 @@ public void setFlyable(boolean flyable) {

@Override
public boolean spawn(Location at) {
return spawn(at, SpawnReason.PLUGIN);
}

@Override
public boolean spawn(Location at, SpawnReason reason) {
Preconditions.checkNotNull(at, "location cannot be null");
Preconditions.checkNotNull(reason, "reason cannot be null");
if (isSpawned()) {
Messaging.debug("Tried to spawn", getId(), "while already spawned.");
return false;
Expand All @@ -203,7 +210,8 @@ public boolean spawn(Location at) {

getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true));

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

// send skin packets, if applicable, before other NMS packets are sent
if (couldSpawn) {
Expand All @@ -230,7 +238,7 @@ public boolean spawn(Location at) {
getTrait(CurrentLocation.class).setLocation(at);
getTrait(Spawned.class).setSpawned(true);

NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, at);
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, at, reason);
Bukkit.getPluginManager().callEvent(spawnEvent);

if (spawnEvent.isCancelled()) {
Expand Down
3 changes: 2 additions & 1 deletion main/src/main/java/net/citizensnpcs/npc/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.MemoryDataKey;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void apply(NPC npc) {
}
npc.load(memoryKey);
if (wasSpawned && npc.getStoredLocation() != null) {
npc.spawn(npc.getStoredLocation());
npc.spawn(npc.getStoredLocation(), SpawnReason.RESPAWN);
}
}

Expand Down
3 changes: 2 additions & 1 deletion main/src/main/java/net/citizensnpcs/npc/skin/Skin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.profile.ProfileFetchHandler;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void applyAndRespawn(SkinnableEntity entity) {

if (npc.isSpawned()) {
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.spawn(npc.getStoredLocation());
npc.spawn(npc.getStoredLocation(), SpawnReason.RESPAWN);
}
}

Expand Down
3 changes: 2 additions & 1 deletion main/src/main/java/net/citizensnpcs/trait/Poses.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.collect.Maps;

import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
Expand Down Expand Up @@ -37,7 +38,7 @@ public boolean addPose(String name, Location location) {

private void assumePose(float yaw, float pitch) {
if (!npc.isSpawned()) {
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation(), SpawnReason.COMMAND);
}
Util.assumePose(npc.getEntity(), yaw, pitch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.Maps;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void removeMarker(T marker) {

public Entity spawnMarker(World world, Location at) {
NPC npc = registry.createNPC(EntityType.ENDER_SIGNAL, "");
npc.spawn(at);
npc.spawn(at, SpawnReason.CREATE);
return npc.getEntity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.Maps;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;

Expand Down Expand Up @@ -46,7 +47,7 @@ public void removeWaypointMarker(Waypoint waypoint) {
public Entity spawnMarker(World world, Location at) {
NPC npc = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore()).createNPC(EntityType.ENDER_SIGNAL,
"");
npc.spawn(at);
npc.spawn(at, SpawnReason.CREATE);
return npc.getEntity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.bukkit.entity.FallingBlock;
import org.bukkit.util.Vector;

import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_10_R1.util.NMSImpl;
import net.citizensnpcs.npc.AbstractEntityController;
Expand Down Expand Up @@ -118,15 +120,6 @@ public NPC getNPC() {
return npc;
}

@Override
public void setSize(float f, float f1) {
if (npc == null) {
super.setSize(f, f1);
} else {
NMSImpl.setSize(this, f, f1, justCreated);
}
}

@Override
public void m() {
if (npc != null) {
Expand All @@ -142,6 +135,15 @@ public void m() {
}
}

@Override
public void setSize(float f, float f1) {
if (npc == null) {
super.setSize(f, f1);
} else {
NMSImpl.setSize(this, f, f1, justCreated);
}
}

private static final double EPSILON = 0.001;
}

Expand All @@ -162,8 +164,8 @@ public void setType(Material material, int data) {
npc.data().setPersistent(NPC.ITEM_ID_METADATA, material.name());
npc.data().setPersistent(NPC.ITEM_DATA_METADATA, data);
if (npc.isSpawned()) {
npc.despawn();
npc.spawn(npc.getStoredLocation());
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.spawn(npc.getStoredLocation(), SpawnReason.RESPAWN);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package net.citizensnpcs.nms.v1_10_R1.entity.nonliving;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_10_R1.CraftServer;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftItem;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.util.Vector;

import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.CitizensNPC;
Expand All @@ -13,18 +27,6 @@
import net.minecraft.server.v1_10_R1.World;
import net.minecraft.server.v1_10_R1.WorldServer;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_10_R1.CraftServer;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftItem;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.util.Vector;

public class ItemController extends AbstractEntityController {
public ItemController() {
super(EntityItemNPC.class);
Expand Down Expand Up @@ -145,8 +147,8 @@ public void setType(Material material, int data) {
npc.data().setPersistent(NPC.ITEM_ID_METADATA, material.name());
npc.data().setPersistent(NPC.ITEM_DATA_METADATA, data);
if (npc.isSpawned()) {
npc.despawn();
npc.spawn(npc.getStoredLocation());
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.spawn(npc.getStoredLocation(), SpawnReason.RESPAWN);
}
}
}
Expand Down
Loading

0 comments on commit 505ef8a

Please sign in to comment.