mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-26 20:55:44 +01:00
possible fix and lots of other changes
This commit is contained in:
parent
d0e964153d
commit
6bd85f50c0
@ -14,6 +14,7 @@ import net.citizensnpcs.npc.trait.CitizensCharacterManager;
|
|||||||
import net.citizensnpcs.npc.trait.CitizensTraitManager;
|
import net.citizensnpcs.npc.trait.CitizensTraitManager;
|
||||||
import net.citizensnpcs.storage.Storage;
|
import net.citizensnpcs.storage.Storage;
|
||||||
import net.citizensnpcs.storage.flatfile.YamlStorage;
|
import net.citizensnpcs.storage.flatfile.YamlStorage;
|
||||||
|
import net.citizensnpcs.util.ByIdArray;
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -92,7 +93,6 @@ public class Citizens extends JavaPlugin {
|
|||||||
// TODO possibly separate this out some more
|
// TODO possibly separate this out some more
|
||||||
private void setupNPCs() throws NPCLoadException {
|
private void setupNPCs() throws NPCLoadException {
|
||||||
traitManager.registerTrait("location", SpawnLocation.class);
|
traitManager.registerTrait("location", SpawnLocation.class);
|
||||||
int spawned = 0;
|
|
||||||
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
||||||
int id = Integer.parseInt(key.name());
|
int id = Integer.parseInt(key.name());
|
||||||
if (!key.keyExists("name"))
|
if (!key.keyExists("name"))
|
||||||
@ -121,19 +121,18 @@ public class Citizens extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Spawn the NPC
|
// Spawn the NPC
|
||||||
if (key.getBoolean("spawned")) {
|
if (key.getBoolean("spawned"))
|
||||||
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
|
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
|
||||||
spawned++;
|
|
||||||
}
|
}
|
||||||
}
|
Messaging.log("Loaded " + ((ByIdArray<NPC>) npcManager.getAllNPCs()).size() + " NPCs ("
|
||||||
Messaging.log("Loaded " + npcManager.size() + " NPCs (" + spawned + " spawned).");
|
+ ((ByIdArray<NPC>) npcManager.getSpawnedNPCs()).size() + " spawned).");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveNPCs() {
|
private void saveNPCs() {
|
||||||
for (NPC npc : npcManager.getAllNPCs()) {
|
for (NPC npc : npcManager.getAllNPCs()) {
|
||||||
DataKey root = saves.getKey("npc." + npc.getId());
|
DataKey root = saves.getKey("npc." + npc.getId());
|
||||||
root.setString("name", npc.getFullName());
|
root.setString("name", npc.getFullName());
|
||||||
root.setBoolean("spawned", npc.isSpawned());
|
root.setBoolean("spawned", npc.getBukkitEntity().isDead());
|
||||||
|
|
||||||
// Save the character if it exists
|
// Save the character if it exists
|
||||||
if (npc.getCharacter() != null) {
|
if (npc.getCharacter() != null) {
|
||||||
@ -142,10 +141,9 @@ public class Citizens extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save all existing traits
|
// Save all existing traits
|
||||||
for (Trait trait : npc.getTraits()) {
|
for (Trait trait : npc.getTraits())
|
||||||
trait.save(root.getRelative(trait.getName()));
|
trait.save(root.getRelative(trait.getName()));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
saves.save();
|
saves.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -51,6 +51,7 @@ public class EventListen implements Listener {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
NPC npc = manager.getNPC(event.getEntity());
|
NPC npc = manager.getNPC(event.getEntity());
|
||||||
|
if (npc.getCharacter() != null)
|
||||||
npc.getCharacter().onRightClick(npc, (Player) event.getTarget());
|
npc.getCharacter().onRightClick(npc, (Player) event.getTarget());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ import org.bukkit.entity.Entity;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
public class CitizensNPCManager implements NPCManager {
|
public class CitizensNPCManager implements NPCManager {
|
||||||
private final ByIdArray<NPC> spawned = ByIdArray.create();
|
private final ByIdArray<NPC> spawned = new ByIdArray<NPC>();
|
||||||
private final ByIdArray<NPC> byID = ByIdArray.create();
|
private final ByIdArray<NPC> byID = new ByIdArray<NPC>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NPC createNPC(String name) {
|
public NPC createNPC(String name) {
|
||||||
@ -65,7 +65,7 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
@Override
|
@Override
|
||||||
public Collection<NPC> getNPCs(Class<? extends Trait> trait) {
|
public Collection<NPC> getNPCs(Class<? extends Trait> trait) {
|
||||||
List<NPC> npcs = new ArrayList<NPC>();
|
List<NPC> npcs = new ArrayList<NPC>();
|
||||||
for (NPC npc : byID) {
|
for (NPC npc : getAllNPCs()) {
|
||||||
if (npc.hasTrait(trait))
|
if (npc.hasTrait(trait))
|
||||||
npcs.add(npc);
|
npcs.add(npc);
|
||||||
}
|
}
|
||||||
@ -88,8 +88,6 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CraftNPC spawn(NPC npc, Location loc) {
|
public CraftNPC spawn(NPC npc, Location loc) {
|
||||||
if (spawned.contains(npc.getBukkitEntity().getEntityId()))
|
|
||||||
throw new IllegalStateException("The NPC with ID '" + npc.getId() + "' is already spawned.");
|
|
||||||
WorldServer ws = getWorldServer(loc.getWorld());
|
WorldServer ws = getWorldServer(loc.getWorld());
|
||||||
CraftNPC mcEntity = new CraftNPC(getMinecraftServer(ws.getServer()), ws, npc.getFullName(),
|
CraftNPC mcEntity = new CraftNPC(getMinecraftServer(ws.getServer()), ws, npc.getFullName(),
|
||||||
new ItemInWorldManager(ws));
|
new ItemInWorldManager(ws));
|
||||||
@ -103,8 +101,6 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void despawn(NPC npc) {
|
public void despawn(NPC npc) {
|
||||||
if (!spawned.contains(npc.getBukkitEntity().getEntityId()))
|
|
||||||
throw new IllegalStateException("The NPC with ID '" + npc.getId() + "' is already despawned.");
|
|
||||||
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
||||||
for (Player player : Bukkit.getOnlinePlayers())
|
for (Player player : Bukkit.getOnlinePlayers())
|
||||||
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet29DestroyEntity(mcEntity.id));
|
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet29DestroyEntity(mcEntity.id));
|
||||||
@ -128,8 +124,4 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
private MinecraftServer getMinecraftServer(Server server) {
|
private MinecraftServer getMinecraftServer(Server server) {
|
||||||
return ((CraftServer) server).getServer();
|
return ((CraftServer) server).getServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
|
||||||
return byID.size();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,10 +1,10 @@
|
|||||||
package net.citizensnpcs.test;
|
package net.citizensnpcs.test;
|
||||||
|
|
||||||
import org.junit.Test;
|
//import org.junit.Test;
|
||||||
|
|
||||||
public class StorageTest {
|
public class StorageTest {
|
||||||
|
|
||||||
@Test
|
// @Test
|
||||||
public void testYaml() {
|
public void testYaml() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ public class ByIdArray<T> implements Iterable<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void recalcLowest() {
|
private void recalcLowest() {
|
||||||
while (elementData.length > lowest && elementData[lowest++] == null)
|
while (elementData.length > lowest && highest > lowest && elementData[lowest++] == null)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,10 +129,6 @@ public class ByIdArray<T> implements Iterable<T> {
|
|||||||
elementData = Arrays.copyOf(elementData, highest + 1);
|
elementData = Arrays.copyOf(elementData, highest + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ByIdArray<T> create() {
|
|
||||||
return new ByIdArray<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean contains(int index) {
|
public boolean contains(int index) {
|
||||||
return elementData.length > index && elementData[index] != null;
|
return elementData.length > index && elementData[index] != null;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user