Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/brenoepics/Grasscutter
Browse files Browse the repository at this point in the history
… into development
  • Loading branch information
brenoepics committed Jun 9, 2024
2 parents dbb4f85 + 2bb2da4 commit c313939
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 97 deletions.
170 changes: 85 additions & 85 deletions src/main/java/emu/grasscutter/game/inventory/Inventory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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;
Expand All @@ -23,21 +25,17 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import lombok.Getter;
import lombok.val;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.val;

public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
private final Long2ObjectMap<GameItem> store;
@Getter
private final Int2ObjectMap<InventoryTab> inventoryTypes;
@Getter private final Int2ObjectMap<InventoryTab> inventoryTypes;

public Inventory(Player player) {
super(player);
Expand All @@ -47,11 +45,11 @@ public Inventory(Player player) {

this.createInventoryTab(ItemType.ITEM_WEAPON, new EquipInventoryTab(INVENTORY_LIMITS.weapons));
this.createInventoryTab(
ItemType.ITEM_RELIQUARY, new EquipInventoryTab(INVENTORY_LIMITS.relics));
ItemType.ITEM_RELIQUARY, new EquipInventoryTab(INVENTORY_LIMITS.relics));
this.createInventoryTab(
ItemType.ITEM_MATERIAL, new MaterialInventoryTab(INVENTORY_LIMITS.materials));
ItemType.ITEM_MATERIAL, new MaterialInventoryTab(INVENTORY_LIMITS.materials));
this.createInventoryTab(
ItemType.ITEM_FURNITURE, new MaterialInventoryTab(INVENTORY_LIMITS.furniture));
ItemType.ITEM_FURNITURE, new MaterialInventoryTab(INVENTORY_LIMITS.furniture));
}

