Exemplo n.º 1
0
        private static void SharedStartAction(ICharacter character, IWorldObject worldObject)
        {
            if (worldObject is null)
            {
                return;
            }

            if (!SharedCheckCanInteract(character, worldObject, writeToLog: true))
            {
                return;
            }

            if (worldObject.ProtoGameObject is not IProtoObjectStructure protoObjectStructure)
            {
                throw new Exception("Not a structure: " + worldObject);
            }

            if (!SharedIsDeconstructable(protoObjectStructure))
            {
                throw new Exception("Not deconstructable: " + worldObject);
            }

            var characterPrivateState = PlayerCharacter.GetPrivateState(character);
            var characterPublicState  = PlayerCharacter.GetPublicState(character);

            if (characterPrivateState.CurrentActionState is DeconstructionActionState actionState &&
                actionState.WorldObject == worldObject)
            {
                // already deconstructing
                return;
            }

            var selectedHotbarItem = characterPublicState.SelectedItem;

            if (selectedHotbarItem?.ProtoGameObject is not IProtoItemToolCrowbar)
            {
                selectedHotbarItem = null;
                if (worldObject.ProtoWorldObject is not ProtoObjectConstructionSite)
                {
                    // no crowbar tool is selected, only construction sites
                    // can be deconstructed without the crowbar
                    return;
                }
            }

            actionState = new DeconstructionActionState(character, (IStaticWorldObject)worldObject, selectedHotbarItem);
            if (!actionState.CheckIsAllowed(out var hasNoFactionPermission))
            {
                // not allowed to deconstruct
                Logger.Warning(
                    $"Deconstruction is not allowed: {worldObject} by {character}",
                    character);

                if (Api.IsClient)
                {
                    if (hasNoFactionPermission)
                    {
                        NotificationSystem.ClientShowNotification(
                            title: CoreStrings.Notification_ActionForbidden,
                            string.Format(CoreStrings.Faction_Permission_Required_Format,
                                          CoreStrings.Faction_Permission_LandClaimManagement_Title),
                            NotificationColor.Bad,
                            selectedHotbarItem?.ProtoItem.Icon);
                    }
                    else
                    {
                        NotificationSystem.ClientShowNotification(
                            NotificationCannotDeconstruct_Title,
                            LandClaimSystem.ErrorNotLandOwner_Message,
                            NotificationColor.Bad,
                            selectedHotbarItem?.ProtoItem.Icon);
                    }
                }

                return;
            }

            if (!actionState.CheckIsNeeded())
            {
                // action is not needed
                Logger.Important($"Deconstruction is not required: {worldObject} by {character}", character);
                return;
            }

            characterPrivateState.SetCurrentActionState(actionState);

            Logger.Important($"Deconstruction started: {worldObject} by {character}", character);

            if (IsClient)
            {
                // TODO: display crowbar started animation? Send animation to other players?
                Instance.CallServer(_ => _.ServerRemote_StartAction(worldObject));
            }
        }
Exemplo n.º 2
0
        private static void SharedStartAction(ICharacter character, IWorldObject worldObject)
        {
            if (worldObject == null)
            {
                return;
            }

            if (!SharedCheckCanInteract(character, worldObject, writeToLog: true))
            {
                return;
            }

            if (!(worldObject.ProtoGameObject is IProtoObjectStructure))
            {
                throw new Exception("Not a structure: " + worldObject);
            }

            var characterPrivateState = PlayerCharacter.GetPrivateState(character);
            var characterPublicState  = PlayerCharacter.GetPublicState(character);

            if (characterPrivateState.CurrentActionState is DeconstructionActionState actionState &&
                actionState.WorldObject == worldObject)
            {
                // already deconstructing
                return;
            }

            var selectedHotbarItem = characterPublicState.SelectedHotbarItem;

            if (!(selectedHotbarItem?.ProtoGameObject is IProtoItemToolCrowbar))
            {
                selectedHotbarItem = null;
                if (!(worldObject.ProtoWorldObject is ProtoObjectConstructionSite))
                {
                    // no crowbar tool is selected, only construction sites can be deconstructed without the crowbar
                    return;
                }
            }

            actionState = new DeconstructionActionState(character, (IStaticWorldObject)worldObject, selectedHotbarItem);
            if (!actionState.CheckIsAllowed())
            {
                // not allowed to deconstruct
                Logger.Warning(
                    $"Deconstruction is not allowed: {worldObject} by {character}",
                    character);

                if (Api.IsClient)
                {
                    NotificationSystem.ClientShowNotification(
                        NotificationNotLandOwner_Title,
                        NotificationNotLandOwner_Message,
                        NotificationColor.Bad,
                        selectedHotbarItem?.ProtoItem.Icon);
                }

                return;
            }

            if (!actionState.CheckIsNeeded())
            {
                // action is not needed
                Logger.Important($"Deconstruction is not required: {worldObject} by {character}", character);
                return;
            }

            characterPrivateState.SetCurrentActionState(actionState);

            Logger.Important($"Deconstruction started: {worldObject} by {character}", character);

            if (IsClient)
            {
                // TODO: display crowbar started animation? Send animation to other players?
                Instance.CallServer(_ => _.ServerRemote_StartAction(worldObject));
            }
        }