Commit bb372011 authored by AnimeGitB's avatar AnimeGitB Committed by Melledy
Browse files

Small putItem refactor

parent bcc20c2d
...@@ -174,22 +174,29 @@ public class Inventory implements Iterable<GameItem> { ...@@ -174,22 +174,29 @@ public class Inventory implements Iterable<GameItem> {
InventoryTab tab = getInventoryTab(type); InventoryTab tab = getInventoryTab(type);
// Add // Add
if (type == ItemType.ITEM_WEAPON || type == ItemType.ITEM_RELIQUARY) { switch (type) {
case ITEM_WEAPON:
case ITEM_RELIQUARY:
if (tab.getSize() >= tab.getMaxCapacity()) { if (tab.getSize() >= tab.getMaxCapacity()) {
return null; return null;
} }
// Duplicates cause problems // Duplicates cause problems
item.setCount(Math.max(item.getCount(), 1)); item.setCount(Math.max(item.getCount(), 1));
// Adds to inventory // Adds to inventory
putItem(item, tab); this.putItem(item, tab);
} else if (type == ItemType.ITEM_VIRTUAL) { // Set ownership and save to db
item.save();
return item;
case ITEM_VIRTUAL:
// Handle // Handle
this.addVirtualItem(item.getItemId(), item.getCount()); this.addVirtualItem(item.getItemId(), item.getCount());
return item; return item;
} else if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_ADSORBATE) { default:
switch (item.getItemData().getMaterialType()) {
case MATERIAL_ADSORBATE:
this.player.getEnergyManager().handlePickupElemBall(item); this.player.getEnergyManager().handlePickupElemBall(item);
return null; return null;
} else if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_AVATAR) { case MATERIAL_AVATAR:
// Get avatar id // Get avatar id
int avatarId = (item.getItemId() % 1000) + 10000000; int avatarId = (item.getItemId() % 1000) + 10000000;
// Dont let people give themselves extra main characters // Dont let people give themselves extra main characters
...@@ -198,56 +205,55 @@ public class Inventory implements Iterable<GameItem> { ...@@ -198,56 +205,55 @@ public class Inventory implements Iterable<GameItem> {
} }
// Add avatar // Add avatar
AvatarData avatarData = GameData.getAvatarDataMap().get(avatarId); AvatarData avatarData = GameData.getAvatarDataMap().get(avatarId);
if (avatarData != null && !player.getAvatars().hasAvatar(avatarId)) { if (avatarData != null && !this.player.getAvatars().hasAvatar(avatarId)) {
this.getPlayer().addAvatar(new Avatar(avatarData)); this.player.addAvatar(new Avatar(avatarData));
} }
return null; return null;
} else if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_FLYCLOAK) { case MATERIAL_FLYCLOAK:
AvatarFlycloakData flycloakData = GameData.getAvatarFlycloakDataMap().get(item.getItemId()); AvatarFlycloakData flycloakData = GameData.getAvatarFlycloakDataMap().get(item.getItemId());
if (flycloakData != null && !player.getFlyCloakList().contains(item.getItemId())) { if (flycloakData != null && !this.player.getFlyCloakList().contains(item.getItemId())) {
getPlayer().addFlycloak(item.getItemId()); this.player.addFlycloak(item.getItemId());
} }
return null; return null;
} else if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_COSTUME) { case MATERIAL_COSTUME:
AvatarCostumeData costumeData = GameData.getAvatarCostumeDataItemIdMap().get(item.getItemId()); AvatarCostumeData costumeData = GameData.getAvatarCostumeDataItemIdMap().get(item.getItemId());
if (costumeData != null && !player.getCostumeList().contains(costumeData.getId())) { if (costumeData != null && !this.player.getCostumeList().contains(costumeData.getId())) {
getPlayer().addCostume(costumeData.getId()); this.player.addCostume(costumeData.getId());
} }
return null; return null;
} else if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_NAMECARD) { case MATERIAL_NAMECARD:
if (!player.getNameCardList().contains(item.getItemId())) { if (!this.player.getNameCardList().contains(item.getItemId())) {
getPlayer().addNameCard(item.getItemId()); this.player.addNameCard(item.getItemId());
} }
return null; return null;
} else if (tab != null) { default:
if (tab == null) {
return null;
}
GameItem existingItem = tab.getItemById(item.getItemId()); GameItem existingItem = tab.getItemById(item.getItemId());
if (existingItem == null) { if (existingItem == null) {
// Item type didnt exist before, we will add it to main inventory map if there is enough space // Item type didnt exist before, we will add it to main inventory map if there is enough space
if (tab.getSize() >= tab.getMaxCapacity()) { if (tab.getSize() >= tab.getMaxCapacity()) {
return null; return null;
} }
putItem(item, tab); this.putItem(item, tab);
// Set ownership and save to db
item.save();
return item;
} else { } else {
// Add count // Add count
existingItem.setCount(Math.min(existingItem.getCount() + item.getCount(), item.getItemData().getStackLimit())); existingItem.setCount(Math.min(existingItem.getCount() + item.getCount(), item.getItemData().getStackLimit()));
existingItem.save(); existingItem.save();
return existingItem; return existingItem;
} }
} else {
return null;
} }
}
// Set ownership and save to db
if (item.getItemData().getItemType() != ItemType.ITEM_VIRTUAL)
item.save();
return item;
} }
private synchronized void putItem(GameItem item, InventoryTab tab) { private synchronized void putItem(GameItem item, InventoryTab tab) {
getPlayer().getCodex().checkAddedItem(item); this.player.getCodex().checkAddedItem(item);
// Set owner and guid FIRST! // Set owner and guid FIRST!
item.setOwner(getPlayer()); item.setOwner(this.player);
// Put in item store // Put in item store
getItems().put(item.getGuid(), item); getItems().put(item.getGuid(), item);
if (tab != null) { if (tab != null) {
...@@ -258,36 +264,36 @@ public class Inventory implements Iterable<GameItem> { ...@@ -258,36 +264,36 @@ public class Inventory implements Iterable<GameItem> {
private void addVirtualItem(int itemId, int count) { private void addVirtualItem(int itemId, int count) {
switch (itemId) { switch (itemId) {
case 101 -> // Character exp case 101 -> // Character exp
getPlayer().getServer().getInventoryManager().upgradeAvatar(player, getPlayer().getTeamManager().getCurrentAvatarEntity().getAvatar(), count); this.player.getServer().getInventoryManager().upgradeAvatar(this.player, this.player.getTeamManager().getCurrentAvatarEntity().getAvatar(), count);
case 102 -> // Adventure exp case 102 -> // Adventure exp
getPlayer().addExpDirectly(count); this.player.addExpDirectly(count);
case 105 -> // Companionship exp case 105 -> // Companionship exp
getPlayer().getServer().getInventoryManager().upgradeAvatarFetterLevel(player, getPlayer().getTeamManager().getCurrentAvatarEntity().getAvatar(), count); this.player.getServer().getInventoryManager().upgradeAvatarFetterLevel(this.player, this.player.getTeamManager().getCurrentAvatarEntity().getAvatar(), count);
case 106 -> // Resin case 106 -> // Resin
getPlayer().getResinManager().addResin(count); this.player.getResinManager().addResin(count);
case 201 -> // Primogem case 201 -> // Primogem
getPlayer().setPrimogems(player.getPrimogems() + count); this.player.setPrimogems(this.player.getPrimogems() + count);
case 202 -> // Mora case 202 -> // Mora
getPlayer().setMora(player.getMora() + count); this.player.setMora(this.player.getMora() + count);
case 203 -> // Genesis Crystals case 203 -> // Genesis Crystals
getPlayer().setCrystals(player.getCrystals() + count); this.player.setCrystals(this.player.getCrystals() + count);
case 204 -> // Home Coin case 204 -> // Home Coin
getPlayer().setHomeCoin(player.getHomeCoin() + count); this.player.setHomeCoin(this.player.getHomeCoin() + count);
} }
} }
private int getVirtualItemCount(int itemId) { private int getVirtualItemCount(int itemId) {
switch (itemId) { switch (itemId) {
case 201: // Primogem case 201: // Primogem
return player.getPrimogems(); return this.player.getPrimogems();
case 202: // Mora case 202: // Mora
return player.getMora(); return this.player.getMora();
case 203: // Genesis Crystals case 203: // Genesis Crystals
return player.getCrystals(); return this.player.getCrystals();
case 106: // Resin case 106: // Resin
return player.getProperty(PlayerProperty.PROP_PLAYER_RESIN); return this.player.getProperty(PlayerProperty.PROP_PLAYER_RESIN);
case 204: // Home Coin case 204: // Home Coin
return player.getHomeCoin(); return this.player.getHomeCoin();
default: default:
GameItem item = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId); // What if we ever want to operate on weapons/relics/furniture? :S GameItem item = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId); // What if we ever want to operate on weapons/relics/furniture? :S
return (item == null) ? 0 : item.getCount(); return (item == null) ? 0 : item.getCount();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment