Fix initialization NPE.

This commit is contained in:
Conrad S 2016-04-08 18:48:16 -04:00
parent 8a2efa2f0b
commit 05b31725a8
1 changed files with 34 additions and 36 deletions

View File

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