Files
prestonbane/src/engine/db/handlers/dbItemHandler.java
T

562 lines
20 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers;
2024-03-22 13:03:47 -04:00
import engine.Enum;
2022-04-30 09:41:17 -04:00
import engine.Enum.ItemContainerType;
import engine.Enum.ItemType;
2023-05-21 08:57:53 -04:00
import engine.gameManager.DbManager;
2023-05-21 08:39:19 -04:00
import engine.objects.AbstractCharacter;
import engine.objects.CharacterItemManager;
import engine.objects.Item;
2024-02-18 11:08:46 -05:00
import engine.objects.ItemTemplate;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
2023-05-21 08:57:53 -04:00
import org.pmw.tinylog.Logger;
2022-04-30 09:41:17 -04:00
2023-05-21 08:57:53 -04:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
2022-04-30 09:41:17 -04:00
import java.util.ArrayList;
import java.util.HashSet;
public class dbItemHandler extends dbHandlerBase {
2023-05-23 10:27:03 -04:00
public dbItemHandler() {
this.localClass = Item.class;
this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
}
private static String formatTradeString(HashSet<Integer> list) {
int size = list.size();
String ret = "";
if (size == 0)
return ret;
boolean start = true;
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
for (int i : list) {
if (start) {
ret += i;
start = false;
} else
ret += "," + i;
}
return ret;
}
2024-03-02 09:55:30 -05:00
public Item PERSIST(Item toAdd) {
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:35:08 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("CALL `item_CREATE`(?, ?, ?, ?, ?, ?, ?, ?,?);")) {
2023-05-23 10:27:03 -04:00
2024-03-02 10:34:45 -05:00
preparedStatement.setInt(1, toAdd.ownerID);
2024-03-30 08:26:08 -04:00
preparedStatement.setInt(2, toAdd.templateID);
2024-03-01 21:32:51 -05:00
preparedStatement.setInt(3, (byte) toAdd.chargesRemaining);
2024-03-25 05:25:09 -04:00
preparedStatement.setInt(4, (short) toAdd.combat_health_current);
2023-05-23 10:27:03 -04:00
if (toAdd.getNumOfItems() < 1)
2024-03-28 04:35:08 -04:00
preparedStatement.setInt(5, 1);
2023-05-23 10:27:03 -04:00
else
2024-03-28 04:35:08 -04:00
preparedStatement.setInt(5, toAdd.getNumOfItems());
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
switch (toAdd.containerType) {
case INVENTORY:
2024-03-28 04:35:08 -04:00
preparedStatement.setString(6, "inventory");
2023-05-23 10:27:03 -04:00
break;
case EQUIPPED:
2024-03-28 04:35:08 -04:00
preparedStatement.setString(6, "equip");
2023-05-23 10:27:03 -04:00
break;
case BANK:
2024-03-28 04:35:08 -04:00
preparedStatement.setString(6, "bank");
2023-05-23 10:27:03 -04:00
break;
case VAULT:
2024-03-28 04:35:08 -04:00
preparedStatement.setString(6, "vault");
2023-05-23 10:27:03 -04:00
break;
case FORGE:
2024-03-28 04:35:08 -04:00
preparedStatement.setString(6, "forge");
2023-05-23 10:27:03 -04:00
break;
default:
2024-03-28 04:35:08 -04:00
preparedStatement.setString(6, "none"); //Shouldn't be here
2023-05-23 10:27:03 -04:00
break;
}
2022-04-30 09:41:17 -04:00
2024-03-30 13:04:35 -04:00
preparedStatement.setString(7, toAdd.equipSlot.name());
2024-03-22 13:03:47 -04:00
String flagString = "";
for (Enum.ItemFlags itemflag : toAdd.flags)
flagString += itemflag.toString() + ";";
2024-03-25 04:50:58 -04:00
flagString = flagString.replaceAll(";$", "");
2024-03-28 04:35:08 -04:00
preparedStatement.setString(8, flagString);
preparedStatement.setString(9, toAdd.name);
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 08:57:53 -04:00
2023-05-23 12:24:11 -04:00
if (rs.next()) {
int objectUUID = (int) rs.getLong("UID");
2022-04-30 09:41:17 -04:00
2023-05-23 12:24:11 -04:00
if (objectUUID > 0)
return GET_ITEM(objectUUID);
}
2023-05-23 14:29:08 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
}
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
return null;
}
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
public boolean DO_TRADE(HashSet<Integer> from1, HashSet<Integer> from2,
CharacterItemManager man1, CharacterItemManager man2,
Item inventoryGold1, Item inventoryGold2, int goldFrom1, int goldFrom2) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
AbstractCharacter ac1 = man1.getOwner();
AbstractCharacter ac2 = man2.getOwner();
boolean worked = false;
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
if (ac1 == null || ac2 == null || inventoryGold1 == null || inventoryGold2 == null)
return false;
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("CALL `item_TRADE`(?, ?, ?, ?, ?, ?, ?, ?)")) {
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setString(1, formatTradeString(from1));
preparedStatement.setLong(2, ac1.getObjectUUID());
preparedStatement.setString(3, formatTradeString(from2));
preparedStatement.setLong(4, ac2.getObjectUUID());
preparedStatement.setInt(5, goldFrom1);
preparedStatement.setLong(6, inventoryGold1.getObjectUUID());
preparedStatement.setInt(7, goldFrom2);
preparedStatement.setLong(8, inventoryGold2.getObjectUUID());
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
ResultSet rs = preparedStatement.executeQuery();
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
if (rs.next())
worked = rs.getBoolean("result");
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
}
return worked;
}
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
public ArrayList<Item> GET_EQUIPPED_ITEMS(final int targetId) {
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
ArrayList<Item> itemList;
2023-05-21 08:57:53 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_item`.*, `object`.`parent`, `object`.`type` FROM `object` INNER JOIN `obj_item` ON `object`.`UID` = `obj_item`.`UID` WHERE `object`.`parent`=? && `obj_item`.`container`='equip';")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, targetId);
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
itemList = getObjectsFromRs(rs, 10);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return null;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
return itemList;
}
2023-05-21 14:39:57 -04:00
2024-02-18 11:08:46 -05:00
public void LOAD_ITEM_TEMPLATES() {
JSONParser jsonParser = new JSONParser();
try (Connection connection = DbManager.getConnection();
2024-03-17 09:01:35 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_item_templates`;");
ResultSet rs = preparedStatement.executeQuery()) {
2024-02-18 11:08:46 -05:00
while (rs.next()) {
2024-02-19 02:51:26 -05:00
int templateID = rs.getInt("id");
2024-02-18 11:08:46 -05:00
JSONObject jsonObject = (JSONObject) jsonParser.parse(rs.getString("template"));
ItemTemplate itemTemplate = new ItemTemplate(jsonObject);
2024-03-01 08:23:28 -05:00
itemTemplate.template_id = templateID;
2024-03-16 07:57:17 -04:00
ItemTemplate.templates.put(templateID, itemTemplate);
2024-02-18 11:08:46 -05:00
}
} catch (Exception e) {
Logger.error(e);
}
}
2024-03-28 12:08:09 -04:00
public void LOAD_TEMPLATE_MODTABLES() {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_vendor_items`;");
ResultSet rs = preparedStatement.executeQuery()) {
while (rs.next()) {
int templateID = rs.getInt("templateID");
int modTable = rs.getInt("modTable");
ItemTemplate template = ItemTemplate.templates.get(templateID);
template.modTable = modTable;
}
} catch (Exception e) {
Logger.error(e);
}
}
2023-05-23 10:27:03 -04:00
public Item GET_ITEM(final int itemUUID) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
Item item;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_item`.*, `object`.`parent`, `object`.`type` FROM `object` INNER JOIN `obj_item` ON `object`.`UID` = `obj_item`.`UID` WHERE `object`.`UID`=?;")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, itemUUID);
ResultSet rs = preparedStatement.executeQuery();
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
item = (Item) getObjectFromRs(rs);
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return null;
}
return item;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public ArrayList<Item> GET_ITEMS_FOR_ACCOUNT(final int accountId) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
ArrayList<Item> itemList;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_item`.*, `object`.`parent`, `object`.`type` FROM `object` INNER JOIN `obj_item` ON `object`.`UID` = `obj_item`.`UID` WHERE `object`.`parent`=?;")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, accountId);
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
itemList = getObjectsFromRs(rs, 100);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return null;
}
return itemList;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public ArrayList<Item> GET_ITEMS_FOR_NPC(final int npcId) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
ArrayList<Item> itemList;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_item`.*, `object`.`parent`, `object`.`type` FROM `object` INNER JOIN `obj_item` ON `object`.`UID` = `obj_item`.`UID` WHERE `object`.`parent`=?;")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, npcId);
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
itemList = getObjectsFromRs(rs, 20);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return null;
}
return itemList;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public ArrayList<Item> GET_ITEMS_FOR_PC(final int id) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
ArrayList<Item> itemList;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_item`.*, `object`.`parent`, `object`.`type` FROM `object` INNER JOIN `obj_item` ON `object`.`UID` = `obj_item`.`UID` WHERE `object`.`parent`=?")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, id);
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
itemList = getObjectsFromRs(rs, 100);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return null;
}
return itemList;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean MOVE_GOLD(final Item from, final Item to, final int amt) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `numberOfItems` = CASE WHEN `UID`=? THEN ? WHEN `UID`=? THEN ? END WHERE `UID` IN (?, ?);")) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
int newFromAmt = from.getNumOfItems() - amt;
int newToAmt = to.getNumOfItems() + amt;
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, from.getObjectUUID());
preparedStatement.setInt(2, newFromAmt);
preparedStatement.setLong(3, to.getObjectUUID());
preparedStatement.setInt(4, newToAmt);
preparedStatement.setLong(5, from.getObjectUUID());
preparedStatement.setLong(6, to.getObjectUUID());
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
}
return false;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean ORPHAN_INVENTORY(final HashSet<Item> inventory) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
boolean worked = true;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
for (Item item : inventory) {
2022-04-30 09:41:17 -04:00
2024-03-10 13:34:24 -04:00
if (item.template.item_type.equals(ItemType.GOLD))
2023-05-23 10:27:03 -04:00
continue;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` LEFT JOIN `object` ON `object`.`UID` = `obj_item`.`UID` SET `object`.`parent`=NULL, `obj_item`.`container`='none' WHERE `object`.`UID`=?;")) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, item.getObjectUUID());
worked = (preparedStatement.executeUpdate() > 0);
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
if (worked)
item.zeroItem();
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
}
return worked;
}
2023-05-21 14:39:57 -04:00
2024-03-28 11:50:22 -04:00
public HashSet<Integer> GET_VENDOR_CAN_ROLL_LIST(final int vendorID) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
HashSet<Integer> itemSet = new HashSet<>();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 20:37:45 -05:00
PreparedStatement preparedStatement = connection.prepareStatement("SELECT templateID FROM static_vendor_items WHERE vendorType = ?")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setInt(1, vendorID);
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
while (rs.next())
itemSet.add(rs.getInt(1));
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return itemSet;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
return itemSet;
}
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
//Used to transfer a single item between owners or equip or vault or bank or inventory
public boolean UPDATE_OWNER(final Item item, int newOwnerID, boolean ownerNPC, boolean ownerPlayer,
boolean ownerAccount, ItemContainerType containerType, int slot) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
boolean worked = false;
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("CALL `item_TRANSFER_OWNER`(?, ?, ?, ? )")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
if (newOwnerID != 0)
preparedStatement.setLong(2, newOwnerID);
else
preparedStatement.setNull(2, java.sql.Types.BIGINT);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
switch (containerType) {
case INVENTORY:
preparedStatement.setString(3, "inventory");
break;
case EQUIPPED:
preparedStatement.setString(3, "equip");
break;
case BANK:
preparedStatement.setString(3, "bank");
break;
case VAULT:
preparedStatement.setString(3, "vault");
break;
case FORGE:
preparedStatement.setString(3, "forge");
break;
default:
preparedStatement.setString(3, "none"); //Shouldn't be here
break;
}
preparedStatement.setInt(4, slot);
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
if (rs.next())
worked = rs.getBoolean("result");
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
return worked;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean SET_DURABILITY(final Item item, int value) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `combat_health_current`=? WHERE `UID`=? AND `combat_health_current`=?")) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setInt(1, value);
preparedStatement.setLong(2, item.getObjectUUID());
2024-03-25 05:25:09 -04:00
preparedStatement.setInt(3, (short) item.combat_health_current);
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean UPDATE_FORGE_TO_INVENTORY(final Item item) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `container` = ? WHERE `UID` = ? AND `container` = 'forge';")) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setString(1, "inventory");
preparedStatement.setLong(2, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
/**
* Attempts to update the quantity of this gold item
*
* @param value New quantity of gold
* @return True on success
*/
public boolean UPDATE_GOLD(final Item item, int value) {
if (item == null)
return false;
return UPDATE_GOLD(item, value, item.getNumOfItems());
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
/**
* Attempts to update the quantity of this gold item using CAS
*
* @return True on success
*/
public boolean UPDATE_GOLD(final Item item, int newValue, int oldValue) {
2023-05-21 14:39:57 -04:00
2024-03-10 13:34:24 -04:00
if (!item.template.item_type.equals(ItemType.GOLD))
2023-05-23 10:27:03 -04:00
return false;
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `numberOfItems`=? WHERE `UID`=?")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setInt(1, newValue);
preparedStatement.setLong(2, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean UPDATE_REMAINING_CHARGES(final Item item) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `chargesRemaining` = ? WHERE `UID` = ?")) {
2022-04-30 09:41:17 -04:00
2024-03-01 21:32:51 -05:00
preparedStatement.setInt(1, (byte) item.chargesRemaining);
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(2, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
// This is necessary because default number of items is 1.
// When we create gold, we want it to start at 0 quantity.
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean ZERO_ITEM_STACK(Item item) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `numberOfItems`=0 WHERE `UID` = ?")) {
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(1, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean UPDATE_FLAGS(Item item) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `flags`=? WHERE `UID` = ?")) {
2023-05-21 14:39:57 -04:00
2024-03-22 13:03:47 -04:00
String flagString = "";
for (Enum.ItemFlags itemflag : item.flags)
flagString += itemflag.toString() + ";";
flagString = flagString.replaceAll(";$", "");
2024-03-22 13:03:47 -04:00
preparedStatement.setString(1, flagString);
2023-05-23 10:27:03 -04:00
preparedStatement.setLong(2, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
}
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
public boolean UPDATE_VALUE(Item item, int value) {
2023-05-21 14:39:57 -04:00
2024-03-25 06:04:32 -04:00
// Write 0 if we will not modify the value from template
if (item.value == item.template.item_value)
value = 0;
2023-05-23 10:27:03 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-28 04:24:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_item` SET `value`=? WHERE `UID` = ?")) {
2023-05-21 14:39:57 -04:00
2023-05-23 10:27:03 -04:00
preparedStatement.setInt(1, value);
preparedStatement.setLong(2, item.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
return (preparedStatement.executeUpdate() > 0);
2022-04-30 09:41:17 -04:00
2023-05-23 10:27:03 -04:00
} catch (SQLException e) {
Logger.error(e);
return false;
}
}
2022-04-30 09:41:17 -04:00
}