public AvatarStorage getAvatarStorage() {
Expand All @@ -78,26 +76,24 @@ public void createInventoryTab(ItemType type, InventoryTab tab) {
*/
public GameItem getFirstItem(int itemId) {
return this.getItems().values().stream()
.filter(item -> item.getItemId() == itemId)
.findFirst()
.orElse(null);
.filter(item -> item.getItemId() == itemId)
.findFirst()
.orElse(null);
}

public GameItem getItemByGuid(long id) {
return this.getItems().get(id);
}

@Nullable
public InventoryTab getInventoryTabByItemId(int itemId) {
@Nullable public InventoryTab getInventoryTabByItemId(int itemId) {
val itemData = GameData.getItemDataMap().get(itemId);
if (itemData == null || itemData.getItemType() == null) {
return null;
}
return getInventoryTab(itemData.getItemType());
}

@Nullable
public GameItem getItemById(int itemId) {
@Nullable public GameItem getItemById(int itemId) {
val inventoryTab = this.getInventoryTabByItemId(itemId);
return inventoryTab != null ? inventoryTab.getItemById(itemId) : null;
}
Expand Down Expand Up @@ -151,9 +147,9 @@ public boolean addItem(GameItem item, ActionReason reason, boolean forceNotify)

if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_AVATAR) {
getPlayer()
.sendPacket(
new PacketAddNoGachaAvatarCardNotify(
(item.getItemId() % 1000) + 10000000, reason, item));
.sendPacket(
new PacketAddNoGachaAvatarCardNotify(
(item.getItemId() % 1000) + 10000000, reason, item));
}

if (reason != null && (forceNotify || result)) {
Expand Down Expand Up @@ -202,7 +198,7 @@ public void addItems(Collection<GameItem> items, ActionReason reason) {
* Checks to see if the player has the item in their inventory. This will succeed if the player
* has at least the minimum count of the item.
*
* @param itemId The item id to check for.
* @param itemId The item id to check for.
* @param minCount The minimum count of the item to check for.
* @return True if the player has the item, false otherwise.
*/
Expand All @@ -213,10 +209,10 @@ public boolean hasItem(int itemId, int minCount) {
/**
* Checks to see if the player has the item in their inventory.
*
* @param itemId The item id to check for.
* @param count The count of the item to check for.
* @param itemId The item id to check for.
* @param count The count of the item to check for.
* @param enforce If true, the player must have the exact amount. If false, the player must have
* at least the amount.
* at least the amount.
* @return True if the player has the item, false otherwise.
*/
public boolean hasItem(int itemId, int count, boolean enforce) {
Expand All @@ -243,15 +239,15 @@ public boolean hasAllItems(Collection<ItemParam> items) {
private void triggerAddItemEvents(GameItem result) {
try {
getPlayer()
.getBattlePassManager()
.triggerMission(
WatcherTriggerType.TRIGGER_OBTAIN_MATERIAL_NUM,
result.getItemId(),
result.getCount());
.getBattlePassManager()
.triggerMission(
WatcherTriggerType.TRIGGER_OBTAIN_MATERIAL_NUM,
result.getItemId(),
result.getCount());
getPlayer()
.getQuestManager()
.queueEvent(
QuestContent.QUEST_CONTENT_OBTAIN_ITEM, result.getItemId(), result.getCount());
.getQuestManager()
.queueEvent(
QuestContent.QUEST_CONTENT_OBTAIN_ITEM, result.getItemId(), result.getCount());
} catch (Exception e) {
Grasscutter.getLogger().debug("triggerAddItemEvents failed", e);
}
Expand All @@ -260,20 +256,20 @@ private void triggerAddItemEvents(GameItem result) {
private void triggerRemItemEvents(GameItem item, int removeCount) {
try {
getPlayer()
.getBattlePassManager()
.triggerMission(WatcherTriggerType.TRIGGER_COST_MATERIAL, item.getItemId(), removeCount);
.getBattlePassManager()
.triggerMission(WatcherTriggerType.TRIGGER_COST_MATERIAL, item.getItemId(), removeCount);
getPlayer()
.getQuestManager()
.queueEvent(QuestContent.QUEST_CONTENT_ITEM_LESS_THAN, item.getItemId(), item.getCount());
.getQuestManager()
.queueEvent(QuestContent.QUEST_CONTENT_ITEM_LESS_THAN, item.getItemId(), item.getCount());
} catch (Exception e) {
Grasscutter.getLogger().debug("triggerRemItemEvents failed", e);
}
}

public void addItemParams(Collection<ItemParam> items) {
addItems(
items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(),
null);
items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(),
null);
}

public void addItemParamDatas(Collection<ItemParamData> items) {
Expand All @@ -282,8 +278,8 @@ public void addItemParamDatas(Collection<ItemParamData> items) {

public void addItemParamDatas(Collection<ItemParamData> items, ActionReason reason) {
addItems(
items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(),
reason);
items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(),
reason);
}

private synchronized GameItem putItem(GameItem item) {
Expand Down Expand Up @@ -332,7 +328,9 @@ private synchronized GameItem putItem(GameItem item) {
case MATERIAL_COSTUME:
case MATERIAL_NAMECARD:
Grasscutter.getLogger()
.warn("Attempted to add a {} to inventory, but item definition lacks isUseOnGain. This indicates a Resources error.", item.getItemData().getMaterialType().name());
.warn(
"Attempted to add a {} to inventory, but item definition lacks isUseOnGain. This indicates a Resources error.",
item.getItemData().getMaterialType().name());
return null;
default:
if (tab == null) {
Expand All @@ -352,9 +350,9 @@ private synchronized GameItem putItem(GameItem item) {
} else {
// Add count
existingItem.setCount(
Math.min(
existingItem.getCount() + item.getCount(),
item.getItemData().getStackLimit()));
Math.min(
existingItem.getCount() + item.getCount(),
item.getItemData().getStackLimit()));
existingItem.save();
return existingItem;
}
Expand All @@ -377,57 +375,57 @@ private synchronized void putItem(GameItem item, InventoryTab tab) {
private void addVirtualItem(int itemId, int count) {
switch (itemId) {
case 101 -> // Character exp
this.player.getTeamManager().getActiveTeam().stream()
this.player.getTeamManager().getActiveTeam().stream()
.map(EntityAvatar::getAvatar)
.forEach(
avatar ->
this.player
.getServer()
.getInventorySystem()
.upgradeAvatar(this.player, avatar, count));
avatar ->
this.player
.getServer()
.getInventorySystem()
.upgradeAvatar(this.player, avatar, count));
case 102 -> // Adventure exp
this.player.addExpDirectly(count);
this.player.addExpDirectly(count);
case 105 -> // Companionship exp
this.player.getTeamManager().getActiveTeam().stream()
this.player.getTeamManager().getActiveTeam().stream()
.map(EntityAvatar::getAvatar)
.forEach(
avatar ->
this.player
.getServer()
.getInventorySystem()
.upgradeAvatarFetterLevel(
this.player, avatar, count * (this.player.isInMultiplayer() ? 2 : 1)));
avatar ->
this.player
.getServer()
.getInventorySystem()
.upgradeAvatarFetterLevel(
this.player, avatar, count * (this.player.isInMultiplayer() ? 2 : 1)));
case 106 -> // Resin
this.player.getResinManager().addResin(count);
this.player.getResinManager().addResin(count);
case 107 -> // Legendary Key
this.player.addLegendaryKey(count);
this.player.addLegendaryKey(count);
case 121 -> // Home exp
this.player.getHome().addExp(this.player, count);
this.player.getHome().addExp(this.player, count);
case 201 -> // Primogem
this.player.setPrimogems(this.player.getPrimogems() + count);
this.player.setPrimogems(this.player.getPrimogems() + count);
case 202 -> // Mora
this.player.setMora(this.player.getMora() + count);
this.player.setMora(this.player.getMora() + count);
case 203 -> // Genesis Crystals
this.player.setCrystals(this.player.getCrystals() + count);
this.player.setCrystals(this.player.getCrystals() + count);
case 204 -> // Home Coin
this.player.setHomeCoin(this.player.getHomeCoin() + count);
this.player.setHomeCoin(this.player.getHomeCoin() + count);
}
}

private GameItem payVirtualItem(int itemId, int count) {
switch (itemId) {
case 201 -> // Primogem
player.setPrimogems(player.getPrimogems() - count);
player.setPrimogems(player.getPrimogems() - count);
case 202 -> // Mora
player.setMora(player.getMora() - count);
player.setMora(player.getMora() - count);
case 203 -> // Genesis Crystals
player.setCrystals(player.getCrystals() - count);
player.setCrystals(player.getCrystals() - count);
case 106 -> // Resin
player.getResinManager().useResin(count);
player.getResinManager().useResin(count);
case 107 -> // LegendaryKey
player.useLegendaryKey(count);
player.useLegendaryKey(count);
case 204 -> // Home Coin
player.setHomeCoin(player.getHomeCoin() - count);
player.setHomeCoin(player.getHomeCoin() - count);
default -> {
var gameItem = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId);
removeItem(gameItem, count);
Expand All @@ -440,22 +438,24 @@ private GameItem payVirtualItem(int itemId, int count) {
private int getVirtualItemCount(int itemId) {
return switch (itemId) {
case 201 -> // Primogem
this.player.getPrimogems();
this.player.getPrimogems();
case 202 -> // Mora
this.player.getMora();
this.player.getMora();
case 203 -> // Genesis Crystals
this.player.getCrystals();
this.player.getCrystals();
case 106 -> // Resin
this.player.getProperty(PlayerProperty.PROP_PLAYER_RESIN);
this.player.getProperty(PlayerProperty.PROP_PLAYER_RESIN);
case 107 -> // Legendary Key
this.player.getProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY);
this.player.getProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY);
case 204 -> // Home Coin
this.player.getHomeCoin();
this.player.getHomeCoin();
default -> {
GameItem item =
getInventoryTab(ItemType.ITEM_MATERIAL)
.getItemById(
itemId); // What if we ever want to operate on weapons/relics/furniture? :Syield (item == null) ? 0 : item.getCount(); // What if we ever want to operate on weapons/relics/furniture? :S
getInventoryTab(ItemType.ITEM_MATERIAL)
.getItemById(
itemId); // What if we ever want to operate on weapons/relics/furniture? :Syield
// (item == null) ? 0 : item.getCount(); // What if we ever want to
// operate on weapons/relics/furniture? :S
yield (item == null) ? 0 : item.getCount();
}
};
Expand All @@ -480,7 +480,7 @@ public boolean payItems(ItemParamData[] costItems, int quantity) {
}

public synchronized boolean payItems(
ItemParamData[] costItems, int quantity, ActionReason reason) {
ItemParamData[] costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items
for (ItemParamData cost : costItems)
if (this.getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) return false;
Expand All @@ -505,7 +505,7 @@ public boolean payItems(Iterable<ItemParamData> costItems, int quantity) {
}

public synchronized boolean payItems(
Iterable<ItemParamData> costItems, int quantity, ActionReason reason) {
Iterable<ItemParamData> costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items
for (ItemParamData cost : costItems)
if (getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) return false;
Expand Down Expand Up @@ -542,7 +542,7 @@ public boolean removeItem(long guid) {
* the item's type.
*
* @param itemId The ID of the item to remove.
* @param count The amount of items to remove.
* @param count The amount of items to remove.
* @return True if the item was removed, false otherwise.
*/
public synchronized boolean removeItem(int itemId, int count) {
Expand All @@ -566,7 +566,7 @@ public synchronized boolean removeItem(long guid, int count) {
* Removes an item by its item ID.
*
* @param itemId The ID of the item to remove.
* @param count The amount of items to remove.
* @param count The amount of items to remove.
* @return True if the item was removed, false otherwise.
*/
public synchronized boolean removeItemById(int itemId, int count) {
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/emu/grasscutter/net/packet/BasePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@

import com.google.protobuf.GeneratedMessageV3;
import emu.grasscutter.net.proto.PacketHeadOuterClass.PacketHead;
import java.io.*;
import lombok.Getter;
import lombok.Setter;

import java.io.*;

public class BasePacket {
private static final int const1 = 17767; // 0x4567
private static final int const2 = -30293; // 0x89ab
public final boolean shouldEncrypt = true;
@Setter
@Getter
private int opcode;
@Setter @Getter private int opcode;
private boolean shouldBuildHeader = false;
@Setter
@Getter
private byte[] header;
@Getter
private byte[] data;
@Setter @Getter private byte[] header;
@Getter private byte[] data;
// Encryption
@Setter
private boolean useDispatchKey;
@Setter private boolean useDispatchKey;

public BasePacket(int opcode) {
this.opcode = opcode;
Expand Down

0 comments on commit c313939

Please sign in to comment.