Optimize Clan Banners (#221)

* Optimize BannerRepository.java

* Optimize banner loading in BannerManager.java

* Optimize banner loading in ClansManager.java

* Remove unnecessary runAsync

* Optimize Siege Weapon storage

* Forgot a ! in my while loop
This commit is contained in:
Alexander Meech 2016-09-29 23:04:19 -04:00 committed by Shaun Bennett
parent 7d4c817856
commit 0caa84f703
5 changed files with 101 additions and 27 deletions

View File

@ -398,8 +398,9 @@ public class ClansManager extends MiniClientPlugin<ClientClan>implements IRelati
for (ClanToken token : _clanDataAccess.getRepository().retrieveClans())
{
loadClan(token);
loadClan(token, false);
}
_bannerManager.loadBanners(this);
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "Replay|Restrict");
@ -475,7 +476,7 @@ public class ClansManager extends MiniClientPlugin<ClientClan>implements IRelati
addCommand(new SpeedCommand(this));
}
public void loadClan(ClanToken clanToken)
public void loadClan(ClanToken clanToken, boolean loadBanner)
{
ClanInfo clan = new ClanInfo(this, clanToken);
_clanMap.put(clanToken.Name, clan);
@ -489,7 +490,13 @@ public class ClansManager extends MiniClientPlugin<ClientClan>implements IRelati
for (ClanTerritoryToken territoryToken : clanToken.Territories)
_claimMap.put(territoryToken.Chunk, new ClanTerritory(territoryToken));
_bannerManager.loadBanner(clan);
if (loadBanner)
_bannerManager.loadBanner(clan);
}
public void loadClan(ClanToken clanToken)
{
loadClan(clanToken, true);
}
public DisguiseManager getDisguiseManager()

View File

