Commit 89d80ae8 by 李进峰

repair null pointer question!

parent b76cae13
......@@ -23,14 +23,14 @@ public class GameEnv implements Serializable{
// 角色快速查找数据
public Map<String, NpcClass> npcClassesMap = new HashMap<String, NpcClass>();
public Map<String, NpcInstance> npcInstancesMap = new HashMap<String, NpcInstance>();
// 玩家跟随的角色列表
public Map<String, NpcInstance> followNpcesMap = new HashMap<String, NpcInstance>();
// 物品快速查找数据
public Map<String, GoodsClass> goodsClassesMap = new HashMap<String, GoodsClass>();
public Map<String, GoodsInstance> goodsInstancesMap = new HashMap<String, GoodsInstance>();
// 全局变量快速查找数据
public Map<String, String> globalAttrsMap = new HashMap<String, String>();
// 地点相关快速查找数据
......@@ -39,16 +39,16 @@ public class GameEnv implements Serializable{
public Map<String, Place> placeLinkDirectGoesMap = new HashMap<String, Place>();
// goGlag=2 任意可达到
public Map<String, Place> placeNoLinkDirectGoesMap = new HashMap<String, Place>();
// 二维联通地图
public byte[][] mapUnitMatrix;
// 联通图助手
public Map<String, Integer> placeNameIndexMap = new HashMap<String, Integer>();
public Map<Integer, String> indexPlaceNameMap = new HashMap<Integer, String>();
// 数据别名集合
public Map<String, Nickname> nicknamesMap = new HashMap<String, Nickname>();
// 获取别名的助手,帮助创建
private Nickname getNicknameUtil(String nickname) {
Nickname nicknameObject = this.nicknamesMap.get(nickname);
......@@ -58,17 +58,17 @@ public class GameEnv implements Serializable{
this.nicknamesMap.put(nickname, nicknameObject);
return nicknameObject;
}
@SuppressWarnings("unchecked")
public GameEnv(String botName) throws Exception {
// 从数据库获取环境数据的json字符串
String jsonString = DBUtil.queryEnvJsonByBotName(botName);
// 从文件中获取json字符串
// String jsonString = Util.getJsonString("./data/195.json");
// System.out.println(jsonString);
Map<String, Object> gameProjectMap = JSON.parseObject(jsonString);
if(gameProjectMap!=null && !gameProjectMap.isEmpty()) {
// 地图数据处理
......@@ -88,10 +88,10 @@ public class GameEnv implements Serializable{
this.placeNameIndexMap.put(placeName, new Integer(i));
this.indexPlaceNameMap.put(new Integer(i), placeName);
}
// 定义的单位矩阵
this.mapUnitMatrix = new byte[this.placesMap.size()][this.placesMap.size()];
// 可去地点、别名、图的数据完善
for(String placeName:placeTmpMap.keySet()) {
Map<String,Object> placeMap = (Map<String,Object>)placeTmpMap.get(placeName);
......@@ -111,6 +111,7 @@ public class GameEnv implements Serializable{
// 别名
List<String> nicknames = (List<String>)placeMap.get("nicknames");
for(int i=0; i<nicknames.size(); i++) {
System.out.println(nicknames.get(i));
Nickname nickname = this.getNicknameUtil(nicknames.get(i));
Map<String,Place> nicknamePlaceMap = nickname.getPlaceMap();
nicknamePlaceMap.put(place.getName(), place);
......@@ -118,7 +119,7 @@ public class GameEnv implements Serializable{
place.setNickname(nicknames);
place.setName(placeName);
}
// 格式化图显示结构
System.out.println("\n\n");
System.out.print("\t\t");
......@@ -134,7 +135,7 @@ public class GameEnv implements Serializable{
System.out.print("\n\n");
}
}
/*************************************** 角色 **********************************/
Map<String, Object> npcDataTmpMap = (Map<String, Object>)gameProjectMap.get("npc");
// 角色类
......@@ -154,7 +155,7 @@ public class GameEnv implements Serializable{
Map<String, String> npcInstanceAttr = (Map<String, String>)npcInstanceTmpMap.get(npcInstanceName);
NpcInstance npcInstance = new NpcInstance();
npcInstance.setName(npcInstanceName);
// 角色类
NpcClass npcClass = this.npcClassesMap.get(npcInstanceAttr.get("className"));
npcInstance.setNpcClass(npcClass);
......@@ -167,7 +168,7 @@ public class GameEnv implements Serializable{
Map<String, NpcInstance> npcInstancesPlaceMap = place.getNpcInstances();
npcInstancesPlaceMap.put(npcInstanceName, npcInstance);
npcInstance.setPlace(place);
// 别名
List<String> nicknames = (List<String>)((Map<String, Object>)npcInstanceTmpMap.get(npcInstanceName)).get("nicknames");
for(int i=0; i<nicknames.size(); i++) {
......@@ -183,8 +184,8 @@ public class GameEnv implements Serializable{
npcInstance.setAttrs((Map<String, String>)npcInstanceAttr);
this.npcInstancesMap.put(npcInstanceName, npcInstance);
}
/************************************* 物品 **********************************/
Map<String, Object> goodsDataTmpMap = (Map<String,Object>)gameProjectMap.get("goods");
// 物品类
......@@ -198,14 +199,14 @@ public class GameEnv implements Serializable{
goodsClass.setAttrs(goodsClassAttr);
this.goodsClassesMap.put(goodsClassName, goodsClass);
}
// 物品实例
Map<String, Object> goodsInstanceTmpMap = (Map<String, Object>)goodsDataTmpMap.get("instance");
for(String goodsInstanceName:goodsInstanceTmpMap.keySet()){
Map<String, String> goodsInstanceAttr = (Map<String, String>)goodsInstanceTmpMap.get(goodsInstanceName);
GoodsInstance goodsInstance = new GoodsInstance();
goodsInstance.setName(goodsInstanceName);
// 物品类
GoodsClass goodsClass = this.goodsClassesMap.get(goodsInstanceAttr.get("className"));
goodsInstance.setGoodsClass(goodsClass);
......@@ -218,7 +219,7 @@ public class GameEnv implements Serializable{
Map<String, GoodsInstance> goodsInstancesPlaceMap = place.getGoodsInstances();
goodsInstancesPlaceMap.put(goodsInstanceName, goodsInstance);
goodsInstance.setPlace(place);
// 别名
List<String> nicknames = (List<String>)(((Map<String, Object>)goodsInstanceTmpMap.get(goodsInstanceName)).get("nicknames"));
for(int i=0; i<nicknames.size(); i++) {
......@@ -242,7 +243,7 @@ public class GameEnv implements Serializable{
goodsInstance.setAttrs((Map<String, String>)goodsInstanceAttr);
this.goodsInstancesMap.put(goodsInstanceName, goodsInstance);
}
// 全局变量
this.globalAttrsMap = (Map<String,String>)gameProjectMap.get("globalAttr");
}
......@@ -423,24 +424,24 @@ public class GameEnv implements Serializable{
if(place == null) {
return new Integer(5);
}
// 地点名称相同
if(placeA.equals(placeB)) {
return 0;
}
// 地点名和地点别名相同
if(this.queryNickname(placeB, "place").indexOf(placeA) != -1) {
return 0;
}
// 任意可达
for(String placeName : this.placeNoLinkDirectGoesMap.keySet()) {
if(placeName.equals(placeB) || this.queryNickname(placeB, "place").indexOf(placeName) != -1) {
return 4;
return 4;
}
}
// 相邻可以去的地点
int x = this.placeNameIndexMap.get(placeA);
for(int y=0; y<this.mapUnitMatrix[x].length; y++) {
......@@ -451,7 +452,7 @@ public class GameEnv implements Serializable{
}
}
}
// 联通不相邻可直达
for(String placeName:this.placeLinkDirectGoesMap.keySet()) {
if(placeName.equals(placeB) || this.queryNickname(placeB, "place").indexOf(placeName) != -1) {
......@@ -461,7 +462,7 @@ public class GameEnv implements Serializable{
}
}
}
// 联通不相邻不可以直达
for(String placeName:this.placesMap.keySet()) {
if(!this.placeLinkDirectGoesMap.keySet().contains(placeName) && !this.placeNoLinkDirectGoesMap.keySet().contains(placeName)) {
......@@ -473,7 +474,7 @@ public class GameEnv implements Serializable{
}
}
}
// 既不相邻,也不能到达
return new Integer(5);
}
......@@ -516,7 +517,7 @@ public class GameEnv implements Serializable{
}
return null;
}
// 通过别名查询当前地点最近的npc
public String getNpcNameByNickname(String curPlaceName, String nickname) {
// 寻找两层
......@@ -535,7 +536,7 @@ public class GameEnv implements Serializable{
}
return nickname;
}
// 通过别名查询当前地点最近的物品
public String getGoodsNameByNickname(String curPlaceName, String nickname) {
// 寻找两层
......@@ -554,7 +555,7 @@ public class GameEnv implements Serializable{
}
return nickname;
}
/*********************************通过npc获取物品列表**********************************/
// 通过npcName获取所有物品名称列表
public List<String> queryGoodsByNpcName(String npcName){
......@@ -601,7 +602,7 @@ public class GameEnv implements Serializable{
return null;
}
}
// 查询指定物品的属性值
public String queryGoodsAttrByGoodsName(String str1, String str2){
GoodsInstance goodsInstance = this.findGoodsInstanceByNameOrNickname(str1);
......@@ -643,7 +644,7 @@ public class GameEnv implements Serializable{
public String queryGoodsSiteByGoodsName(String goodsName) {
return this.goodsInstancesMap.get(goodsName).getPlace().getName();
}
/**************************************npc*****************************************/
// 通过地点和类查询所有的NPC实例
public List<String> queryNpcByClassAndSite(String site, String className){
......@@ -657,7 +658,7 @@ public class GameEnv implements Serializable{
}
}
}
return npcNames;
}
// 查询指定的NPC
......@@ -714,69 +715,78 @@ public class GameEnv implements Serializable{
String[] names = name.split("_");
if(names.length == 4){
if(names[0].equals("goods")){
Map<String, GoodsInstance> goodsInstancesByClass = this.goodsClassesMap.get(names[1]).getGoodsInstances();
GoodsInstance goodsInstance = this.goodsInstancesMap.get(names[2]);
if(goodsInstancesByClass.get(goodsInstance.getName())!=null) {
Map<String, String> goodsInstanceAttrsMap = goodsInstance.getAttrs();
if(names[3].equals("所在地点")) {
Place placeOrigin = goodsInstance.getPlace();
Map<String, GoodsInstance> goodsInstancesMap = placeOrigin.getGoodsInstances();
goodsInstancesMap.remove(goodsInstance.getName());
Place place= this.placesMap.get(value);
goodsInstance.setPlace(place);
goodsInstancesMap = place.getGoodsInstances();
goodsInstancesMap.put(goodsInstance.getName(), goodsInstance);
return true;
}else if(names[3].equals("所属角色")) {
NpcInstance npcInstance= goodsInstance.getNpcInstance();
if(npcInstance != null) {
Map<String, GoodsInstance> goodsInstancesMap = npcInstance.getGoodsInstances();
goodsInstancesMap.remove(goodsInstance.getName());
}
npcInstance = this.npcInstancesMap.get(value);
goodsInstance.setNpcInstance(npcInstance);
Map<String,GoodsInstance> goodsInstances = npcInstance.getGoodsInstances();
goodsInstances.put(goodsInstance.getName(), goodsInstance);
npcInstance.setGoodsInstances(goodsInstances);
return true;
}else {
goodsInstanceAttrsMap.put(names[3], value);
return true;
}
if(this.goodsClassesMap.get(names[1])!=null) {
Map<String, GoodsInstance> goodsInstancesByClass = this.goodsClassesMap.get(names[1]).getGoodsInstances();
GoodsInstance goodsInstance = this.goodsInstancesMap.get(names[2]);
if(goodsInstancesByClass.get(goodsInstance.getName())!=null) {
Map<String, String> goodsInstanceAttrsMap = goodsInstance.getAttrs();
if(names[3].equals("所在地点")) {
Place placeOrigin = goodsInstance.getPlace();
Map<String, GoodsInstance> goodsInstancesMap = placeOrigin.getGoodsInstances();
goodsInstancesMap.remove(goodsInstance.getName());
Place place= this.placesMap.get(value);
goodsInstance.setPlace(place);
goodsInstancesMap = place.getGoodsInstances();
goodsInstancesMap.put(goodsInstance.getName(), goodsInstance);
return true;
}else if(names[3].equals("所属角色")) {
NpcInstance npcInstance= goodsInstance.getNpcInstance();
if(npcInstance != null) {
Map<String, GoodsInstance> goodsInstancesMap = npcInstance.getGoodsInstances();
goodsInstancesMap.remove(goodsInstance.getName());
}
npcInstance = this.npcInstancesMap.get(value);
goodsInstance.setNpcInstance(npcInstance);
Map<String,GoodsInstance> goodsInstances = npcInstance.getGoodsInstances();
goodsInstances.put(goodsInstance.getName(), goodsInstance);
npcInstance.setGoodsInstances(goodsInstances);
return true;
}else {
goodsInstanceAttrsMap.put(names[3], value);
return true;
}
}else {
return false;
}
}else {
return false;
}
}else if(names[0].equals("npc")) {
NpcInstance npcInstance = this.npcInstancesMap.get(names[2]);
if(npcInstance.getNpcClass().getName().equals(names[1])) {
Map<String, String> npcInstanceAttrMap = npcInstance.getAttrs();
if(names[3].equals("跟随玩家")) {
if(value.equals("是")) {
this.followNpcesMap.put(npcInstance.getName(), npcInstance);
}else if(value.equals("否")) {
this.followNpcesMap.remove(npcInstance.getName());
}else {
return true;
}
}else if(names[3].equals("所在地点")) {
Place placeOrigin = npcInstance.getPlace();
Map<String, NpcInstance> npcInstancesMap = placeOrigin.getNpcInstances();
npcInstancesMap.remove(npcInstance.getName());
Place place= this.placesMap.get(value);
npcInstance.setPlace(place);
npcInstancesMap = place.getNpcInstances();
npcInstancesMap.put(npcInstance.getName(), npcInstance);
// 更新跟随的NPC物品的位置
return true;
}else {
npcInstanceAttrMap.put(names[3], value);
return true;
}
if(npcInstance!=null) {
if(npcInstance.getNpcClass().getName().equals(names[1])) {
Map<String, String> npcInstanceAttrMap = npcInstance.getAttrs();
if(names[3].equals("跟随玩家")) {
if(value.equals("是")) {
this.followNpcesMap.put(npcInstance.getName(), npcInstance);
}else if(value.equals("否")) {
this.followNpcesMap.remove(npcInstance.getName());
}else {
return true;
}
}else if(names[3].equals("所在地点")) {
Place placeOrigin = npcInstance.getPlace();
Map<String, NpcInstance> npcInstancesMap = placeOrigin.getNpcInstances();
npcInstancesMap.remove(npcInstance.getName());
Place place= this.placesMap.get(value);
npcInstance.setPlace(place);
npcInstancesMap = place.getNpcInstances();
npcInstancesMap.put(npcInstance.getName(), npcInstance);
// 更新跟随的NPC物品的位置
return true;
}else {
npcInstanceAttrMap.put(names[3], value);
return true;
}
}else {
return false;
}
}else {
return false;
}
}else if(names.length == 3 && names[0].equals("place")) {
Place place = this.placesMap.get(names[1]);
Place place = this.placesMap.get(names[1]);
if(place!=null) {
if(names[2].equals("到达条件")) {
place.setGoFlag(Integer.parseInt(value));
if(place.getGoFlag().equals(1)) {
......@@ -812,6 +822,9 @@ public class GameEnv implements Serializable{
}else {
return false;
}
}else {
return false;
}
}else if(names[0].equals("globalVariable")) {
this.globalAttrsMap.put(names[1], value);
}
......@@ -840,6 +853,9 @@ public class GameEnv implements Serializable{
}
// 查询玩家
public String queryPlayerName() {
return this.npcClassesMap.get("玩家").getNpcInstances().get(0).getName();
for(String name:this.npcClassesMap.get("玩家").getNpcInstances().keySet()) {
return name;
}
return "";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment