2010-11-27 03:33:28 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
2010-11-27 03:33:28 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.blocks;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2012-08-23 23:52:37 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
2012-12-25 18:09:24 +00:00
|
|
|
import com.sk89q.jnbt.ListTag;
|
2012-08-23 23:52:37 +00:00
|
|
|
import com.sk89q.jnbt.NBTUtils;
|
|
|
|
import com.sk89q.jnbt.ShortTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
|
|
|
import com.sk89q.worldedit.MobType;
|
|
|
|
import com.sk89q.worldedit.data.DataException;
|
2012-12-25 18:09:24 +00:00
|
|
|
import com.sk89q.worldedit.data.InvalidFormatException;
|
2010-11-27 03:33:28 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* A mob spawner block.
|
2010-11-27 03:33:28 +00:00
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
|
|
|
public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
|
2012-12-25 18:09:24 +00:00
|
|
|
|
2010-11-27 03:33:28 +00:00
|
|
|
private String mobType;
|
|
|
|
private short delay;
|
|
|
|
|
2012-12-25 18:09:24 +00:00
|
|
|
// advanced mob spawner features
|
|
|
|
private short spawnCount;
|
|
|
|
private short spawnRange;
|
|
|
|
private CompoundTag spawnData;
|
|
|
|
private ListTag spawnPotentials;
|
|
|
|
private short minSpawnDelay;
|
|
|
|
private short maxSpawnDelay;
|
|
|
|
private short maxNearbyEntities;
|
|
|
|
private short requiredPlayerRange;
|
|
|
|
|
2010-11-27 03:33:28 +00:00
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Construct the mob spawner block with a pig as the mob type.
|
2010-11-27 03:33:28 +00:00
|
|
|
*/
|
|
|
|
public MobSpawnerBlock() {
|
2011-09-03 16:54:20 +00:00
|
|
|
super(BlockID.MOB_SPAWNER);
|
2011-09-06 08:06:17 +00:00
|
|
|
this.mobType = MobType.PIG.getName();
|
2010-11-27 03:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Construct the mob spawner block with a given mob type.
|
2010-11-27 03:33:28 +00:00
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param mobType mob type
|
2010-11-27 03:33:28 +00:00
|
|
|
*/
|
|
|
|
public MobSpawnerBlock(String mobType) {
|
2011-09-03 16:54:20 +00:00
|
|
|
super(BlockID.MOB_SPAWNER);
|
2010-11-27 03:33:28 +00:00
|
|
|
this.mobType = mobType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Construct the mob spawner block with a specified data value.
|
2010-11-27 03:33:28 +00:00
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param data data value
|
2011-01-23 08:36:26 +00:00
|
|
|
*/
|
|
|
|
public MobSpawnerBlock(int data) {
|
2011-09-03 16:54:20 +00:00
|
|
|
super(BlockID.MOB_SPAWNER, data);
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the mob spawner block.
|
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param data data value
|
|
|
|
* @param mobType mob type
|
2010-11-27 03:33:28 +00:00
|
|
|
*/
|
|
|
|
public MobSpawnerBlock(int data, String mobType) {
|
2011-09-03 16:54:20 +00:00
|
|
|
super(BlockID.MOB_SPAWNER, data);
|
2010-11-27 03:33:28 +00:00
|
|
|
this.mobType = mobType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mob type.
|
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @return the mob type
|
2010-11-27 03:33:28 +00:00
|
|
|
*/
|
|
|
|
public String getMobType() {
|
|
|
|
return mobType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the mob type.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param mobType the mob type
|
2010-11-27 03:33:28 +00:00
|
|
|
*/
|
|
|
|
public void setMobType(String mobType) {
|
|
|
|
this.mobType = mobType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Get the spawn delay.
|
|
|
|
*
|
2010-11-27 03:33:28 +00:00
|
|
|
* @return the delay
|
|
|
|
*/
|
|
|
|
public short getDelay() {
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Set the spawn delay.
|
|
|
|
*
|
2010-11-27 03:33:28 +00:00
|
|
|
* @param delay the delay to set
|
|
|
|
*/
|
|
|
|
public void setDelay(short delay) {
|
|
|
|
this.delay = delay;
|
|
|
|
}
|
2012-08-23 23:52:37 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNbtData() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-27 03:33:28 +00:00
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public String getNbtId() {
|
2010-11-27 03:33:28 +00:00
|
|
|
return "MobSpawner";
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public CompoundTag getNbtData() {
|
2011-11-23 01:29:48 +00:00
|
|
|
Map<String, Tag> values = new HashMap<String, Tag>();
|
2010-11-27 03:33:28 +00:00
|
|
|
values.put("EntityId", new StringTag("EntityId", mobType));
|
2011-01-23 08:36:26 +00:00
|
|
|
values.put("Delay", new ShortTag("Delay", delay));
|
2012-12-25 18:09:24 +00:00
|
|
|
values.put("SpawnCount", new ShortTag("SpawnCount", spawnCount));
|
|
|
|
values.put("SpawnRange", new ShortTag("SpawnRange", spawnRange));
|
|
|
|
values.put("MinSpawnDelay", new ShortTag("MinSpawnDelay", minSpawnDelay));
|
|
|
|
values.put("MaxSpawnDelay", new ShortTag("MaxSpawnDelay", maxSpawnDelay));
|
|
|
|
values.put("MaxNearbyEntities", new ShortTag("MaxNearbyEntities", maxNearbyEntities));
|
|
|
|
values.put("RequiredPlayerRange", new ShortTag("RequiredPlayerRange", requiredPlayerRange));
|
|
|
|
values.put("SpawnData", new CompoundTag("SpawnData", spawnData.getValue()));
|
|
|
|
values.put("SpawnPotentials", new ListTag("SpawnPotentials", CompoundTag.class, spawnPotentials.getValue()));
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
return new CompoundTag(getNbtId(), values);
|
2010-11-27 03:33:28 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public void setNbtData(CompoundTag rootTag) throws DataException {
|
|
|
|
if (rootTag == null) {
|
2010-11-27 03:33:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
Map<String, Tag> values = rootTag.getValue();
|
|
|
|
|
2010-11-27 03:33:28 +00:00
|
|
|
Tag t = values.get("id");
|
2011-11-23 01:29:48 +00:00
|
|
|
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("MobSpawner")) {
|
2010-11-27 03:33:28 +00:00
|
|
|
throw new DataException("'MobSpawner' tile entity expected");
|
|
|
|
}
|
|
|
|
|
2012-03-30 04:11:49 +00:00
|
|
|
StringTag mobTypeTag = NBTUtils.getChildTag(values, "EntityId", StringTag.class);
|
|
|
|
ShortTag delayTag = NBTUtils.getChildTag(values, "Delay", ShortTag.class);
|
2010-11-27 03:33:28 +00:00
|
|
|
|
|
|
|
this.mobType = mobTypeTag.getValue();
|
|
|
|
this.delay = delayTag.getValue();
|
2012-12-25 18:09:24 +00:00
|
|
|
|
|
|
|
ShortTag spawnCountTag = null;
|
|
|
|
ShortTag spawnRangeTag = null;
|
|
|
|
ShortTag minSpawnDelayTag = null;
|
|
|
|
ShortTag maxSpawnDelayTag = null;
|
|
|
|
ShortTag maxNearbyEntitiesTag = null;
|
|
|
|
ShortTag requiredPlayerRangeTag = null;
|
|
|
|
ListTag spawnPotentialsTag = null;
|
|
|
|
CompoundTag spawnDataTag = null;
|
|
|
|
try {
|
|
|
|
spawnCountTag = NBTUtils.getChildTag(values, "SpawnCount", ShortTag.class);
|
|
|
|
spawnRangeTag = NBTUtils.getChildTag(values, "SpawnRange", ShortTag.class);
|
|
|
|
minSpawnDelayTag = NBTUtils.getChildTag(values, "MinSpawnDelay", ShortTag.class);
|
|
|
|
maxSpawnDelayTag = NBTUtils.getChildTag(values, "MaxSpawnDelay", ShortTag.class);
|
|
|
|
maxNearbyEntitiesTag = NBTUtils.getChildTag(values, "MaxNearbyEntities", ShortTag.class);
|
|
|
|
requiredPlayerRangeTag = NBTUtils.getChildTag(values, "RequiredPlayerRange", ShortTag.class);
|
|
|
|
spawnPotentialsTag = NBTUtils.getChildTag(values, "SpawnPotentials", ListTag.class);
|
|
|
|
spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class);
|
|
|
|
} catch (InvalidFormatException e) { // leave tag as null, handle later
|
|
|
|
}
|
|
|
|
|
|
|
|
this.spawnCount = spawnCountTag.getValue();
|
|
|
|
this.spawnRange = spawnRangeTag.getValue();
|
|
|
|
this.minSpawnDelay = minSpawnDelayTag.getValue();
|
|
|
|
this.maxSpawnDelay = maxSpawnDelayTag.getValue();
|
|
|
|
this.maxNearbyEntities = maxNearbyEntitiesTag.getValue();
|
|
|
|
this.requiredPlayerRange = requiredPlayerRangeTag.getValue();
|
|
|
|
this.spawnPotentials = new ListTag("SpawnPotentials", CompoundTag.class, spawnPotentialsTag.getValue());
|
|
|
|
this.spawnData = new CompoundTag("SpawnData", spawnDataTag.getValue());
|
|
|
|
|
2010-11-27 03:33:28 +00:00
|
|
|
}
|
|
|
|
}
|