Format code [skip actions]

This commit is contained in:
github-actions 2024-06-09 15:12:09 +00:00
parent bfc2c7c096
commit 2bb2da44bc
2 changed files with 90 additions and 97 deletions

View File

@ -1,5 +1,7 @@
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;
@ -23,21 +25,17 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import lombok.Getter;
import lombok.val;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.annotation.Nullable;
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS; import lombok.Getter;
import lombok.val;
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;
@Getter @Getter private final Int2ObjectMap<InventoryTab> inventoryTypes;
private final Int2ObjectMap<InventoryTab> inventoryTypes;
public Inventory(Player player) { public Inventory(Player player) {
super(player); super(player);
@ -47,11 +45,11 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
this.createInventoryTab(ItemType.ITEM_WEAPON, new EquipInventoryTab(INVENTORY_LIMITS.weapons)); this.createInventoryTab(ItemType.ITEM_WEAPON, new EquipInventoryTab(INVENTORY_LIMITS.weapons));
this.createInventoryTab( this.createInventoryTab(
ItemType.ITEM_RELIQUARY, new EquipInventoryTab(INVENTORY_LIMITS.relics)); ItemType.ITEM_RELIQUARY, new EquipInventoryTab(INVENTORY_LIMITS.relics));
this.createInventoryTab( this.createInventoryTab(
ItemType.ITEM_MATERIAL, new MaterialInventoryTab(INVENTORY_LIMITS.materials)); ItemType.ITEM_MATERIAL, new MaterialInventoryTab(INVENTORY_LIMITS.materials));
this.createInventoryTab( this.createInventoryTab(
ItemType.ITEM_FURNITURE, new MaterialInventoryTab(INVENTORY_LIMITS.furniture)); ItemType.ITEM_FURNITURE, new MaterialInventoryTab(INVENTORY_LIMITS.furniture));
} }
public AvatarStorage getAvatarStorage() { public AvatarStorage getAvatarStorage() {
@ -78,17 +76,16 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
*/ */
public GameItem getFirstItem(int itemId) { public GameItem getFirstItem(int itemId) {
return this.getItems().values().stream() return this.getItems().values().stream()
.filter(item -> item.getItemId() == itemId) .filter(item -> item.getItemId() == itemId)
.findFirst() .findFirst()
.orElse(null); .orElse(null);
} }
public GameItem getItemByGuid(long id) { public GameItem getItemByGuid(long id) {
return this.getItems().get(id); return this.getItems().get(id);
} }
@Nullable @Nullable public InventoryTab getInventoryTabByItemId(int itemId) {
public InventoryTab getInventoryTabByItemId(int itemId) {
val itemData = GameData.getItemDataMap().get(itemId); val itemData = GameData.getItemDataMap().get(itemId);
if (itemData == null || itemData.getItemType() == null) { if (itemData == null || itemData.getItemType() == null) {
return null; return null;
@ -96,8 +93,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
return getInventoryTab(itemData.getItemType()); return getInventoryTab(itemData.getItemType());
} }
@Nullable @Nullable public GameItem getItemById(int itemId) {
public GameItem getItemById(int itemId) {
val inventoryTab = this.getInventoryTabByItemId(itemId); val inventoryTab = this.getInventoryTabByItemId(itemId);
return inventoryTab != null ? inventoryTab.getItemById(itemId) : null; return inventoryTab != null ? inventoryTab.getItemById(itemId) : null;
} }
@ -151,9 +147,9 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_AVATAR) { if (item.getItemData().getMaterialType() == MaterialType.MATERIAL_AVATAR) {
getPlayer() getPlayer()
.sendPacket( .sendPacket(
new PacketAddNoGachaAvatarCardNotify( new PacketAddNoGachaAvatarCardNotify(
(item.getItemId() % 1000) + 10000000, reason, item)); (item.getItemId() % 1000) + 10000000, reason, item));
} }
if (reason != null && (forceNotify || result)) { if (reason != null && (forceNotify || result)) {
@ -202,7 +198,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
* Checks to see if the player has the item in their inventory. This will succeed if the player * 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. * 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. * @param minCount The minimum count of the item to check for.
* @return True if the player has the item, false otherwise. * @return True if the player has the item, false otherwise.
*/ */
@ -213,10 +209,10 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
/** /**
* Checks to see if the player has the item in their inventory. * Checks to see if the player has the item in their inventory.
* *
* @param itemId The item id to check for. * @param itemId The item id to check for.
* @param count The count of the item 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 * @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. * @return True if the player has the item, false otherwise.
*/ */
public boolean hasItem(int itemId, int count, boolean enforce) { public boolean hasItem(int itemId, int count, boolean enforce) {
@ -243,15 +239,15 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
private void triggerAddItemEvents(GameItem result) { private void triggerAddItemEvents(GameItem result) {
try { try {
getPlayer() getPlayer()
.getBattlePassManager() .getBattlePassManager()
.triggerMission( .triggerMission(
WatcherTriggerType.TRIGGER_OBTAIN_MATERIAL_NUM, WatcherTriggerType.TRIGGER_OBTAIN_MATERIAL_NUM,
result.getItemId(), result.getItemId(),
result.getCount()); result.getCount());
getPlayer() getPlayer()
.getQuestManager() .getQuestManager()
.queueEvent( .queueEvent(
QuestContent.QUEST_CONTENT_OBTAIN_ITEM, result.getItemId(), result.getCount()); QuestContent.QUEST_CONTENT_OBTAIN_ITEM, result.getItemId(), result.getCount());
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().debug("triggerAddItemEvents failed", e); Grasscutter.getLogger().debug("triggerAddItemEvents failed", e);
} }
@ -260,11 +256,11 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
private void triggerRemItemEvents(GameItem item, int removeCount) { private void triggerRemItemEvents(GameItem item, int removeCount) {
try { try {
getPlayer() getPlayer()
.getBattlePassManager() .getBattlePassManager()
.triggerMission(WatcherTriggerType.TRIGGER_COST_MATERIAL, item.getItemId(), removeCount); .triggerMission(WatcherTriggerType.TRIGGER_COST_MATERIAL, item.getItemId(), removeCount);
getPlayer() getPlayer()
.getQuestManager() .getQuestManager()
.queueEvent(QuestContent.QUEST_CONTENT_ITEM_LESS_THAN, item.getItemId(), item.getCount()); .queueEvent(QuestContent.QUEST_CONTENT_ITEM_LESS_THAN, item.getItemId(), item.getCount());
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().debug("triggerRemItemEvents failed", e); Grasscutter.getLogger().debug("triggerRemItemEvents failed", e);
} }
@ -272,8 +268,8 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
public void addItemParams(Collection<ItemParam> items) { public void addItemParams(Collection<ItemParam> items) {
addItems( addItems(
items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(), items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(),
null); null);
} }
public void addItemParamDatas(Collection<ItemParamData> items) { public void addItemParamDatas(Collection<ItemParamData> items) {
@ -282,8 +278,8 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
public void addItemParamDatas(Collection<ItemParamData> items, ActionReason reason) { public void addItemParamDatas(Collection<ItemParamData> items, ActionReason reason) {
addItems( addItems(
items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(), items.stream().map(param -> new GameItem(param.getItemId(), param.getCount())).toList(),
reason); reason);
} }
private synchronized GameItem putItem(GameItem item) { private synchronized GameItem putItem(GameItem item) {
@ -332,7 +328,9 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
case MATERIAL_COSTUME: case MATERIAL_COSTUME:
case MATERIAL_NAMECARD: case MATERIAL_NAMECARD:
Grasscutter.getLogger() 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; return null;
default: default:
if (tab == null) { if (tab == null) {
@ -352,9 +350,9 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
} else { } else {
// Add count // Add count
existingItem.setCount( existingItem.setCount(
Math.min( Math.min(
existingItem.getCount() + item.getCount(), existingItem.getCount() + item.getCount(),
item.getItemData().getStackLimit())); item.getItemData().getStackLimit()));
existingItem.save(); existingItem.save();
return existingItem; return existingItem;
} }
@ -377,57 +375,57 @@ public class Inventory extends BasePlayerManager 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
this.player.getTeamManager().getActiveTeam().stream() this.player.getTeamManager().getActiveTeam().stream()
.map(EntityAvatar::getAvatar) .map(EntityAvatar::getAvatar)
.forEach( .forEach(
avatar -> avatar ->
this.player this.player
.getServer() .getServer()
.getInventorySystem() .getInventorySystem()
.upgradeAvatar(this.player, avatar, count)); .upgradeAvatar(this.player, avatar, count));
case 102 -> // Adventure exp case 102 -> // Adventure exp
this.player.addExpDirectly(count); this.player.addExpDirectly(count);
case 105 -> // Companionship exp case 105 -> // Companionship exp
this.player.getTeamManager().getActiveTeam().stream() this.player.getTeamManager().getActiveTeam().stream()
.map(EntityAvatar::getAvatar) .map(EntityAvatar::getAvatar)
.forEach( .forEach(
avatar -> avatar ->
this.player this.player
.getServer() .getServer()
.getInventorySystem() .getInventorySystem()
.upgradeAvatarFetterLevel( .upgradeAvatarFetterLevel(
this.player, avatar, count * (this.player.isInMultiplayer() ? 2 : 1))); this.player, avatar, count * (this.player.isInMultiplayer() ? 2 : 1)));
case 106 -> // Resin case 106 -> // Resin
this.player.getResinManager().addResin(count); this.player.getResinManager().addResin(count);
case 107 -> // Legendary Key case 107 -> // Legendary Key
this.player.addLegendaryKey(count); this.player.addLegendaryKey(count);
case 121 -> // Home exp case 121 -> // Home exp
this.player.getHome().addExp(this.player, count); this.player.getHome().addExp(this.player, count);
case 201 -> // Primogem case 201 -> // Primogem
this.player.setPrimogems(this.player.getPrimogems() + count); this.player.setPrimogems(this.player.getPrimogems() + count);
case 202 -> // Mora case 202 -> // Mora
this.player.setMora(this.player.getMora() + count); this.player.setMora(this.player.getMora() + count);
case 203 -> // Genesis Crystals case 203 -> // Genesis Crystals
this.player.setCrystals(this.player.getCrystals() + count); this.player.setCrystals(this.player.getCrystals() + count);
case 204 -> // Home Coin 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) { private GameItem payVirtualItem(int itemId, int count) {
switch (itemId) { switch (itemId) {
case 201 -> // Primogem case 201 -> // Primogem
player.setPrimogems(player.getPrimogems() - count); player.setPrimogems(player.getPrimogems() - count);
case 202 -> // Mora case 202 -> // Mora
player.setMora(player.getMora() - count); player.setMora(player.getMora() - count);
case 203 -> // Genesis Crystals case 203 -> // Genesis Crystals
player.setCrystals(player.getCrystals() - count); player.setCrystals(player.getCrystals() - count);
case 106 -> // Resin case 106 -> // Resin
player.getResinManager().useResin(count); player.getResinManager().useResin(count);
case 107 -> // LegendaryKey case 107 -> // LegendaryKey
player.useLegendaryKey(count); player.useLegendaryKey(count);
case 204 -> // Home Coin case 204 -> // Home Coin
player.setHomeCoin(player.getHomeCoin() - count); player.setHomeCoin(player.getHomeCoin() - count);
default -> { default -> {
var gameItem = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId); var gameItem = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId);
removeItem(gameItem, count); removeItem(gameItem, count);
@ -440,22 +438,24 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
private int getVirtualItemCount(int itemId) { private int getVirtualItemCount(int itemId) {
return switch (itemId) { return switch (itemId) {
case 201 -> // Primogem case 201 -> // Primogem
this.player.getPrimogems(); this.player.getPrimogems();
case 202 -> // Mora case 202 -> // Mora
this.player.getMora(); this.player.getMora();
case 203 -> // Genesis Crystals case 203 -> // Genesis Crystals
this.player.getCrystals(); this.player.getCrystals();
case 106 -> // Resin case 106 -> // Resin
this.player.getProperty(PlayerProperty.PROP_PLAYER_RESIN); this.player.getProperty(PlayerProperty.PROP_PLAYER_RESIN);
case 107 -> // Legendary Key case 107 -> // Legendary Key
this.player.getProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY); this.player.getProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY);
case 204 -> // Home Coin case 204 -> // Home Coin
this.player.getHomeCoin(); this.player.getHomeCoin();
default -> { default -> {
GameItem item = GameItem item =
getInventoryTab(ItemType.ITEM_MATERIAL) getInventoryTab(ItemType.ITEM_MATERIAL)
.getItemById( .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 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(); yield (item == null) ? 0 : item.getCount();
} }
}; };
@ -480,7 +480,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
} }
public synchronized boolean payItems( public synchronized boolean payItems(
ItemParamData[] costItems, int quantity, ActionReason reason) { ItemParamData[] costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items // Make sure player has requisite items
for (ItemParamData cost : costItems) for (ItemParamData cost : costItems)
if (this.getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) return false; if (this.getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) return false;
@ -505,7 +505,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
} }
public synchronized boolean payItems( public synchronized boolean payItems(
Iterable<ItemParamData> costItems, int quantity, ActionReason reason) { Iterable<ItemParamData> costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items // Make sure player has requisite items
for (ItemParamData cost : costItems) for (ItemParamData cost : costItems)
if (getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) return false; if (getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) return false;
@ -542,7 +542,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
* the item's type. * the item's type.
* *
* @param itemId The ID of the item to remove. * @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. * @return True if the item was removed, false otherwise.
*/ */
public synchronized boolean removeItem(int itemId, int count) { public synchronized boolean removeItem(int itemId, int count) {
@ -566,7 +566,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
* Removes an item by its item ID. * Removes an item by its item ID.
* *
* @param itemId The ID of the item to remove. * @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. * @return True if the item was removed, false otherwise.
*/ */
public synchronized boolean removeItemById(int itemId, int count) { public synchronized boolean removeItemById(int itemId, int count) {

View File

@ -2,27 +2,20 @@ package emu.grasscutter.net.packet;
import com.google.protobuf.GeneratedMessageV3; import com.google.protobuf.GeneratedMessageV3;
import emu.grasscutter.net.proto.PacketHeadOuterClass.PacketHead; import emu.grasscutter.net.proto.PacketHeadOuterClass.PacketHead;
import java.io.*;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.*;
public class BasePacket { public class BasePacket {
private static final int const1 = 17767; // 0x4567 private static final int const1 = 17767; // 0x4567
private static final int const2 = -30293; // 0x89ab private static final int const2 = -30293; // 0x89ab
public final boolean shouldEncrypt = true; public final boolean shouldEncrypt = true;
@Setter @Setter @Getter private int opcode;
@Getter
private int opcode;
private boolean shouldBuildHeader = false; private boolean shouldBuildHeader = false;
@Setter @Setter @Getter private byte[] header;
@Getter @Getter private byte[] data;
private byte[] header;
@Getter
private byte[] data;
// Encryption // Encryption
@Setter @Setter private boolean useDispatchKey;
private boolean useDispatchKey;
public BasePacket(int opcode) { public BasePacket(int opcode) {
this.opcode = opcode; this.opcode = opcode;