Commit d9fc1595 authored by Melledy's avatar Melledy
Browse files

Refactor some commands and move inventory/team limits to the config

parent 7417a1b6
......@@ -40,6 +40,13 @@ public final class Config {
}
public static class ServerOptions {
public int InventoryLimitWeapon = 2000;
public int InventoryLimitRelic = 2000;
public int InventoryLimitMaterial = 2000;
public int InventoryLimitFurniture = 2000;
public int InventoryLimitAll = 30000;
public int MaxAvatarsInTeam = 4;
public int MaxAvatarsInTeamMultiplayer = 4;
public int MaxEntityLimit = 1000; // Max entity limit per world. // TODO: Enforce later.
public int[] WelcomeEmotes = {2007, 1002, 4010};
public String WelcomeMotd = "Welcome to Grasscutter emu";
......
......@@ -9,14 +9,6 @@ public final class GenshinConstants {
public static String VERSION = "2.6.0";
public static final int MAX_TEAMS = 4;
public static final int MAX_AVATARS_IN_TEAM = 4;
public static final int LIMIT_WEAPON = 2000;
public static final int LIMIT_RELIC = 2000;
public static final int LIMIT_MATERIAL = 2000;
public static final int LIMIT_FURNITURE = 2000;
public static final int LIMIT_ALL = 30000;
public static final int MAIN_CHARACTER_MALE = 10000005;
public static final int MAIN_CHARACTER_FEMALE = 10000007;
public static final Position START_POSITION = new Position(2747, 194, -1719);
......
......@@ -42,6 +42,7 @@ public final class Grasscutter {
// Load server configuration.
Grasscutter.loadConfig();
// Check server structure.
Utils.startupCheck();
}
......
......@@ -52,8 +52,8 @@ public final class PlayerCommands {
case 2:
try {
target = Integer.parseInt(args.get(0));
if(Grasscutter.getGameServer().getPlayerById(target) == null) {
target = player.getId(); amount = Integer.parseInt(args.get(1));
if(Grasscutter.getGameServer().getPlayerByUid(target) == null) {
target = player.getUid(); amount = Integer.parseInt(args.get(1));
item = Integer.parseInt(args.get(0));
} else {
item = Integer.parseInt(args.get(1));
......@@ -67,7 +67,7 @@ public final class PlayerCommands {
case 3:
try {
target = Integer.parseInt(args.get(0));
if(Grasscutter.getGameServer().getPlayerById(target) == null) {
if(Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(player, "Invalid player ID."); return;
}
......@@ -81,7 +81,7 @@ public final class PlayerCommands {
break;
}
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(player, "Player not found."); return;
}
......@@ -108,7 +108,7 @@ public final class PlayerCommands {
int item = Integer.parseInt(args.get(1));
int amount = 1; if(args.size() > 2) amount = Integer.parseInt(args.get(2));
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(null, "Player not found."); return;
}
......@@ -233,7 +233,7 @@ public final class PlayerCommands {
int playerUid = Integer.parseInt(args.get(0));
int sceneId = Integer.parseInt(args.get(1));
GenshinPlayer player = Grasscutter.getGameServer().getPlayerById(playerUid);
GenshinPlayer player = Grasscutter.getGameServer().getPlayerByUid(playerUid);
if (player == null) {
CommandHandler.sendMessage(null, "Player not found or offline.");
return;
......
......@@ -43,7 +43,7 @@ public final class ServerCommands {
int target = Integer.parseInt(args.get(0));
String message = String.join(" ", args.subList(1, args.size()));
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(null, "Player not found."); return;
}
......@@ -65,7 +65,7 @@ public final class ServerCommands {
int target = Integer.parseInt(args.get(0));
String message = String.join(" ", args.subList(1, args.size()));
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target);
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(player, "Player not found."); return;
}
......
......@@ -124,13 +124,13 @@ public class DatabaseHelper {
int id = 0;
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId;
character.setId(id);
character.setUid(id);
} else {
do {
id = DatabaseManager.getNextId(character);
}
while (checkPlayerExists(id));
character.setId(id);
character.setUid(id);
}
// Save to database
DatabaseManager.getDatastore().save(character);
......@@ -160,7 +160,7 @@ public class DatabaseHelper {
}
public static List<GenshinAvatar> getAvatars(GenshinPlayer player) {
Query<GenshinAvatar> query = DatabaseManager.getDatastore().createQuery(GenshinAvatar.class).filter("ownerId", player.getId());
Query<GenshinAvatar> query = DatabaseManager.getDatastore().createQuery(GenshinAvatar.class).filter("ownerId", player.getUid());
return query.find().toList();
}
......@@ -174,16 +174,16 @@ public class DatabaseHelper {
}
public static List<GenshinItem> getInventoryItems(GenshinPlayer player) {
Query<GenshinItem> query = DatabaseManager.getDatastore().createQuery(GenshinItem.class).filter("ownerId", player.getId());
Query<GenshinItem> query = DatabaseManager.getDatastore().createQuery(GenshinItem.class).filter("ownerId", player.getUid());
return query.find().toList();
}
public static List<Friendship> getFriends(GenshinPlayer player) {
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class).filter("ownerId", player.getId());
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class).filter("ownerId", player.getUid());
return query.find().toList();
}
public static List<Friendship> getReverseFriends(GenshinPlayer player) {
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class).filter("friendId", player.getId());
Query<Friendship> query = DatabaseManager.getDatastore().createQuery(Friendship.class).filter("friendId", player.getUid());
return query.find().toList();
}
......
......@@ -156,17 +156,17 @@ public class GenshinPlayer {
this.getRotation().set(0, 307, 0);
}
public int getId() {
public int getUid() {
return id;
}
public void setId(int id) {
public void setUid(int id) {
this.id = id;
}
public long getNextGuid() {
public long getNextGenshinGuid() {
long nextId = ++this.nextGuid;
return ((long) this.getId() << 32) + nextId;
return ((long) this.getUid() << 32) + nextId;
}
public Account getAccount() {
......@@ -175,7 +175,7 @@ public class GenshinPlayer {
public void setAccount(Account account) {
this.account = account;
this.account.setPlayerId(getId());
this.account.setPlayerId(getUid());
}
public GameSession getSession() {
......@@ -560,7 +560,7 @@ public class GenshinPlayer {
}
public void dropMessage(Object message) {
this.sendPacket(new PacketPrivateChatNotify(GenshinConstants.SERVER_CONSOLE_UID, getId(), message.toString()));
this.sendPacket(new PacketPrivateChatNotify(GenshinConstants.SERVER_CONSOLE_UID, getUid(), message.toString()));
}
/**
......@@ -569,7 +569,7 @@ public class GenshinPlayer {
* @param message The message to send.
*/
public void sendMessage(GenshinPlayer sender, Object message) {
this.sendPacket(new PacketPrivateChatNotify(sender.getId(), this.getId(), message.toString()));
this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString()));
}
public void interactWith(int gadgetEntityId) {
......@@ -614,7 +614,7 @@ public class GenshinPlayer {
public OnlinePlayerInfo getOnlinePlayerInfo() {
OnlinePlayerInfo.Builder onlineInfo = OnlinePlayerInfo.newBuilder()
.setUid(this.getId())
.setUid(this.getUid())
.setNickname(this.getNickname())
.setPlayerLevel(this.getLevel())
.setMpSettingType(this.getMpSetting())
......@@ -633,7 +633,7 @@ public class GenshinPlayer {
public SocialDetail.Builder getSocialDetail() {
SocialDetail.Builder social = SocialDetail.newBuilder()
.setUid(this.getId())
.setUid(this.getUid())
.setAvatar(HeadImage.newBuilder().setAvatarId(this.getHeadImage()))
.setNickname(this.getNickname())
.setSignature(this.getSignature())
......@@ -649,7 +649,7 @@ public class GenshinPlayer {
public PlayerLocationInfo getPlayerLocationInfo() {
return PlayerLocationInfo.newBuilder()
.setUid(this.getId())
.setUid(this.getUid())
.setPos(this.getPos().toProto())
.setRot(this.getRotation().toProto())
.build();
......@@ -699,7 +699,7 @@ public class GenshinPlayer {
// Check if player object exists in server
// TODO - optimize
GenshinPlayer exists = this.getServer().getPlayerById(getId());
GenshinPlayer exists = this.getServer().getPlayerByUid(getUid());
if (exists != null) {
exists.getSession().close();
}
......
......@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.avatar.GenshinAvatar;
public class TeamInfo {
......@@ -12,7 +13,7 @@ public class TeamInfo {
public TeamInfo() {
this.name = "";
this.avatars = new ArrayList<>(GenshinConstants.MAX_AVATARS_IN_TEAM);
this.avatars = new ArrayList<>(Grasscutter.getConfig().getServerOptions().MaxAvatarsInTeam);
}
public String getName() {
......@@ -36,7 +37,7 @@ public class TeamInfo {
}
public boolean addAvatar(GenshinAvatar avatar) {
if (size() >= GenshinConstants.MAX_AVATARS_IN_TEAM || contains(avatar)) {
if (size() >= Grasscutter.getConfig().getServerOptions().MaxAvatarsInTeam || contains(avatar)) {
return false;
}
......@@ -56,7 +57,7 @@ public class TeamInfo {
}
public void copyFrom(TeamInfo team) {
copyFrom(team, GenshinConstants.MAX_AVATARS_IN_TEAM);
copyFrom(team, Grasscutter.getConfig().getServerOptions().MaxAvatarsInTeam);
}
public void copyFrom(TeamInfo team, int maxTeamSize) {
......
......@@ -10,6 +10,7 @@ import java.util.Set;
import dev.morphia.annotations.Transient;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.def.AvatarSkillDepotData;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.entity.EntityAvatar;
......@@ -163,12 +164,13 @@ public class TeamManager {
public int getMaxTeamSize() {
if (getPlayer().isInMultiplayer()) {
int max = Grasscutter.getConfig().getServerOptions().MaxAvatarsInTeamMultiplayer;
if (getPlayer().getWorld().getHost() == this.getPlayer()) {
return Math.max(1, (int) Math.ceil(GenshinConstants.MAX_AVATARS_IN_TEAM / (double) getWorld().getPlayerCount()));
return Math.max(1, (int) Math.ceil(max / (double) getWorld().getPlayerCount()));
}
return Math.max(1, (int) Math.floor(GenshinConstants.MAX_AVATARS_IN_TEAM / (double) getWorld().getPlayerCount()));
return Math.max(1, (int) Math.floor(max / (double) getWorld().getPlayerCount()));
}
return GenshinConstants.MAX_AVATARS_IN_TEAM;
return Grasscutter.getConfig().getServerOptions().MaxAvatarsInTeam;
}
// Methods
......
......@@ -148,8 +148,8 @@ public class GenshinAvatar {
public void setOwner(GenshinPlayer player) {
this.owner = player;
this.ownerId = player.getId();
this.guid = player.getNextGuid();
this.ownerId = player.getUid();
this.guid = player.getNextGenshinGuid();
}
public int getSatiation() {
......
......@@ -107,7 +107,7 @@ public class EntityAvatar extends GenshinEntity {
public SceneAvatarInfo getSceneAvatarInfo() {
SceneAvatarInfo.Builder avatarInfo = SceneAvatarInfo.newBuilder()
.setPlayerId(this.getPlayer().getId())
.setPlayerId(this.getPlayer().getUid())
.setAvatarId(this.getAvatar().getAvatarId())
.setGuid(this.getAvatar().getGuid())
.setPeerId(this.getPlayer().getPeerId())
......
......@@ -36,7 +36,7 @@ public class EntityItem extends EntityGadget {
this.id = getScene().getWorld().getNextEntityId(EntityIdType.GADGET);
this.pos = new Position(pos);
this.rot = new Position();
this.guid = player.getNextGuid();
this.guid = player.getNextGenshinGuid();
this.item = new GenshinItem(itemData, count);
}
......
......@@ -79,11 +79,11 @@ public class FriendsList {
}
// Make sure asker cant do anything
if (myFriendship.getAskerId() == this.getPlayer().getId()) {
if (myFriendship.getAskerId() == this.getPlayer().getUid()) {
return;
}
GenshinPlayer target = getPlayer().getSession().getServer().forceGetPlayerById(targetUid);
GenshinPlayer target = getPlayer().getSession().getServer().getPlayerByUid(targetUid, true);
if (target == null) {
return; // Should never happen
}
......@@ -91,7 +91,7 @@ public class FriendsList {
// Get target's friendship
Friendship theirFriendship = null;
if (target.isOnline()) {
theirFriendship = target.getFriendsList().getPendingFriendById(this.getPlayer().getId());
theirFriendship = target.getFriendsList().getPendingFriendById(this.getPlayer().getUid());
} else {
theirFriendship = DatabaseHelper.getReverseFriendship(myFriendship);
}
......@@ -112,7 +112,7 @@ public class FriendsList {
this.addFriend(myFriendship);
if (target.isOnline()) {
target.getFriendsList().getPendingFriends().remove(this.getPlayer().getId());
target.getFriendsList().getPendingFriends().remove(this.getPlayer().getUid());
target.getFriendsList().addFriend(theirFriendship);
}
......@@ -124,7 +124,7 @@ public class FriendsList {
myFriendship.delete();
// Delete from target uid
if (target.isOnline()) {
theirFriendship = target.getFriendsList().getPendingFriendById(this.getPlayer().getId());
theirFriendship = target.getFriendsList().getPendingFriendById(this.getPlayer().getUid());
}
theirFriendship.delete();
}
......@@ -146,7 +146,7 @@ public class FriendsList {
GenshinPlayer friend = myFriendship.getFriendProfile().getPlayer();
if (friend != null) {
// Friend online
theirFriendship = friend.getFriendsList().getFriendById(this.getPlayer().getId());
theirFriendship = friend.getFriendsList().getFriendById(this.getPlayer().getUid());
if (theirFriendship != null) {
friend.getFriendsList().getFriends().remove(theirFriendship.getFriendId());
theirFriendship.delete();
......@@ -165,7 +165,7 @@ public class FriendsList {
}
public synchronized void sendFriendRequest(int targetUid) {
GenshinPlayer target = getPlayer().getSession().getServer().forceGetPlayerById(targetUid);
GenshinPlayer target = getPlayer().getSession().getServer().getPlayerByUid(targetUid, true);
if (target == null || target == this.getPlayer()) {
return;
......@@ -220,14 +220,14 @@ public class FriendsList {
friendship.setOwner(getPlayer());
// Check if friend is online
GenshinPlayer friend = getPlayer().getSession().getServer().getPlayerById(friendship.getFriendProfile().getId());
GenshinPlayer friend = getPlayer().getSession().getServer().getPlayerByUid(friendship.getFriendProfile().getId());
if (friend != null) {
// Set friend to online mode
friendship.setFriendProfile(friend);
// Update our status on friend's client if theyre online
if (friend.getFriendsList().hasLoaded()) {
Friendship theirFriendship = friend.getFriendsList().getFriendshipById(getPlayer().getId());
Friendship theirFriendship = friend.getFriendsList().getFriendshipById(getPlayer().getUid());
if (theirFriendship != null) {
// Update friend profile
theirFriendship.setFriendProfile(getPlayer());
......
......@@ -27,10 +27,10 @@ public class Friendship {
public Friendship(GenshinPlayer owner, GenshinPlayer friend, GenshinPlayer asker) {
this.setOwner(owner);
this.ownerId = owner.getId();
this.friendId = friend.getId();
this.ownerId = owner.getUid();
this.friendId = friend.getUid();
this.profile = friend.getProfile();
this.askerId = asker.getId();
this.askerId = asker.getUid();
}
public GenshinPlayer getOwner() {
......@@ -70,7 +70,7 @@ public class Friendship {
}
public void setFriendProfile(GenshinPlayer character) {
if (character == null || this.friendId != character.getId()) return;
if (character == null || this.friendId != character.getUid()) return;
this.profile = character.getProfile();
}
......
......@@ -22,7 +22,7 @@ public class PlayerProfile {
public PlayerProfile() { }
public PlayerProfile(GenshinPlayer player) {
this.id = player.getId();
this.id = player.getUid();
this.syncWithCharacter(player);
}
......
......@@ -125,8 +125,8 @@ public class GenshinItem {
}
public void setOwner(GenshinPlayer player) {
this.ownerId = player.getId();
this.guid = player.getNextGuid();
this.ownerId = player.getUid();
this.guid = player.getNextGenshinGuid();
}
public int getItemId() {
return itemId;
......
......@@ -6,6 +6,7 @@ import java.util.LinkedList;
import java.util.List;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.AvatarCostumeData;
import emu.grasscutter.data.def.AvatarData;
......@@ -36,10 +37,10 @@ public class Inventory implements Iterable<GenshinItem> {
this.store = new Long2ObjectOpenHashMap<>();
this.inventoryTypes = new Int2ObjectOpenHashMap<>();
this.createInventoryTab(ItemType.ITEM_WEAPON, new EquipInventoryTab(GenshinConstants.LIMIT_WEAPON));
this.createInventoryTab(ItemType.ITEM_RELIQUARY, new EquipInventoryTab(GenshinConstants.LIMIT_RELIC));
this.createInventoryTab(ItemType.ITEM_MATERIAL, new MaterialInventoryTab(GenshinConstants.LIMIT_MATERIAL));
this.createInventoryTab(ItemType.ITEM_FURNITURE, new MaterialInventoryTab(GenshinConstants.LIMIT_FURNITURE));
this.createInventoryTab(ItemType.ITEM_WEAPON, new EquipInventoryTab(Grasscutter.getConfig().getServerOptions().InventoryLimitWeapon));
this.createInventoryTab(ItemType.ITEM_RELIQUARY, new EquipInventoryTab(Grasscutter.getConfig().getServerOptions().InventoryLimitRelic));
this.createInventoryTab(ItemType.ITEM_MATERIAL, new MaterialInventoryTab(Grasscutter.getConfig().getServerOptions().InventoryLimitMaterial));
this.createInventoryTab(ItemType.ITEM_FURNITURE, new MaterialInventoryTab(Grasscutter.getConfig().getServerOptions().InventoryLimitFurniture));
}
public GenshinPlayer getPlayer() {
......
......@@ -36,14 +36,14 @@ public class ChatManager {
}
// Get target
GenshinPlayer target = getServer().getPlayerById(targetUid);
GenshinPlayer target = getServer().getPlayerByUid(targetUid);
if (target == null) {
return;
}
// Create chat packet
GenshinPacket packet = new PacketPrivateChatNotify(player.getId(), target.getId(), message);
GenshinPacket packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), message);
player.sendPacket(packet);
target.sendPacket(packet);
......@@ -51,14 +51,14 @@ public class ChatManager {
public void sendPrivateMessage(GenshinPlayer player, int targetUid, int emote) {
// Get target
GenshinPlayer target = getServer().getPlayerById(targetUid);
GenshinPlayer target = getServer().getPlayerByUid(targetUid);
if (target == null) {
return;
}
// Create chat packet
GenshinPacket packet = new PacketPrivateChatNotify(player.getId(), target.getId(), emote);
GenshinPacket packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), emote);
player.sendPacket(packet);
target.sendPacket(packet);
......
......@@ -24,7 +24,7 @@ public class MultiplayerManager {
}
public void applyEnterMp(GenshinPlayer player, int targetUid) {
GenshinPlayer target = getServer().getPlayerById(targetUid);
GenshinPlayer target = getServer().getPlayerByUid(targetUid);
if (target == null) {
player.sendPacket(new PacketPlayerApplyEnterMpResultNotify(targetUid, "", false, PlayerApplyEnterMpReason.PlayerCannotEnterMp));
return;
......@@ -43,7 +43,7 @@ public class MultiplayerManager {
*/
// Get request
CoopRequest request = target.getCoopRequests().get(player.getId());
CoopRequest request = target.getCoopRequests().get(player.getUid());
if (request != null && !request.isExpired()) {
// Join request already exists
......@@ -52,7 +52,7 @@ public class MultiplayerManager {
// Put request in
request = new CoopRequest(player);
target.getCoopRequests().put(player.getId(), request);
target.getCoopRequests().put(player.getUid(), request);
// Packet
target.sendPacket(new PacketPlayerApplyEnterMpNotify(player));
......@@ -137,7 +137,7 @@ public class MultiplayerManager {
}
// Get victim and sanity checks
GenshinPlayer victim = player.getServer().getPlayerById(targetUid);
GenshinPlayer victim = player.getServer().getPlayerByUid(targetUid);
if (victim == null || victim == player) {
return false;
......
......@@ -104,21 +104,25 @@ public final class GameServer extends MihoyoKcpServer {
}
public void registerPlayer(GenshinPlayer player) {
getPlayers().put(player.getId(), player);
getPlayers().put(player.getUid(), player);
}
public GenshinPlayer getPlayerById(int id) {
return this.getPlayers().get(id);
public GenshinPlayer getPlayerByUid(int id) {
return this.getPlayerByUid(id, false);
}
public GenshinPlayer forceGetPlayerById(int id) {
public GenshinPlayer getPlayerByUid(int id, boolean allowOfflinePlayers) {
// Console check
if (id == GenshinConstants.SERVER_CONSOLE_UID) {
return null;
}
// Get from online players
GenshinPlayer player = this.getPlayerById(id);
GenshinPlayer player = this.getPlayerByUid(id);
if (!allowOfflinePlayers) {
return player;
}
// Check database if character isnt here
if (player == null) {
......@@ -128,9 +132,9 @@ public final class GameServer extends MihoyoKcpServer {
return player;
}
public SocialDetail.Builder getSocialDetailById(int id) {
public SocialDetail.Builder getSocialDetailByUid(int id) {
// Get from online players
GenshinPlayer player = this.forceGetPlayerById(id);
GenshinPlayer player = this.getPlayerByUid(id, true);
if (player == null) {
return null;
......
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