Fix initialization NPE.

This commit is contained in:
Conrad S 2016-04-08 18:48:16 -04:00
parent 8a2efa2f0b
commit 05b31725a8

View File

@ -34,64 +34,64 @@ public class SiegeWeaponRepository extends MinecraftRepository
+ "lastFired LONG," + "lastFired LONG,"
+ "entities VARCHAR(200)," + "entities VARCHAR(200),"
+ "PRIMARY KEY (uniqueId));"; + "PRIMARY KEY (uniqueId));";
private static final String GET_WEAPON_BY_ID = "SELECT * FROM clansSiegeWeapons WHERE uniqueId=?;"; private static final String GET_WEAPON_BY_ID = "SELECT * FROM clansSiegeWeapons WHERE uniqueId=?;";
private static final String GET_WEAPONS_BY_CLAN = "SELECT * FROM clansSiegeWeapons WHERE ownerClan=?;"; private static final String GET_WEAPONS_BY_CLAN = "SELECT * FROM clansSiegeWeapons WHERE ownerClan=?;";
private static final String GET_WEAPONS_BY_SERVER = "SELECT * FROM clansSiegeWeapons WHERE serverId=?;"; private static final String GET_WEAPONS_BY_SERVER = "SELECT * FROM clansSiegeWeapons WHERE serverId=?;";
private static final String UPDATE_WEAPON = "UPDATE clansSiegeWeapons SET health=?,yaw=?,lastFired=? WHERE uniqueId=?;"; private static final String UPDATE_WEAPON = "UPDATE clansSiegeWeapons SET health=?,yaw=?,lastFired=? WHERE uniqueId=?;";
private static final String INSERT_WEAPON = "INSERT INTO clansSiegeWeapons VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"; private static final String INSERT_WEAPON = "INSERT INTO clansSiegeWeapons VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
private static final String DELETE_WEAPON = "DELETE FROM clansSiegeWeapons WHERE uniqueId=?;"; private static final String DELETE_WEAPON = "DELETE FROM clansSiegeWeapons WHERE uniqueId=?;";
private SiegeManager _siegeManager; private SiegeManager _siegeManager;
public SiegeWeaponRepository(JavaPlugin plugin, SiegeManager siegeManager) public SiegeWeaponRepository(JavaPlugin plugin, SiegeManager siegeManager)
{ {
super(plugin, DBPool.getAccount()); super(plugin, DBPool.getAccount());
_siegeManager = siegeManager; _siegeManager = siegeManager;
} }
public void deleteWeapon(final int uniqueId) public void deleteWeapon(final int uniqueId)
{ {
System.out.println("Siege Repo> Deleting weapon " + uniqueId); System.out.println("Siege Repo> Deleting weapon " + uniqueId);
_siegeManager.runAsync(() -> _siegeManager.runAsync(() ->
executeUpdate(DELETE_WEAPON, new ColumnInt("uniqueId", uniqueId)) executeUpdate(DELETE_WEAPON, new ColumnInt("uniqueId", uniqueId))
); );
} }
public void getWeaponById(final int uniqueId, final Callback<SiegeWeaponToken> callback) public void getWeaponById(final int uniqueId, final Callback<SiegeWeaponToken> callback)
{ {
_siegeManager.runAsync(() -> _siegeManager.runAsync(() ->
executeQuery(GET_WEAPON_BY_ID, resultSet -> { executeQuery(GET_WEAPON_BY_ID, resultSet -> {
SiegeWeaponToken token = new SiegeWeaponToken(); SiegeWeaponToken token = new SiegeWeaponToken();
resultSet.next(); resultSet.next();
load(token, resultSet); load(token, resultSet);
callback.run(token); callback.run(token);
}, new ColumnInt("uniqueId", uniqueId)) }, new ColumnInt("uniqueId", uniqueId))
); );
} }
public void getWeaponsByServer(final int serverId, final Callback<List<SiegeWeaponToken>> callback) public void getWeaponsByServer(final int serverId, final Callback<List<SiegeWeaponToken>> callback)
{ {
_siegeManager.runAsync(() -> _siegeManager.runAsync(() ->
executeQuery(GET_WEAPONS_BY_SERVER, resultSet -> { executeQuery(GET_WEAPONS_BY_SERVER, resultSet -> {
List<SiegeWeaponToken> tokens = Lists.newArrayList(); List<SiegeWeaponToken> tokens = Lists.newArrayList();
while (resultSet.next()) while (resultSet.next())
{ {
SiegeWeaponToken token = new SiegeWeaponToken(); SiegeWeaponToken token = new SiegeWeaponToken();
load(token, resultSet); load(token, resultSet);
tokens.add(token); tokens.add(token);
} }
callback.run(tokens); callback.run(tokens);
}, new ColumnInt("serverId", serverId)) }, new ColumnInt("serverId", serverId))
); );
@ -102,21 +102,21 @@ public class SiegeWeaponRepository extends MinecraftRepository
_siegeManager.runAsync(() -> _siegeManager.runAsync(() ->
executeQuery(GET_WEAPONS_BY_CLAN, resultSet -> { executeQuery(GET_WEAPONS_BY_CLAN, resultSet -> {
List<SiegeWeaponToken> tokens = Lists.newArrayList(); List<SiegeWeaponToken> tokens = Lists.newArrayList();
while (resultSet.next()) while (resultSet.next())
{ {
SiegeWeaponToken token = new SiegeWeaponToken(); SiegeWeaponToken token = new SiegeWeaponToken();
load(token, resultSet); load(token, resultSet);
tokens.add(token); tokens.add(token);
} }
callback.run(tokens); callback.run(tokens);
}, new ColumnInt("ownerClan", clan.getId())) }, new ColumnInt("ownerClan", clan.getId()))
); );
} }
private void load(SiegeWeaponToken token, ResultSet columns) throws SQLException private void load(SiegeWeaponToken token, ResultSet columns) throws SQLException
{ {
token.UniqueId = columns.getInt("uniqueId"); token.UniqueId = columns.getInt("uniqueId");
@ -127,14 +127,14 @@ public class SiegeWeaponRepository extends MinecraftRepository
token.Yaw = columns.getShort("yaw"); token.Yaw = columns.getShort("yaw");
token.LastFired = columns.getTimestamp("lastFired").getTime(); token.LastFired = columns.getTimestamp("lastFired").getTime();
token.Entities = decodeEntities(columns.getString("entities")); token.Entities = decodeEntities(columns.getString("entities"));
System.out.println("Siege Repo> Loaded weapon " + token.UniqueId); System.out.println("Siege Repo> Loaded weapon " + token.UniqueId);
} }
public void updateWeapon(SiegeWeaponToken token) public void updateWeapon(SiegeWeaponToken token)
{ {
System.out.println("Siege Repo> Updating weapon " + token.UniqueId); System.out.println("Siege Repo> Updating weapon " + token.UniqueId);
_siegeManager.runAsync(() -> _siegeManager.runAsync(() ->
executeUpdate(UPDATE_WEAPON, executeUpdate(UPDATE_WEAPON,
new ColumnInt("health", token.Health), new ColumnInt("health", token.Health),
@ -147,9 +147,9 @@ public class SiegeWeaponRepository extends MinecraftRepository
public void insertWeapon(SiegeWeaponToken token) public void insertWeapon(SiegeWeaponToken token)
{ {
System.out.println("Siege Repo> Inserting new weapon " + token.UniqueId); System.out.println("Siege Repo> Inserting new weapon " + token.UniqueId);
_siegeManager.runAsync(() -> _siegeManager.runAsync(() ->
executeUpdate(INSERT_WEAPON, executeUpdate(INSERT_WEAPON,
new ColumnInt("uniqueId", token.UniqueId), new ColumnInt("uniqueId", token.UniqueId),
new ColumnInt("serverId", _siegeManager.getClansManager().getServerId()), new ColumnInt("serverId", _siegeManager.getClansManager().getServerId()),
new ColumnVarChar("location", 30, UtilWorld.locToStr(token.Location)), new ColumnVarChar("location", 30, UtilWorld.locToStr(token.Location)),
@ -161,11 +161,11 @@ public class SiegeWeaponRepository extends MinecraftRepository
new ColumnVarChar("entities", 100, encodeEntities(token.Entities))) new ColumnVarChar("entities", 100, encodeEntities(token.Entities)))
); );
} }
private String encodeEntities(Map<String, String> entities) private String encodeEntities(Map<String, String> entities)
{ {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
int l = 0; int l = 0;
for (String name : entities.keySet()) for (String name : entities.keySet())
{ {
@ -173,32 +173,30 @@ public class SiegeWeaponRepository extends MinecraftRepository
{ {
builder.append(","); builder.append(",");
} }
builder.append(name + ":" + entities.get(name)); builder.append(name + ":" + entities.get(name));
l++; l++;
} }
return builder.toString(); return builder.toString();
} }
private Map<String, String> decodeEntities(String data) private Map<String, String> decodeEntities(String data)
{ {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
for (String entries : data.split(",")) for (String entries : data.split(","))
{ {
map.put(entries.split(":")[0], entries.split(":")[1]); map.put(entries.split(":")[0], entries.split(":")[1]);
} }
return map; return map;
} }
@Override @Override
protected void initialize() protected void initialize()
{ {
_siegeManager.runAsync(() -> executeUpdate(CREATE);
executeUpdate(CREATE)
);
} }
@Override @Override