Persistence for item NPC ids and item frame NPC ids

This commit is contained in:
fullwall 2014-04-11 14:02:22 +08:00
parent 81afd8bada
commit 960b54d7d3
4 changed files with 45 additions and 7 deletions

View File

@ -33,6 +33,8 @@ import net.citizensnpcs.npc.EntityControllers;
import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.npc.Template;
import net.citizensnpcs.npc.entity.nonliving.FallingBlockController.FallingBlockNPC;
import net.citizensnpcs.npc.entity.nonliving.ItemController.ItemNPC;
import net.citizensnpcs.npc.entity.nonliving.ItemFrameController.ItemFrameNPC;
import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.Controllable;
@ -584,15 +586,18 @@ public class NPCCommands {
Material mat = Material.matchMaterial(args.getString(1));
if (mat == null)
throw new CommandException(Messages.UNKNOWN_MATERIAL);
int data = args.getInteger(2, 0);
switch (npc.getEntity().getType()) {
case DROPPED_ITEM:
((org.bukkit.entity.Item) npc.getEntity()).getItemStack().setType(mat);
((ItemNPC) npc.getEntity()).setType(mat, data);
break;
case ITEM_FRAME:
((ItemFrame) npc.getEntity()).getItem().setType(mat);
((ItemFrameNPC) npc.getEntity()).setType(mat, data);
break;
case FALLING_BLOCK:
((FallingBlockNPC) npc.getEntity()).setType(mat, args.argsLength() > 2 ? args.getInteger(2) : 0);
((FallingBlockNPC) npc.getEntity()).setType(mat, data);
break;
default:
break;

View File

@ -33,9 +33,11 @@ public class FallingBlockController extends AbstractEntityController {
protected Entity createEntity(Location at, NPC npc) {
WorldServer ws = ((CraftWorld) at.getWorld()).getHandle();
Block id = Blocks.STONE;
int data = npc.data().get("falling-block-data", 0);
if (npc.data().has("falling-block-id"))
id = CraftMagicNumbers.getBlock(Material.getMaterial(npc.data().<String> get("falling-block-id")));
int data = npc.data().get(NPC.ITEM_DATA_METADATA, npc.data().get("falling-block-data", 0));
if (npc.data().has("falling-block-id") || npc.data().has(NPC.ITEM_ID_METADATA)) {
id = CraftMagicNumbers.getBlock(Material.getMaterial(npc.data().<String> get(NPC.ITEM_ID_METADATA,
npc.data().<String> get("falling-block-id"))));
}
final EntityFallingBlockNPC handle = new EntityFallingBlockNPC(ws, npc, at.getX(), at.getY(), at.getZ(), id,
data);
return handle.getBukkitEntity();
@ -140,8 +142,8 @@ public class FallingBlockController extends AbstractEntityController {
}
public void setType(Material material, int data) {
npc.data().setPersistent("falling-block-id", material.name());
npc.data().setPersistent("falling-block-data", 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());

View File

@ -32,8 +32,13 @@ public class ItemController extends AbstractEntityController {
@Override
protected Entity createEntity(Location at, NPC npc) {
WorldServer ws = ((CraftWorld) at.getWorld()).getHandle();
Material id = Material.STONE;
int data = npc.data().get(NPC.ITEM_DATA_METADATA, npc.data().get("falling-block-data", 0));
if (npc.data().has(NPC.ITEM_ID_METADATA)) {
id = Material.getMaterial(npc.data().<String> get(NPC.ITEM_ID_METADATA));
}
final EntityItemNPC handle = new EntityItemNPC(ws, npc, at.getX(), at.getY(), at.getZ(),
CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE)));
CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(id, 1, (short) data)));
return handle.getBukkitEntity();
}
@ -129,5 +134,14 @@ public class ItemController extends AbstractEntityController {
public NPC getNPC() {
return npc;
}
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());
}
}
}
}

View File

@ -10,6 +10,7 @@ import net.minecraft.server.v1_7_R2.EntityItemFrame;
import net.minecraft.server.v1_7_R2.World;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_7_R2.CraftServer;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftItemFrame;
@ -107,11 +108,27 @@ public class ItemFrameController extends MobEntityController {
public ItemFrameNPC(EntityItemFrameNPC entity) {
super((CraftServer) Bukkit.getServer(), entity);
this.npc = entity.npc;
Material id = Material.STONE;
int data = npc.data().get(NPC.ITEM_DATA_METADATA, npc.data().get("falling-block-data", 0));
if (npc.data().has(NPC.ITEM_ID_METADATA)) {
id = Material.getMaterial(npc.data().<String> get(NPC.ITEM_ID_METADATA));
}
getItem().setType(id);
getItem().setDurability((short) data);
}
@Override
public NPC getNPC() {
return npc;
}
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());
}
}
}
}