Add deferrable saving to GameItem

This commit is contained in:
KingRainbow44 2023-09-13 00:34:17 -04:00
parent fb215e06cd
commit a90a81c705
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 44 additions and 19 deletions

View File

@ -20,13 +20,14 @@ import emu.grasscutter.net.proto.ReliquaryOuterClass.Reliquary;
import emu.grasscutter.net.proto.SceneReliquaryInfoOuterClass.SceneReliquaryInfo;
import emu.grasscutter.net.proto.SceneWeaponInfoOuterClass.SceneWeaponInfo;
import emu.grasscutter.net.proto.WeaponOuterClass.Weapon;
import emu.grasscutter.utils.objects.WeightedList;
import java.util.*;
import emu.grasscutter.utils.objects.*;
import lombok.*;
import org.bson.types.ObjectId;
import java.util.*;
@Entity(value = "items", useDiscriminator = false)
public class GameItem {
public class GameItem implements DatabaseObject<GameItem> {
@Id private ObjectId id;
@Indexed private int ownerId;
@Getter @Setter private int itemId;
@ -261,14 +262,36 @@ public class GameItem {
}
}
/**
* Saves this object to the database.
* As of Grasscutter 1.7.1, this is by default a {@link DatabaseObject#deferSave()} call.
*/
public void save() {
if (this.count > 0 && this.ownerId > 0) {
DatabaseHelper.saveItem(this);
} else if (this.getObjectId() != null) {
this.deferSave();
} else {
DatabaseHelper.deleteItem(this);
}
}
/**
* Saves this object to the database.
*
* @param immediate If true, this will be a {@link DatabaseObject#save()} call instead of a {@link DatabaseObject#deferSave()} call.
*/
public void save(boolean immediate) {
if (this.count < 0 || this.ownerId <= 0) {
DatabaseHelper.deleteItem(this);
return;
}
if (immediate) {
DatabaseObject.super.save();
} else {
this.save();
}
}
public SceneWeaponInfo createSceneWeaponInfo() {
var weaponInfo =
SceneWeaponInfo.newBuilder()

View File

@ -1,7 +1,5 @@
package emu.grasscutter.game.inventory;
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
@ -18,10 +16,13 @@ import emu.grasscutter.server.packet.send.*;
import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.ints.*;
import it.unimi.dsi.fastutil.longs.*;
import java.util.*;
import javax.annotation.Nullable;
import lombok.val;
import javax.annotation.Nullable;
import java.util.*;
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
private final Long2ObjectMap<GameItem> store;
private final Int2ObjectMap<InventoryTab> inventoryTypes;
@ -178,7 +179,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
changedItems.add(result);
}
}
if (changedItems.size() == 0) {
if (changedItems.isEmpty()) {
return;
}
if (reason != null) {
@ -298,8 +299,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
// Add
switch (type) {
case ITEM_WEAPON:
case ITEM_RELIQUARY:
case ITEM_WEAPON, ITEM_RELIQUARY -> {
if (tab.getSize() >= tab.getMaxCapacity()) {
return null;
}
@ -310,23 +310,23 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
// Set ownership and save to db
item.save();
return item;
case ITEM_VIRTUAL:
}
case ITEM_VIRTUAL -> {
// Handle
this.addVirtualItem(item.getItemId(), item.getCount());
return item;
default:
}
default -> {
switch (item.getItemData().getMaterialType()) {
case MATERIAL_AVATAR:
case MATERIAL_FLYCLOAK:
case MATERIAL_COSTUME:
case MATERIAL_NAMECARD:
case MATERIAL_AVATAR, MATERIAL_FLYCLOAK, MATERIAL_COSTUME, MATERIAL_NAMECARD -> {
Grasscutter.getLogger()
.warn(
"Attempted to add a "
+ item.getItemData().getMaterialType().name()
+ " to inventory, but item definition lacks isUseOnGain. This indicates a Resources error.");
return null;
default:
}
default -> {
if (tab == null) {
return null;
}
@ -350,7 +350,9 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
existingItem.save();
return existingItem;
}
}
}
}
}
}