@ -83,6 +83,15 @@ public class BannerManager extends MiniPlugin
_repo.loadBanner(LoadedBanners, clan);
}
/**
* Loads all banners for this clans server
* @param manager The Clans Manager instance triggering this load
*/
public void loadBanners(ClansManager manager)
{
_repo.loadBanners(LoadedBanners, manager);
}
/**
* Saves a banner to the database
* @param banner The banner to save
@ -238,4 +247,4 @@ public class BannerManager extends MiniPlugin
event.getItemDrop().remove();
}
}
}
}

View File

@ -7,6 +7,7 @@ import java.util.List;
import mineplex.core.database.MinecraftRepository;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansManager;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnVarChar;
@ -26,6 +27,7 @@ public class BannerRepository extends MinecraftRepository
+ "PRIMARY KEY (clanId));";
private static final String GET_BANNER_BY_CLAN = "SELECT * FROM clanBanners WHERE clanId=? LIMIT 1;";
private static final String GET_BANNERS_BY_SERVER = "SELECT clans.name, clanBanners.baseColor, clanBanners.patterns FROM clans INNER JOIN clanBanners ON clans.id=clanBanners.clanId AND clans.serverId=?;";
private static final String INSERT_BANNER = "INSERT INTO clanBanners (clanId, baseColor, patterns) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE baseColor=VALUES(baseColor), patterns=VALUES(patterns);";
private static final String DELETE_BANNER = "DELETE FROM clanBanners WHERE clanId=?;";
@ -82,6 +84,52 @@ public class BannerRepository extends MinecraftRepository
});
}
/**
* Loads all banners for a certain clans server into a given hashmap
* @param map The hashmap to load the banner into
* @param clan The clan whose banner to fetch
*/
public void loadBanners(final HashMap<String, ClanBanner> map, ClansManager clanManager)
{
_bannerManager.runAsync(() ->
{
executeQuery(GET_BANNERS_BY_SERVER, resultSet ->
{
while(resultSet.next())
{
String clanName = resultSet.getString("name");
DyeColor baseColor = DyeColor.valueOf(resultSet.getString("baseColor"));
List<String> patternStrs = Arrays.asList(resultSet.getString("patterns").split("/"));
LinkedList<BannerPattern> patterns = new LinkedList<>();
int layer = 1;
for (String patternStr : patternStrs)
{
if (patternStr.equalsIgnoreCase("Blank"))
{
patterns.add(new BannerPattern(layer));
}
else
{
try
{
DyeColor patternColor = DyeColor.valueOf(patternStr.split(",")[0]);
PatternType patternType = PatternType.valueOf(patternStr.split(",")[1]);
patterns.add(new BannerPattern(layer, patternColor, patternType));
}
catch (Exception e)
{
e.printStackTrace();
patterns.add(new BannerPattern(layer));
}
}
layer++;
}
map.put(clanName, new ClanBanner(_bannerManager, clanManager.getClanMap().get(clanName), baseColor, patterns));
}
}, new ColumnInt("serverId", clanManager.getServerId()));
});
}
/**
* Saves a banner into the database
* @param banner The banner to save
@ -118,9 +166,9 @@ public class BannerRepository extends MinecraftRepository
@Override
protected void initialize()
{
executeUpdate(CREATE);
//executeUpdate(CREATE);
}
@Override
protected void update() {}
}
}

View File

@ -1,5 +1,7 @@
package mineplex.game.clans.clans.siege;
import java.util.Stack;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.F;
import mineplex.core.common.util.NautHashMap;
@ -43,6 +45,8 @@ public class SiegeManager extends MiniPlugin
private SiegeWeaponRepository _repository;
private long _lastDatabaseSave = -1L;
public SiegeManager(ClansManager clans)
{
super("Siege Manager", clans.getPlugin());
@ -136,8 +140,12 @@ public class SiegeManager extends MiniPlugin
if (event.getType() == UpdateType.SLOW)
{
_outpostManager.saveOutposts();
saveSiegeWeapons();
if (UtilTime.elapsed(_lastDatabaseSave, 10000))
{
_lastDatabaseSave = System.currentTimeMillis();
_outpostManager.saveOutposts();
saveSiegeWeapons();
}
for (Entity entity : Spawn.getSpawnWorld().getEntitiesByClass(ArmorStand.class))
{
@ -184,17 +192,22 @@ public class SiegeManager extends MiniPlugin
private void saveSiegeWeapons()
{
final Stack<Runnable> queue = new Stack<>();
for (final SiegeWeapon weapon : LiveSiegeWeapons.values())
{
final SiegeWeaponToken token = weapon.toToken();
if (UnsyncedSiegeWeapons.containsKey(Integer.valueOf(token.UniqueId)))
continue;
runAsync(() -> {
_repository.updateWeapon(token);
});
queue.push(() -> _repository.updateWeapon(token));
}
runAsync(() -> {
while (!queue.isEmpty())
queue.pop().run();
});
}
@EventHandler
@ -307,5 +320,4 @@ public class SiegeManager extends MiniPlugin
{
return _repository;
}
}

View File

@ -146,25 +146,23 @@ 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,
new ColumnInt("uniqueId", token.UniqueId),
new ColumnInt("serverId", _siegeManager.getClansManager().getServerId()),
new ColumnVarChar("location", 30, UtilWorld.locToStr(token.Location)),
new ColumnInt("ownerClan", token.OwnerClan.getId()),
new ColumnInt("weaponType", token.WeaponType),
new ColumnInt("health", token.Health),
new ColumnInt("yaw", token.Yaw),
new ColumnTimestamp("lastFired", new Timestamp(token.LastFired)),
new ColumnVarChar("entities", 100, ""))
);
executeUpdate(INSERT_WEAPON,
new ColumnInt("uniqueId", token.UniqueId),
new ColumnInt("serverId", _siegeManager.getClansManager().getServerId()),
new ColumnVarChar("location", 30, UtilWorld.locToStr(token.Location)),
new ColumnInt("ownerClan", token.OwnerClan.getId()),
new ColumnInt("weaponType", token.WeaponType),
new ColumnInt("health", token.Health),
new ColumnInt("yaw", token.Yaw),
new ColumnTimestamp("lastFired", new Timestamp(token.LastFired)),
new ColumnVarChar("entities", 100, ""));
}
@Override
protected void initialize()
{
executeUpdate(CREATE);
//executeUpdate(CREATE);
}
@Override