From 43b212ed42323c9c378dded082a8942264395e87 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Fri, 5 Apr 2024 08:53:18 -0400 Subject: [PATCH] refactored out dependency --- src/engine/db/handlers/dbContractHandler.java | 2 +- src/engine/devcmd/cmds/SlotNpcCmd.java | 7 ++++--- src/engine/mbEnums.java | 12 +++++++++--- src/engine/objects/Contract.java | 10 +++++----- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/engine/db/handlers/dbContractHandler.java b/src/engine/db/handlers/dbContractHandler.java index 9354882b..69fde2de 100644 --- a/src/engine/db/handlers/dbContractHandler.java +++ b/src/engine/db/handlers/dbContractHandler.java @@ -135,7 +135,7 @@ public class dbContractHandler extends dbHandlerBase { preparedStatement.setInt(5, (con.getVendorDialog() != null) ? con.getVendorDialog().getObjectUUID() : 0); preparedStatement.setInt(6, con.getIconID()); preparedStatement.setInt(8, con.getObjectUUID()); - preparedStatement.setLong(7, con.getAllowedBuildings().toLong()); + preparedStatement.setLong(7, mbEnums.toLong(con.getAllowedBuildings())); return (preparedStatement.executeUpdate() > 0); diff --git a/src/engine/devcmd/cmds/SlotNpcCmd.java b/src/engine/devcmd/cmds/SlotNpcCmd.java index 2ca7d820..e933cd92 100644 --- a/src/engine/devcmd/cmds/SlotNpcCmd.java +++ b/src/engine/devcmd/cmds/SlotNpcCmd.java @@ -9,10 +9,11 @@ package engine.devcmd.cmds; -import engine.mbEnums.BuildingGroup; import engine.devcmd.AbstractDevCmd; import engine.gameManager.ChatManager; import engine.gameManager.DbManager; +import engine.mbEnums; +import engine.mbEnums.BuildingGroup; import engine.objects.*; import engine.util.StringUtils; import org.pmw.tinylog.Logger; @@ -112,7 +113,7 @@ public class SlotNpcCmd extends AbstractDevCmd { case "ON": contract.getAllowedBuildings().add(buildingGroup); - if (!DbManager.ContractQueries.updateAllowedBuildings(contract, contract.getAllowedBuildings().toLong())) { + if (!DbManager.ContractQueries.updateAllowedBuildings(contract, mbEnums.toLong(contract.getAllowedBuildings()))) { Logger.error("Failed to update Database for Contract Allowed buildings"); ChatManager.chatSystemError(pc, "Failed to update Database for Contract Allowed buildings. "); return; @@ -122,7 +123,7 @@ public class SlotNpcCmd extends AbstractDevCmd { break; case "OFF": contract.getAllowedBuildings().remove(buildingGroup); - if (!DbManager.ContractQueries.updateAllowedBuildings(contract, contract.getAllowedBuildings().toLong())) { + if (!DbManager.ContractQueries.updateAllowedBuildings(contract, mbEnums.toLong(contract.getAllowedBuildings()))) { Logger.error("Failed to update Database for Contract Allowed buildings"); ChatManager.chatSystemError(pc, "Failed to update Database for Contract Allowed buildings. "); return; diff --git a/src/engine/mbEnums.java b/src/engine/mbEnums.java index e80cd8aa..0358f48a 100644 --- a/src/engine/mbEnums.java +++ b/src/engine/mbEnums.java @@ -24,8 +24,6 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.concurrent.ThreadLocalRandom; -; - /* * MagicBane engine enumeration class. * @@ -36,6 +34,14 @@ import java.util.concurrent.ThreadLocalRandom; public class mbEnums { + public static > long toLong(EnumSet enumSet) { + long r = 0; + for (T value : enumSet) { + r |= 1L << value.ordinal(); + } + return r; + } + public static > EnumSet fromLong(long bitVector, Class enumClass) { // Bitvector -> EnumSet without the EnumBitvector dependency @@ -1582,7 +1588,7 @@ public class mbEnums { // It is also used as a bitvector flag in the npc // building slot mechanics. - public enum BuildingGroup implements EnumBitSetHelper { + public enum BuildingGroup { NONE(0, 0), TOL(64f, 64f), BARRACK(32f, 64f), diff --git a/src/engine/objects/Contract.java b/src/engine/objects/Contract.java index 004f7785..53944489 100644 --- a/src/engine/objects/Contract.java +++ b/src/engine/objects/Contract.java @@ -9,7 +9,6 @@ package engine.objects; -import ch.claude_martin.enumbitset.EnumBitSet; import engine.gameManager.DbManager; import engine.mbEnums; import org.pmw.tinylog.Logger; @@ -17,6 +16,7 @@ import org.pmw.tinylog.Logger; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.EnumSet; public class Contract extends AbstractGameObject { @@ -36,7 +36,7 @@ public class Contract extends AbstractGameObject { private ArrayList npcModSuffixTable = new ArrayList<>(); private ArrayList itemModTable = new ArrayList<>(); private ArrayList sellInventory = new ArrayList<>(); - private EnumBitSet allowedBuildings; + private EnumSet allowedBuildings; private ArrayList buyItemType = new ArrayList<>(); private ArrayList buySkillToken = new ArrayList<>(); private ArrayList buyUnknownToken = new ArrayList<>(); @@ -85,7 +85,7 @@ public class Contract extends AbstractGameObject { this.vendorDialog = VendorDialog.getVendorDialog(rs.getInt("dialogID")); this.iconID = rs.getInt("iconID"); this.vendorID = rs.getInt("vendorID"); - this.allowedBuildings = EnumBitSet.asEnumBitSet(rs.getLong("allowedBuildingTypeID"), mbEnums.BuildingGroup.class); + this.allowedBuildings = mbEnums.fromLong(rs.getLong("allowedBuildingTypeID"), mbEnums.BuildingGroup.class); this.equipmentSet = rs.getInt("equipSetID"); this.inventorySet = rs.getInt("inventorySet"); @@ -220,7 +220,7 @@ public class Contract extends AbstractGameObject { DbManager.ContractQueries.updateDatabase(this); } - public EnumBitSet getAllowedBuildings() { + public EnumSet getAllowedBuildings() { return allowedBuildings; } @@ -251,7 +251,7 @@ public class Contract extends AbstractGameObject { return false; // Binary match - return (building.getBlueprint().getBuildingGroup().elementOf(this.allowedBuildings)); + return (this.allowedBuildings.contains(building.getBlueprint().getBuildingGroup())); } public int getEquipmentSet() {