From 838776c471c7ba5a6dc588f365dae80a564cb135 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Wed, 18 Oct 2023 11:30:22 -0400 Subject: [PATCH] Method made static and moved to manager. --- src/engine/devcmd/cmds/MakeBaneCmd.java | 2 +- src/engine/devcmd/cmds/SetRankCmd.java | 7 +- src/engine/gameManager/BuildingManager.java | 82 +++++++++++++++- src/engine/jobs/UpgradeBuildingJob.java | 3 +- .../handlers/DestroyBuildingHandler.java | 2 +- .../handlers/ObjectActionMsgHandler.java | 2 +- src/engine/objects/Building.java | 98 ++----------------- src/engine/workthreads/DestroyCityThread.java | 3 +- src/engine/workthreads/HourlyJobThread.java | 2 +- .../workthreads/TransferCityThread.java | 2 +- 10 files changed, 102 insertions(+), 101 deletions(-) diff --git a/src/engine/devcmd/cmds/MakeBaneCmd.java b/src/engine/devcmd/cmds/MakeBaneCmd.java index 4b4b379d..fb521c0f 100644 --- a/src/engine/devcmd/cmds/MakeBaneCmd.java +++ b/src/engine/devcmd/cmds/MakeBaneCmd.java @@ -167,7 +167,7 @@ public class MakeBaneCmd extends AbstractDevCmd { return; } stone.addEffectBit((1 << 19)); - stone.setRank((byte) rank); + BuildingManager.setRank(stone, (byte) rank); stone.setMaxHitPoints(blueprint.getMaxHealth(stone.getRank())); stone.setCurrentHitPoints(stone.getMaxHitPoints()); BuildingManager.setUpgradeDateTime(stone, null, 0); diff --git a/src/engine/devcmd/cmds/SetRankCmd.java b/src/engine/devcmd/cmds/SetRankCmd.java index 614701c4..08ee01b0 100644 --- a/src/engine/devcmd/cmds/SetRankCmd.java +++ b/src/engine/devcmd/cmds/SetRankCmd.java @@ -57,7 +57,7 @@ public class SetRankCmd extends AbstractDevCmd { Blueprint blueprint = targetBuilding.getBlueprint(); if (blueprint == null) { - targetBuilding.setRank(targetRank); + BuildingManager.setRank(targetBuilding, targetRank); ChatManager.chatSayInfo(player, "Building ranked without blueprint" + targetBuilding.getObjectUUID()); return; } @@ -68,9 +68,8 @@ public class SetRankCmd extends AbstractDevCmd { } // Set the current targetRank - int lastMeshID = targetBuilding.getMeshUUID(); - targetBuilding.setRank(targetRank); + BuildingManager.setRank(targetBuilding, targetRank); ChatManager.chatSayInfo(player, "Rank set for building with ID " + targetBuilding.getObjectUUID() + " to rank " + targetRank); break; case NPC: @@ -114,7 +113,7 @@ public class SetRankCmd extends AbstractDevCmd { // Set the current targetRank int lastMeshID = targetBuilding.getMeshUUID(); - targetBuilding.setRank(targetRank); + BuildingManager.setRank(targetBuilding, targetRank); if (lastMeshID != targetBuilding.getMeshUUID()) targetBuilding.refresh(true); diff --git a/src/engine/gameManager/BuildingManager.java b/src/engine/gameManager/BuildingManager.java index 1d68332b..d603a4ce 100644 --- a/src/engine/gameManager/BuildingManager.java +++ b/src/engine/gameManager/BuildingManager.java @@ -853,7 +853,7 @@ public enum BuildingManager { } public static void rebuildMine(Building mineBuilding) { - mineBuilding.setRank(1); + setRank(mineBuilding, 1); mineBuilding.meshUUID = mineBuilding.getBlueprint().getMeshForRank(mineBuilding.rank); // New rank mean new max hit points. @@ -862,4 +862,84 @@ public enum BuildingManager { mineBuilding.setCurrentHitPoints(mineBuilding.healthMax); mineBuilding.getBounds().setBounds(mineBuilding); } + + public static void setRank(Building building, int rank) { + + int newMeshUUID; + boolean success; + + + // If this building has no blueprint then set rank and exit immediatly. + + if (building.blueprintUUID == 0 || building.getBlueprint() != null && building.getBlueprint().getBuildingGroup().equals(BuildingGroup.MINE)) { + building.rank = rank; + DbManager.BuildingQueries.CHANGE_RANK(building.getObjectUUID(), rank); + return; + } + + // Delete any upgrade jobs before doing anything else. It won't quite work + // if in a few lines we happen to delete this building. + + JobContainer jc = building.getTimers().get("UPGRADE"); + + if (jc != null) { + if (!JobScheduler.getInstance().cancelScheduledJob(jc)) + Logger.error("failed to cancel existing upgrade job."); + } + + // Attempt write to database, or delete the building + // if we are destroying it. + + if (rank == -1) + success = DbManager.BuildingQueries.DELETE_FROM_DATABASE(building); + else + success = DbManager.BuildingQueries.updateBuildingRank(building, rank); + + if (success == false) { + Logger.error("Error writing to database UUID: " + building.getObjectUUID()); + return; + } + + building.isDeranking.compareAndSet(false, true); + + // Change the building's rank + + building.rank = rank; + + // New rank means new mesh + + newMeshUUID = building.getBlueprint().getMeshForRank(building.rank); + building.meshUUID = newMeshUUID; + + // New rank mean new max hitpoints. + + building.healthMax = building.getBlueprint().getMaxHealth(building.rank); + building.setCurrentHitPoints(building.healthMax); + + if (building.getUpgradeDateTime() != null) + setUpgradeDateTime(building, null, 0); + + // If we destroyed this building make sure to turn off + // protection + + if (building.rank == -1) + building.protectionState = Enum.ProtectionState.NONE; + + if ((building.getBlueprint().getBuildingGroup() == BuildingGroup.TOL) + && (building.rank == 8)) + building.meshUUID = Realm.getRealmMesh(building.getCity()); + + // update object to clients + + building.refresh(true); + + if (building.getBounds() != null) + building.getBounds().setBounds(building); + + // Cleanup hirelings resulting from rank change + + cleanupHirelings(building); + + building.isDeranking.compareAndSet(true, false); + } } diff --git a/src/engine/jobs/UpgradeBuildingJob.java b/src/engine/jobs/UpgradeBuildingJob.java index c9032af9..4170e90e 100644 --- a/src/engine/jobs/UpgradeBuildingJob.java +++ b/src/engine/jobs/UpgradeBuildingJob.java @@ -1,5 +1,6 @@ package engine.jobs; +import engine.gameManager.BuildingManager; import engine.job.AbstractScheduleJob; import engine.objects.Building; import org.pmw.tinylog.Logger; @@ -39,7 +40,7 @@ public class UpgradeBuildingJob extends AbstractScheduleJob { // SetCurrentRank also changes the mesh and maxhp // accordingly for buildings with blueprints - rankingBuilding.setRank(rankingBuilding.getRank() + 1); + BuildingManager.setRank(rankingBuilding, rankingBuilding.getRank() + 1); // Reload the object diff --git a/src/engine/net/client/handlers/DestroyBuildingHandler.java b/src/engine/net/client/handlers/DestroyBuildingHandler.java index 18ce4d05..b1b90bf4 100644 --- a/src/engine/net/client/handlers/DestroyBuildingHandler.java +++ b/src/engine/net/client/handlers/DestroyBuildingHandler.java @@ -95,7 +95,7 @@ public class DestroyBuildingHandler extends AbstractClientMsgHandler { city.setWarehouseBuildingID(0); } - building.setRank(-1); + BuildingManager.setRank(building, -1); WorldGrid.RemoveWorldObject(building); WorldGrid.removeObject(building); building.getParentZone().zoneBuildingSet.remove(building); diff --git a/src/engine/net/client/handlers/ObjectActionMsgHandler.java b/src/engine/net/client/handlers/ObjectActionMsgHandler.java index 5f4c4451..6896f566 100644 --- a/src/engine/net/client/handlers/ObjectActionMsgHandler.java +++ b/src/engine/net/client/handlers/ObjectActionMsgHandler.java @@ -182,7 +182,7 @@ public class ObjectActionMsgHandler extends AbstractClientMsgHandler { realm.claimRealmForCity(city, charterUUID); - tol.setRank(8); + BuildingManager.setRank(tol, 8); WorldGrid.updateObject(tol); for (Building building : city.getParent().zoneBuildingSet) { diff --git a/src/engine/objects/Building.java b/src/engine/objects/Building.java index 05d9984d..04113af4 100644 --- a/src/engine/objects/Building.java +++ b/src/engine/objects/Building.java @@ -80,7 +80,7 @@ public class Building extends AbstractWorldObject { /* The Blueprint class has methods able to derive * all defining characteristics of this building, */ - private int blueprintUUID = 0; + public int blueprintUUID = 0; private float w = 1.0f; private Vector3f meshScale = new Vector3f(1.0f, 1.0f, 1.0f); private int doorState = 0; @@ -96,7 +96,7 @@ public class Building extends AbstractWorldObject { private ConcurrentHashMap timestamps = null; private ConcurrentHashMap friends = new ConcurrentHashMap<>(); private ConcurrentHashMap condemned = new ConcurrentHashMap<>(); - private ProtectionState protectionState = ProtectionState.NONE; + public ProtectionState protectionState = ProtectionState.NONE; private ArrayList children = null; /** @@ -277,86 +277,6 @@ public class Building extends AbstractWorldObject { return rank; } - public final void setRank(int newRank) { - - int newMeshUUID; - boolean success; - - - // If this building has no blueprint then set rank and exit immediatly. - - if (this.blueprintUUID == 0 || this.getBlueprint() != null && this.getBlueprint().getBuildingGroup().equals(BuildingGroup.MINE)) { - this.rank = newRank; - DbManager.BuildingQueries.CHANGE_RANK(this.getObjectUUID(), newRank); - return; - } - - // Delete any upgrade jobs before doing anything else. It won't quite work - // if in a few lines we happen to delete this building. - - JobContainer jc = this.getTimers().get("UPGRADE"); - - if (jc != null) { - if (!JobScheduler.getInstance().cancelScheduledJob(jc)) - Logger.error("failed to cancel existing upgrade job."); - } - - // Attempt write to database, or delete the building - // if we are destroying it. - - if (newRank == -1) - success = DbManager.BuildingQueries.DELETE_FROM_DATABASE(this); - else - success = DbManager.BuildingQueries.updateBuildingRank(this, newRank); - - if (success == false) { - Logger.error("Error writing to database UUID: " + this.getObjectUUID()); - return; - } - - this.isDeranking.compareAndSet(false, true); - - // Change the building's rank - - this.rank = newRank; - - // New rank means new mesh - - newMeshUUID = this.getBlueprint().getMeshForRank(this.rank); - this.meshUUID = newMeshUUID; - - // New rank mean new max hitpoints. - - this.healthMax = this.getBlueprint().getMaxHealth(this.rank); - this.setCurrentHitPoints(this.healthMax); - - if (this.getUpgradeDateTime() != null) - BuildingManager.setUpgradeDateTime(this, null, 0); - - // If we destroyed this building make sure to turn off - // protection - - if (this.rank == -1) - this.protectionState = ProtectionState.NONE; - - if ((this.getBlueprint().getBuildingGroup() == BuildingGroup.TOL) - && (this.rank == 8)) - this.meshUUID = Realm.getRealmMesh(this.getCity()); - ; - - // update object to clients - - this.refresh(true); - if (this.getBounds() != null) - this.getBounds().setBounds(this); - - // Cleanup hirelings resulting from rank change - - BuildingManager.cleanupHirelings(this); - - this.isDeranking.compareAndSet(true, false); - } - public final int getOwnerUUID() { return ownerUUID; } @@ -528,7 +448,7 @@ public class Building extends AbstractWorldObject { MineRecord mineRecord = MineRecord.borrow(mine, attacker, RecordEventType.DESTROY); DataWarehouse.pushToWarehouse(mineRecord); - this.setRank(-1); + BuildingManager.setRank(this, -1); this.setCurrentHitPoints((float) 1); this.healthMax = (float) 1; this.meshUUID = this.getBlueprint().getMeshForRank(this.rank); @@ -551,9 +471,9 @@ public class Building extends AbstractWorldObject { // Time to either derank or destroy the building. if ((this.rank - 1) < 1) - this.setRank(-1); + BuildingManager.setRank(this, -1); else - this.setRank(this.rank - 1); + BuildingManager.setRank(this, this.rank - 1); } @@ -611,7 +531,7 @@ public class Building extends AbstractWorldObject { if (spireBuilding != null) { spireBuilding.disableSpire(true); - spireBuilding.setRank(-1); + BuildingManager.setRank(spireBuilding, -1); } } @@ -622,7 +542,7 @@ public class Building extends AbstractWorldObject { // Delete a random shrine if (shrineBuilding != null) - shrineBuilding.setRank(-1); + BuildingManager.setRank(shrineBuilding, -1); } if (barracksBuildings.size() > this.rank - 1) { @@ -632,7 +552,7 @@ public class Building extends AbstractWorldObject { // Delete a random barrack if (barracksBuilding != null) - barracksBuilding.setRank(-1); + BuildingManager.setRank(barracksBuilding, -1); } // If the tree is R8 and deranking, we need to update it's @@ -661,7 +581,7 @@ public class Building extends AbstractWorldObject { // Let's do so and early exit if (this.rank > 1) { - this.setRank(rank - 1); + BuildingManager.setRank(this, rank - 1); City.lastCityUpdate = System.currentTimeMillis(); return; } diff --git a/src/engine/workthreads/DestroyCityThread.java b/src/engine/workthreads/DestroyCityThread.java index 22cb72e1..25f18fcb 100644 --- a/src/engine/workthreads/DestroyCityThread.java +++ b/src/engine/workthreads/DestroyCityThread.java @@ -19,6 +19,7 @@ package engine.workthreads; */ import engine.Enum; +import engine.gameManager.BuildingManager; import engine.gameManager.DbManager; import engine.gameManager.GuildManager; import engine.gameManager.ZoneManager; @@ -127,7 +128,7 @@ public class DestroyCityThread implements Runnable { || (cityBuilding.getBlueprint().getBuildingGroup() == Enum.BuildingGroup.WAREHOUSE)) { if (cityBuilding.getRank() != -1) - cityBuilding.setRank(-1); + BuildingManager.setRank(cityBuilding, -1); } } diff --git a/src/engine/workthreads/HourlyJobThread.java b/src/engine/workthreads/HourlyJobThread.java index 811a9c90..26795745 100644 --- a/src/engine/workthreads/HourlyJobThread.java +++ b/src/engine/workthreads/HourlyJobThread.java @@ -176,7 +176,7 @@ public class HourlyJobThread implements Runnable { MineRecord mineRecord = MineRecord.borrow(mine, mine.lastClaimer, Enum.RecordEventType.CAPTURE); DataWarehouse.pushToWarehouse(mineRecord); - mineBuilding.setRank(mineBuilding.getRank()); + BuildingManager.setRank(mineBuilding, mineBuilding.getRank()); mine.lastClaimer = null; mine.setActive(false); mine.wasClaimed = true; diff --git a/src/engine/workthreads/TransferCityThread.java b/src/engine/workthreads/TransferCityThread.java index 812f82fe..cd4d1cb4 100644 --- a/src/engine/workthreads/TransferCityThread.java +++ b/src/engine/workthreads/TransferCityThread.java @@ -74,7 +74,7 @@ public class TransferCityThread implements Runnable { //Reset TOL to rank 1 - city.getTOL().setRank(1); + BuildingManager.setRank(city.getTOL(), 1); // Transfer all assets to new owner