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.SceneReliquaryInfoOuterClass.SceneReliquaryInfo;
import emu.grasscutter.net.proto.SceneWeaponInfoOuterClass.SceneWeaponInfo; import emu.grasscutter.net.proto.SceneWeaponInfoOuterClass.SceneWeaponInfo;
import emu.grasscutter.net.proto.WeaponOuterClass.Weapon; import emu.grasscutter.net.proto.WeaponOuterClass.Weapon;
import emu.grasscutter.utils.objects.WeightedList; import emu.grasscutter.utils.objects.*;
import java.util.*;
import lombok.*; import lombok.*;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import java.util.*;
@Entity(value = "items", useDiscriminator = false) @Entity(value = "items", useDiscriminator = false)
public class GameItem { public class GameItem implements DatabaseObject<GameItem> {
@Id private ObjectId id; @Id private ObjectId id;
@Indexed private int ownerId; @Indexed private int ownerId;
@Getter @Setter private int itemId; @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() { public void save() {
if (this.count > 0 && this.ownerId > 0) { if (this.count > 0 && this.ownerId > 0) {
DatabaseHelper.saveItem(this); this.deferSave();
} else if (this.getObjectId() != null) { } else {
DatabaseHelper.deleteItem(this); 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() { public SceneWeaponInfo createSceneWeaponInfo() {
var weaponInfo = var weaponInfo =
SceneWeaponInfo.newBuilder() SceneWeaponInfo.newBuilder()

View File

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